From 208284a0f8f1f441d988bdb2c34798fe43c36150 Mon Sep 17 00:00:00 2001 From: Sergey Rozhnov Date: Wed, 16 May 2018 18:29:44 +0400 Subject: [PATCH 1/5] claim_list and claim_list_mine in Daemon return sorted results --- lbrynet/daemon/Daemon.py | 5 +- lbrynet/daemon/claims_comparator.py | 36 +++++++++++++ lbrynet/tests/unit/daemon/__init__.py | 0 .../unit/daemon/test_claims_comparator.py | 52 +++++++++++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 lbrynet/daemon/claims_comparator.py create mode 100644 lbrynet/tests/unit/daemon/__init__.py create mode 100644 lbrynet/tests/unit/daemon/test_claims_comparator.py diff --git a/lbrynet/daemon/Daemon.py b/lbrynet/daemon/Daemon.py index 43c67e0d7..9da91e320 100644 --- a/lbrynet/daemon/Daemon.py +++ b/lbrynet/daemon/Daemon.py @@ -36,6 +36,7 @@ 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 @@ -2293,6 +2294,7 @@ class Daemon(AuthJSONRPCServer): """ d = self.session.wallet.get_name_claims() + d.addCallback(arrange_results) d.addCallback(lambda claims: self._render_response(claims)) return d @@ -2331,7 +2333,8 @@ class Daemon(AuthJSONRPCServer): """ claims = yield self.session.wallet.get_claims_for_name(name) - defer.returnValue(claims) + result = arrange_results(claims) + defer.returnValue(result) @defer.inlineCallbacks def jsonrpc_claim_list_by_channel(self, page=0, page_size=10, uri=None, uris=[]): diff --git a/lbrynet/daemon/claims_comparator.py b/lbrynet/daemon/claims_comparator.py new file mode 100644 index 000000000..4a1ea9870 --- /dev/null +++ b/lbrynet/daemon/claims_comparator.py @@ -0,0 +1,36 @@ +_comparison_order = ['height', 'name', 'claim_id'] # TODO outpoint + + +def arrange_results(claims): + for claim in claims: + results = claim['result'] + sorted_results = sorted(results, cmp=_compare_results) + claim['result'] = sorted_results + return claims + + +def _compare_results(left, right): + """ + :type left: dict + :type right: dict + """ + result = 0 + + for attribute in _comparison_order: + left_value = left[attribute] + right_value = right[attribute] + sub_result = _cmp(left_value, right_value) + if sub_result is not 0: + result = sub_result + break + + return result + + +def _cmp(left, right): + if left == right: + return 0 + elif left < right: + return -1 + else: + return 1 diff --git a/lbrynet/tests/unit/daemon/__init__.py b/lbrynet/tests/unit/daemon/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/lbrynet/tests/unit/daemon/test_claims_comparator.py b/lbrynet/tests/unit/daemon/test_claims_comparator.py new file mode 100644 index 000000000..9651c2137 --- /dev/null +++ b/lbrynet/tests/unit/daemon/test_claims_comparator.py @@ -0,0 +1,52 @@ +import unittest + +from lbrynet.daemon.claims_comparator import arrange_results + + +class ClaimsComparatorTest(unittest.TestCase): + def test_arrange_results(self): + results = [ + { + 'height': 1, + 'name': 'res', + 'claim_id': 'ccc' + }, + { + 'height': 1, + 'name': 'res', + 'claim_id': 'aaa' + }, + { + 'height': 1, + 'name': 'res', + 'claim_id': 'bbb' + } + ] + data = {'result': results} + + expected = [ + { + 'height': 1, + 'name': 'res', + 'claim_id': 'aaa' + }, + { + 'height': 1, + 'name': 'res', + 'claim_id': 'bbb' + }, + { + 'height': 1, + 'name': 'res', + 'claim_id': 'ccc' + } + ] + claims = arrange_results([data]) + claim = claims[0] + actual = claim['result'] + + self.assertEqual(expected, actual) + + +if __name__ == '__main__': + unittest.main() From a7a5bb8887c1e98e4d99dbc2c548f2a3be8bfa82 Mon Sep 17 00:00:00 2001 From: Sergey Rozhnov Date: Thu, 17 May 2018 13:36:32 +0400 Subject: [PATCH 2/5] simplified claims comparison logic; refactored unit tests --- lbrynet/daemon/claims_comparator.py | 32 ++--------- .../unit/daemon/claims_comparator_cases.json | 53 +++++++++++++++++++ .../unit/daemon/test_claims_comparator.py | 53 +++++-------------- 3 files changed, 70 insertions(+), 68 deletions(-) create mode 100644 lbrynet/tests/unit/daemon/claims_comparator_cases.json diff --git a/lbrynet/daemon/claims_comparator.py b/lbrynet/daemon/claims_comparator.py index 4a1ea9870..44d27a955 100644 --- a/lbrynet/daemon/claims_comparator.py +++ b/lbrynet/daemon/claims_comparator.py @@ -1,36 +1,10 @@ -_comparison_order = ['height', 'name', 'claim_id'] # TODO outpoint - - def arrange_results(claims): for claim in claims: results = claim['result'] - sorted_results = sorted(results, cmp=_compare_results) + sorted_results = sorted(results, key=lambda d: (d['height'], d['name'], d['claim_id'], _outpoint(d))) claim['result'] = sorted_results return claims -def _compare_results(left, right): - """ - :type left: dict - :type right: dict - """ - result = 0 - - for attribute in _comparison_order: - left_value = left[attribute] - right_value = right[attribute] - sub_result = _cmp(left_value, right_value) - if sub_result is not 0: - result = sub_result - break - - return result - - -def _cmp(left, right): - if left == right: - return 0 - elif left < right: - return -1 - else: - return 1 +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 new file mode 100644 index 000000000..dbcb732b7 --- /dev/null +++ b/lbrynet/tests/unit/daemon/claims_comparator_cases.json @@ -0,0 +1,53 @@ +{ + "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" + } + ] + } + ] +} \ 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 9651c2137..07fcbc503 100644 --- a/lbrynet/tests/unit/daemon/test_claims_comparator.py +++ b/lbrynet/tests/unit/daemon/test_claims_comparator.py @@ -1,51 +1,26 @@ +import json import unittest from lbrynet.daemon.claims_comparator 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(self): - results = [ - { - 'height': 1, - 'name': 'res', - 'claim_id': 'ccc' - }, - { - 'height': 1, - 'name': 'res', - 'claim_id': 'aaa' - }, - { - 'height': 1, - 'name': 'res', - 'claim_id': 'bbb' - } - ] - data = {'result': results} + for case in self.cases: + results = case['results'] + data = {'result': results} + expected = case['expected'] - expected = [ - { - 'height': 1, - 'name': 'res', - 'claim_id': 'aaa' - }, - { - 'height': 1, - 'name': 'res', - 'claim_id': 'bbb' - }, - { - 'height': 1, - 'name': 'res', - 'claim_id': 'ccc' - } - ] - claims = arrange_results([data]) - claim = claims[0] - actual = claim['result'] + claims = arrange_results([data]) + claim = claims[0] + actual = claim['result'] - self.assertEqual(expected, actual) + self.assertEqual(expected, actual, case['description']) if __name__ == '__main__': From 3d0f74c8ce1469ad527c83cff564c6f76805a62c Mon Sep 17 00:00:00 2001 From: Sergey Rozhnov Date: Thu, 17 May 2018 13:49:09 +0400 Subject: [PATCH 3/5] implemented additional test cases for claims sorting; updated changelog --- CHANGELOG.md | 1 + .../unit/daemon/claims_comparator_cases.json | 147 ++++++++++++++++++ 2 files changed, 148 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b94d5fd44..b95a4f1eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,7 @@ at anytime. * download blockchain headers from s3 before starting the wallet when the local height is more than `s3_headers_depth` (a config setting) blocks behind * track successful reflector uploads in sqlite to minimize how many streams are attempted by auto re-reflect * increase the default `auto_re_reflect_interval` to a day + * `claim_list` and `claim_list_mine` in Daemon `return` sorted results ### Added * virtual kademlia network and mock udp transport for dht integration tests diff --git a/lbrynet/tests/unit/daemon/claims_comparator_cases.json b/lbrynet/tests/unit/daemon/claims_comparator_cases.json index dbcb732b7..11592fbf1 100644 --- a/lbrynet/tests/unit/daemon/claims_comparator_cases.json +++ b/lbrynet/tests/unit/daemon/claims_comparator_cases.json @@ -48,6 +48,153 @@ "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 From 40bcf96c3dad46359f4c01c4989721a0cd6da903 Mon Sep 17 00:00:00 2001 From: Sergey Rozhnov Date: Fri, 18 May 2018 12:53:32 +0400 Subject: [PATCH 4/5] 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__': From 13353bcfe4cdb4c700b1eff1601a7d2099e2748e Mon Sep 17 00:00:00 2001 From: Sergey Rozhnov Date: Fri, 18 May 2018 18:51:28 +0400 Subject: [PATCH 5/5] refactored unit test for sort_claim_results --- lbrynet/daemon/Daemon.py | 6 +- .../unit/daemon/test_claims_comparator.py | 228 +++--------------- 2 files changed, 33 insertions(+), 201 deletions(-) diff --git a/lbrynet/daemon/Daemon.py b/lbrynet/daemon/Daemon.py index c8b7e376f..ac497c37c 100644 --- a/lbrynet/daemon/Daemon.py +++ b/lbrynet/daemon/Daemon.py @@ -163,7 +163,7 @@ class AlwaysSend(object): return d -def arrange_results(claims): +def sort_claim_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 @@ -2299,7 +2299,7 @@ class Daemon(AuthJSONRPCServer): """ d = self.session.wallet.get_name_claims() - d.addCallback(arrange_results) + d.addCallback(sort_claim_results) d.addCallback(lambda claims: self._render_response(claims)) return d @@ -2338,7 +2338,7 @@ class Daemon(AuthJSONRPCServer): """ claims = yield self.session.wallet.get_claims_for_name(name) - result = arrange_results(claims) + result = sort_claim_results(claims) defer.returnValue(result) @defer.inlineCallbacks diff --git a/lbrynet/tests/unit/daemon/test_claims_comparator.py b/lbrynet/tests/unit/daemon/test_claims_comparator.py index 416a394d5..772ecb5e5 100644 --- a/lbrynet/tests/unit/daemon/test_claims_comparator.py +++ b/lbrynet/tests/unit/daemon/test_claims_comparator.py @@ -1,213 +1,45 @@ import unittest -from lbrynet.daemon.Daemon import arrange_results +from lbrynet.daemon.Daemon import sort_claim_results class ClaimsComparatorTest(unittest.TestCase): - 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_sort_claim_results_when_sorted_by_claim_id(self): + 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"}] + self.run_test(results, 'claim_id', ['aaa', 'bbb', 'ccc']) - 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" - } - ]) + def test_sort_claim_results_when_sorted_by_height(self): + 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"}] + self.run_test(results, 'height', [1, 2, 3]) - 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" - } - ]) + def test_sort_claim_results_when_sorted_by_name(self): + 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"}] + self.run_test(results, 'name', ['res1', 'res2', 'res3']) - 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 test_sort_claim_results_when_sorted_by_txid(self): + results = [{"height": 1, "name": "res1", "claim_id": "ccc", "nout": 2, "txid": "111"}, + {"height": 1, "name": "res1", "claim_id": "ccc", "nout": 1, "txid": "222"}, + {"height": 1, "name": "res1", "claim_id": "ccc", "nout": 3, "txid": "333"}] + self.run_test(results, 'txid', ['111', '222', '333']) - def run_test(self, results, expected): + def test_sort_claim_results_when_sorted_by_nout(self): + 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"}] + self.run_test(results, 'nout', [1, 2, 3]) + + def run_test(self, results, field, expected): data = {'result': results} - - claims = arrange_results([data]) + claims = sort_claim_results([data]) claim = claims[0] actual = claim['result'] - - self.assertEqual(expected, actual) + self.assertEqual(expected, [r[field] for r in actual]) if __name__ == '__main__':