Skip to content

Commit

Permalink
[idb_test] fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Jul 22, 2024
1 parent b931329 commit 676cca6
Show file tree
Hide file tree
Showing 2 changed files with 306 additions and 303 deletions.
318 changes: 158 additions & 160 deletions idb_test/lib/sdb_index_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var testStore2 = SdbStoreRef<String, SdbModel>('test2');
void simpleDbIndexTest(TestContext ctx) {
var factory = sdbFactoryFromIdb(ctx.factory);

group('index', () {
group('sdb_index', () {
test('basic', () async {
var dbName = 'test_index.db';
await factory.deleteDatabase(dbName);
Expand Down Expand Up @@ -81,12 +81,15 @@ void simpleDbIndexTest(TestContext ctx) {

var boundaries =
SdbBoundaries(SdbLowerBoundary(1), SdbUpperBoundary(3));

// Find/count
var records = await testIndex.findRecords(txn, boundaries: boundaries);
expect(records.length, 2);
var keys = await testIndex.findRecordKeys(txn, boundaries: boundaries);
expect(keys.length, 2);
expect(await testIndex.count(txn, boundaries: boundaries), 2);

// Delete
await testIndex.delete(txn, boundaries: boundaries);
expect(await testIndex.count(txn), 1);
});
Expand Down Expand Up @@ -120,170 +123,165 @@ void simpleDbIndexTest(TestContext ctx) {
});
});

group(
'index2',
() {
test('index2', () async {
var dbName = 'test_index2.db';
await factory.deleteDatabase(dbName);

// Our item store/table
var itemStore = SdbStoreRef<int, SdbModel>('item');
// Index on 'type' field
var itemTypeIdIndex = itemStore.index2<String, int>('type_id');

var db = await factory.openDatabase(dbName, version: 1,
onVersionChange: (event) {
var db = event.db;
var oldVersion = event.oldVersion;
if (oldVersion < 1) {
var openStoreRef =
db.createStore(itemStore, keyPath: 'id', autoIncrement: true);
openStoreRef.createIndex2(itemTypeIdIndex, 'type', 'id');
}
});

late int keyCatAlbert;
late int keyCatHarriet;
late int keyDogBeethoven;
await db.inStoreTransaction(itemStore, SdbTransactionMode.readWrite,
(txn) async {
keyCatAlbert =
await itemStore.add(txn, {'type': 'cat', 'name': 'Albert'});
keyDogBeethoven =
await itemStore.add(txn, {'type': 'dog', 'name': 'Beethoven'});
keyCatHarriet =
await itemStore.add(txn, {'type': 'cat', 'name': 'Harriet'});
});

var item =
(await itemTypeIdIndex.record(('cat', keyCatHarriet)).get(db))!;
expect(item.indexKey.$1, 'cat');
expect(item.value['name'], 'Harriet');

var items = await itemTypeIdIndex.findRecords(db);
expect(items.keys, [keyCatAlbert, keyCatHarriet, keyDogBeethoven]);
var first = items.first;
expect(first.key, keyCatAlbert);
expect(first.indexKey.$1, 'cat');
expect(first.indexKey.$2, keyCatAlbert);

items = await itemTypeIdIndex.findRecords(db,
boundaries: SdbBoundaries(
itemTypeIdIndex.lowerBoundary('cat', keyCatAlbert,
include: false),
null));
expect(items.keys, [keyCatHarriet, keyDogBeethoven]);

items = await itemTypeIdIndex.findRecords(db,
boundaries: SdbBoundaries(
null,
itemTypeIdIndex.upperBoundary('cat', keyCatHarriet),
));
expect(items.keys, [keyCatAlbert]);

items = await itemTypeIdIndex.findRecords(db,
boundaries: SdbBoundaries(
group('index2', () {
test('index2', () async {
var dbName = 'test_index2.db';
await factory.deleteDatabase(dbName);

// Our item store/table
var itemStore = SdbStoreRef<int, SdbModel>('item');
// Index on 'type' field
var itemTypeIdIndex = itemStore.index2<String, int>('type_id');

var db = await factory.openDatabase(dbName, version: 1,
onVersionChange: (event) {
var db = event.db;
var oldVersion = event.oldVersion;
if (oldVersion < 1) {
var openStoreRef =
db.createStore(itemStore, keyPath: 'id', autoIncrement: true);
openStoreRef.createIndex2(itemTypeIdIndex, 'type', 'id');
}
});

late int keyCatAlbert;
late int keyCatHarriet;
late int keyDogBeethoven;
await db.inStoreTransaction(itemStore, SdbTransactionMode.readWrite,
(txn) async {
keyCatAlbert =
await itemStore.add(txn, {'type': 'cat', 'name': 'Albert'});
keyDogBeethoven =
await itemStore.add(txn, {'type': 'dog', 'name': 'Beethoven'});
keyCatHarriet =
await itemStore.add(txn, {'type': 'cat', 'name': 'Harriet'});
});

var item =
(await itemTypeIdIndex.record(('cat', keyCatHarriet)).get(db))!;
expect(item.indexKey.$1, 'cat');
expect(item.value['name'], 'Harriet');

var items = await itemTypeIdIndex.findRecords(db);
expect(items.keys, [keyCatAlbert, keyCatHarriet, keyDogBeethoven]);
var first = items.first;
expect(first.key, keyCatAlbert);
expect(first.indexKey.$1, 'cat');
expect(first.indexKey.$2, keyCatAlbert);

items = await itemTypeIdIndex.findRecords(db,
boundaries: SdbBoundaries(
itemTypeIdIndex.lowerBoundary('cat', keyCatAlbert,
include: false),
itemTypeIdIndex.upperBoundary('dog', keyDogBeethoven),
));
expect(items.keys, [keyCatHarriet]);

// Beethoven is actually a cat...
await itemStore.put(
db, {'id': keyDogBeethoven, 'type': 'cat', 'name': 'Beethoven'});
// it should change the index order
items = await itemTypeIdIndex.findRecords(db);
expect(items.keys, [keyCatAlbert, keyDogBeethoven, keyCatHarriet]);
// Close the database
await db.close();
null));
expect(items.keys, [keyCatHarriet, keyDogBeethoven]);

items = await itemTypeIdIndex.findRecords(db,
boundaries: SdbBoundaries(
null,
itemTypeIdIndex.upperBoundary('cat', keyCatHarriet),
));
expect(items.keys, [keyCatAlbert]);

items = await itemTypeIdIndex.findRecords(db,
boundaries: SdbBoundaries(
itemTypeIdIndex.lowerBoundary('cat', keyCatAlbert, include: false),
itemTypeIdIndex.upperBoundary('dog', keyDogBeethoven),
));
expect(items.keys, [keyCatHarriet]);

// Beethoven is actually a cat...
await itemStore
.put(db, {'id': keyDogBeethoven, 'type': 'cat', 'name': 'Beethoven'});
// it should change the index order
items = await itemTypeIdIndex.findRecords(db);
expect(items.keys, [keyCatAlbert, keyDogBeethoven, keyCatHarriet]);
// Close the database
await db.close();
});

test('index3', () async {
var dbName = 'test_index3.db';
await factory.deleteDatabase(dbName);

// Our item store/table
var store = SdbStoreRef<int, SdbModel>('test');
// Index on 'type' field
var index = store.index3<String, int, String>('col1_col2_col3');

var db = await factory.openDatabase(dbName, version: 1,
onVersionChange: (event) {
var db = event.db;
var oldVersion = event.oldVersion;
if (oldVersion < 1) {
var openStoreRef =
db.createStore(store, keyPath: 'id', autoIncrement: true);
openStoreRef.createIndex3(index, 'col1', 'col2', 'col3');
}
});

late int key1;
late int key2;
late int key3;
await db.inStoreTransaction(store, SdbTransactionMode.readWrite,
(txn) async {
key1 = await store.add(txn, {'col1': 'a', 'col2': 1, 'col3': 'i'});
key2 = await store.add(txn, {'col1': 'b', 'col2': 1, 'col3': 'i'});
key3 = await store.add(txn, {'col1': 'a', 'col2': 1, 'col3': 'j'});
});

test('index3', () async {
var dbName = 'test_index3.db';
await factory.deleteDatabase(dbName);

// Our item store/table
var store = SdbStoreRef<int, SdbModel>('test');
// Index on 'type' field
var index = store.index3<String, int, String>('col1_col2_col3');

var db = await factory.openDatabase(dbName, version: 1,
onVersionChange: (event) {
var db = event.db;
var oldVersion = event.oldVersion;
if (oldVersion < 1) {
var openStoreRef =
db.createStore(store, keyPath: 'id', autoIncrement: true);
openStoreRef.createIndex3(index, 'col1', 'col2', 'col3');
}
});

late int key1;
late int key2;
late int key3;
await db.inStoreTransaction(store, SdbTransactionMode.readWrite,
(txn) async {
key1 = await store.add(txn, {'col1': 'a', 'col2': 1, 'col3': 'i'});
key2 = await store.add(txn, {'col1': 'b', 'col2': 1, 'col3': 'i'});
key3 = await store.add(txn, {'col1': 'a', 'col2': 1, 'col3': 'j'});
});

var item = (await index.record(('a', 1, 'i')).get(db))!;
expect(item.indexKey, ('a', 1, 'i'));

var items = await index.findRecords(db);
expect(items.keys, [key1, key3, key2]);
expect(items.indexKeys, [('a', 1, 'i'), ('a', 1, 'j'), ('b', 1, 'i')]);
// Close the database
await db.close();
var item = (await index.record(('a', 1, 'i')).get(db))!;
expect(item.indexKey, ('a', 1, 'i'));

var items = await index.findRecords(db);
expect(items.keys, [key1, key3, key2]);
expect(items.indexKeys, [('a', 1, 'i'), ('a', 1, 'j'), ('b', 1, 'i')]);
// Close the database
await db.close();
});

test('index4', () async {
var dbName = 'test_index4.db';
await factory.deleteDatabase(dbName);

// Our item store/table
var store = SdbStoreRef<int, SdbModel>('test');
// Index on 'type' field
var index = store.index4<String, int, String, int>('col1_col2_col3_col4');

var db = await factory.openDatabase(dbName, version: 1,
onVersionChange: (event) {
var db = event.db;
var oldVersion = event.oldVersion;
if (oldVersion < 1) {
var openStoreRef =
db.createStore(store, keyPath: 'id', autoIncrement: true);
openStoreRef.createIndex4(index, 'col1', 'col2', 'col3', 'col4');
}
});

test('index4', () async {
var dbName = 'test_index4.db';
await factory.deleteDatabase(dbName);

// Our item store/table
var store = SdbStoreRef<int, SdbModel>('test');
// Index on 'type' field
var index =
store.index4<String, int, String, int>('col1_col2_col3_col4');

var db = await factory.openDatabase(dbName, version: 1,
onVersionChange: (event) {
var db = event.db;
var oldVersion = event.oldVersion;
if (oldVersion < 1) {
var openStoreRef =
db.createStore(store, keyPath: 'id', autoIncrement: true);
openStoreRef.createIndex4(index, 'col1', 'col2', 'col3', 'col4');
}
});

late int key1;
late int key2;
late int key3;
await db.inStoreTransaction(store, SdbTransactionMode.readWrite,
(txn) async {
key1 = await store
.add(txn, {'col1': 'a', 'col2': 1, 'col3': 'i', 'col4': 2});
key2 = await store
.add(txn, {'col1': 'b', 'col2': 1, 'col3': 'i', 'col4': 3});
key3 = await store
.add(txn, {'col1': 'a', 'col2': 1, 'col3': 'j', 'col4': 4});
});

var item = (await index.record(('a', 1, 'i', 2)).get(db))!;
expect(item.indexKey, ('a', 1, 'i', 2));

var items = await index.findRecords(db);
expect(items.keys, [key1, key3, key2]);
expect(items.indexKeys,
[('a', 1, 'i', 2), ('a', 1, 'j', 4), ('b', 1, 'i', 3)]);
// Close the database
await db.close();
late int key1;
late int key2;
late int key3;
await db.inStoreTransaction(store, SdbTransactionMode.readWrite,
(txn) async {
key1 = await store
.add(txn, {'col1': 'a', 'col2': 1, 'col3': 'i', 'col4': 2});
key2 = await store
.add(txn, {'col1': 'b', 'col2': 1, 'col3': 'i', 'col4': 3});
key3 = await store
.add(txn, {'col1': 'a', 'col2': 1, 'col3': 'j', 'col4': 4});
});
},
);

var item = (await index.record(('a', 1, 'i', 2)).get(db))!;
expect(item.indexKey, ('a', 1, 'i', 2));

var items = await index.findRecords(db);
expect(items.keys, [key1, key3, key2]);
expect(items.indexKeys,
[('a', 1, 'i', 2), ('a', 1, 'j', 4), ('b', 1, 'i', 3)]);
// Close the database
await db.close();
});
});
}
Loading

0 comments on commit 676cca6

Please sign in to comment.