Skip to content

Commit 979150a

Browse files
author
Ivan Dlugos
committed
split tests and finish transactions
1 parent 139b2d0 commit 979150a

File tree

7 files changed

+142
-94
lines changed

7 files changed

+142
-94
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ For more information and code examples, see the tests folder. The docs for other
6969
* ObjectBox Java = https://docs.objectbox.io
7070
* ObjectBox Go - https://golang.objectbox.io
7171
* ObjectBox Swift - https://swift.objectbox.io
72-
* ObjectBox Dart/Flutter - https://github.com
72+
* ObjectBox Dart/Flutter - https://github.com/objectbox/objectbox-dart
7373

7474
Some features
7575
-------------

objectbox/c.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def c_voidp_as_bytes(voidp, size):
336336
# obx_err (OBX_box* box, uint64_t count, obx_id* out_first_id);
337337
obx_box_ids_for_put = fn('obx_box_ids_for_put', obx_err, [OBX_box_p, ctypes.c_uint64, ctypes.POINTER(obx_id)])
338338

339-
# obx_err (OBX_box* box, obx_id id, const void* data, size_t size, OBXPutMode mode);
339+
# obx_err (OBX_box* box, obx_id id, const void* data, size_t size);
340340
obx_box_put = fn('obx_box_put', obx_err, [OBX_box_p, obx_id, ctypes.c_void_p, ctypes.c_size_t])
341341

342342
# obx_err (OBX_box* box, const OBX_bytes_array* objects, const obx_id* ids, OBXPutMode mode);

objectbox/objectbox.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ def __del__(self):
2626
def read_tx(self):
2727
return objectbox.transaction.read(self)
2828

29+
def write_tx(self):
30+
return objectbox.transaction.write(self)
2931

objectbox/transaction.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,7 @@ def write(ob: 'ObjectBox'):
3131
try:
3232
yield
3333
obx_txn_success(tx)
34-
finally:
34+
except:
35+
obx_txn_abort(tx)
3536
obx_txn_close(tx)
37+
raise

tests/test_basics.py

Lines changed: 1 addition & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,11 @@
1-
import pytest
21
import objectbox
3-
from tests.model import TestEntity
42
from tests.common import autocleanup, load_empty_test_objectbox, assert_equal
53

4+
65
def test_version():
76
info = objectbox.version_info()
87
assert len(info) > 10
98

109

1110
def test_open():
1211
load_empty_test_objectbox()
13-
14-
15-
def test_box_basics():
16-
ob = load_empty_test_objectbox()
17-
box = objectbox.Box(ob, TestEntity)
18-
19-
assert box.is_empty()
20-
assert box.count() == 0
21-
22-
# create
23-
object = TestEntity()
24-
id = box.put(object)
25-
assert id == 1
26-
assert id == object.id
27-
28-
# create with a given ID and some data
29-
object = TestEntity()
30-
object.id = 5
31-
object.bool = True
32-
object.int = 42
33-
object.str = "foo"
34-
object.float = 4.2
35-
object.bytes = bytes([1, 1, 2, 3, 5])
36-
object.transient = "abcd"
37-
38-
id = box.put(object)
39-
assert id == 5
40-
assert id == object.id
41-
42-
# check the count
43-
assert not box.is_empty()
44-
assert box.count() == 2
45-
46-
# read
47-
read = box.get(object.id)
48-
assert_equal(read, object)
49-
assert read.transient != object.transient # !=
50-
51-
# update
52-
object.str = "bar"
53-
id = box.put(object)
54-
assert id == 5
55-
56-
# read again
57-
read = box.get(object.id)
58-
assert_equal(read, object)
59-
60-
# remove
61-
box.remove(object)
62-
box.remove(1)
63-
64-
# check they're gone
65-
assert box.count() == 0
66-
with pytest.raises(objectbox.NotFoundException):
67-
box.get(object.id)
68-
with pytest.raises(objectbox.NotFoundException):
69-
box.get(1)
70-
71-
72-
def test_box_bulk():
73-
ob = load_empty_test_objectbox()
74-
box = objectbox.Box(ob, TestEntity)
75-
76-
box.put(TestEntity("first"))
77-
78-
objects = [TestEntity("second"), TestEntity("third"), TestEntity("fourth"), box.get(1)]
79-
box.put(objects)
80-
assert box.count() == 4
81-
assert objects[0].id == 2
82-
assert objects[1].id == 3
83-
assert objects[2].id == 4
84-
assert objects[3].id == 1
85-
86-
assert_equal(box.get(objects[0].id), objects[0])
87-
assert_equal(box.get(objects[1].id), objects[1])
88-
assert_equal(box.get(objects[2].id), objects[2])
89-
assert_equal(box.get(objects[3].id), objects[3])
90-
91-
objects_read = box.get_all()
92-
assert len(objects_read) == 4
93-
assert_equal(objects_read[0], objects[3])
94-
assert_equal(objects_read[1], objects[0])
95-
assert_equal(objects_read[2], objects[1])
96-
assert_equal(objects_read[3], objects[2])
97-
98-
# remove all
99-
removed = box.remove_all()
100-
assert removed == 4
101-
assert box.count() == 0

tests/test_box.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import pytest
2+
import objectbox
3+
from tests.model import TestEntity
4+
from tests.common import autocleanup, load_empty_test_objectbox, assert_equal
5+
6+
7+
def test_box_basics():
8+
ob = load_empty_test_objectbox()
9+
box = objectbox.Box(ob, TestEntity)
10+
11+
assert box.is_empty()
12+
assert box.count() == 0
13+
14+
# create
15+
object = TestEntity()
16+
id = box.put(object)
17+
assert id == 1
18+
assert id == object.id
19+
20+
# create with a given ID and some data
21+
object = TestEntity()
22+
object.id = 5
23+
object.bool = True
24+
object.int = 42
25+
object.str = "foo"
26+
object.float = 4.2
27+
object.bytes = bytes([1, 1, 2, 3, 5])
28+
object.transient = "abcd"
29+
30+
id = box.put(object)
31+
assert id == 5
32+
assert id == object.id
33+
34+
# check the count
35+
assert not box.is_empty()
36+
assert box.count() == 2
37+
38+
# read
39+
read = box.get(object.id)
40+
assert_equal(read, object)
41+
assert read.transient != object.transient # !=
42+
43+
# update
44+
object.str = "bar"
45+
id = box.put(object)
46+
assert id == 5
47+
48+
# read again
49+
read = box.get(object.id)
50+
assert_equal(read, object)
51+
52+
# remove
53+
box.remove(object)
54+
box.remove(1)
55+
56+
# check they're gone
57+
assert box.count() == 0
58+
with pytest.raises(objectbox.NotFoundException):
59+
box.get(object.id)
60+
with pytest.raises(objectbox.NotFoundException):
61+
box.get(1)
62+
63+
64+
def test_box_bulk():
65+
ob = load_empty_test_objectbox()
66+
box = objectbox.Box(ob, TestEntity)
67+
68+
box.put(TestEntity("first"))
69+
70+
objects = [TestEntity("second"), TestEntity("third"), TestEntity("fourth"), box.get(1)]
71+
box.put(objects)
72+
assert box.count() == 4
73+
assert objects[0].id == 2
74+
assert objects[1].id == 3
75+
assert objects[2].id == 4
76+
assert objects[3].id == 1
77+
78+
assert_equal(box.get(objects[0].id), objects[0])
79+
assert_equal(box.get(objects[1].id), objects[1])
80+
assert_equal(box.get(objects[2].id), objects[2])
81+
assert_equal(box.get(objects[3].id), objects[3])
82+
83+
objects_read = box.get_all()
84+
assert len(objects_read) == 4
85+
assert_equal(objects_read[0], objects[3])
86+
assert_equal(objects_read[1], objects[0])
87+
assert_equal(objects_read[2], objects[1])
88+
assert_equal(objects_read[3], objects[2])
89+
90+
# remove all
91+
removed = box.remove_all()
92+
assert removed == 4
93+
assert box.count() == 0

tests/test_transactions.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import pytest
2+
import objectbox
3+
from tests.model import TestEntity
4+
from tests.common import autocleanup, load_empty_test_objectbox, assert_equal
5+
6+
7+
def test_transactions():
8+
ob = load_empty_test_objectbox()
9+
box = objectbox.Box(ob, TestEntity)
10+
11+
assert box.is_empty()
12+
13+
with ob.write_tx():
14+
box.put(TestEntity("first"))
15+
box.put(TestEntity("second"))
16+
17+
assert box.count() == 2
18+
19+
try:
20+
with ob.write_tx():
21+
box.put(TestEntity("third"))
22+
box.put(TestEntity("fourth"))
23+
raise Exception("mission abort!")
24+
25+
# exception must be propagated so this line must not execute
26+
assert 0
27+
except Exception as err:
28+
assert str(err) == "mission abort!"
29+
30+
# changes have been rolled back
31+
assert box.count() == 2
32+
33+
# can't write in a read TX
34+
try:
35+
with ob.read_tx():
36+
box.put(TestEntity("third"))
37+
38+
# exception must be propagated so this line must not execute
39+
assert 0
40+
except Exception as err:
41+
assert "Cannot start a write transaction inside a read only transaction" in str(err)

0 commit comments

Comments
 (0)