refactored unit test for sort_claim_results

This commit is contained in:
Sergey Rozhnov 2018-05-18 18:51:28 +04:00
parent 40bcf96c3d
commit 13353bcfe4
2 changed files with 33 additions and 201 deletions

View file

@ -163,7 +163,7 @@ class AlwaysSend(object):
return d return d
def arrange_results(claims): def sort_claim_results(claims):
for claim in claims: for claim in claims:
claim['result'].sort(key=lambda d: (d['height'], d['name'], d['claim_id'], d['txid'], d['nout'])) claim['result'].sort(key=lambda d: (d['height'], d['name'], d['claim_id'], d['txid'], d['nout']))
return claims return claims
@ -2299,7 +2299,7 @@ class Daemon(AuthJSONRPCServer):
""" """
d = self.session.wallet.get_name_claims() d = self.session.wallet.get_name_claims()
d.addCallback(arrange_results) d.addCallback(sort_claim_results)
d.addCallback(lambda claims: self._render_response(claims)) d.addCallback(lambda claims: self._render_response(claims))
return d return d
@ -2338,7 +2338,7 @@ class Daemon(AuthJSONRPCServer):
""" """
claims = yield self.session.wallet.get_claims_for_name(name) claims = yield self.session.wallet.get_claims_for_name(name)
result = arrange_results(claims) result = sort_claim_results(claims)
defer.returnValue(result) defer.returnValue(result)
@defer.inlineCallbacks @defer.inlineCallbacks

View file

@ -1,213 +1,45 @@
import unittest import unittest
from lbrynet.daemon.Daemon import arrange_results from lbrynet.daemon.Daemon import sort_claim_results
class ClaimsComparatorTest(unittest.TestCase): class ClaimsComparatorTest(unittest.TestCase):
def test_arrange_results_when_sorted_by_claim_id(self): def test_sort_claim_results_when_sorted_by_claim_id(self):
self.run_test( 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"}]
"height": 1, self.run_test(results, 'claim_id', ['aaa', 'bbb', 'ccc'])
"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_when_sorted_by_height(self): def test_sort_claim_results_when_sorted_by_height(self):
self.run_test( 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"}]
"height": 1, self.run_test(results, 'height', [1, 2, 3])
"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_arrange_results_when_sorted_by_name(self): def test_sort_claim_results_when_sorted_by_name(self):
self.run_test( 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"}]
"height": 1, self.run_test(results, 'name', ['res1', 'res2', 'res3'])
"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_arrange_results_when_sort_by_outpoint(self): def test_sort_claim_results_when_sorted_by_txid(self):
self.run_test( 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"}]
"height": 1, self.run_test(results, 'txid', ['111', '222', '333'])
"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): 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} data = {'result': results}
claims = sort_claim_results([data])
claims = arrange_results([data])
claim = claims[0] claim = claims[0]
actual = claim['result'] actual = claim['result']
self.assertEqual(expected, [r[field] for r in actual])
self.assertEqual(expected, actual)
if __name__ == '__main__': if __name__ == '__main__':