From ec6cace8fc59a2d8514300a121d36f790b8ad4e0 Mon Sep 17 00:00:00 2001 From: Mircea MATEI Date: Mon, 6 May 2019 23:48:09 +0300 Subject: [PATCH] #83. Adding tests for generator exceptions --- src/test/exceptions_test.dart | 42 +++++++++++++++++++ .../bad_no_datatable_annotation_probe.dart | 26 ++++++++++++ src/test/src/model/bad_nondbentity_probe.dart | 20 +++++++++ 3 files changed, 88 insertions(+) create mode 100644 src/test/exceptions_test.dart create mode 100644 src/test/src/model/bad_no_datatable_annotation_probe.dart create mode 100644 src/test/src/model/bad_nondbentity_probe.dart diff --git a/src/test/exceptions_test.dart b/src/test/exceptions_test.dart new file mode 100644 index 0000000..aeb30a5 --- /dev/null +++ b/src/test/exceptions_test.dart @@ -0,0 +1,42 @@ +import 'package:f_orm_m8_sqlite/orm_m8_generator.dart'; +import 'package:source_gen/source_gen.dart'; +import 'package:test/test.dart'; +import 'utils/test_file_utils.dart'; +import 'package:source_gen_test/source_gen_test.dart'; + +void main() { + LibraryReader library_1; + LibraryReader library_2; + final path = testFilePath('test', 'src', 'model'); + + setUp(() async { + library_1 = await initializeLibraryReaderForDirectory( + path, "bad_nondbentity_probe.dart"); + library_2 = await initializeLibraryReaderForDirectory( + path, "bad_no_datatable_annotation_probe.dart"); + }); + + group('Exceptions tests', () { + final generator = OrmM8GeneratorForAnnotation(); + + test('Test Database models must implement DbEntity', () async { + String v = await generator.generate(library_1, null); + + expect( + v.contains( + '''/*\nException: Database models must implement `DbEntity` interface!'''), + true); + }); + + test( + 'Test the DbEntity implementations must be annotated with `@DataTable`!', + () async { + String v = await generator.generate(library_2, null); + + expect( + v.contains( + '''/*\nException: The DbEntity implementations must be annotated with `@DataTable`!'''), + true); + }); + }); +} diff --git a/src/test/src/model/bad_no_datatable_annotation_probe.dart b/src/test/src/model/bad_no_datatable_annotation_probe.dart new file mode 100644 index 0000000..8ea04ac --- /dev/null +++ b/src/test/src/model/bad_no_datatable_annotation_probe.dart @@ -0,0 +1,26 @@ +import 'package:f_orm_m8/f_orm_m8.dart'; + +// we do not allow the users to extends the Framework +class DataTableExtension extends DataTable { + const DataTableExtension(); +} + +// exact @DataTable("my_bad_table") requirement is replaced with a user extension +@DataTableExtension() +class BadNonDbEntityProbe implements DbEntity { + @DataColumn("my_id_column", + metadataLevel: ColumnMetadata.primaryKey | + ColumnMetadata.unique | + ColumnMetadata.autoIncrement) + int id; + + @DataColumn("my_description_column", metadataLevel: ColumnMetadata.unique) + String description; + + @DataColumn("my_future_column", + metadataLevel: ColumnMetadata.ignore | ColumnMetadata.unique) + int futureData; + + @DataColumn("my_account_id_column", metadataLevel: ColumnMetadata.notNull) + int accountId; +} diff --git a/src/test/src/model/bad_nondbentity_probe.dart b/src/test/src/model/bad_nondbentity_probe.dart new file mode 100644 index 0000000..bac2c1b --- /dev/null +++ b/src/test/src/model/bad_nondbentity_probe.dart @@ -0,0 +1,20 @@ +import 'package:f_orm_m8/f_orm_m8.dart'; + +@DataTable("my_bad_table") +class BadNonDbEntityProbe { + @DataColumn("my_id_column", + metadataLevel: ColumnMetadata.primaryKey | + ColumnMetadata.unique | + ColumnMetadata.autoIncrement) + int id; + + @DataColumn("my_description_column", metadataLevel: ColumnMetadata.unique) + String description; + + @DataColumn("my_future_column", + metadataLevel: ColumnMetadata.ignore | ColumnMetadata.unique) + int futureData; + + @DataColumn("my_account_id_column", metadataLevel: ColumnMetadata.notNull) + int accountId; +}