|
| 1 | +from objectbox import * |
| 2 | +from objectbox.model import * |
| 3 | + |
| 4 | + |
| 5 | +def test_userclass(): |
| 6 | + @Entity(id=1, uid=1) |
| 7 | + class Person: |
| 8 | + id = Id(id=1, uid=1001) |
| 9 | + firstName = Property(str, id=2, uid=1002) |
| 10 | + lastName = Property(str, id=3, uid=1003) |
| 11 | + |
| 12 | + def __init__(self): |
| 13 | + self.counter = 0 |
| 14 | + |
| 15 | + def fullname(self): |
| 16 | + return f"{self.firstName} {self.lastName}" |
| 17 | + |
| 18 | + def tick(self): |
| 19 | + self.counter += 1 |
| 20 | + |
| 21 | + model = Model() |
| 22 | + model.entity(Person, last_property_id=IdUid(3, 1003)) |
| 23 | + model.last_entity_id = IdUid(1, 1) |
| 24 | + dbpath = "testdb" |
| 25 | + Store.remove_db_files(dbpath) |
| 26 | + |
| 27 | + store = Store(model=model, directory=dbpath) |
| 28 | + box = store.box(Person) |
| 29 | + id_alice = box.put(Person(firstName="Alice", lastName="Adkinson")) |
| 30 | + box.put(Person(firstName="Bob", lastName="Bowman")) |
| 31 | + box.put(Person(firstName="Cydia", lastName="Cervesa")) |
| 32 | + assert box.count() == 3 |
| 33 | + alice = box.get(id_alice) |
| 34 | + assert alice.fullname() == "Alice Adkinson" |
| 35 | + assert alice.counter == 0 |
| 36 | + alice.tick() |
| 37 | + alice.tick() |
| 38 | + assert alice.counter == 2 |
| 39 | + |
| 40 | + alice = box.get(id_alice) |
| 41 | + assert alice.counter == 0 |
| 42 | + |
| 43 | + id_empty = box.put(Person()) |
| 44 | + empty = box.get(id_empty) |
| 45 | + assert empty.fullname() == " " |
0 commit comments