-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhive_autoincrement_keys_test.dart
69 lines (55 loc) · 2.4 KB
/
hive_autoincrement_keys_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import 'package:example/features/books/cache/book_cache_manager.dart';
import 'package:example/features/books/model/book.dart';
import 'package:example/project/hive_constants.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hive/hive.dart';
void main() {
test('Hive Cache Manager Test', () async {
// initialize Hive before using
Hive.init(HiveConstants.hiveDbName);
// create your model objects
final Book firstBook = Book(
title: 'Lord of The Rings - The Fellowship of the Ring',
author: 'J. R. R. Tolkien',
year: 1954,
isbn: '9780618260515',
);
final Book secondBook = Book(
title: 'Lord of The Rings - The Two Towers',
author: 'J. R. R. Tolkien',
year: 1954,
isbn: '9780261102361',
);
final Book thirdBook = Book(
title: 'Lord of The Rings - The Return of the King',
author: 'J. R. R. Tolkien',
year: 1955,
isbn: '9780007136575',
);
// create and explicitly initialize a manager object
final BookCacheManager bookCacheManager = BookCacheManager(
"${HiveConstants.booksHiveBoxName}_autoincrement_keys");
await bookCacheManager.init(isEncrypted: false);
// clear all the objects written from the previous test run
await bookCacheManager.clearAll();
// the hive box should be empty since no items have been added yet
expect(bookCacheManager.getValues(), []);
// add your model objects to hive
final List<Book> books = [firstBook, secondBook, thirdBook];
await bookCacheManager.addItems(items: books);
// test the functionality of the hive_cache_manager
expect(bookCacheManager.getValues().first.isbn, '9780618260515');
// remove the object named "thirdBook" from the hive
// since the objects were added with a autoincrement key the key of the "thirdBook" should be 2 (first key is 0)
await bookCacheManager.removeItem(key: 2);
// there should be only 2 books left in the hive box
expect(bookCacheManager.getValues().length, 2);
// read the values whose keys start at 0 and ends at 1 (inclusive)
final Iterable<Book> otherBooks =
bookCacheManager.getValuesBetween(startKey: 0, endKey: 1);
// put the remaining model objects in an Iterable
final Iterable<Book> remainingBooks = [firstBook, secondBook];
// "otherBooks" and "remainingBooks" should be equal
expect(otherBooks, remainingBooks);
});
}