Skip to content

Commit f07dbff

Browse files
committed
test internals to check property vs internal method/attribute name class #29
1 parent 875599d commit f07dbff

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

tests/test_internals.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from objectbox import *
2+
from objectbox.model import *
3+
import pytest
4+
5+
def test_property_name_clash():
6+
@Entity(id=1, uid=1)
7+
class MyEntity:
8+
id = Id(id=1, uid=5001)
9+
uid = String(id=2, uid=5002)
10+
cls = String(id=3, uid=5003)
11+
name = String(id=4, uid=5004)
12+
last_property_id = String(id=5, uid=5005)
13+
properties = String(id=6, uid=5006)
14+
_id = String(id=7, uid=5007) # a bad one; this one don't work directly
15+
a_safe_one = String(id=8, uid=5008)
16+
17+
model = Model()
18+
model.entity(MyEntity, last_property_id=IdUid(8, 5008))
19+
model.last_entity_id = IdUid(1, 1)
20+
21+
dbpath = "testdb"
22+
Store.remove_db_files(dbpath)
23+
store = Store(model=model, directory=dbpath)
24+
box = store.box(MyEntity)
25+
id1 = box.put(
26+
MyEntity(
27+
uid="123",
28+
cls="foo",
29+
name="bar",
30+
last_property_id="blub",
31+
properties="baz",
32+
_id="fooz",
33+
a_safe_one="blah",
34+
)
35+
)
36+
assert box.count() == 1
37+
38+
assert len(box.query(MyEntity.id.equals(id1)).build().find()) == 1
39+
assert len(box.query(MyEntity.uid.equals("123")).build().find()) == 1
40+
assert len(box.query(MyEntity.cls.equals("foo")).build().find()) == 1
41+
assert len(box.query(MyEntity.name.equals("bar")).build().find()) == 1
42+
assert len(box.query(MyEntity.last_property_id.equals("blub")).build().find()) == 1
43+
assert len(box.query(MyEntity.properties.equals("baz")).build().find()) == 1
44+
with pytest.raises(AttributeError):
45+
MyEntity._id.equals("fooz")
46+
assert len(box.query(MyEntity._get_property("_id").equals("fooz")).build().find()) == 1
47+
assert len(box.query(MyEntity.a_safe_one.equals("blah")).build().find()) == 1
48+
49+
50+
def test_entity_attribute_methods_nameclash_check():
51+
52+
# Test ensures we do not leave occasional instance attributes or class methods/attributes in
53+
# helper class _Entity which might collide with user-defined property names.
54+
# (We expect users not use use underscore to guarantee convient access to properties as-is via '.' operator)
55+
56+
# To check instance as well as class data, we create a dummy entity which we'll scan next.
57+
@Entity(id=1, uid=1)
58+
class MyEntity:
59+
id = Id(id=1, uid=5001)
60+
61+
not_prefixed = []
62+
63+
for attrname in MyEntity.__dict__:
64+
if not attrname.startswith("_"):
65+
not_prefixed.append(attrname)
66+
67+
for methodname in MyEntity.__class__.__dict__:
68+
if not methodname.startswith("_"):
69+
not_prefixed.append(methodname)
70+
71+
assert (
72+
len(not_prefixed) == 0
73+
), f"INTERNAL: Public attributes/methods(s) detected in Class _Entity: {not_prefixed}\nPlease prefix with '_' to prevent name-collision with Property field-names."

0 commit comments

Comments
 (0)