From 7668a2858fa0598e52d90dda797e64a3b0a37c09 Mon Sep 17 00:00:00 2001 From: Vitus Date: Wed, 3 Apr 2019 18:35:26 +0200 Subject: [PATCH] Fix adapter instances naming (#114) --- floor_generator/lib/writer/dao_writer.dart | 18 +- .../lib/writer/deletion_method_writer.dart | 19 +- .../lib/writer/insertion_method_writer.dart | 19 +- .../lib/writer/update_method_writer.dart | 19 +- .../test/writer/dao_writer_test.dart | 214 ++++++++++++++++++ .../writer/deletion_method_writer_test.dart | 126 +++++++++++ .../writer/insert_method_writer_test.dart | 108 ++------- .../test/writer/query_method_writer_test.dart | 12 +- .../writer/update_method_writer_test.dart | 142 ++++++++++++ 9 files changed, 550 insertions(+), 127 deletions(-) create mode 100644 floor_generator/test/writer/dao_writer_test.dart create mode 100644 floor_generator/test/writer/deletion_method_writer_test.dart create mode 100644 floor_generator/test/writer/update_method_writer_test.dart diff --git a/floor_generator/lib/writer/dao_writer.dart b/floor_generator/lib/writer/dao_writer.dart index fed7fa87..af87efb9 100644 --- a/floor_generator/lib/writer/dao_writer.dart +++ b/floor_generator/lib/writer/dao_writer.dart @@ -80,9 +80,9 @@ class DaoWriter extends Writer { final entities = insertionMethods.map((method) => method.entity).toSet(); for (final entity in entities) { - final name = entity.classElement.displayName; - final fieldName = '_${decapitalize(name)}InsertionAdapter'; - final type = refer('InsertionAdapter<$name>'); + final entityClassName = entity.classElement.displayName; + final fieldName = '_${decapitalize(entityClassName)}InsertionAdapter'; + final type = refer('InsertionAdapter<$entityClassName>'); final field = Field((builder) => builder ..name = fieldName @@ -108,9 +108,9 @@ class DaoWriter extends Writer { final entities = updateMethods.map((method) => method.entity).toSet(); for (final entity in entities) { - final name = entity.classElement.displayName; - final fieldName = '_${decapitalize(name)}UpdateAdapter'; - final type = refer('UpdateAdapter<$name>'); + final entityClassName = entity.classElement.displayName; + final fieldName = '_${decapitalize(entityClassName)}UpdateAdapter'; + final type = refer('UpdateAdapter<$entityClassName>'); final field = Field((builder) => builder ..name = fieldName @@ -136,9 +136,9 @@ class DaoWriter extends Writer { final entities = deleteMethods.map((method) => method.entity).toSet(); for (final entity in entities) { - final name = entity.classElement.displayName; - final fieldName = '_${decapitalize(name)}DeletionAdapter'; - final type = refer('DeletionAdapter<$name>'); + final entityClassName = entity.classElement.displayName; + final fieldName = '_${decapitalize(entityClassName)}DeletionAdapter'; + final type = refer('DeletionAdapter<$entityClassName>'); final field = Field((builder) => builder ..name = fieldName diff --git a/floor_generator/lib/writer/deletion_method_writer.dart b/floor_generator/lib/writer/deletion_method_writer.dart index 1499bb3b..026d99f2 100644 --- a/floor_generator/lib/writer/deletion_method_writer.dart +++ b/floor_generator/lib/writer/deletion_method_writer.dart @@ -26,19 +26,20 @@ class DeletionMethodWriter implements Writer { @nonNull String _generateMethodBody() { - final entityName = decapitalize(_method.entity.name); + final entityClassName = + decapitalize(_method.entity.classElement.displayName); final methodSignatureParameterName = _method.parameterElement.name; if (_method.flattenedReturnType.isVoid) { return _generateVoidReturnMethodBody( methodSignatureParameterName, - entityName, + entityClassName, ); } else { // if not void then must be int return return _generateIntReturnMethodBody( methodSignatureParameterName, - entityName, + entityClassName, ); } } @@ -46,24 +47,24 @@ class DeletionMethodWriter implements Writer { @nonNull String _generateVoidReturnMethodBody( final String methodSignatureParameterName, - final String entityName, + final String entityClassName, ) { if (_method.changesMultipleItems) { - return 'await _${entityName}DeletionAdapter.deleteList($methodSignatureParameterName);'; + return 'await _${entityClassName}DeletionAdapter.deleteList($methodSignatureParameterName);'; } else { - return 'await _${entityName}DeletionAdapter.delete($methodSignatureParameterName);'; + return 'await _${entityClassName}DeletionAdapter.delete($methodSignatureParameterName);'; } } @nonNull String _generateIntReturnMethodBody( final String methodSignatureParameterName, - final String entityName, + final String entityClassName, ) { if (_method.changesMultipleItems) { - return 'return _${entityName}DeletionAdapter.deleteListAndReturnChangedRows($methodSignatureParameterName);'; + return 'return _${entityClassName}DeletionAdapter.deleteListAndReturnChangedRows($methodSignatureParameterName);'; } else { - return 'return _${entityName}DeletionAdapter.deleteAndReturnChangedRows($methodSignatureParameterName);'; + return 'return _${entityClassName}DeletionAdapter.deleteAndReturnChangedRows($methodSignatureParameterName);'; } } } diff --git a/floor_generator/lib/writer/insertion_method_writer.dart b/floor_generator/lib/writer/insertion_method_writer.dart index 8e6f686d..863b16aa 100644 --- a/floor_generator/lib/writer/insertion_method_writer.dart +++ b/floor_generator/lib/writer/insertion_method_writer.dart @@ -26,19 +26,20 @@ class InsertionMethodWriter implements Writer { @nonNull String _generateMethodBody() { - final decapitalizedEntityName = decapitalize(_method.entity.name); + final entityClassName = + decapitalize(_method.entity.classElement.displayName); final methodSignatureParameterName = _method.parameterElement.displayName; if (_method.flattenedReturnType.isVoid) { return _generateVoidReturnMethodBody( methodSignatureParameterName, - decapitalizedEntityName, + entityClassName, ); } else { // if not void then must be int return return _generateIntReturnMethodBody( methodSignatureParameterName, - decapitalizedEntityName, + entityClassName, ); } } @@ -46,24 +47,24 @@ class InsertionMethodWriter implements Writer { @nonNull String _generateVoidReturnMethodBody( final String methodSignatureParameterName, - final String entityName, + final String entityClassName, ) { if (_method.changesMultipleItems) { - return 'await _${entityName}InsertionAdapter.insertList($methodSignatureParameterName, ${_method.onConflict});'; + return 'await _${entityClassName}InsertionAdapter.insertList($methodSignatureParameterName, ${_method.onConflict});'; } else { - return 'await _${entityName}InsertionAdapter.insert($methodSignatureParameterName, ${_method.onConflict});'; + return 'await _${entityClassName}InsertionAdapter.insert($methodSignatureParameterName, ${_method.onConflict});'; } } @nonNull String _generateIntReturnMethodBody( final String methodSignatureParameterName, - final String entityName, + final String entityClassName, ) { if (_method.changesMultipleItems) { - return 'return _${entityName}InsertionAdapter.insertListAndReturnIds($methodSignatureParameterName, ${_method.onConflict});'; + return 'return _${entityClassName}InsertionAdapter.insertListAndReturnIds($methodSignatureParameterName, ${_method.onConflict});'; } else { - return 'return _${entityName}InsertionAdapter.insertAndReturnId($methodSignatureParameterName, ${_method.onConflict});'; + return 'return _${entityClassName}InsertionAdapter.insertAndReturnId($methodSignatureParameterName, ${_method.onConflict});'; } } } diff --git a/floor_generator/lib/writer/update_method_writer.dart b/floor_generator/lib/writer/update_method_writer.dart index a3dd2d1a..d21b4eb1 100644 --- a/floor_generator/lib/writer/update_method_writer.dart +++ b/floor_generator/lib/writer/update_method_writer.dart @@ -26,19 +26,20 @@ class UpdateMethodWriter implements Writer { @nonNull String _generateMethodBody() { - final entityName = decapitalize(_method.entity.name); + final entityClassName = + decapitalize(_method.entity.classElement.displayName); final methodSignatureParameterName = _method.parameterElement.displayName; if (_method.flattenedReturnType.isVoid) { return _generateVoidReturnMethodBody( methodSignatureParameterName, - entityName, + entityClassName, ); } else { // if not void then must be int return return _generateIntReturnMethodBody( methodSignatureParameterName, - entityName, + entityClassName, ); } } @@ -46,24 +47,24 @@ class UpdateMethodWriter implements Writer { @nonNull String _generateIntReturnMethodBody( final String methodSignatureParameterName, - final String entityName, + final String entityClassName, ) { if (_method.changesMultipleItems) { - return 'return _${entityName}UpdateAdapter.updateListAndReturnChangedRows($methodSignatureParameterName, ${_method.onConflict});'; + return 'return _${entityClassName}UpdateAdapter.updateListAndReturnChangedRows($methodSignatureParameterName, ${_method.onConflict});'; } else { - return 'return _${entityName}UpdateAdapter.updateAndReturnChangedRows($methodSignatureParameterName, ${_method.onConflict});'; + return 'return _${entityClassName}UpdateAdapter.updateAndReturnChangedRows($methodSignatureParameterName, ${_method.onConflict});'; } } @nonNull String _generateVoidReturnMethodBody( final String methodSignatureParameterName, - final String entityName, + final String entityClassName, ) { if (_method.changesMultipleItems) { - return 'await _${entityName}UpdateAdapter.updateList($methodSignatureParameterName, ${_method.onConflict});'; + return 'await _${entityClassName}UpdateAdapter.updateList($methodSignatureParameterName, ${_method.onConflict});'; } else { - return 'await _${entityName}UpdateAdapter.update($methodSignatureParameterName, ${_method.onConflict});'; + return 'await _${entityClassName}UpdateAdapter.update($methodSignatureParameterName, ${_method.onConflict});'; } } } diff --git a/floor_generator/test/writer/dao_writer_test.dart b/floor_generator/test/writer/dao_writer_test.dart new file mode 100644 index 00000000..b4475922 --- /dev/null +++ b/floor_generator/test/writer/dao_writer_test.dart @@ -0,0 +1,214 @@ +import 'package:build_test/build_test.dart'; +import 'package:code_builder/code_builder.dart'; +import 'package:floor_annotation/floor_annotation.dart' as annotations; +import 'package:floor_generator/misc/type_utils.dart'; +import 'package:floor_generator/processor/dao_processor.dart'; +import 'package:floor_generator/processor/entity_processor.dart'; +import 'package:floor_generator/value_object/dao.dart'; +import 'package:floor_generator/writer/dao_writer.dart'; +import 'package:source_gen/source_gen.dart'; +import 'package:test/test.dart'; + +import '../test_utils.dart'; + +void main() { + useDartfmt(); + + test('create DAO no stream query', () async { + final dao = await _createDao(''' + @dao + abstract class PersonDao { + @Query('SELECT * FROM person') + Future> findAllPersons(); + + @insert + Future insertPerson(Person person); + + @update + Future updatePerson(Person person); + + @delete + Future deletePerson(Person person); + } + '''); + + final actual = DaoWriter(dao).write(); + + expect(actual, equalsDart(r''' + class _$PersonDao extends PersonDao { + _$PersonDao(this.database, this.changeListener) + : _queryAdapter = QueryAdapter(database), + _personInsertionAdapter = InsertionAdapter( + database, + 'Person', + (Person item) => + {'id': item.id, 'name': item.name}), + _personUpdateAdapter = UpdateAdapter( + database, + 'Person', + 'id', + (Person item) => + {'id': item.id, 'name': item.name}), + _personDeletionAdapter = DeletionAdapter( + database, + 'Person', + 'id', + (Person item) => + {'id': item.id, 'name': item.name}); + + final sqflite.DatabaseExecutor database; + + final StreamController changeListener; + + final QueryAdapter _queryAdapter; + + final _personMapper = (Map row) => + Person(row['id'] as int, row['name'] as String); + + final InsertionAdapter _personInsertionAdapter; + + final UpdateAdapter _personUpdateAdapter; + + final DeletionAdapter _personDeletionAdapter; + + @override + Future> findAllPersons() async { + return _queryAdapter.queryList('SELECT * FROM person', _personMapper); + } + + @override + Future insertPerson(Person person) async { + await _personInsertionAdapter.insert(person, sqflite.ConflictAlgorithm.abort); + } + + @override + Future updatePerson(Person person) async { + await _personUpdateAdapter.update(person, sqflite.ConflictAlgorithm.abort); + } + + @override + Future deletePerson(Person person) async { + await _personDeletionAdapter.delete(person); + } + } + ''')); + }); + + test('create DAO stream query', () async { + final dao = await _createDao(''' + @dao + abstract class PersonDao { + @Query('SELECT * FROM person') + Stream> findAllPersonsAsStream(); + + @insert + Future insertPerson(Person person); + + @update + Future updatePerson(Person person); + + @delete + Future deletePerson(Person person); + } + '''); + + final actual = DaoWriter(dao).write(); + + expect(actual, equalsDart(r''' + class _$PersonDao extends PersonDao { + _$PersonDao(this.database, this.changeListener) + : _queryAdapter = QueryAdapter(database, changeListener), + _personInsertionAdapter = InsertionAdapter( + database, + 'Person', + (Person item) => + {'id': item.id, 'name': item.name}, + changeListener), + _personUpdateAdapter = UpdateAdapter( + database, + 'Person', + 'id', + (Person item) => + {'id': item.id, 'name': item.name}, + changeListener), + _personDeletionAdapter = DeletionAdapter( + database, + 'Person', + 'id', + (Person item) => + {'id': item.id, 'name': item.name}, + changeListener); + + final sqflite.DatabaseExecutor database; + + final StreamController changeListener; + + final QueryAdapter _queryAdapter; + + final _personMapper = (Map row) => + Person(row['id'] as int, row['name'] as String); + + final InsertionAdapter _personInsertionAdapter; + + final UpdateAdapter _personUpdateAdapter; + + final DeletionAdapter _personDeletionAdapter; + + @override + Stream> findAllPersonsAsStream() { + return _queryAdapter.queryListStream('SELECT * FROM person', 'Person', _personMapper); + } + + @override + Future insertPerson(Person person) async { + await _personInsertionAdapter.insert(person, sqflite.ConflictAlgorithm.abort); + } + + @override + Future updatePerson(Person person) async { + await _personUpdateAdapter.update(person, sqflite.ConflictAlgorithm.abort); + } + + @override + Future deletePerson(Person person) async { + await _personDeletionAdapter.delete(person); + } + } + ''')); + }); +} + +Future _createDao(final String dao) async { + final library = await resolveSource(''' + library test; + + import 'package:floor_annotation/floor_annotation.dart'; + + $dao + + @entity + class Person { + @primaryKey + final int id; + + final String name; + + Person(this.id, this.name); + } + ''', (resolver) async { + return LibraryReader(await resolver.findLibraryByName('test')); + }); + + final daoClass = library.classes.firstWhere((classElement) => + typeChecker(annotations.dao.runtimeType) + .hasAnnotationOfExact(classElement)); + + final entities = library.classes + .where((classElement) => + typeChecker(annotations.Entity).hasAnnotationOfExact(classElement)) + .map((classElement) => EntityProcessor(classElement).process()) + .toList(); + + return DaoProcessor(daoClass, 'personDao', 'TestDatabase', entities) + .process(); +} diff --git a/floor_generator/test/writer/deletion_method_writer_test.dart b/floor_generator/test/writer/deletion_method_writer_test.dart new file mode 100644 index 00000000..44b2a42f --- /dev/null +++ b/floor_generator/test/writer/deletion_method_writer_test.dart @@ -0,0 +1,126 @@ +import 'package:build_test/build_test.dart'; +import 'package:code_builder/code_builder.dart'; +import 'package:floor_annotation/floor_annotation.dart' as annotations; +import 'package:floor_generator/misc/type_utils.dart'; +import 'package:floor_generator/processor/dao_processor.dart'; +import 'package:floor_generator/processor/entity_processor.dart'; +import 'package:floor_generator/value_object/deletion_method.dart'; +import 'package:floor_generator/writer/deletion_method_writer.dart'; +import 'package:source_gen/source_gen.dart'; +import 'package:test/test.dart'; + +import '../test_utils.dart'; + +void main() { + useDartfmt(); + + group('void return deletion', () { + test('delete person', () async { + final deletionMethod = await _createDeletionMethod(''' + @delete + Future deletePerson(Person person); + '''); + + final actual = DeletionMethodWriter(deletionMethod).write(); + + expect(actual, equalsDart(r''' + @override + Future deletePerson(Person person) async { + await _personDeletionAdapter.delete(person); + } + ''')); + }); + + test('delete multiple persons', () async { + final deletionMethod = await _createDeletionMethod(''' + @delete + Future deletePersons(List persons); + '''); + + final actual = DeletionMethodWriter(deletionMethod).write(); + + expect(actual, equalsDart(r''' + @override + Future deletePersons(List persons) async { + await _personDeletionAdapter.deleteList(persons); + } + ''')); + }); + }); + + group('int return deletion', () { + test('delete person and return changed rows count', () async { + final deletionMethod = await _createDeletionMethod(''' + @delete + Future deletePerson(Person person); + '''); + + final actual = DeletionMethodWriter(deletionMethod).write(); + + expect(actual, equalsDart(r''' + @override + Future deletePerson(Person person) { + return _personDeletionAdapter.deleteAndReturnChangedRows(person); + } + ''')); + }); + + test('delete multiple persons and return changed rows count', () async { + final deletionMethod = await _createDeletionMethod(''' + @delete + Future deletePersons(List persons); + '''); + + final actual = DeletionMethodWriter(deletionMethod).write(); + + expect(actual, equalsDart(r''' + @override + Future deletePersons(List persons) { + return _personDeletionAdapter.deleteListAndReturnChangedRows(persons); + } + ''')); + }); + }); +} + +Future _createDeletionMethod( + final String methodSignature, +) async { + final library = await resolveSource(''' + library test; + + import 'package:floor_annotation/floor_annotation.dart'; + + @dao + abstract class PersonDao { + + $methodSignature + } + + @entity + class Person { + @primaryKey + final int id; + + final String name; + + Person(this.id, this.name); + } + ''', (resolver) async { + return LibraryReader(await resolver.findLibraryByName('test')); + }); + + final daoClass = library.classes.firstWhere((classElement) => + typeChecker(annotations.dao.runtimeType) + .hasAnnotationOfExact(classElement)); + + final entities = library.classes + .where((classElement) => + typeChecker(annotations.Entity).hasAnnotationOfExact(classElement)) + .map((classElement) => EntityProcessor(classElement).process()) + .toList(); + + final dao = + DaoProcessor(daoClass, 'personDao', 'TestDatabase', entities).process(); + return dao.deletionMethods.first; +} diff --git a/floor_generator/test/writer/insert_method_writer_test.dart b/floor_generator/test/writer/insert_method_writer_test.dart index f1e47c87..f256a37f 100644 --- a/floor_generator/test/writer/insert_method_writer_test.dart +++ b/floor_generator/test/writer/insert_method_writer_test.dart @@ -4,6 +4,7 @@ import 'package:floor_annotation/floor_annotation.dart' as annotations; import 'package:floor_generator/misc/type_utils.dart'; import 'package:floor_generator/processor/dao_processor.dart'; import 'package:floor_generator/processor/entity_processor.dart'; +import 'package:floor_generator/value_object/insertion_method.dart'; import 'package:floor_generator/writer/insertion_method_writer.dart'; import 'package:source_gen/source_gen.dart'; import 'package:test/test.dart'; @@ -15,11 +16,13 @@ void main() { group('void return insert', () { test('insert single person', () async { - final actual = await _generateInsertMethod(''' + final insertionMethod = await _createInsertionMethod(''' @insert Future insertPerson(Person person); '''); + final actual = InsertionMethodWriter(insertionMethod).write(); + expect(actual, equalsDart(r''' @override Future insertPerson(Person person) async { @@ -29,11 +32,13 @@ void main() { }); test('insert person list', () async { - final actual = await _generateInsertMethod(''' + final insertionMethod = await _createInsertionMethod(''' @insert Future insertPersons(List persons); '''); + final actual = InsertionMethodWriter(insertionMethod).write(); + expect(actual, equalsDart(''' @override Future insertPersons(List persons) async { @@ -45,11 +50,13 @@ void main() { group('int return insert', () { test('insert single person', () async { - final actual = await _generateInsertMethod(''' + final insertionMethod = await _createInsertionMethod(''' @insert Future insertPersonWithReturn(Person person); '''); + final actual = InsertionMethodWriter(insertionMethod).write(); + expect(actual, equalsDart(''' @override Future insertPersonWithReturn(Person person) { @@ -59,11 +66,13 @@ void main() { }); test('insert person list', () async { - final actual = await _generateInsertMethod(''' + final insertionMethod = await _createInsertionMethod(''' @insert Future> insertPersonsWithReturn(List persons); '''); + final actual = InsertionMethodWriter(insertionMethod).write(); + expect(actual, equalsDart(''' @override Future> insertPersonsWithReturn(List persons) { @@ -73,94 +82,25 @@ void main() { }); }); - group('on conflic strategy', () { - test('insert method on conflict default (abort)', () async { - final actual = await _generateInsertMethod(''' - @insert - Future insertPerson(Person person); - '''); - - expect(actual, equalsDart(r''' - @override - Future insertPerson(Person person) async { - await _personInsertionAdapter.insert(person, sqflite.ConflictAlgorithm.abort); - } - ''')); - }); - - test('insert method on conflict replace', () async { - final actual = await _generateInsertMethod(''' + test('insert method on conflict replace', () async { + final insertionMethod = await _createInsertionMethod(''' @Insert(onConflict: OnConflictStrategy.REPLACE) Future insertPerson(Person person); '''); - expect(actual, equalsDart(r''' - @override - Future insertPerson(Person person) async { - await _personInsertionAdapter.insert(person, sqflite.ConflictAlgorithm.replace); - } - ''')); - }); - - test('insert method on conflict rollback', () async { - final actual = await _generateInsertMethod(''' - @Insert(onConflict: OnConflictStrategy.ROLLBACK) - Future insertPerson(Person person); - '''); + final actual = InsertionMethodWriter(insertionMethod).write(); - expect(actual, equalsDart(r''' - @override - Future insertPerson(Person person) async { - await _personInsertionAdapter.insert(person, sqflite.ConflictAlgorithm.rollback); - } - ''')); - }); - - test('insert method on conflict abort', () async { - final actual = await _generateInsertMethod(''' - @Insert(onConflict: OnConflictStrategy.ABORT) - Future insertPerson(Person person); - '''); - - expect(actual, equalsDart(r''' + expect(actual, equalsDart(r''' @override Future insertPerson(Person person) async { - await _personInsertionAdapter.insert(person, sqflite.ConflictAlgorithm.abort); - } - ''')); - }); - - test('insert method on conflict fail', () async { - final actual = await _generateInsertMethod(''' - @Insert(onConflict: OnConflictStrategy.FAIL) - Future insertPerson(Person person); - '''); - - expect(actual, equalsDart(r''' - @override - Future insertPerson(Person person) async { - await _personInsertionAdapter.insert(person, sqflite.ConflictAlgorithm.fail); - } - ''')); - }); - - test('insert method on conflict ignore', () async { - final actual = await _generateInsertMethod(''' - @Insert(onConflict: OnConflictStrategy.IGNORE) - Future insertPerson(Person person); - '''); - - expect(actual, equalsDart(r''' - @override - Future insertPerson(Person person) async { - await _personInsertionAdapter.insert(person, sqflite.ConflictAlgorithm.ignore); + await _personInsertionAdapter.insert(person, sqflite.ConflictAlgorithm.replace); } ''')); - }); }); } -Future _generateInsertMethod(final String methodSignature) async { +Future _createInsertionMethod( + final String methodSignature) async { final library = await resolveSource(''' library test; @@ -172,12 +112,11 @@ Future _generateInsertMethod(final String methodSignature) async { $methodSignature } - @Entity(tableName: 'person') + @entity class Person { - @PrimaryKey() + @primaryKey final int id; - @ColumnInfo(name: 'custom_name', nullable: false) final String name; Person(this.id, this.name); @@ -198,6 +137,5 @@ Future _generateInsertMethod(final String methodSignature) async { final dao = DaoProcessor(daoClass, 'personDao', 'TestDatabase', entities).process(); - final insertMethod = dao.insertionMethods.first; - return InsertionMethodWriter(insertMethod).write(); + return dao.insertionMethods.first; } diff --git a/floor_generator/test/writer/query_method_writer_test.dart b/floor_generator/test/writer/query_method_writer_test.dart index ca3b4d44..6e38358a 100644 --- a/floor_generator/test/writer/query_method_writer_test.dart +++ b/floor_generator/test/writer/query_method_writer_test.dart @@ -15,7 +15,7 @@ void main() { useDartfmt(); test('query no return', () async { - final queryMethod = await _generateQueryMethod(''' + final queryMethod = await _createQueryMethod(''' @Query('DELETE FROM Person') Future deleteAll(); '''); @@ -31,7 +31,7 @@ void main() { }); test('query item', () async { - final queryMethod = await _generateQueryMethod(''' + final queryMethod = await _createQueryMethod(''' @Query('SELECT * FROM Person WHERE id = :id') Future findById(int id); '''); @@ -47,7 +47,7 @@ void main() { }); test('query list', () async { - final queryMethod = await _generateQueryMethod(''' + final queryMethod = await _createQueryMethod(''' @Query('SELECT * FROM Person') Future> findAll(); '''); @@ -63,7 +63,7 @@ void main() { }); test('query item stream', () async { - final queryMethod = await _generateQueryMethod(''' + final queryMethod = await _createQueryMethod(''' @Query('SELECT * FROM Person WHERE id = :id') Stream findByIdAsStream(int id); '''); @@ -79,7 +79,7 @@ void main() { }); test('query list stream', () async { - final queryMethod = await _generateQueryMethod(''' + final queryMethod = await _createQueryMethod(''' @Query('SELECT * FROM Person') Stream> findAllAsStream(); '''); @@ -95,7 +95,7 @@ void main() { }); } -Future _generateQueryMethod(final String method) async { +Future _createQueryMethod(final String method) async { final library = await resolveSource(''' library test; diff --git a/floor_generator/test/writer/update_method_writer_test.dart b/floor_generator/test/writer/update_method_writer_test.dart new file mode 100644 index 00000000..8df92dfc --- /dev/null +++ b/floor_generator/test/writer/update_method_writer_test.dart @@ -0,0 +1,142 @@ +import 'package:build_test/build_test.dart'; +import 'package:code_builder/code_builder.dart'; +import 'package:floor_annotation/floor_annotation.dart' as annotations; +import 'package:floor_generator/misc/type_utils.dart'; +import 'package:floor_generator/processor/dao_processor.dart'; +import 'package:floor_generator/processor/entity_processor.dart'; +import 'package:floor_generator/value_object/update_method.dart'; +import 'package:floor_generator/writer/update_method_writer.dart'; +import 'package:source_gen/source_gen.dart'; +import 'package:test/test.dart'; + +import '../test_utils.dart'; + +void main() { + useDartfmt(); + + group('void return update', () { + test('update person', () async { + final updateMethod = await _createUpdateMethod(''' + @update + Future updatePerson(Person person); + '''); + + final actual = UpdateMethodWriter(updateMethod).write(); + + expect(actual, equalsDart(r''' + @override + Future updatePerson(Person person) async { + await _personUpdateAdapter.update(person, sqflite.ConflictAlgorithm.abort); + } + ''')); + }); + + test('update multiple persons', () async { + final updateMethod = await _createUpdateMethod(''' + @update + Future updatePersons(List persons); + '''); + + final actual = UpdateMethodWriter(updateMethod).write(); + + expect(actual, equalsDart(r''' + @override + Future updatePersons(List persons) async { + await _personUpdateAdapter.updateList(persons, sqflite.ConflictAlgorithm.abort); + } + ''')); + }); + }); + + group('int return update', () { + test('update person and return changed rows count', () async { + final updateMethod = await _createUpdateMethod(''' + @update + Future updatePerson(Person person); + '''); + + final actual = UpdateMethodWriter(updateMethod).write(); + + expect(actual, equalsDart(r''' + @override + Future updatePerson(Person person) { + return _personUpdateAdapter.updateAndReturnChangedRows(person, sqflite.ConflictAlgorithm.abort); + } + ''')); + }); + + test('update multiple persons and return changed rows count', () async { + final updateMethod = await _createUpdateMethod(''' + @update + Future updatePersons(List persons); + '''); + + final actual = UpdateMethodWriter(updateMethod).write(); + + expect(actual, equalsDart(r''' + @override + Future updatePersons(List persons) { + return _personUpdateAdapter.updateListAndReturnChangedRows(persons, sqflite.ConflictAlgorithm.abort); + } + ''')); + }); + }); + + test('update person on conflict fail', () async { + final updateMethod = await _createUpdateMethod(''' + @Update(onConflict: OnConflictStrategy.FAIL) + Future updatePerson(Person person); + '''); + + final actual = UpdateMethodWriter(updateMethod).write(); + + expect(actual, equalsDart(r''' + @override + Future updatePerson(Person person) async { + await _personUpdateAdapter.update(person, sqflite.ConflictAlgorithm.fail); + } + ''')); + }); +} + +Future _createUpdateMethod( + final String methodSignature, +) async { + final library = await resolveSource(''' + library test; + + import 'package:floor_annotation/floor_annotation.dart'; + + @dao + abstract class PersonDao { + + $methodSignature + } + + @entity + class Person { + @primaryKey + final int id; + + final String name; + + Person(this.id, this.name); + } + ''', (resolver) async { + return LibraryReader(await resolver.findLibraryByName('test')); + }); + + final daoClass = library.classes.firstWhere((classElement) => + typeChecker(annotations.dao.runtimeType) + .hasAnnotationOfExact(classElement)); + + final entities = library.classes + .where((classElement) => + typeChecker(annotations.Entity).hasAnnotationOfExact(classElement)) + .map((classElement) => EntityProcessor(classElement).process()) + .toList(); + + final dao = + DaoProcessor(daoClass, 'personDao', 'TestDatabase', entities).process(); + return dao.updateMethods.first; +}