|
| 1 | +from objectbox.model import * |
| 2 | +from objectbox import * |
| 3 | +from objectbox.model.idsync import sync_model |
| 4 | +import os |
| 5 | +import os.path |
| 6 | + |
| 7 | +def test_reuse_model(): |
| 8 | + @Entity() |
| 9 | + class MyEntity: |
| 10 | + id = Id() |
| 11 | + name = String() |
| 12 | + |
| 13 | + model = Model() |
| 14 | + model.entity(MyEntity) |
| 15 | + model_filepath = "test-model.json" |
| 16 | + if os.path.exists(model_filepath): |
| 17 | + os.remove(model_filepath) |
| 18 | + sync_model(model, model_filepath) |
| 19 | + |
| 20 | + db1path = "test-db1" |
| 21 | + db2path = "test-db2" |
| 22 | + Store.remove_db_files(db1path) |
| 23 | + Store.remove_db_files(db2path) |
| 24 | + |
| 25 | + store1 = Store(model=model, directory=db1path) |
| 26 | + store2 = Store(model=model, directory=db2path) |
| 27 | + |
| 28 | + store1.close() |
| 29 | + store2.close() |
| 30 | + |
| 31 | + |
| 32 | +def test_reuse_entity(): |
| 33 | + @Entity() |
| 34 | + class MyEntity: |
| 35 | + id = Id() |
| 36 | + name = String() |
| 37 | + |
| 38 | + m1 = Model() |
| 39 | + m1.entity(MyEntity) |
| 40 | + model_filepath = "test-model1.json" |
| 41 | + if os.path.exists(model_filepath): |
| 42 | + os.remove(model_filepath) |
| 43 | + sync_model(m1, model_filepath) |
| 44 | + |
| 45 | + db1path = "test-db1" |
| 46 | + db2path = "test-db2" |
| 47 | + Store.remove_db_files(db1path) |
| 48 | + Store.remove_db_files(db2path) |
| 49 | + |
| 50 | + store1 = Store(model=m1, directory=db1path) |
| 51 | + |
| 52 | + box1 = store1.box(MyEntity) |
| 53 | + box1.put(MyEntity(name="foo")) |
| 54 | + assert box1.count() == 1 |
| 55 | + |
| 56 | + m2 = Model() |
| 57 | + |
| 58 | + @Entity() |
| 59 | + class MyEntity2: |
| 60 | + id = Id() |
| 61 | + name = String() |
| 62 | + value = Int64() |
| 63 | + |
| 64 | + m2.entity(MyEntity2) |
| 65 | + m2.entity(MyEntity) |
| 66 | + model_filepath = "test-model2.json" |
| 67 | + if os.path.exists(model_filepath): |
| 68 | + os.remove(model_filepath) |
| 69 | + sync_model(m2, model_filepath) |
| 70 | + |
| 71 | + store2 = Store(model=m2, directory=db2path) |
| 72 | + box2 = store2.box(MyEntity) |
| 73 | + box2.put(MyEntity(name="bar")) |
| 74 | + box2.put(MyEntity(name="bar")) |
| 75 | + box2.put(MyEntity(name="bar")) |
| 76 | + assert box2.count() == 3 |
| 77 | + |
| 78 | + box1.put(MyEntity(name="foo")) |
| 79 | + box1.put(MyEntity(name="foo")) |
| 80 | + assert box1.count() == 3 |
| 81 | + |
| 82 | + store1.close() |
| 83 | + store2.close() |
0 commit comments