|
1 | | -import pytest |
2 | 1 | import objectbox |
3 | | -from tests.model import TestEntity |
4 | 2 | from tests.common import autocleanup, load_empty_test_objectbox, assert_equal |
5 | 3 |
|
| 4 | + |
6 | 5 | def test_version(): |
7 | 6 | info = objectbox.version_info() |
8 | 7 | assert len(info) > 10 |
9 | 8 |
|
10 | 9 |
|
11 | 10 | def test_open(): |
12 | 11 | 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 |
0 commit comments