Skip to content

Commit

Permalink
fixup!:
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolas Rimikis <[email protected]>
  • Loading branch information
Leptopoda committed Sep 29, 2024
1 parent 44b227e commit 5d9397f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include: package:neon_lints/dart.yaml

custom_lint:
enable_all_lint_rules: false
rules:
- avoid_exports: false
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:meta/meta.dart';
import 'package:neon_storage/src/sqlite/sqlite.dart';
import 'package:sqflite_common/sqflite.dart';

/// A wrapper for the SQLite [Database] that manages multiple tables.
/// A wrapper for the SQLite [Database] managing multiple tables.
abstract class MultiTableDatabase {
/// Creates a new database for the given [tables].
///
Expand Down Expand Up @@ -69,9 +69,9 @@ abstract class MultiTableDatabase {

/// Closes the database.
///
/// Once completed [init] must be called again.
/// A closed database must be initialized again by calling [init].
@visibleForTesting
Future<void> resetDatabase() async {
Future<void> close() async {
await _database?.close();
_database = null;
}
Expand Down Expand Up @@ -102,14 +102,17 @@ abstract class MultiTableDatabase {
for (final table in _tables) {
table.controller = this;
}
await Future.wait(
_tables.map((t) => t.onOpen()),
);

_database = database;
}

Future<void> _createMetaTable(Database db, int version) async {
await db.execute('''
CREATE TABLE IF NOT EXISTS "$_metaTable" (
"name" TEXT NOT NULL,
"name" TEXT NOT NULL,
"version" INTEGER NOT NULL,
PRIMARY KEY("name")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ abstract mixin class Table {
/// The name of the table.
String get name;

/// The value must be a 32-bits integer greater than `0`.
/// The value must be a 32-bit integer greater than `0`.
int get version;

/// The owning multi table database.
Expand All @@ -26,17 +26,17 @@ abstract mixin class Table {
/// A callback for upgrading the schema of a table.
///
/// It is only called called if the database already exists and [version] is
/// higher than the last database version-
/// higher than the last database version.
void onUpgrade(Batch db, int oldVersion, int newVersion) {}

/// A callback for downgrading the required schema for the table.
///
/// It is only called called when [version] is lower than the last database
/// version. This is a rare case and should only come up if a newer version of
/// your code has created a database that is then interacted with by an older
/// version of your code. You should try to avoid this scenario
/// version of your code. You should try to avoid this scenario.
void onDowngrade(Batch db, int oldVersion, int newVersion) {}

/// A callback invoked after all the table version is set.
FutureOr<void> onOpen() {}
/// A callback invoked after all the table versions are set.
Future<void> onOpen() async {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class _TestDatabase extends MultiTableDatabase {
FutureOr<String> get path => controller.path;
}

class _FakeMultiTableDatabase extends Fake implements MultiTableDatabase {}

class _TableMock extends Mock implements Table {}

class _FakeBatch extends Fake implements Batch {}
Expand All @@ -38,6 +40,7 @@ void main() {

setUpAll(() {
registerFallbackValue(_FakeBatch());
registerFallbackValue(_FakeMultiTableDatabase());
});

setUp(() {
Expand All @@ -51,7 +54,7 @@ void main() {
});

tearDown(() async {
await database.resetDatabase();
await database.close();
});

test('init initializes the database', () async {
Expand Down Expand Up @@ -91,6 +94,7 @@ void main() {

final table2 = _TableMock();
when(() => table2.name).thenReturn('cache_table');

when(() => controller.tables).thenReturn([table, table2]);

expect(
Expand All @@ -102,6 +106,7 @@ void main() {
test('throws a TableNameError for conflicting table names', () {
final table = _TableMock();
when(() => table.name).thenReturn('_database_meta');

when(() => controller.tables).thenReturn([table]);

expect(
Expand All @@ -117,6 +122,7 @@ void main() {
table = _TableMock();
when(() => table.name).thenReturn('table');
when(() => table.version).thenReturn(2);
when(() => table.onOpen()).thenAnswer((_) async {});

controller = _DatabaseControllerMock();
when(() => controller.path).thenReturn(inMemoryDatabasePath);
Expand All @@ -126,54 +132,51 @@ void main() {
database = _TestDatabase(controller);

await database.init();
resetMocktailState();
clearInteractions(table);
});

tearDown(() {
verify(table.onOpen).called(1);

verify(() => table.name);
verify(() => table.version);
verify(() => table.controller = any()).called(1);

verifyNoMoreInteractions(table);
});

test('calls onCreate when table is not present', () async {
final table2 = _TableMock();
when(() => table2.name).thenReturn('table2');
when(() => table2.version).thenReturn(1);
when(() => controller.tables).thenReturn([table, table2]);
when(() => table.name).thenReturn('table2');
when(() => table.version).thenReturn(1);

database = _TestDatabase(controller);
await database.init();

verify(() => table2.onCreate(any(), any(that: equals(1)))).called(1);
verifyNever(() => table2.onUpgrade(any(), any(), any()));
verifyNever(() => table2.onDowngrade(any(), any(), any()));
verify(() => table.onCreate(any(), any(that: equals(1)))).called(1);
});

test('does not call onCreate when table version did not change', () async {
verify(() => table.onCreate(any(), any(that: equals(2)))).called(1);

database = _TestDatabase(controller);
await database.init();

verifyNever(() => table.onCreate(any(), any()));
verifyNever(() => table.onUpgrade(any(), any(), any()));
verifyNever(() => table.onDowngrade(any(), any(), any()));
});

test('calls onUpgrade when table version is higher', () async {
verify(() => table.onCreate(any(), any(that: equals(2)))).called(1);

when(() => table.version).thenReturn(3);

database = _TestDatabase(controller);
await database.init();

verifyNever(() => table.onCreate(any(), any()));
verify(() => table.onUpgrade(any(), any(that: equals(2)), any(that: equals(3)))).called(1);
verifyNever(() => table.onDowngrade(any(), any(), any()));
});

test('calls onUpgrade when table version is lower', () async {
verify(() => table.onCreate(any(), any(that: equals(2)))).called(1);

test('calls onDowngrade when table version is lower', () async {
when(() => table.version).thenReturn(1);

database = _TestDatabase(controller);
await database.init();

verifyNever(() => table.onCreate(any(), any()));
verifyNever(() => table.onUpgrade(any(), any(), any()));
verify(() => table.onDowngrade(any(), any(that: equals(2)), any(that: equals(1)))).called(1);
});
});
Expand Down

0 comments on commit 5d9397f

Please sign in to comment.