Skip to content

Commit c40b738

Browse files
committed
tests: fix query test & other minor issue objectbox#24
1 parent 7c5464b commit c40b738

File tree

3 files changed

+60
-28
lines changed

3 files changed

+60
-28
lines changed

objectbox/box.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def remove_all(self) -> int:
149149
count = ctypes.c_uint64()
150150
obx_box_remove_all(self._c_box, ctypes.byref(count))
151151
return int(count.value)
152-
153-
def query(self, condition: QueryCondition) -> QueryBuilder:
154-
qb = QueryBuilder(self._ob, self, self._entity, condition)
155-
return qb
152+
153+
def query(self) -> QueryBuilder:
154+
""" Creates a QueryBuilder for the Entity managed by the Box. """
155+
return QueryBuilder(self._ob, self)

tests/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def load_empty_test_objectbox(db_name: str = test_dir) -> objectbox.ObjectBox:
2828
model = objectbox.Model()
2929
model.entity(TestEntity, last_property_id=IdUid(27, 1027))
3030
model.last_entity_id = IdUid(2, 2)
31+
model.last_index_id = IdUid(2, 10002)
3132

3233
return objectbox.Builder().model(model).directory(db_name).build()
3334

tests/test_query.py

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,85 +18,112 @@ def test_query_basics():
1818
id1 = box.put(object1)
1919
box.put(object2)
2020

21-
2221
# String queries
22+
str_prop: Property = TestEntity.get_property("str")
2323

24-
str_prop: Property = TestEntity.properties[1]
25-
query = box.query(str_prop.equals("bar")).build()
24+
query = box.query() \
25+
.equals_string(str_prop._id, "bar", True) \
26+
.build()
2627
assert query.count() == 1
2728
assert query.find()[0].str == "bar"
2829

29-
query = box.query(str_prop.not_equals("bar")).build()
30+
query = box.query() \
31+
.not_equals_string(str_prop._id, "bar", True) \
32+
.build()
3033
assert query.count() == 1
3134
assert query.find()[0].str == "foo"
3235

33-
query = box.query(str_prop.contains("ba")).build()
36+
query = box.query() \
37+
.contains_string(str_prop._id, "ba", True) \
38+
.build()
3439
assert query.count() == 1
3540
assert query.find()[0].str == "bar"
3641

37-
query = box.query(str_prop.starts_with("f")).build()
42+
query = box.query() \
43+
.starts_with_string(str_prop._id, "f", True) \
44+
.build()
3845
assert query.count() == 1
3946
assert query.find()[0].str == "foo"
4047

41-
query = box.query(str_prop.ends_with("o")).build()
48+
query = box.query() \
49+
.ends_with_string(str_prop._id, "o", True) \
50+
.build()
4251
assert query.count() == 1
4352
assert query.find()[0].str == "foo"
4453

45-
query = box.query(str_prop.greater_than("bar")).build()
54+
query = box.query() \
55+
.greater_than_string(str_prop._id, "bar", True) \
56+
.build()
4657
assert query.count() == 1
4758
assert query.find()[0].str == "foo"
4859

49-
query = box.query(str_prop.greater_or_equal("bar")).build()
60+
query = box.query() \
61+
.greater_or_equal_string(str_prop._id, "bar", True) \
62+
.build()
5063
assert query.count() == 2
5164
assert query.find()[0].str == "foo"
5265
assert query.find()[1].str == "bar"
5366

54-
query = box.query(str_prop.less_than("foo")).build()
67+
query = box.query() \
68+
.less_than_string(str_prop._id, "foo", True) \
69+
.build()
5570
assert query.count() == 1
5671
assert query.find()[0].str == "bar"
5772

58-
query = box.query(str_prop.less_or_equal("foo")).build()
73+
query = box.query() \
74+
.less_or_equal_string(str_prop._id, "foo", True) \
75+
.build()
5976
assert query.count() == 2
6077
assert query.find()[0].str == "foo"
6178
assert query.find()[1].str == "bar"
6279

63-
6480
# Int queries
81+
int_prop: Property = TestEntity.get_property("int64")
6582

66-
int_prop: Property = TestEntity.properties[3]
67-
query = box.query(int_prop.equals(123)).build()
83+
query = box.query() \
84+
.equals_int(int_prop._id, 123) \
85+
.build()
6886
assert query.count() == 1
6987
assert query.find()[0].int64 == 123
7088

71-
query = box.query(int_prop.not_equals(123)).build()
89+
query = box.query() \
90+
.not_equals_int(int_prop._id, 123) \
91+
.build()
7292
assert query.count() == 1
7393
assert query.find()[0].int64 == 456
7494

75-
query = box.query(int_prop.greater_than(123)).build()
95+
query = box.query() \
96+
.greater_than_int(int_prop._id, 123) \
97+
.build()
7698
assert query.count() == 1
7799
assert query.find()[0].int64 == 456
78100

79-
query = box.query(int_prop.greater_or_equal(123)).build()
101+
query = box.query() \
102+
.greater_or_equal_int(int_prop._id, 123) \
103+
.build()
80104
assert query.count() == 2
81105
assert query.find()[0].int64 == 123
82106
assert query.find()[1].int64 == 456
83107

84-
query = box.query(int_prop.less_than(456)).build()
108+
query = box.query() \
109+
.less_than_int(int_prop._id, 456) \
110+
.build()
85111
assert query.count() == 1
86112
assert query.find()[0].int64 == 123
87113

88-
query = box.query(int_prop.less_or_equal(456)).build()
114+
query = box.query() \
115+
.less_or_equal_int(int_prop._id, 456) \
116+
.build()
89117
assert query.count() == 2
90118
assert query.find()[0].int64 == 123
91119
assert query.find()[1].int64 == 456
92120

93-
query = box.query(int_prop.between(100, 200)).build()
121+
query = box.query() \
122+
.between_2ints(int_prop._id, 100, 200) \
123+
.build()
94124
assert query.count() == 1
95125
assert query.find()[0].int64 == 123
96126

97-
with pytest.raises(CoreException):
98-
box.query(int_prop.equals("foo")).build()
99-
100127
assert query.remove() == 1
101128

102129
ob.close()
@@ -114,7 +141,11 @@ def test_offset_limit():
114141
object3.str = "c"
115142
box.put([object0, object1, object2, object3])
116143

117-
query = box.query(TestEntity.properties[3].equals(0)).build()
144+
int_prop: Property = TestEntity.get_property("int64")
145+
146+
query = box.query() \
147+
.equals_int(int_prop._id, 0) \
148+
.build()
118149
assert query.count() == 4
119150

120151
query.offset(2)

0 commit comments

Comments
 (0)