From eeb91ab2c3c96c2cc9e097a9d29d76fefa617ca2 Mon Sep 17 00:00:00 2001 From: Vitus Ortner Date: Fri, 1 Mar 2019 15:11:58 +0100 Subject: [PATCH] Add more tests for insert method writer, database writer --- .../test/database_writer_test.dart | 123 +++++++++ .../test/insert_method_writer_test.dart | 261 ++++++++++++------ 2 files changed, 294 insertions(+), 90 deletions(-) create mode 100644 floor_generator/test/database_writer_test.dart diff --git a/floor_generator/test/database_writer_test.dart b/floor_generator/test/database_writer_test.dart new file mode 100644 index 00000000..3033cb2a --- /dev/null +++ b/floor_generator/test/database_writer_test.dart @@ -0,0 +1,123 @@ +import 'package:build_test/build_test.dart'; +import 'package:code_builder/code_builder.dart'; +import 'package:floor_generator/writer/database_writer.dart'; +import 'package:source_gen/source_gen.dart'; +import 'package:test/test.dart'; + +import 'test_utils.dart'; + +void main() { + useDartfmt(); + + test('open database for simple entity', () async { + final actual = await _generateDatabase(''' + @entity + class Person { + @PrimaryKey() + final int id; + + final String name; + + Person(this.id, this.name); + } + '''); + + expect(actual, equalsDart(r''' + Future _$open() async { + final database = _$TestDatabase(); + database.database = await database.open(); + return database; + } + + class _$TestDatabase extends TestDatabase { + @override + Future open() async { + final path = join(await sqflite.getDatabasesPath(), 'testdatabase.db'); + + return sqflite.openDatabase( + path, + version: 1, + onConfigure: (database) async { + await database.execute('PRAGMA foreign_keys = ON'); + }, + onCreate: (database, version) async { + await database.execute( + 'CREATE TABLE IF NOT EXISTS `Person` (`id` INTEGER PRIMARY KEY NOT NULL, `name` TEXT)'); + }, + ); + } + } + ''')); + }); + + test('open database for complex entity', () async { + final actual = await _generateDatabase(''' + @Entity(tableName: 'custom_table_name') + class Person { + @PrimaryKey(autoGenerate: true) + final int id; + + @ColumnInfo(name: 'custom_name', nullable: false) + final String name; + + Person(this.id, this.name); + } + '''); + + expect(actual, equalsDart(r''' + Future _$open() async { + final database = _$TestDatabase(); + database.database = await database.open(); + return database; + } + + class _$TestDatabase extends TestDatabase { + @override + Future open() async { + final path = join(await sqflite.getDatabasesPath(), 'testdatabase.db'); + + return sqflite.openDatabase( + path, + version: 1, + onConfigure: (database) async { + await database.execute('PRAGMA foreign_keys = ON'); + }, + onCreate: (database, version) async { + await database.execute( + 'CREATE TABLE IF NOT EXISTS `custom_table_name` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `custom_name` TEXT NOT NULL)'); + }, + ); + } + } + ''')); + }); + + test('exception when no entitiy defined', () async { + expect( + () => _generateDatabase(''), + throwsInvalidGenerationSourceError, + ); + }); +} + +Future _generateDatabase(final String entity) async { + final library = await resolveSource(''' + library test; + + import 'package:floor_annotation/floor_annotation.dart'; + + @Database() + abstract class TestDatabase extends FloorDatabase { + static Future openDatabase() async => _\$open(); + } + + $entity + ''', (resolver) async { + return LibraryReader(await resolver.findLibraryByName('test')); + }); + + return DatabaseWriter(library).write(); +} + +final throwsInvalidGenerationSourceError = + throwsA(const TypeMatcher()); diff --git a/floor_generator/test/insert_method_writer_test.dart b/floor_generator/test/insert_method_writer_test.dart index 979a5212..13591b70 100644 --- a/floor_generator/test/insert_method_writer_test.dart +++ b/floor_generator/test/insert_method_writer_test.dart @@ -12,106 +12,186 @@ import 'test_utils.dart'; void main() { useDartfmt(); - 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 { - final item = person; - final values = {'id': item.id, 'custom_name': item.name}; - await database.insert('person', values, - conflictAlgorithm: sqflite.ConflictAlgorithm.abort); - } + group('void return insert', () { + test('insert single person', () async { + final actual = await _generateInsertMethod(''' + @insert + Future insertPerson(Person person); + '''); + + expect(actual, equalsDart(r''' + @override + Future insertPerson(Person person) async { + final item = person; + final values = {'id': item.id, 'custom_name': item.name}; + await database.insert('person', values, + conflictAlgorithm: sqflite.ConflictAlgorithm.abort); + } ''')); - }); - - test('insert method on conflict replace', () async { - final actual = await _generateInsertMethod(''' - @Insert(onConflict: OnConflictStrategy.REPLACE) - Future insertPerson(Person person); - '''); - - expect(actual, equalsDart(r''' - @override - Future insertPerson(Person person) async { - final item = person; - final values = {'id': item.id, 'custom_name': item.name}; - await database.insert('person', values, - conflictAlgorithm: sqflite.ConflictAlgorithm.replace); - } + }); + + test('insert person list', () async { + final actual = await _generateInsertMethod(''' + @insert + Future insertPersons(List persons); + '''); + + expect(actual, equalsDart(''' + @override + Future insertPersons(List persons) async { + final batch = database.batch(); + for (final item in persons) { + final values = {'id': item.id, 'custom_name': item.name}; + batch.insert('person', values, + conflictAlgorithm: sqflite.ConflictAlgorithm.abort); + } + await batch.commit(noResult: true); + } ''')); + }); }); - test('insert method on conflict rollback', () async { - final actual = await _generateInsertMethod(''' - @Insert(onConflict: OnConflictStrategy.ROLLBACK) - Future insertPerson(Person person); - '''); - - expect(actual, equalsDart(r''' - @override - Future insertPerson(Person person) async { - final item = person; - final values = {'id': item.id, 'custom_name': item.name}; - await database.insert('person', values, - conflictAlgorithm: sqflite.ConflictAlgorithm.rollback); - } + group('int return insert', () { + test('insert single person', () async { + final actual = await _generateInsertMethod(''' + @insert + Future insertPersonWithReturn(Person person); + '''); + + expect(actual, equalsDart(''' + @override + Future insertPersonWithReturn(Person person) { + final item = person; + final values = {'id': item.id, 'custom_name': item.name}; + return database.insert('person', values, + conflictAlgorithm: sqflite.ConflictAlgorithm.abort); + } ''')); - }); - - test('insert method on conflict abort', () async { - final actual = await _generateInsertMethod(''' - @Insert(onConflict: OnConflictStrategy.ABORT) - Future insertPerson(Person person); - '''); - - expect(actual, equalsDart(r''' - @override - Future insertPerson(Person person) async { - final item = person; - final values = {'id': item.id, 'custom_name': item.name}; - await database.insert('person', values, - conflictAlgorithm: sqflite.ConflictAlgorithm.abort); - } + }); + + test('insert person list', () async { + final actual = await _generateInsertMethod(''' + @insert + Future> insertPersonsWithReturn(List persons); + '''); + + expect(actual, equalsDart(''' + @override + Future> insertPersonsWithReturn(List persons) async { + final batch = database.batch(); + for (final item in persons) { + final values = {'id': item.id, 'custom_name': item.name}; + batch.insert('person', values, + conflictAlgorithm: sqflite.ConflictAlgorithm.abort); + } + return (await batch.commit(noResult: false)).cast(); + } ''')); + }); }); - 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 { - final item = person; - final values = {'id': item.id, 'custom_name': item.name}; - await database.insert('person', values, - conflictAlgorithm: sqflite.ConflictAlgorithm.fail); - } + 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 { + final item = person; + final values = {'id': item.id, 'custom_name': item.name}; + await database.insert('person', values, + conflictAlgorithm: sqflite.ConflictAlgorithm.abort); + } + ''')); + }); + + test('insert method on conflict replace', () async { + final actual = await _generateInsertMethod(''' + @Insert(onConflict: OnConflictStrategy.REPLACE) + Future insertPerson(Person person); + '''); + + expect(actual, equalsDart(r''' + @override + Future insertPerson(Person person) async { + final item = person; + final values = {'id': item.id, 'custom_name': item.name}; + await database.insert('person', values, + conflictAlgorithm: sqflite.ConflictAlgorithm.replace); + } ''')); - }); - - 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 { - final item = person; - final values = {'id': item.id, 'custom_name': item.name}; - await database.insert('person', values, - conflictAlgorithm: sqflite.ConflictAlgorithm.ignore); - } + }); + + test('insert method on conflict rollback', () async { + final actual = await _generateInsertMethod(''' + @Insert(onConflict: OnConflictStrategy.ROLLBACK) + Future insertPerson(Person person); + '''); + + expect(actual, equalsDart(r''' + @override + Future insertPerson(Person person) async { + final item = person; + final values = {'id': item.id, 'custom_name': item.name}; + await database.insert('person', values, + conflictAlgorithm: 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''' + @override + Future insertPerson(Person person) async { + final item = person; + final values = {'id': item.id, 'custom_name': item.name}; + await database.insert('person', values, + conflictAlgorithm: 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 { + final item = person; + final values = {'id': item.id, 'custom_name': item.name}; + await database.insert('person', values, + conflictAlgorithm: 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 { + final item = person; + final values = {'id': item.id, 'custom_name': item.name}; + await database.insert('person', values, + conflictAlgorithm: sqflite.ConflictAlgorithm.ignore); + } + ''')); + }); }); } @@ -141,6 +221,7 @@ Future _generateInsertMethod(final String methodSignature) async { ''', (resolver) async { return LibraryReader(await resolver.findLibraryByName('test')); }); + final databaseClass = library.classes .where((clazz) => clazz.isAbstract && clazz.metadata.any(isDatabaseAnnotation))