|
| 1 | +import shutil |
1 | 2 | import unittest |
| 3 | +from pathlib import ( |
| 4 | + Path, |
| 5 | +) |
2 | 6 |
|
3 | 7 | from minos.common import ( |
| 8 | + DatabaseClient, |
4 | 9 | DatabaseOperation, |
5 | 10 | ) |
6 | 11 | from minos.plugins.lmdb import ( |
| 12 | + LmdbDatabaseClient, |
7 | 13 | LmdbDatabaseOperation, |
8 | 14 | ) |
| 15 | +from minos.plugins.lmdb.operations import ( |
| 16 | + LmdbDatabaseOperationType, |
| 17 | +) |
| 18 | +from tests.utils import ( |
| 19 | + BASE_PATH, |
| 20 | +) |
9 | 21 |
|
10 | 22 |
|
11 | | -class TestLmdbDatabaseClient(unittest.TestCase): |
| 23 | +class TestLmdbDatabaseClient(unittest.IsolatedAsyncioTestCase): |
| 24 | + def setUp(self) -> None: |
| 25 | + super().setUp() |
| 26 | + self.path = BASE_PATH / "order.lmdb" |
| 27 | + |
12 | 28 | def test_subclass(self) -> None: |
13 | | - self.assertTrue(issubclass(LmdbDatabaseOperation, DatabaseOperation)) |
| 29 | + self.assertTrue(issubclass(LmdbDatabaseClient, DatabaseClient)) |
| 30 | + |
| 31 | + def tearDown(self) -> None: |
| 32 | + shutil.rmtree(self.path, ignore_errors=True) |
| 33 | + shutil.rmtree(".lmdb", ignore_errors=True) |
| 34 | + |
| 35 | + async def test_constructor_default_path(self): |
| 36 | + async with LmdbDatabaseClient(): |
| 37 | + self.assertTrue(Path(".lmdb").exists()) |
| 38 | + |
| 39 | + async def test_is_valid(self): |
| 40 | + async with LmdbDatabaseClient(self.path) as client: |
| 41 | + self.assertTrue(await client.is_valid()) |
| 42 | + |
| 43 | + async def test_execute_raises_unsupported(self): |
| 44 | + class _DatabaseOperation(DatabaseOperation): |
| 45 | + """For testing purposes.""" |
| 46 | + |
| 47 | + async with LmdbDatabaseClient(self.path) as client: |
| 48 | + with self.assertRaises(ValueError): |
| 49 | + await client.execute(_DatabaseOperation()) |
| 50 | + |
| 51 | + async def test_execute_create_text(self): |
| 52 | + create_op = LmdbDatabaseOperation(LmdbDatabaseOperationType.CREATE, "TestOne", "first", "Text Value") |
| 53 | + read_op = LmdbDatabaseOperation(LmdbDatabaseOperationType.READ, "TestOne", "first") |
| 54 | + |
| 55 | + async with LmdbDatabaseClient(self.path) as client: |
| 56 | + await client.execute(create_op) |
| 57 | + await client.execute(read_op) |
| 58 | + |
| 59 | + self.assertEqual("Text Value", await client.fetch_one()) |
| 60 | + |
| 61 | + async def test_execute_create_int(self): |
| 62 | + create_op = LmdbDatabaseOperation(LmdbDatabaseOperationType.CREATE, "TestOne", "first", 123) |
| 63 | + read_op = LmdbDatabaseOperation(LmdbDatabaseOperationType.READ, "TestOne", "first") |
| 64 | + |
| 65 | + async with LmdbDatabaseClient(self.path) as client: |
| 66 | + await client.execute(create_op) |
| 67 | + await client.execute(read_op) |
| 68 | + |
| 69 | + self.assertEqual(123, await client.fetch_one()) |
| 70 | + |
| 71 | + async def test_execute_create_dict(self): |
| 72 | + create_op = LmdbDatabaseOperation( |
| 73 | + LmdbDatabaseOperationType.CREATE, "TestOne", "first", {"key_one": "hello", "key_two": "minos"} |
| 74 | + ) |
| 75 | + read_op = LmdbDatabaseOperation(LmdbDatabaseOperationType.READ, "TestOne", "first") |
| 76 | + |
| 77 | + async with LmdbDatabaseClient(self.path) as client: |
| 78 | + await client.execute(create_op) |
| 79 | + await client.execute(read_op) |
| 80 | + |
| 81 | + self.assertEqual({"key_one": "hello", "key_two": "minos"}, await client.fetch_one()) |
| 82 | + |
| 83 | + async def test_execute_create_multi_dict(self): |
| 84 | + create_op = LmdbDatabaseOperation( |
| 85 | + LmdbDatabaseOperationType.CREATE, |
| 86 | + "TestOne", |
| 87 | + "first", |
| 88 | + {"key_one": "hello", "key_two": {"sub_key": "this is a sub text"}}, |
| 89 | + ) |
| 90 | + read_op = LmdbDatabaseOperation(LmdbDatabaseOperationType.READ, "TestOne", "first") |
| 91 | + |
| 92 | + async with LmdbDatabaseClient(self.path) as client: |
| 93 | + await client.execute(create_op) |
| 94 | + await client.execute(read_op) |
| 95 | + |
| 96 | + self.assertEqual( |
| 97 | + {"key_one": "hello", "key_two": {"sub_key": "this is a sub text"}}, await client.fetch_one() |
| 98 | + ) |
| 99 | + |
| 100 | + async def test_execute_create_list(self): |
| 101 | + create_op = LmdbDatabaseOperation(LmdbDatabaseOperationType.CREATE, "TestOne", "first", ["hello", "minos"]) |
| 102 | + read_op = LmdbDatabaseOperation(LmdbDatabaseOperationType.READ, "TestOne", "first") |
| 103 | + |
| 104 | + async with LmdbDatabaseClient(self.path) as client: |
| 105 | + await client.execute(create_op) |
| 106 | + await client.execute(read_op) |
| 107 | + |
| 108 | + self.assertEqual(["hello", "minos"], await client.fetch_one()) |
| 109 | + |
| 110 | + async def test_execute_create_multi_table(self): |
| 111 | + create_op_1 = LmdbDatabaseOperation(LmdbDatabaseOperationType.CREATE, "TestOne", "first", "Text Value") |
| 112 | + create_op_2 = LmdbDatabaseOperation( |
| 113 | + LmdbDatabaseOperationType.CREATE, "TestTwo", "first_double", "Text Double Value" |
| 114 | + ) |
| 115 | + create_op_3 = LmdbDatabaseOperation(LmdbDatabaseOperationType.CREATE, "TestTwo", "first", "Text Value Diff") |
| 116 | + |
| 117 | + read_op_1 = LmdbDatabaseOperation(LmdbDatabaseOperationType.READ, "TestOne", "first") |
| 118 | + read_op_2 = LmdbDatabaseOperation(LmdbDatabaseOperationType.READ, "TestTwo", "first_double") |
| 119 | + read_op_3 = LmdbDatabaseOperation(LmdbDatabaseOperationType.READ, "TestTwo", "first") |
| 120 | + |
| 121 | + async with LmdbDatabaseClient(self.path) as client: |
| 122 | + await client.execute(create_op_1) |
| 123 | + await client.execute(create_op_2) |
| 124 | + await client.execute(create_op_3) |
| 125 | + |
| 126 | + await client.execute(read_op_1) |
| 127 | + self.assertEqual("Text Value", await client.fetch_one()) |
| 128 | + |
| 129 | + await client.execute(read_op_2) |
| 130 | + self.assertEqual("Text Double Value", await client.fetch_one()) |
| 131 | + |
| 132 | + await client.execute(read_op_3) |
| 133 | + self.assertEqual("Text Value Diff", await client.fetch_one()) |
| 134 | + |
| 135 | + async def test_execute_delete(self): |
| 136 | + create_op_1 = LmdbDatabaseOperation(LmdbDatabaseOperationType.CREATE, "TestOne", "first", "Text Value") |
| 137 | + create_op_2 = LmdbDatabaseOperation(LmdbDatabaseOperationType.CREATE, "TestOne", "second", "Text Second Value") |
| 138 | + delete_op_1 = LmdbDatabaseOperation(LmdbDatabaseOperationType.DELETE, "TestOne", "first") |
| 139 | + read_op_1 = LmdbDatabaseOperation(LmdbDatabaseOperationType.READ, "TestOne", "second") |
| 140 | + read_op_2 = LmdbDatabaseOperation(LmdbDatabaseOperationType.READ, "TestOne", "first") |
| 141 | + |
| 142 | + async with LmdbDatabaseClient(self.path) as client: |
| 143 | + await client.execute(create_op_1) |
| 144 | + await client.execute(create_op_2) |
| 145 | + await client.execute(delete_op_1) |
| 146 | + |
| 147 | + await client.execute(read_op_1) |
| 148 | + self.assertEqual("Text Second Value", await client.fetch_one()) |
| 149 | + |
| 150 | + await client.execute(read_op_2) |
| 151 | + self.assertEqual(None, await client.fetch_one()) |
| 152 | + |
| 153 | + async def test_execute_update(self): |
| 154 | + create_op = LmdbDatabaseOperation(LmdbDatabaseOperationType.CREATE, "TestOne", "first", "Text Value") |
| 155 | + update_op = LmdbDatabaseOperation(LmdbDatabaseOperationType.UPDATE, "TestOne", "first", "Updated Text Value") |
| 156 | + read_op = LmdbDatabaseOperation(LmdbDatabaseOperationType.READ, "TestOne", "first") |
| 157 | + |
| 158 | + async with LmdbDatabaseClient(self.path) as client: |
| 159 | + await client.execute(create_op) |
| 160 | + await client.execute(read_op) |
| 161 | + |
| 162 | + self.assertEqual("Text Value", await client.fetch_one()) |
| 163 | + |
| 164 | + await client.execute(update_op) |
| 165 | + await client.execute(read_op) |
| 166 | + |
| 167 | + self.assertEqual("Updated Text Value", await client.fetch_one()) |
14 | 168 |
|
15 | 169 |
|
16 | 170 | if __name__ == "__main__": |
|
0 commit comments