From 1826311ec2f541a2380e0a5975c6164dc43d0867 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Mon, 4 Jul 2022 19:57:04 +0200 Subject: [PATCH] Add tests --- .../expressions/datetime_expression_test.dart | 11 +++++------ .../expressions_integration_test.dart | 16 ++++++++++++---- drift/test/test_utils/matchers.dart | 5 ++--- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/drift/test/database/expressions/datetime_expression_test.dart b/drift/test/database/expressions/datetime_expression_test.dart index 748b2eb9..8b35bd34 100644 --- a/drift/test/database/expressions/datetime_expression_test.dart +++ b/drift/test/database/expressions/datetime_expression_test.dart @@ -3,7 +3,7 @@ import 'package:test/test.dart'; import '../../test_utils/test_utils.dart'; -typedef _Extractor = Expression Function(Expression d); +typedef _Extractor = Expression Function(Expression d); void main() { const column = @@ -17,15 +17,14 @@ void main() { (d) => d.hour: "CAST(strftime('%H', val, 'unixepoch') AS INTEGER)", (d) => d.minute: "CAST(strftime('%M', val, 'unixepoch') AS INTEGER)", (d) => d.second: "CAST(strftime('%S', val, 'unixepoch') AS INTEGER)", + (d) => d.date: "DATE(val, 'unixepoch')", + (d) => d.datetime: "DATETIME(val, 'unixepoch')", + (d) => d.time: "TIME(val, 'unixepoch')", }; expectedResults.forEach((key, value) { test('should extract field', () { - final ctx = GenerationContext(SqlTypeSystem.defaultInstance, null); - key(column).writeInto(ctx); - - expect(ctx.sql, value); - + expect(key(column), generates(value)); expectEquals(key(column), key(column)); }); }); diff --git a/drift/test/database/expressions/expressions_integration_test.dart b/drift/test/database/expressions/expressions_integration_test.dart index a78ceded..bfa0e3df 100644 --- a/drift/test/database/expressions/expressions_integration_test.dart +++ b/drift/test/database/expressions/expressions_integration_test.dart @@ -37,11 +37,19 @@ void main() { const Duration(days: 1).inSeconds); }); - test('datetime.date format', () { - final expr = Variable.withDateTime(DateTime(2020, 09, 04, 8, 55)); - final asDate = expr.date; + test('extracting values from datetime', () { + final expr = Variable.withDateTime(DateTime.utc(2020, 09, 04, 8, 55)); - expect(eval(asDate), completion('2020-09-04')); + expect(eval(expr.year), completion(2020)); + expect(eval(expr.month), completion(9)); + expect(eval(expr.day), completion(4)); + expect(eval(expr.hour), completion(8)); + expect(eval(expr.minute), completion(55)); + expect(eval(expr.second), completion(0)); + + expect(eval(expr.date), completion('2020-09-04')); + expect(eval(expr.time), completion('08:55:00')); + expect(eval(expr.datetime), completion('2020-09-04 08:55:00')); }); test('rowid', () { diff --git a/drift/test/test_utils/matchers.dart b/drift/test/test_utils/matchers.dart index f192b2e9..41f5861c 100644 --- a/drift/test/test_utils/matchers.dart +++ b/drift/test/test_utils/matchers.dart @@ -13,9 +13,8 @@ void expectNotEquals(dynamic a, dynamic expected) { /// Matcher for [Component]-subclasses. Expect that a component generates the /// matching [sql] and, optionally, the matching [variables]. -Matcher generates(dynamic sql, [dynamic variables]) { - final variablesMatcher = variables != null ? wrapMatcher(variables) : isEmpty; - return _GeneratesSqlMatcher(wrapMatcher(sql), variablesMatcher); +Matcher generates(dynamic sql, [dynamic variables = isEmpty]) { + return _GeneratesSqlMatcher(wrapMatcher(sql), wrapMatcher(variables)); } class _GeneratesSqlMatcher extends Matcher {