Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#342 database tests #358

Open
wants to merge 3 commits into
base: feature/#339-autofill
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/database/drift_schemas/drift_schema_v1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"_meta":{"description":"This file contains a serialized version of schema entities for drift.","version":"1.1.0"},"options":{"store_date_time_values_as_text":false},"entities":[{"id":0,"references":[],"type":"table","data":{"name":"db_todo_table","was_declared_in_moor":false,"columns":[{"name":"id","getter_name":"id","moor_type":"int","nullable":false,"customConstraints":null,"defaultConstraints":"PRIMARY KEY AUTOINCREMENT","default_dart":null,"default_client_dart":null,"dsl_features":["auto-increment"]},{"name":"title","getter_name":"title","moor_type":"string","nullable":false,"customConstraints":null,"default_dart":null,"default_client_dart":null,"dsl_features":[]},{"name":"completed","getter_name":"completed","moor_type":"bool","nullable":false,"customConstraints":null,"defaultConstraints":"CHECK (\"completed\" IN (0, 1))","default_dart":null,"default_client_dart":null,"dsl_features":[]}],"is_virtual":false,"without_rowid":false,"constraints":[]}}]}
7 changes: 6 additions & 1 deletion test/database/flutter_template_database_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import 'package:drift/native.dart';
import 'package:flutter_template/database/flutter_template_database.dart';
import 'package:flutter_test/flutter_test.dart';

/// The schema version of the database that is being tested.
/// Update this when a new migration is added.
/// Also run ./tool/save_database_schema.sh to create the latest version schema file.
const schemaVersion = 1;

void main() {
late FlutterTemplateDatabase sut;

Expand All @@ -16,7 +21,7 @@ void main() {
});

test('FlutterTemplateDatabase should have the correct version', () {
expect(sut.schemaVersion, 1);
expect(sut.schemaVersion, schemaVersion);
});

test('FlutterTemplateDatabase should delete all tables', () async {
Expand Down
33 changes: 33 additions & 0 deletions test/database/flutter_template_migration_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// ignore_for_file: avoid_print

import 'package:drift_dev/api/migrations.dart';
import 'package:flutter_template/database/flutter_template_database.dart';
import 'package:flutter_test/flutter_test.dart';

import 'flutter_template_database_test.dart';
import 'schema_versions/schema.dart';


void main() {
group('database migration tests', () {
late SchemaVerifier verifier;

setUpAll(() {
verifier = SchemaVerifier(GeneratedHelper());
});

test('upgrade from any to latest', () async {
for (var start = schemaVersion; start >= 1; start--) {
final connection = await verifier.startAt(start);
final db = FlutterTemplateDatabase(connection);
try {
await verifier.migrateAndValidate(db, schemaVersion);
} catch (e) {
print('Failed to migrate from $start to $schemaVersion');
rethrow;
}
await db.close();
}
});
});
}
18 changes: 18 additions & 0 deletions test/database/schema_versions/schema.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// GENERATED CODE, DO NOT EDIT BY HAND.
// ignore_for_file: type=lint
//@dart=2.12
import 'package:drift/drift.dart';
import 'package:drift/internal/migrations.dart';
import 'schema_v1.dart' as v1;

class GeneratedHelper implements SchemaInstantiationHelper {
@override
GeneratedDatabase databaseForVersion(QueryExecutor db, int version) {
switch (version) {
case 1:
return v1.DatabaseAtV1(db);
default:
throw MissingSchemaException(version, const {1});
}
}
}
57 changes: 57 additions & 0 deletions test/database/schema_versions/schema_v1.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// GENERATED CODE, DO NOT EDIT BY HAND.
// ignore_for_file: type=lint
//@dart=2.12
import 'package:drift/drift.dart';

class DbTodoTable extends Table with TableInfo {
@override
final GeneratedDatabase attachedDatabase;
final String? _alias;
DbTodoTable(this.attachedDatabase, [this._alias]);
late final GeneratedColumn<int> id = GeneratedColumn<int>(
'id', aliasedName, false,
hasAutoIncrement: true,
type: DriftSqlType.int,
requiredDuringInsert: false,
defaultConstraints:
GeneratedColumn.constraintIsAlways('PRIMARY KEY AUTOINCREMENT'));
late final GeneratedColumn<String> title = GeneratedColumn<String>(
'title', aliasedName, false,
type: DriftSqlType.string, requiredDuringInsert: true);
late final GeneratedColumn<bool> completed = GeneratedColumn<bool>(
'completed', aliasedName, false,
type: DriftSqlType.bool,
requiredDuringInsert: true,
defaultConstraints:
GeneratedColumn.constraintIsAlways('CHECK ("completed" IN (0, 1))'));
@override
List<GeneratedColumn> get $columns => [id, title, completed];
@override
String get aliasedName => _alias ?? actualTableName;
@override
String get actualTableName => $name;
static const String $name = 'db_todo_table';
@override
Set<GeneratedColumn> get $primaryKey => {id};
@override
Never map(Map<String, dynamic> data, {String? tablePrefix}) {
throw UnsupportedError('TableInfo.map in schema verification code');
}

@override
DbTodoTable createAlias(String alias) {
return DbTodoTable(attachedDatabase, alias);
}
}

class DatabaseAtV1 extends GeneratedDatabase {
DatabaseAtV1(QueryExecutor e) : super(e);
late final DbTodoTable dbTodoTable = DbTodoTable(this);
@override
Iterable<TableInfo<Table, Object?>> get allTables =>
allSchemaEntities.whereType<TableInfo<Table, Object?>>();
@override
List<DatabaseSchemaEntity> get allSchemaEntities => [dbTodoTable];
@override
int get schemaVersion => 1;
}
1 change: 1 addition & 0 deletions test/viewmodel/login/login_viewmodel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void main() {
navigator = MockMainNavigator();
onboardingNavigator = MockOnboardingNavigator();
sut = LoginViewModel(loginRepo, navigator, onboardingNavigator);
TestWidgetsFlutterBinding.ensureInitialized();
});

test('LoginViewModel init with loggedin user', () async {
Expand Down
12 changes: 12 additions & 0 deletions tool/save_database_schema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CURRENT=`pwd`
DIR_NAME=`basename "$CURRENT"`
if [ $DIR_NAME == 'tool' ]
then
cd ..
fi

echo What is the current database version?
read version

fvm dart run drift_dev schema dump lib/database/flutter_template_database.dart test/database/drift_schemas/drift_schema_v$version.json
fvm dart run drift_dev schema generate test/database/drift_schemas/ test/database/schema_versions