Skip to content

Commit 4598b21

Browse files
dan-obxgreenrobot
authored andcommitted
test: entity userclass with methods and non-persistent attributes #56
1 parent e41eff1 commit 4598b21

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

tests/test_userclass.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from objectbox import *
2+
from objectbox.model import *
3+
4+
def test_userclass():
5+
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+
def __init__(self):
12+
self.counter = 0
13+
def fullname(self):
14+
return f"{self.firstName} {self.lastName}"
15+
def tick(self):
16+
self.counter += 1
17+
18+
model = Model()
19+
model.entity(Person, last_property_id=IdUid(3, 1003))
20+
model.last_entity_id = IdUid(1,1)
21+
dbpath = "testdb"
22+
Store.remove_db_files(dbpath)
23+
store = Store(model = model, directory = dbpath)
24+
box = store.box(Person)
25+
id_alice = box.put( Person(firstName="Alice", lastName="Adkinson"))
26+
id2 = box.put( Person(firstName="Bob", lastName="Bowman"))
27+
id3 = box.put( Person(firstName="Cydia", lastName="Cervesa"))
28+
assert box.count() == 3
29+
alice = box.get(id_alice)
30+
assert alice.fullname() == "Alice Adkinson"
31+
assert alice.counter == 0
32+
alice.tick()
33+
alice.tick()
34+
assert alice.counter == 2

0 commit comments

Comments
 (0)