Skip to content

Commit ab13e8a

Browse files
Add unit tests for bulk put and get (#4, #15)
1 parent 7efb031 commit ab13e8a

File tree

1 file changed

+57
-7
lines changed

1 file changed

+57
-7
lines changed

objectbox_test/test/test.dart

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class TestEntity {
1212
String text;
1313

1414
TestEntity();
15+
TestEntity.constructWithId(this.id, this.text);
1516
TestEntity.construct(this.text);
1617
}
1718

@@ -30,20 +31,69 @@ main() {
3031
expect(putId, greaterThan(0));
3132
});
3233

33-
test(".get() returns the correct instance", () {
34-
int putId = box.put(TestEntity.construct("Hello"));
35-
final TestEntity inst = box.get(putId);
36-
expect(inst.id, equals(putId));
37-
expect(inst.text, equals("Hello"));
34+
test(".get() returns the correct item", () {
35+
final int putId = box.put(TestEntity.construct("Hello"));
36+
final TestEntity item = box.get(putId);
37+
expect(item.id, equals(putId));
38+
expect(item.text, equals("Hello"));
3839
});
3940

4041
test(".put() and box.get() keep Unicode characters", () {
41-
final text = "😄你好";
42+
final String text = "😄你好";
4243
final TestEntity inst = box.get(box.put(TestEntity.construct(text)));
4344
expect(inst.text, equals(text));
4445
});
4546

46-
// TODO: test box.putMany, box.getMany, box.getAll
47+
test(".put() can update an item", () {
48+
final int putId1 = box.put(TestEntity.construct("One"));
49+
final int putId2 = box.put(TestEntity.constructWithId(putId1, "Two"));
50+
expect(putId2, equals(putId1));
51+
final TestEntity item = box.get(putId2);
52+
expect(item.text, equals("Two"));
53+
});
54+
55+
test(".getAll retrieves all items", () {
56+
final int id1 = box.put(TestEntity.construct("One"));
57+
final int id2 = box.put(TestEntity.construct("Two"));
58+
final int id3 = box.put(TestEntity.construct("Three"));
59+
final List<TestEntity> items = box.getAll();
60+
expect(items.length, equals(3));
61+
expect(items.where((i) => i.id == id1).single.text, equals("One"));
62+
expect(items.where((i) => i.id == id2).single.text, equals("Two"));
63+
expect(items.where((i) => i.id == id3).single.text, equals("Three"));
64+
});
65+
66+
test(".putMany inserts multiple items", () {
67+
final List<TestEntity> items = [
68+
TestEntity.construct("One"),
69+
TestEntity.construct("Two"),
70+
TestEntity.construct("Three")
71+
];
72+
box.putMany(items);
73+
final List<TestEntity> itemsFetched = box.getAll();
74+
expect(itemsFetched.length, equals(items.length));
75+
});
76+
77+
test(".putMany returns the new item IDs", () {
78+
final List<TestEntity> items = ["One", "Two", "Three", "Four", "Five", "Six", "Seven"].map((s) => TestEntity.construct(s)).toList();
79+
final List<int> ids = box.putMany(items);
80+
expect(ids.length, equals(items.length));
81+
for(int i = 0; i < items.length; ++i)
82+
expect(box.get(ids[i]).text, equals(items[i].text));
83+
});
84+
85+
test(".getMany correctly handles non-existant items", () {
86+
final List<TestEntity> items = ["One", "Two"].map((s) => TestEntity.construct(s)).toList();
87+
final List<int> ids = box.putMany(items);
88+
int otherId = 1;
89+
while(ids.indexWhere((id) => id == otherId) != -1)
90+
++otherId;
91+
final List<TestEntity> fetchedItems = box.getMany([ids[0], otherId, ids[1]]);
92+
expect(fetchedItems.length, equals(3));
93+
expect(fetchedItems[0].text, equals("One"));
94+
expect(fetchedItems[1], equals(null));
95+
expect(fetchedItems[2].text, equals("Two"));
96+
});
4797
});
4898

4999
tearDown(() {

0 commit comments

Comments
 (0)