From d66bce814c4017f139e2427121de8798dfd660d2 Mon Sep 17 00:00:00 2001 From: Sergey Rozhnov Date: Fri, 18 May 2018 18:51:28 +0400 Subject: [PATCH] refactored unit test for sort_claim_results --- lbrynet/daemon/Daemon.py | 2 +- .../unit/daemon/test_claims_comparator.py | 228 +++--------------- 2 files changed, 31 insertions(+), 199 deletions(-) diff --git a/lbrynet/daemon/Daemon.py b/lbrynet/daemon/Daemon.py index 909c538e3..23ecc7d67 100644 --- a/lbrynet/daemon/Daemon.py +++ b/lbrynet/daemon/Daemon.py @@ -2329,7 +2329,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 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__':