Skip to content

Commit f2de55b

Browse files
committed
Merge branch '32-get-remove-should-return-values-instead-of-raising-exceptions-on-not-found' into 'dev'
Resolve "get/remove should return values instead of raising exceptions on not found" Closes #32 See merge request objectbox/objectbox-python!22
2 parents e05aa2e + e7e3b24 commit f2de55b

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

objectbox/box.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,12 @@ def get(self, id: int):
112112
with self._ob.read_tx():
113113
c_data = ctypes.c_void_p()
114114
c_size = ctypes.c_size_t()
115-
obx_box_get(self._c_box, id, ctypes.byref(
116-
c_data), ctypes.byref(c_size))
117-
115+
code : obx_err = obx_box_get(self._c_box, id, ctypes.byref(
116+
c_data), ctypes.byref(c_size))
117+
if code == 404:
118+
return None
119+
elif code != 0:
120+
raise CoreException(code)
118121
data = c_voidp_as_bytes(c_data, c_size.value)
119122
return self._entity.unmarshal(data)
120123

@@ -143,7 +146,12 @@ def remove(self, id_or_object):
143146
id = self._entity.get_object_id(id_or_object)
144147
else:
145148
id = id_or_object
146-
obx_box_remove(self._c_box, id)
149+
code : obx_err = obx_box_remove(self._c_box, id)
150+
if code == 404:
151+
return False
152+
elif code != 0:
153+
raise CoreException(code)
154+
return True
147155

148156
def remove_all(self) -> int:
149157
count = ctypes.c_uint64()

objectbox/c.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,13 @@ def c_fn(name: str, restype: Optional[type], argtypes):
292292

293293
return func
294294

295+
# creates a global function "name" with the given restype & argtypes, calling C function with the same name.
296+
# no error checking is done on restype as this is defered to higher-level functions.
297+
def c_fn_nocheck(name: str, restype: type, argtypes):
298+
func = C.__getattr__(name)
299+
func.argtypes = argtypes
300+
func.restype = restype
301+
return func
295302

296303
# like c_fn, but for functions returning obx_err
297304
def c_fn_rc(name: str, argtypes):
@@ -302,7 +309,6 @@ def c_fn_rc(name: str, argtypes):
302309
func.errcheck = check_obx_err
303310
return func
304311

305-
306312
def c_fn_qb_cond(name: str, argtypes):
307313
""" Like c_fn, but for functions returning obx_qb_cond (checks obx_qb_cond validity). """
308314
func = C.__getattr__(name)
@@ -462,7 +468,7 @@ def c_array_pointer(py_list: Union[List[Any], np.ndarray], c_type):
462468
obx_box = c_fn('obx_box', OBX_box_p, [OBX_store_p, obx_schema_id])
463469

464470
# obx_err (OBX_box* box, obx_id id, const void** data, size_t* size);
465-
obx_box_get = c_fn_rc('obx_box_get', [
471+
obx_box_get = c_fn_nocheck('obx_box_get', obx_err, [
466472
OBX_box_p, obx_id, ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_size_t)])
467473

468474
# OBX_bytes_array* (OBX_box* box);
@@ -483,7 +489,7 @@ def c_array_pointer(py_list: Union[List[Any], np.ndarray], c_type):
483489
OBX_box_p, OBX_bytes_array_p, ctypes.POINTER(obx_id), OBXPutMode])
484490

485491
# obx_err (OBX_box* box, obx_id id);
486-
obx_box_remove = c_fn_rc('obx_box_remove', [OBX_box_p, obx_id])
492+
obx_box_remove = c_fn_nocheck('obx_box_remove', obx_err, [OBX_box_p, obx_id])
487493

488494
# obx_err (OBX_box* box, uint64_t* out_count);
489495
obx_box_remove_all = c_fn_rc('obx_box_remove_all', [

tests/test_box.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,17 @@ def test_box_basics():
7777

7878
# remove
7979
box.remove(object)
80-
box.remove(1)
80+
81+
# remove should return success
82+
success = box.remove(1)
83+
assert success == True
84+
success = box.remove(1)
85+
assert success == False
8186

8287
# check they're gone
8388
assert box.count() == 0
84-
with pytest.raises(objectbox.NotFoundException):
85-
box.get(object.id)
86-
with pytest.raises(objectbox.NotFoundException):
87-
box.get(1)
89+
assert box.get(object.id) == None
90+
assert box.get(1) == None
8891

8992
ob.close()
9093

@@ -166,14 +169,13 @@ def test_datetime():
166169
assert pytest.approx(read.date.timestamp()) == object.date.timestamp()
167170

168171
# remove
169-
box.remove(object)
172+
success = box.remove(object)
173+
assert success == True
170174

171175
# check they're gone
172176
assert box.count() == 0
173-
with pytest.raises(objectbox.NotFoundException):
174-
box.get(object.id)
175-
with pytest.raises(objectbox.NotFoundException):
176-
box.get(1)
177+
assert box.get(object.id) == None
178+
assert box.get(1) == None
177179

178180
ob.close()
179181

0 commit comments

Comments
 (0)