|
1 | 1 | import pytest |
2 | 2 | import objectbox |
| 3 | +from tests.model import TestEntity, TestEntityDatetime |
| 4 | +from tests.common import ( |
| 5 | + autocleanup, |
| 6 | + load_empty_test_objectbox, |
| 7 | + load_empty_test_datetime, |
| 8 | + assert_equal, |
| 9 | +) |
3 | 10 | import numpy as np |
4 | | -from tests.model import TestEntity |
5 | | -from tests.common import autocleanup, load_empty_test_objectbox, assert_equal |
| 11 | +from datetime import datetime |
6 | 12 | import time |
7 | 13 | from math import floor |
8 | 14 |
|
@@ -110,3 +116,83 @@ def test_box_bulk(): |
110 | 116 | removed = box.remove_all() |
111 | 117 | assert removed == 4 |
112 | 118 | assert box.count() == 0 |
| 119 | + |
| 120 | + |
| 121 | +def test_datetime(): |
| 122 | + ob = load_empty_test_datetime() |
| 123 | + box = objectbox.Box(ob, TestEntityDatetime) |
| 124 | + |
| 125 | + assert box.is_empty() |
| 126 | + assert box.count() == 0 |
| 127 | + |
| 128 | + # create |
| 129 | + object = TestEntityDatetime() |
| 130 | + id = box.put(object) |
| 131 | + assert id == 1 |
| 132 | + assert id == object.id |
| 133 | + |
| 134 | + # create with a given ID and some data |
| 135 | + object = TestEntityDatetime() |
| 136 | + object.id = 5 |
| 137 | + object.date = datetime.utcnow() # milliseconds since UNIX epoch |
| 138 | + object.date_nano = datetime.utcnow() # nanoseconds since UNIX epoch |
| 139 | + |
| 140 | + id = box.put(object) |
| 141 | + assert id == 5 |
| 142 | + assert id == object.id |
| 143 | + # check the count |
| 144 | + assert not box.is_empty() |
| 145 | + assert box.count() == 2 |
| 146 | + |
| 147 | + # read |
| 148 | + read = box.get(object.id) |
| 149 | + assert pytest.approx(read.date.timestamp()) == object.date.timestamp() |
| 150 | + |
| 151 | + # update |
| 152 | + object.str = "bar" |
| 153 | + object.date = datetime.utcnow() |
| 154 | + object.date_nano = datetime.utcnow() |
| 155 | + id = box.put(object) |
| 156 | + assert id == 5 |
| 157 | + |
| 158 | + # read again |
| 159 | + read = box.get(object.id) |
| 160 | + assert pytest.approx(read.date.timestamp()) == object.date.timestamp() |
| 161 | + |
| 162 | + # remove |
| 163 | + box.remove(object) |
| 164 | + box.remove(1) |
| 165 | + |
| 166 | + # check they're gone |
| 167 | + assert box.count() == 0 |
| 168 | + with pytest.raises(objectbox.NotFoundException): |
| 169 | + box.get(object.id) |
| 170 | + with pytest.raises(objectbox.NotFoundException): |
| 171 | + box.get(1) |
| 172 | + |
| 173 | + |
| 174 | +def test_box_bulk_datetime(): |
| 175 | + ob = load_empty_test_datetime() |
| 176 | + box = objectbox.Box(ob, TestEntityDatetime) |
| 177 | + |
| 178 | + box.put(TestEntityDatetime("first")) |
| 179 | + |
| 180 | + objects = [TestEntityDatetime("second"), TestEntityDatetime("third"), |
| 181 | + TestEntityDatetime("fourth"), box.get(1)] |
| 182 | + box.put(objects) |
| 183 | + assert box.count() == 4 |
| 184 | + assert objects[0].id == 2 |
| 185 | + assert objects[1].id == 3 |
| 186 | + assert objects[2].id == 4 |
| 187 | + assert objects[3].id == 1 |
| 188 | + |
| 189 | + objects_read = box.get_all() |
| 190 | + assert len(objects_read) == 4 |
| 191 | + for object_read in objects_read: |
| 192 | + assert object_read.date == datetime.fromtimestamp(0) |
| 193 | + assert object_read.date_nano == datetime.fromtimestamp(0) |
| 194 | + |
| 195 | + # remove all |
| 196 | + removed = box.remove_all() |
| 197 | + assert removed == 4 |
| 198 | + assert box.count() == 0 |
0 commit comments