|
| 1 | +from objectbox import * |
| 2 | +from objectbox.model import * |
| 3 | +from objectbox.model.idsync import sync_model |
| 4 | +from objectbox.c import CoreException |
| 5 | +import json |
| 6 | +from pprint import pprint |
| 7 | +import os |
| 8 | +import tests.model |
| 9 | +import pytest |
| 10 | + |
| 11 | +class _TestEnv: |
| 12 | + """Test setup/tear-down of model json files, db store and utils.""" |
| 13 | + def __init__(self): |
| 14 | + self.model_path = 'test.json' |
| 15 | + if os.path.exists(self.model_path): |
| 16 | + os.remove(self.model_path) |
| 17 | + self.db_path = 'testdb' |
| 18 | + Store.remove_db_files(self.db_path) |
| 19 | + def sync(self, model): |
| 20 | + self.model = model |
| 21 | + sync_model(self.model, self.model_path) |
| 22 | + def json(self): |
| 23 | + return json.load(open(self.model_path)) |
| 24 | + def store(self): |
| 25 | + return Store(model=self.model, directory=self.db_path) |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def env(): |
| 29 | + return _TestEnv() |
| 30 | + |
| 31 | +def test_empty(env): |
| 32 | + model = Model() |
| 33 | + env.sync(model) |
| 34 | + doc = env.json() |
| 35 | + assert doc['_note1'] |
| 36 | + assert doc['_note2'] |
| 37 | + assert doc['_note3'] |
| 38 | + assert len(doc['entities']) == 0 |
| 39 | + assert doc['lastEntityId'] == '0:0' |
| 40 | + assert doc['lastIndexId'] == '0:0' # NOTE: objectbox-generator outputs "" |
| 41 | + assert doc['modelVersionParserMinimum'] >= 5 |
| 42 | + # debug: pprint(doc) |
| 43 | + # |
| 44 | + # TODO: sync with objectbox-generator empty fbs |
| 45 | + # assert doc['modelVersion'] == 5 |
| 46 | + # assert doc['lastIndex'] == "" |
| 47 | + # assert doc['lastRelationId'] == "" |
| 48 | + # assert len(doc['retiredEntityUids']) == 0 |
| 49 | + # assert len(doc['retiredIndexUids']) == 0 |
| 50 | + # assert len(doc['retiredPropertyUids']) == 0 |
| 51 | + # assert len(doc['retiredRelationUids']) == 0 |
| 52 | + # assert len(doc['version']) == 1 |
| 53 | + |
| 54 | +def test_basics(env): |
| 55 | + @Entity() |
| 56 | + class MyEntity: |
| 57 | + id = Id() |
| 58 | + name = Property(str) |
| 59 | + model = Model() |
| 60 | + model.entity(MyEntity) |
| 61 | + env.sync(model) |
| 62 | + doc = env.json() |
| 63 | + # debug: pprint(doc) |
| 64 | + e0 = doc['entities'][0] |
| 65 | + assert e0['id'] == str(MyEntity.iduid) |
| 66 | + assert e0['name'] == "MyEntity" |
| 67 | + props = e0['properties'] |
| 68 | + assert props[0]['id'] == str(MyEntity.get_property('id').iduid) |
| 69 | + |
| 70 | + # create new database and populate with two objects |
| 71 | + store = env.store() |
| 72 | + entityBox = store.box(MyEntity) |
| 73 | + entityBox.put(MyEntity(name="foo"),MyEntity(name="bar")) |
| 74 | + assert entityBox.count() == 2 |
| 75 | + del entityBox |
| 76 | + store.close() |
| 77 | + del store |
| 78 | + |
| 79 | + # recreate model using existing model json and open existing database |
| 80 | + model = Model() |
| 81 | + @Entity() |
| 82 | + class MyEntity: |
| 83 | + id = Id() |
| 84 | + name = Property(str) |
| 85 | + model.entity(MyEntity) |
| 86 | + env.sync(model) |
| 87 | + |
| 88 | + # open existing database |
| 89 | + store = env.store() |
| 90 | + entityBox = store.box(MyEntity) |
| 91 | + assert entityBox.count() == 2 |
| 92 | + |
| 93 | +def test_entity_add(env): |
| 94 | + @Entity() |
| 95 | + class MyEntity1: |
| 96 | + id = Id() |
| 97 | + name = Property(str) |
| 98 | + model = Model() |
| 99 | + model.entity(MyEntity1) |
| 100 | + env.sync(model) |
| 101 | + store = env.store() |
| 102 | + box = store.box(MyEntity1) |
| 103 | + box.put( MyEntity1(name="foo"), MyEntity1(name="bar")) |
| 104 | + assert box.count() == 2 |
| 105 | + store.close() |
| 106 | + del store |
| 107 | + |
| 108 | + @Entity() |
| 109 | + class MyEntity2: |
| 110 | + id = Id() |
| 111 | + name = Property(str) |
| 112 | + value = Property(int) |
| 113 | + model = Model() |
| 114 | + model.entity(MyEntity1) |
| 115 | + model.entity(MyEntity2) |
| 116 | + env.sync(model) |
| 117 | + store = env.store() |
| 118 | + box1 = store.box(MyEntity1) |
| 119 | + assert box1.count() == 2 |
| 120 | + box2 = store.box(MyEntity2) |
| 121 | + box2.put( MyEntity2(name="foo"), MyEntity2(name="bar")) |
| 122 | + assert box2.count() == 2 |
| 123 | + |
| 124 | +def test_entity_remove(env): |
| 125 | + @Entity() |
| 126 | + class MyEntity1: |
| 127 | + id = Id() |
| 128 | + name = Property(str) |
| 129 | + @Entity() |
| 130 | + class MyEntity2: |
| 131 | + id = Id() |
| 132 | + name = Property(str) |
| 133 | + value = Property(int) |
| 134 | + model = Model() |
| 135 | + model.entity(MyEntity1) |
| 136 | + model.entity(MyEntity2) |
| 137 | + env.sync(model) |
| 138 | + store = env.store() |
| 139 | + box1 = store.box(MyEntity1) |
| 140 | + box1.put( MyEntity1(name="foo"), MyEntity1(name="bar")) |
| 141 | + box2 = store.box(MyEntity2) |
| 142 | + box2.put( MyEntity2(name="foo"), MyEntity2(name="bar")) |
| 143 | + assert box1.count() == 2 |
| 144 | + assert box2.count() == 2 |
| 145 | + |
| 146 | + store.close() |
| 147 | + del store |
| 148 | + |
| 149 | + # Re-create a model without MyEntity2 |
| 150 | + |
| 151 | + model = Model() |
| 152 | + model.entity(MyEntity1) |
| 153 | + env.sync(model) |
| 154 | + store = env.store() |
| 155 | + box1 = store.box(MyEntity1) |
| 156 | + assert box1.count() == 2 |
| 157 | + |
| 158 | + # MyEntity2 is gone and should raise CoreException |
| 159 | + with pytest.raises(CoreException): |
| 160 | + box2 = store.box(MyEntity2) |
| 161 | + |
| 162 | +def test_entity_rename(env): |
| 163 | + model = Model() |
| 164 | + @Entity() |
| 165 | + class MyEntity: |
| 166 | + id = Id() |
| 167 | + name = Property(str) |
| 168 | + model.entity(MyEntity) |
| 169 | + env.sync(model) |
| 170 | + |
| 171 | + # Save uid of entity for renaming purposes.. |
| 172 | + uid = MyEntity.uid # iduid.uid |
| 173 | + # Debug: print("UID: "+ str(uid)) |
| 174 | + |
| 175 | + store = env.store() |
| 176 | + box = store.box(MyEntity) |
| 177 | + box.put(MyEntity(name="foo"),MyEntity(name="bar")) |
| 178 | + assert box.count() == 2 |
| 179 | + del box |
| 180 | + store.close() |
| 181 | + del store |
| 182 | + |
| 183 | + @Entity(uid=uid) |
| 184 | + class MyRenamedEntity: |
| 185 | + id = Id() |
| 186 | + name = Property(str) |
| 187 | + |
| 188 | + model = Model() |
| 189 | + model.entity(MyRenamedEntity) |
| 190 | + env.sync(model) |
| 191 | + store = env.store() |
| 192 | + box = store.box(MyRenamedEntity) |
| 193 | + assert box.count() == 2 |
| 194 | + |
| 195 | + |
| 196 | +def test_prop_add(env): |
| 197 | + |
| 198 | + @Entity() |
| 199 | + class MyEntity: |
| 200 | + id = Id() |
| 201 | + name = Property(str) |
| 202 | + model = Model() |
| 203 | + model.entity(MyEntity) |
| 204 | + env.sync(model) |
| 205 | + store = env.store() |
| 206 | + box = store.box(MyEntity) |
| 207 | + box.put( MyEntity(name="foo"), MyEntity(name="bar")) |
| 208 | + del box |
| 209 | + store.close() |
| 210 | + del store |
| 211 | + |
| 212 | + @Entity() |
| 213 | + class MyEntity: |
| 214 | + id = Id() |
| 215 | + name = Property(str) |
| 216 | + value = Property(int, type=PropertyType.int) |
| 217 | + |
| 218 | + model = Model() |
| 219 | + model.entity(MyEntity) |
| 220 | + env.sync(model) |
| 221 | + store = env.store() |
| 222 | + box = store.box(MyEntity) |
| 223 | + |
| 224 | + assert box.count() == 2 |
| 225 | + |
| 226 | +def test_prop_remove(env): |
| 227 | + |
| 228 | + @Entity() |
| 229 | + class MyEntity: |
| 230 | + id = Id() |
| 231 | + name = Property(str) |
| 232 | + value = Property(int, type=PropertyType.int) |
| 233 | + |
| 234 | + model = Model() |
| 235 | + model.entity(MyEntity) |
| 236 | + env.sync(model) |
| 237 | + store = env.store() |
| 238 | + box = store.box(MyEntity) |
| 239 | + box.put( MyEntity(name="foo"), MyEntity(name="bar")) |
| 240 | + del box |
| 241 | + store.close() |
| 242 | + del store |
| 243 | + |
| 244 | + @Entity() |
| 245 | + class MyEntity: |
| 246 | + id = Id() |
| 247 | + name = Property(str) |
| 248 | + |
| 249 | + model = Model() |
| 250 | + model.entity(MyEntity) |
| 251 | + env.sync(model) |
| 252 | + store = env.store() |
| 253 | + box = store.box(MyEntity) |
| 254 | + assert box.count() == 2 |
| 255 | + |
| 256 | +# TODO: test_prop_rename ? Do we need a uid annotation for properties then? |
0 commit comments