Inject mock streams via .connect constructor

This commit is contained in:
Simon Binder 2019-10-30 19:25:26 +01:00
parent c5d4e38ea9
commit 5cc1f85441
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
13 changed files with 41 additions and 9 deletions

View File

@ -4,4 +4,5 @@ targets:
moor_generator:
options:
override_hash_and_equals_in_result_sets: true
use_column_name_as_json_key_when_defined_in_moor_file: true
use_column_name_as_json_key_when_defined_in_moor_file: true
generate_connect_constructor: true

View File

@ -832,6 +832,7 @@ class $IngredientInRecipesTable extends IngredientInRecipes
abstract class _$Database extends GeneratedDatabase {
_$Database(QueryExecutor e) : super(SqlTypeSystem.defaultInstance, e);
_$Database.connect(DatabaseConnection c) : super.connect(c);
$CategoriesTable _categories;
$CategoriesTable get categories => _categories ??= $CategoriesTable(this);
$RecipesTable _recipes;

View File

@ -12,7 +12,9 @@ void main() {
setUp(() {
executor = MockExecutor();
streamQueries = MockStreamQueries();
db = TodoDb(executor)..streamQueries = streamQueries;
final connection = createConnection(executor, streamQueries);
db = TodoDb.connect(connection);
});
group('compiled custom queries', () {

View File

@ -877,6 +877,7 @@ class Mytable extends Table with TableInfo<Mytable, MytableData> {
abstract class _$CustomTablesDb extends GeneratedDatabase {
_$CustomTablesDb(QueryExecutor e) : super(SqlTypeSystem.defaultInstance, e);
_$CustomTablesDb.connect(DatabaseConnection c) : super.connect(c);
NoIds _noIds;
NoIds get noIds => _noIds ??= NoIds(this);
WithDefaults _withDefaults;

View File

@ -100,6 +100,7 @@ class CustomConverter extends TypeConverter<MyCustomObject, String> {
)
class TodoDb extends _$TodoDb {
TodoDb(QueryExecutor e) : super(e);
TodoDb.connect(DatabaseConnection connection) : super.connect(connection);
@override
MigrationStrategy get migration => MigrationStrategy();

View File

@ -1287,6 +1287,7 @@ class $PureDefaultsTable extends PureDefaults
abstract class _$TodoDb extends GeneratedDatabase {
_$TodoDb(QueryExecutor e) : super(SqlTypeSystem.defaultInstance, e);
_$TodoDb.connect(DatabaseConnection c) : super.connect(c);
$TodosTableTable _todosTable;
$TodosTableTable get todosTable => _todosTable ??= $TodosTableTable(this);
$CategoriesTable _categories;

View File

@ -81,3 +81,9 @@ abstract class SqlExecutorAsClass {
}
class MockQueryExecutor extends Mock implements SqlExecutorAsClass {}
DatabaseConnection createConnection(QueryExecutor executor,
[StreamQueryStore streams]) {
return DatabaseConnection(
SqlTypeSystem.defaultInstance, executor, streams ?? StreamQueryStore());
}

View File

@ -14,7 +14,9 @@ void main() {
setUp(() {
executor = MockExecutor();
streamQueries = MockStreamQueries();
db = TodoDb(executor)..streamQueries = streamQueries;
final connection = createConnection(executor, streamQueries);
db = TodoDb.connect(connection);
});
group('Generates DELETE statements', () {

View File

@ -12,7 +12,9 @@ void main() {
setUp(() {
executor = MockExecutor();
streamQueries = MockStreamQueries();
db = TodoDb(executor)..streamQueries = streamQueries;
final connection = createConnection(executor, streamQueries);
db = TodoDb.connect(connection);
});
test('generates insert statements', () async {

View File

@ -28,7 +28,9 @@ void main() {
setUp(() {
executor = MockExecutor();
streamQueries = MockStreamQueries();
db = TodoDb(executor)..streamQueries = streamQueries;
final connection = createConnection(executor, streamQueries);
db = TodoDb.connect(connection);
});
test("transactions don't allow creating streams", () {

View File

@ -14,7 +14,9 @@ void main() {
setUp(() {
executor = MockExecutor();
streamQueries = MockStreamQueries();
db = TodoDb(executor)..streamQueries = streamQueries;
final connection = createConnection(executor, streamQueries);
db = TodoDb.connect(connection);
});
group('generates update statements', () {

View File

@ -7,6 +7,7 @@ class MoorOptions {
final bool skipVerificationCode;
final bool useDataClassNameForCompanions;
final bool useColumnNameAsJsonKeyWhenDefinedInMoorFile;
final bool generateConnectConstructor;
const MoorOptions(
{this.generateFromJsonStringConstructor = false,
@ -14,7 +15,8 @@ class MoorOptions {
this.compactQueryMethods = false,
this.skipVerificationCode = false,
this.useDataClassNameForCompanions = false,
this.useColumnNameAsJsonKeyWhenDefinedInMoorFile = false});
this.useColumnNameAsJsonKeyWhenDefinedInMoorFile = false,
this.generateConnectConstructor = false});
factory MoorOptions.fromBuilder(Map<String, dynamic> config) {
final writeFromString =
@ -37,6 +39,9 @@ class MoorOptions {
as bool ??
false;
final generateConnectConstructor =
config['generate_connect_constructor'] as bool ?? false;
return MoorOptions(
generateFromJsonStringConstructor: writeFromString,
overrideHashAndEqualsInResultSets: overrideInResultSets,
@ -45,6 +50,7 @@ class MoorOptions {
useDataClassNameForCompanions: dataClassNamesForCompanions,
useColumnNameAsJsonKeyWhenDefinedInMoorFile:
useColumnNameAsJsonKeyForMoor,
generateConnectConstructor: generateConnectConstructor,
);
}
}

View File

@ -21,10 +21,15 @@ class DatabaseWriter {
final dbScope = scope.child();
final className = '_\$${db.fromClass.name}';
dbScope.leaf().write(
'abstract class $className extends GeneratedDatabase {\n'
final firstLeaf = dbScope.leaf();
firstLeaf.write('abstract class $className extends GeneratedDatabase {\n'
'$className(QueryExecutor e) : super(SqlTypeSystem.defaultInstance, e); \n');
if (dbScope.options.generateConnectConstructor) {
firstLeaf.write(
'$className.connect(DatabaseConnection c): super.connect(c); \n');
}
final tableGetters = <String>[];
for (var table in db.allTables) {