refactored sorting of claims and unit tests

This commit is contained in:
Sergey Rozhnov 2018-05-18 12:53:32 +04:00 committed by jleute
parent 156b132070
commit c290fb5908
4 changed files with 202 additions and 226 deletions

View file

@ -36,7 +36,6 @@ from lbrynet.daemon.Downloader import GetStream
from lbrynet.daemon.Publisher import Publisher from lbrynet.daemon.Publisher import Publisher
from lbrynet.daemon.ExchangeRateManager import ExchangeRateManager from lbrynet.daemon.ExchangeRateManager import ExchangeRateManager
from lbrynet.daemon.auth.server import AuthJSONRPCServer from lbrynet.daemon.auth.server import AuthJSONRPCServer
from lbrynet.daemon.claims_comparator import arrange_results
from lbrynet.core.PaymentRateManager import OnlyFreePaymentsManager from lbrynet.core.PaymentRateManager import OnlyFreePaymentsManager
from lbrynet.core import utils, system_info from lbrynet.core import utils, system_info
from lbrynet.core.StreamDescriptor import StreamDescriptorIdentifier, download_sd_blob from lbrynet.core.StreamDescriptor import StreamDescriptorIdentifier, download_sd_blob

View file

@ -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'])

View file

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

View file

@ -1,26 +1,213 @@
import json
import unittest import unittest
from lbrynet.daemon.claims_comparator import arrange_results from lbrynet.daemon.Daemon import arrange_results
class ClaimsComparatorTest(unittest.TestCase): class ClaimsComparatorTest(unittest.TestCase):
def setUp(self): def test_arrange_results_when_sorted_by_claim_id(self):
with open('claims_comparator_cases.json') as f: self.run_test(
document = json.load(f) [
self.cases = document['cases'] {
"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): def test_arrange_results_when_sorted_by_height(self):
for case in self.cases: self.run_test(
results = case['results'] [
data = {'result': results} {
expected = case['expected'] "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]) def test_arrange_results_when_sorted_by_name(self):
claim = claims[0] self.run_test(
actual = claim['result'] [
{
"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__': if __name__ == '__main__':