From 65fed3f5aa2295bf330460de36a12b30ad8dc142 Mon Sep 17 00:00:00 2001 From: Sergey Rozhnov Date: Fri, 18 May 2018 12:53:32 +0400 Subject: [PATCH] refactored sorting of claims and unit tests --- lbrynet/daemon/Daemon.py | 7 +- lbrynet/daemon/claims_comparator.py | 10 - .../unit/daemon/claims_comparator_cases.json | 200 ---------------- .../unit/daemon/test_claims_comparator.py | 217 ++++++++++++++++-- 4 files changed, 208 insertions(+), 226 deletions(-) delete mode 100644 lbrynet/daemon/claims_comparator.py delete mode 100644 lbrynet/tests/unit/daemon/claims_comparator_cases.json diff --git a/lbrynet/daemon/Daemon.py b/lbrynet/daemon/Daemon.py index 9da91e320..c8b7e376f 100644 --- a/lbrynet/daemon/Daemon.py +++ b/lbrynet/daemon/Daemon.py @@ -36,7 +36,6 @@ from lbrynet.daemon.Downloader import GetStream from lbrynet.daemon.Publisher import Publisher from lbrynet.daemon.ExchangeRateManager import ExchangeRateManager from lbrynet.daemon.auth.server import AuthJSONRPCServer -from lbrynet.daemon.claims_comparator import arrange_results from lbrynet.core.PaymentRateManager import OnlyFreePaymentsManager from lbrynet.core import utils, system_info from lbrynet.core.StreamDescriptor import StreamDescriptorIdentifier, download_sd_blob @@ -164,6 +163,12 @@ class AlwaysSend(object): return d +def arrange_results(claims): + for claim in claims: + claim['result'].sort(key=lambda d: (d['height'], d['name'], d['claim_id'], d['txid'], d['nout'])) + return claims + + class Daemon(AuthJSONRPCServer): """ LBRYnet daemon, a jsonrpc interface to lbry functions diff --git a/lbrynet/daemon/claims_comparator.py b/lbrynet/daemon/claims_comparator.py deleted file mode 100644 index 44d27a955..000000000 --- a/lbrynet/daemon/claims_comparator.py +++ /dev/null @@ -1,10 +0,0 @@ -def arrange_results(claims): - for claim in claims: - results = claim['result'] - sorted_results = sorted(results, key=lambda d: (d['height'], d['name'], d['claim_id'], _outpoint(d))) - claim['result'] = sorted_results - return claims - - -def _outpoint(claim): - return '{}:{}'.format(claim['txid'], claim['nout']) diff --git a/lbrynet/tests/unit/daemon/claims_comparator_cases.json b/lbrynet/tests/unit/daemon/claims_comparator_cases.json deleted file mode 100644 index 11592fbf1..000000000 --- a/lbrynet/tests/unit/daemon/claims_comparator_cases.json +++ /dev/null @@ -1,200 +0,0 @@ -{ - "cases": [ - { - "description": "sort by claim_id", - "results": [ - { - "height": 1, - "name": "res", - "claim_id": "ccc", - "nout": 0, - "txid": "fdsafa" - }, - { - "height": 1, - "name": "res", - "claim_id": "aaa", - "nout": 0, - "txid": "w5tv8uorgt" - }, - { - "height": 1, - "name": "res", - "claim_id": "bbb", - "nout": 0, - "txid": "aecfaewcfa" - } - ], - "expected": [ - { - "height": 1, - "name": "res", - "claim_id": "aaa", - "nout": 0, - "txid": "w5tv8uorgt" - }, - { - "height": 1, - "name": "res", - "claim_id": "bbb", - "nout": 0, - "txid": "aecfaewcfa" - }, - { - "height": 1, - "name": "res", - "claim_id": "ccc", - "nout": 0, - "txid": "fdsafa" - } - ] - }, - { - "description": "sort by height", - "results": [ - { - "height": 1, - "name": "res", - "claim_id": "ccc", - "nout": 0, - "txid": "aecfaewcfa" - }, - { - "height": 3, - "name": "res", - "claim_id": "ccc", - "nout": 0, - "txid": "aecfaewcfa" - }, - { - "height": 2, - "name": "res", - "claim_id": "ccc", - "nout": 0, - "txid": "aecfaewcfa" - } - ], - "expected": [ - { - "claim_id": "ccc", - "height": 1, - "name": "res", - "nout": 0, - "txid": "aecfaewcfa" - }, - { - "claim_id": "ccc", - "height": 2, - "name": "res", - "nout": 0, - "txid": "aecfaewcfa" - }, - { - "claim_id": "ccc", - "height": 3, - "name": "res", - "nout": 0, - "txid": "aecfaewcfa" - } - ] - }, - { - "description": "sort by name", - "results": [ - { - "height": 1, - "name": "res1", - "claim_id": "ccc", - "nout": 0, - "txid": "aecfaewcfa" - }, - { - "height": 1, - "name": "res3", - "claim_id": "ccc", - "nout": 0, - "txid": "aecfaewcfa" - }, - { - "height": 1, - "name": "res2", - "claim_id": "ccc", - "nout": 0, - "txid": "aecfaewcfa" - } - ], - "expected": [ - { - "height": 1, - "name": "res1", - "claim_id": "ccc", - "nout": 0, - "txid": "aecfaewcfa" - }, - { - "height": 1, - "name": "res2", - "claim_id": "ccc", - "nout": 0, - "txid": "aecfaewcfa" - }, - { - "height": 1, - "name": "res3", - "claim_id": "ccc", - "nout": 0, - "txid": "aecfaewcfa" - } - ] - }, - { - "description": "sort by outpoint", - "results": [ - { - "height": 1, - "name": "res1", - "claim_id": "ccc", - "nout": 2, - "txid": "aecfaewcfa" - }, - { - "height": 1, - "name": "res1", - "claim_id": "ccc", - "nout": 1, - "txid": "aecfaewcfa" - }, - { - "height": 1, - "name": "res1", - "claim_id": "ccc", - "nout": 3, - "txid": "aecfaewcfa" - } - ], - "expected": [ - { - "height": 1, - "name": "res1", - "claim_id": "ccc", - "nout": 1, - "txid": "aecfaewcfa" - }, - { - "height": 1, - "name": "res1", - "claim_id": "ccc", - "nout": 2, - "txid": "aecfaewcfa" - }, - { - "height": 1, - "name": "res1", - "claim_id": "ccc", - "nout": 3, - "txid": "aecfaewcfa" - } - ] - } - ] -} \ No newline at end of file diff --git a/lbrynet/tests/unit/daemon/test_claims_comparator.py b/lbrynet/tests/unit/daemon/test_claims_comparator.py index 07fcbc503..416a394d5 100644 --- a/lbrynet/tests/unit/daemon/test_claims_comparator.py +++ b/lbrynet/tests/unit/daemon/test_claims_comparator.py @@ -1,26 +1,213 @@ -import json import unittest -from lbrynet.daemon.claims_comparator import arrange_results +from lbrynet.daemon.Daemon import arrange_results class ClaimsComparatorTest(unittest.TestCase): - def setUp(self): - with open('claims_comparator_cases.json') as f: - document = json.load(f) - self.cases = document['cases'] + def test_arrange_results_when_sorted_by_claim_id(self): + self.run_test( + [ + { + "height": 1, + "name": "res", + "claim_id": "ccc", + "nout": 0, + "txid": "fdsafa" + }, + { + "height": 1, + "name": "res", + "claim_id": "aaa", + "nout": 0, + "txid": "w5tv8uorgt" + }, + { + "height": 1, + "name": "res", + "claim_id": "bbb", + "nout": 0, + "txid": "aecfaewcfa" + } + ], + [ + { + "height": 1, + "name": "res", + "claim_id": "aaa", + "nout": 0, + "txid": "w5tv8uorgt" + }, + { + "height": 1, + "name": "res", + "claim_id": "bbb", + "nout": 0, + "txid": "aecfaewcfa" + }, + { + "height": 1, + "name": "res", + "claim_id": "ccc", + "nout": 0, + "txid": "fdsafa" + } + ]) - def test_arrange_results(self): - for case in self.cases: - results = case['results'] - data = {'result': results} - expected = case['expected'] + def test_arrange_results_when_sorted_by_height(self): + self.run_test( + [ + { + "height": 1, + "name": "res", + "claim_id": "ccc", + "nout": 0, + "txid": "aecfaewcfa" + }, + { + "height": 3, + "name": "res", + "claim_id": "ccc", + "nout": 0, + "txid": "aecfaewcfa" + }, + { + "height": 2, + "name": "res", + "claim_id": "ccc", + "nout": 0, + "txid": "aecfaewcfa" + } + ], + [ + { + "claim_id": "ccc", + "height": 1, + "name": "res", + "nout": 0, + "txid": "aecfaewcfa" + }, + { + "claim_id": "ccc", + "height": 2, + "name": "res", + "nout": 0, + "txid": "aecfaewcfa" + }, + { + "claim_id": "ccc", + "height": 3, + "name": "res", + "nout": 0, + "txid": "aecfaewcfa" + } + ]) - claims = arrange_results([data]) - claim = claims[0] - actual = claim['result'] + def test_arrange_results_when_sorted_by_name(self): + self.run_test( + [ + { + "height": 1, + "name": "res1", + "claim_id": "ccc", + "nout": 0, + "txid": "aecfaewcfa" + }, + { + "height": 1, + "name": "res3", + "claim_id": "ccc", + "nout": 0, + "txid": "aecfaewcfa" + }, + { + "height": 1, + "name": "res2", + "claim_id": "ccc", + "nout": 0, + "txid": "aecfaewcfa" + } + ], + [ + { + "height": 1, + "name": "res1", + "claim_id": "ccc", + "nout": 0, + "txid": "aecfaewcfa" + }, + { + "height": 1, + "name": "res2", + "claim_id": "ccc", + "nout": 0, + "txid": "aecfaewcfa" + }, + { + "height": 1, + "name": "res3", + "claim_id": "ccc", + "nout": 0, + "txid": "aecfaewcfa" + } + ]) - self.assertEqual(expected, actual, case['description']) + def test_arrange_results_when_sort_by_outpoint(self): + self.run_test( + [ + { + "height": 1, + "name": "res1", + "claim_id": "ccc", + "nout": 2, + "txid": "aecfaewcfa" + }, + { + "height": 1, + "name": "res1", + "claim_id": "ccc", + "nout": 1, + "txid": "aecfaewcfa" + }, + { + "height": 1, + "name": "res1", + "claim_id": "ccc", + "nout": 3, + "txid": "aecfaewcfa" + } + ], + [ + { + "height": 1, + "name": "res1", + "claim_id": "ccc", + "nout": 1, + "txid": "aecfaewcfa" + }, + { + "height": 1, + "name": "res1", + "claim_id": "ccc", + "nout": 2, + "txid": "aecfaewcfa" + }, + { + "height": 1, + "name": "res1", + "claim_id": "ccc", + "nout": 3, + "txid": "aecfaewcfa" + } + ]) + + def run_test(self, results, expected): + data = {'result': results} + + claims = arrange_results([data]) + claim = claims[0] + actual = claim['result'] + + self.assertEqual(expected, actual) if __name__ == '__main__':