Add tests

This commit is contained in:
Simon Binder 2023-10-03 23:56:49 +02:00
parent 2581eda145
commit f0ce04a542
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 50 additions and 14 deletions

View File

@ -175,4 +175,16 @@ void main() {
generates('(a OR b) AND c AND d'),
);
});
test('dialect-specific custom expression', () {
final expr = CustomExpression.dialectSpecific({
SqlDialect.mariadb: 'mariadb',
SqlDialect.postgres: 'pg',
SqlDialect.sqlite: 'default',
});
expect(expr, generatesWithOptions('mariadb', dialect: SqlDialect.mariadb));
expect(expr, generatesWithOptions('pg', dialect: SqlDialect.postgres));
expect(expr, generatesWithOptions('default', dialect: SqlDialect.sqlite));
});
}

View File

@ -17,14 +17,25 @@ void expectNotEquals(dynamic a, dynamic expected) {
/// matching [sql] and, optionally, the matching [variables].
Matcher generates(dynamic sql, [dynamic variables = isEmpty]) {
return _GeneratesSqlMatcher(
wrapMatcher(sql), wrapMatcher(variables), const DriftDatabaseOptions());
wrapMatcher(sql),
wrapMatcher(variables),
const DriftDatabaseOptions(),
SqlDialect.sqlite,
);
}
Matcher generatesWithOptions(dynamic sql,
{dynamic variables = isEmpty,
DriftDatabaseOptions options = const DriftDatabaseOptions()}) {
Matcher generatesWithOptions(
dynamic sql, {
dynamic variables = isEmpty,
DriftDatabaseOptions options = const DriftDatabaseOptions(),
SqlDialect dialect = SqlDialect.sqlite,
}) {
return _GeneratesSqlMatcher(
wrapMatcher(sql), wrapMatcher(variables), options);
wrapMatcher(sql),
wrapMatcher(variables),
options,
dialect,
);
}
class _GeneratesSqlMatcher extends Matcher {
@ -32,8 +43,14 @@ class _GeneratesSqlMatcher extends Matcher {
final Matcher? _matchVariables;
final DriftDatabaseOptions options;
final SqlDialect dialect;
_GeneratesSqlMatcher(this._matchSql, this._matchVariables, this.options);
_GeneratesSqlMatcher(
this._matchSql,
this._matchVariables,
this.options,
this.dialect,
);
@override
Description describe(Description description) {
@ -78,7 +95,7 @@ class _GeneratesSqlMatcher extends Matcher {
return false;
}
final ctx = stubContext(options: options);
final ctx = stubContext(options: options, dialect: dialect);
item.writeInto(ctx);
var matches = true;

View File

@ -43,15 +43,20 @@ DatabaseConnection createConnection(QueryExecutor executor,
streamQueries: streams ?? StreamQueryStore());
}
GenerationContext stubContext({DriftDatabaseOptions? options}) {
return GenerationContext(
options ?? const DriftDatabaseOptions(), _NullDatabase.instance);
GenerationContext stubContext({
DriftDatabaseOptions? options,
SqlDialect dialect = SqlDialect.sqlite,
}) {
final warnBefore = driftRuntimeOptions.dontWarnAboutMultipleDatabases;
driftRuntimeOptions.dontWarnAboutMultipleDatabases = true;
final database = _NullDatabase(_NullExecutor(dialect: dialect));
driftRuntimeOptions.dontWarnAboutMultipleDatabases = warnBefore;
return GenerationContext(options ?? const DriftDatabaseOptions(), database);
}
class _NullDatabase extends GeneratedDatabase {
static final instance = _NullDatabase();
_NullDatabase() : super(_NullExecutor());
_NullDatabase([QueryExecutor? e]) : super(e ?? _NullExecutor());
@override
Iterable<TableInfo<Table, dynamic>> get allTables =>
@ -63,7 +68,9 @@ class _NullDatabase extends GeneratedDatabase {
class _NullExecutor extends Fake implements QueryExecutor {
@override
SqlDialect get dialect => SqlDialect.sqlite;
final SqlDialect dialect;
_NullExecutor({this.dialect = SqlDialect.sqlite});
}
class CustomTable extends Table with TableInfo<CustomTable, void> {