diff --git a/tests/unit/db/__init__.py b/tests/unit/db/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/unit/db/test_event.py b/tests/unit/db/test_event.py new file mode 100644 index 000000000..982bbadc5 --- /dev/null +++ b/tests/unit/db/test_event.py @@ -0,0 +1,11 @@ +from unittest import TestCase + +from lbry.db.query_context import Event + + +class TestDBEvents(TestCase): + + def test_enum(self): + self.assertEqual(Event.BLOCK_READ.value, 1) + self.assertEqual(Event.BLOCK_READ.label, "blockchain.sync.block.read") + self.assertEqual(Event(1).label, "blockchain.sync.block.read") diff --git a/tests/unit/db/test_result.py b/tests/unit/db/test_result.py new file mode 100644 index 000000000..47bcd7dd7 --- /dev/null +++ b/tests/unit/db/test_result.py @@ -0,0 +1,21 @@ +from unittest import TestCase + +from lbry.db import Result + + +class TestResult(TestCase): + + def test_result(self): + result = Result([], 0) + self.assertFalse(result) + self.assertEqual(0, len(result)) + self.assertEqual(0, result.total) + + result = Result(['a', 'b', 'c'], 100) + self.assertTrue(result) + self.assertEqual(3, len(result)) + self.assertEqual(100, result.total) + self.assertEqual('b', result[1]) + self.assertEqual(['a', 'b', 'c'], [o for o in result]) + self.assertEqual(['a', 'b', 'c'], list(result)) + self.assertEqual("['a', 'b', 'c']", repr(result)) diff --git a/tests/unit/db/test_sync.py b/tests/unit/db/test_sync.py new file mode 100644 index 000000000..51e7248c9 --- /dev/null +++ b/tests/unit/db/test_sync.py @@ -0,0 +1,78 @@ +from lbry.testcase import UnitDBTestCase + + +class TestClientDBSync(UnitDBTestCase): + + async def asyncSetUp(self): + await super().asyncSetUp() + await self.add(self.coinbase()) + + async def test_process_inputs(self): + await self.add(self.tx()) + await self.add(self.tx()) + txo1, txo2a, txo2b, txo3a, txo3b = self.outputs + self.assertEqual([ + (txo1.id, None), + (txo2b.id, None), + ], await self.get_txis()) + self.assertEqual([ + (txo1.id, False), + (txo2a.id, False), + (txo2b.id, False), + (txo3a.id, False), + (txo3b.id, False), + ], await self.get_txos()) + await self.db.process_all_things_after_sync() + self.assertEqual([ + (txo1.id, txo1.get_address(self.ledger)), + (txo2b.id, txo2b.get_address(self.ledger)), + ], await self.get_txis()) + self.assertEqual([ + (txo1.id, True), + (txo2a.id, False), + (txo2b.id, True), + (txo3a.id, False), + (txo3b.id, False), + ], await self.get_txos()) + + async def test_process_claims(self): + claim1 = await self.add(self.create_claim()) + await self.db.process_all_things_after_sync() + self.assertEqual([claim1.claim_id], await self.get_claims()) + + claim2 = await self.add(self.create_claim()) + self.assertEqual([claim1.claim_id], await self.get_claims()) + await self.db.process_all_things_after_sync() + self.assertEqual([claim1.claim_id, claim2.claim_id], await self.get_claims()) + + claim3 = await self.add(self.create_claim()) + claim4 = await self.add(self.create_claim()) + await self.db.process_all_things_after_sync() + self.assertEqual([ + claim1.claim_id, + claim2.claim_id, + claim3.claim_id, + claim4.claim_id, + ], await self.get_claims()) + + await self.add(self.abandon_claim(claim4)) + await self.db.process_all_things_after_sync() + self.assertEqual([ + claim1.claim_id, claim2.claim_id, claim3.claim_id + ], await self.get_claims()) + + # create and abandon in same block + claim5 = await self.add(self.create_claim()) + await self.add(self.abandon_claim(claim5)) + await self.db.process_all_things_after_sync() + self.assertEqual([ + claim1.claim_id, claim2.claim_id, claim3.claim_id + ], await self.get_claims()) + + # create and abandon in different blocks but with bulk sync + claim6 = await self.add(self.create_claim()) + await self.add(self.abandon_claim(claim6)) + await self.db.process_all_things_after_sync() + self.assertEqual([ + claim1.claim_id, claim2.claim_id, claim3.claim_id + ], await self.get_claims()) diff --git a/tests/unit/db/test_utils.py b/tests/unit/db/test_utils.py new file mode 100644 index 000000000..8229006f4 --- /dev/null +++ b/tests/unit/db/test_utils.py @@ -0,0 +1,15 @@ +import unittest +from lbry.db.utils import chunk + + +class TestChunk(unittest.TestCase): + + def test_chunk(self): + self.assertEqual(list(chunk([], 3)), []) + self.assertEqual(list(chunk(['a'], 3)), [['a']]) + self.assertEqual(list(chunk(['a', 'b', 'c'], 3)), [['a', 'b', 'c']]) + self.assertEqual(list(chunk(['a', 'b', 'c', 'd'], 3)), [['a', 'b', 'c'], ['d']]) + self.assertEqual( + list(chunk(['a', 'b', 'c', 'd', 'e', 'f', 'g'], 3)), + [['a', 'b', 'c'], ['d', 'e', 'f'], ['g']] + )