simplified claims comparison logic; refactored unit tests

This commit is contained in:
Sergey Rozhnov 2018-05-17 13:36:32 +04:00
parent 208284a0f8
commit a7a5bb8887
3 changed files with 70 additions and 68 deletions

View file

@ -1,36 +1,10 @@
_comparison_order = ['height', 'name', 'claim_id'] # TODO outpoint
def arrange_results(claims): def arrange_results(claims):
for claim in claims: for claim in claims:
results = claim['result'] 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 claim['result'] = sorted_results
return claims return claims
def _compare_results(left, right): def _outpoint(claim):
""" return '{}:{}'.format(claim['txid'], claim['nout'])
: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

View file

@ -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"
}
]
}
]
}

View file

@ -1,51 +1,26 @@
import json
import unittest import unittest
from lbrynet.daemon.claims_comparator import arrange_results from lbrynet.daemon.claims_comparator import arrange_results
class ClaimsComparatorTest(unittest.TestCase): class ClaimsComparatorTest(unittest.TestCase):
def test_arrange_results(self): def setUp(self):
results = [ with open('claims_comparator_cases.json') as f:
{ document = json.load(f)
'height': 1, self.cases = document['cases']
'name': 'res',
'claim_id': 'ccc' def test_arrange_results(self):
}, for case in self.cases:
{ results = case['results']
'height': 1, data = {'result': results}
'name': 'res', expected = case['expected']
'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]) claims = arrange_results([data])
claim = claims[0] claim = claims[0]
actual = claim['result'] actual = claim['result']
self.assertEqual(expected, actual) self.assertEqual(expected, actual, case['description'])
if __name__ == '__main__': if __name__ == '__main__':