Skip to content

Commit

Permalink
test store clear/create/delete in onUpgradeNeeded
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Jan 24, 2025
1 parent a56c830 commit 9d7bc43
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 32 deletions.
2 changes: 2 additions & 0 deletions idb_test/lib/idb_test_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const String testNameIndex = 'name_index';
const String testNameField = 'name';
const String testValueIndex = 'value_index';
const String testValueField = 'value';
const String testValue = 'my_value';
const String testKey = 'my_key';

const String testNameIndex2 = 'name_index_2';
const String testNameField2 = 'name_2';
Expand Down
80 changes: 48 additions & 32 deletions idb_test/lib/open_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,8 @@ void defineTests(TestContext ctx) {
var db = await idbFactory.open(dbName, version: 1,
onUpgradeNeeded: (event) async {
var store = event.database.createObjectStore('note');
await store.put('my_value', 'my_key').then((key) async {
putKey = key;
putValue = await store.getObject('my_key');
});
putKey = await store.put('my_value', 'my_key');
putValue = await store.getObject('my_key');
});
expect(putKey, 'my_key');
expect(putValue, 'my_value');
Expand Down Expand Up @@ -327,33 +325,51 @@ void defineTests(TestContext ctx) {
}
});

// const String MILESTONE_STORE = 'milestoneStore';
// const String NAME_INDEX = 'name_index';
//
// void onUpgradeNeeded(VersionChangeEvent e) {
// Database db = (e.target as Request).result;
//
// var objectStore = db.createObjectStore(MILESTONE_STORE, autoIncrement: true);
// var index = objectStore.createIndex(NAME_INDEX, 'milestoneName', unique: true);
// }
//
//
// void _initializeTestDatabase(VersionChangeEvent e) {
// expect(e.oldVersion, equals(0));
// expect(e.newVersion, equals(1));
// Database db = (e.target as Request).result;
//
// var objectStore = db.createObjectStore(MILESTONE_STORE, autoIncrement: true);
// var index = objectStore.createIndex(NAME_INDEX, 'milestoneName', unique: true);
// }
// test('open initialize', () {
// Function done = expectAsync0(() => null);
// idbFactory.deleteDatabase(DB_NAME).then((_) {
// idbFactory.open('test', version: 1, onUpgradeNeeded: _initializeTestDatabase).then((Database database) {
// database.close();
// done();
// });
// });
// });
test('clear_object_store_in_open_transaction', () async {
await setupDeleteDb();
var db = await idbFactory.open(dbName, version: 1,
onUpgradeNeeded: (event) async {
var store = event.database.createObjectStore(testStoreName);
await store.put(testValue, testKey);
});

var txn = db.transaction(testStoreName, idbModeReadOnly);
var store = txn.objectStore(testStoreName);
expect(await store.getObject(testKey), testValue);
db.close();
db = await idbFactory.open(dbName, version: 2,
onUpgradeNeeded: (event) async {
var store = event.transaction.objectStore(testStoreName);
await store.clear();
});
txn = db.transaction(testStoreName, idbModeReadOnly);
store = txn.objectStore(testStoreName);
expect(await store.getObject(testKey), isNull);
});

test('delete_recreate_object_store_in_open_transaction', () async {
await setupDeleteDb();
var db = await idbFactory.open(dbName, version: 1,
onUpgradeNeeded: (event) async {
var store = event.database.createObjectStore(testStoreName);
await store.put(testValue, testKey);
});

var txn = db.transaction(testStoreName, idbModeReadOnly);
var store = txn.objectStore(testStoreName);
expect(await store.getObject(testKey), testValue);
db.close();
db = await idbFactory.open(dbName, version: 2,
onUpgradeNeeded: (event) async {
var db = event.database;
db.deleteObjectStore(testStoreName);
expect(db.objectStoreNames, isNot(contains(testStoreName)));
db.createObjectStore(testStoreName);
expect(db.objectStoreNames, contains(testStoreName));
});
txn = db.transaction(testStoreName, idbModeReadOnly);
store = txn.objectStore(testStoreName);
expect(await store.getObject(testKey), isNull);
});
});
}

0 comments on commit 9d7bc43

Please sign in to comment.