-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmigrator_test.dart
144 lines (116 loc) · 4.14 KB
/
migrator_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import 'package:flutter_test/flutter_test.dart';
import 'package:matcher/matcher.dart';
import 'package:mockito/mockito.dart';
import 'package:sqflite/sqflite.dart';
import 'package:sqflite_migration/sqflite_migration.dart';
import 'package:sqflite_migration/src/migrator.dart';
class MockDatabase extends Mock implements DatabaseExecutor {
@override
Future<void> execute(String sql, [List<Object?>? arguments]) async {
return super.noSuchMethod(Invocation.method(#execute, [sql]));
}
}
void main() {
test('should not run any executions on an empty initializationScript list',
() async {
var db = MockDatabase();
var config = MigrationConfig(
initializationScript: [],
migrationScripts: [],
);
await Migrator(config).executeInitialization(db, 1);
verifyZeroInteractions(db);
});
test('should run executions on an initializationScript list of 1', () async {
var db = MockDatabase();
var config = MigrationConfig(
initializationScript: ['script line 1'],
migrationScripts: [],
);
await Migrator(config).executeInitialization(db, 1);
verify(db.execute('script line 1'));
});
test('should run all executions on an initializationScript list', () async {
var db = MockDatabase();
var config = MigrationConfig(
initializationScript: ['script line 1', 'script line 2'],
migrationScripts: [],
);
await Migrator(config).executeInitialization(db, 1);
verify(db.execute('script line 1'));
verify(db.execute('script line 2'));
});
test('should run any migrations when initializing', () async {
var db = MockDatabase();
var config = MigrationConfig(
initializationScript: ['init script line 1', 'init script line 2'],
migrationScripts: ['migration script line 1', 'migration script line 2'],
);
await Migrator(config)
.executeInitialization(db, config.migrationScripts.length + 1);
verify(db.execute('init script line 1'));
verify(db.execute('init script line 2'));
verify(db.execute('migration script line 1'));
verify(db.execute('migration script line 2'));
});
test('should throw error if the new version is not greater than the old',
() async {
var db = MockDatabase();
var config = MigrationConfig(
initializationScript: [],
migrationScripts: [],
);
expect(
() async => await Migrator(config).executeMigration(db, 1, 1),
throwsA(TypeMatcher<AssertionError>().having(
(e) => e.message,
'message',
equals(
'The newVersion(1) should always be greater than the oldVersion(1).'))));
});
test(
'should throw error if the new version is greater than the migration scripts',
() async {
var db = MockDatabase();
var config = MigrationConfig(
initializationScript: ['init script line 1', 'init script line 2'],
migrationScripts: [],
);
expect(
() async => await Migrator(config).executeMigration(db, 1, 2),
throwsA(TypeMatcher<AssertionError>().having(
(e) => e.message,
'message',
equals('New version (2) requires exact 2 migrations.'))));
});
test(
'should throw error if the new version is greater than the migration scripts',
() async {
var db = MockDatabase();
var config = MigrationConfig(
initializationScript: [],
migrationScripts: [],
);
expect(
() async => await Migrator(config).executeMigration(db, 1, 2),
throwsA(TypeMatcher<AssertionError>().having(
(e) => e.message,
'message',
equals('New version (2) requires exact 2 migrations.'))));
});
test('should not execute migrations older than the oldVersion', () async {
var db = MockDatabase();
var config = MigrationConfig(
initializationScript: [],
migrationScripts: [
'migration line 1',
'migration line 2',
'migration line 3',
],
);
await Migrator(config)
.executeMigration(db, 2, config.migrationScripts.length + 1);
verify(db.execute('migration line 2'));
verify(db.execute('migration line 3'));
});
}