diff --git a/angel_orm_generator/test/common.dart b/angel_orm_generator/test/common.dart deleted file mode 100644 index 4f7843c3..00000000 --- a/angel_orm_generator/test/common.dart +++ /dev/null @@ -1,67 +0,0 @@ -import 'dart:async'; -import 'dart:io'; -import 'package:angel_orm/angel_orm.dart'; -import 'package:postgres/postgres.dart'; - -Future connectToPostgres(Iterable schemas) async { - var conn = new PostgreSQLConnection('127.0.0.1', 5432, 'angel_orm_test', - username: Platform.environment['POSTGRES_USERNAME'] ?? 'postgres', - password: Platform.environment['POSTGRES_PASSWORD'] ?? 'password'); - await conn.open(); - - for (var s in schemas) - await conn.execute(await new File('test/migrations/$s.sql').readAsString()); - - return new PostgresExecutor(conn); -} - -class PostgresExecutor extends QueryExecutor { - PostgreSQLExecutionContext connection; - - PostgresExecutor(this.connection); - - Future close() => (connection as PostgreSQLConnection).close(); - - @override - Future> query( - String tableName, String query, Map substitutionValues, - [List returningFields]) async { - if (returningFields != null) { - var fields = returningFields.join(', '); - var returning = 'RETURNING $fields'; - query = '$query $returning'; - } - - if (!Platform.environment.containsKey('STFU')) { - print('Running: $query'); - if (substitutionValues.isNotEmpty) print('Values: $substitutionValues'); - print(substitutionValues.map((k, v) => MapEntry(k, v.runtimeType))); - } - - var rows = - await connection.query(query, substitutionValues: substitutionValues); - - if (!Platform.environment.containsKey('STFU')) { - print('Got ${rows.length} row(s):'); - rows.forEach(print); - } - - return rows; - } - - @override - Future transaction(FutureOr Function() f) async { - if (connection is! PostgreSQLConnection) return await f(); - var old = connection; - T result; - try { - await (connection as PostgreSQLConnection).transaction((ctx) async { - connection = ctx; - result = await f(); - }); - } finally { - connection = old; - return result; - } - } -} diff --git a/angel_orm_postgres/test/all_test.dart b/angel_orm_postgres/test/all_test.dart index 19ed3f60..aaa3c4ef 100644 --- a/angel_orm_postgres/test/all_test.dart +++ b/angel_orm_postgres/test/all_test.dart @@ -1,27 +1,31 @@ -import 'dart:async'; - -import 'package:angel_orm_postgres/angel_orm_postgres.dart'; import 'package:angel_orm_test/angel_orm_test.dart'; -import 'package:postgres/postgres.dart'; +import 'package:logging/logging.dart'; import 'package:test/test.dart'; +import 'common.dart'; void main() { - PostgreSQLConnection _connect() { - return PostgreSQLConnection('localhost', 5432, 'angel_orm_test'); - } + Logger.root.onRecord.listen((rec) { + print(rec); + if (rec.error != null) print(rec.error); + if (rec.stackTrace != null) print(rec.stackTrace); + }); - group('single', () { - PostgreSqlExecutor executor; - PostgreSQLConnection c = _connect(); - - setUp(() async { - var c = _connect(); - await c.open(); - executor = PostgreSqlExecutor(c); - }); - - tearDown(() => executor.close()); - - ormTests(() => executor); + group('postgresql', () { + group('belongsTo', + () => belongsToTests(pg(['author', 'book']), close: closePg)); + group( + 'edgeCase', + () => edgeCaseTests(pg(['unorthodox', 'weird_join', 'song', 'numba']), + close: closePg)); + group('enumAndNested', + () => enumAndNestedTests(pg(['has_car']), close: closePg)); + group('hasMany', () => hasManyTests(pg(['tree', 'fruit']), close: closePg)); + group('hasMap', () => hasMapTests(pg(['has_map']), close: closePg)); + group('hasOne', () => hasOneTests(pg(['leg', 'foot']), close: closePg)); + group( + 'manyToMany', + () => + manyToManyTests(pg(['user', 'role', 'user_role']), close: closePg)); + group('standalone', () => standaloneTests(pg(['car']), close: closePg)); }); } diff --git a/angel_orm_postgres/test/common.dart b/angel_orm_postgres/test/common.dart new file mode 100644 index 00000000..84be5128 --- /dev/null +++ b/angel_orm_postgres/test/common.dart @@ -0,0 +1,25 @@ +import 'dart:async'; +import 'dart:io'; +import 'package:angel_orm/angel_orm.dart'; +import 'package:angel_orm_postgres/angel_orm_postgres.dart'; +import 'package:logging/logging.dart'; +import 'package:postgres/postgres.dart'; + +FutureOr Function() pg(Iterable schemas) { + return () => connectToPostgres(schemas); +} + +Future closePg(QueryExecutor executor) => + (executor as PostgreSqlExecutor).close(); + +Future connectToPostgres(Iterable schemas) async { + var conn = new PostgreSQLConnection('127.0.0.1', 5432, 'angel_orm_test', + username: Platform.environment['POSTGRES_USERNAME'] ?? 'postgres', + password: Platform.environment['POSTGRES_PASSWORD'] ?? 'password'); + await conn.open(); + + for (var s in schemas) + await conn.execute(await new File('test/migrations/$s.sql').readAsString()); + + return new PostgreSqlExecutor(conn, logger: Logger.root); +} diff --git a/angel_orm_generator/test/migrations/author.sql b/angel_orm_postgres/test/migrations/author.sql similarity index 100% rename from angel_orm_generator/test/migrations/author.sql rename to angel_orm_postgres/test/migrations/author.sql diff --git a/angel_orm_generator/test/migrations/book.sql b/angel_orm_postgres/test/migrations/book.sql similarity index 100% rename from angel_orm_generator/test/migrations/book.sql rename to angel_orm_postgres/test/migrations/book.sql diff --git a/angel_orm_generator/test/migrations/car.sql b/angel_orm_postgres/test/migrations/car.sql similarity index 100% rename from angel_orm_generator/test/migrations/car.sql rename to angel_orm_postgres/test/migrations/car.sql diff --git a/angel_orm_generator/test/migrations/foot.sql b/angel_orm_postgres/test/migrations/foot.sql similarity index 100% rename from angel_orm_generator/test/migrations/foot.sql rename to angel_orm_postgres/test/migrations/foot.sql diff --git a/angel_orm_generator/test/migrations/fruit.sql b/angel_orm_postgres/test/migrations/fruit.sql similarity index 100% rename from angel_orm_generator/test/migrations/fruit.sql rename to angel_orm_postgres/test/migrations/fruit.sql diff --git a/angel_orm_generator/test/migrations/has_car.sql b/angel_orm_postgres/test/migrations/has_car.sql similarity index 100% rename from angel_orm_generator/test/migrations/has_car.sql rename to angel_orm_postgres/test/migrations/has_car.sql diff --git a/angel_orm_generator/test/migrations/has_map.sql b/angel_orm_postgres/test/migrations/has_map.sql similarity index 100% rename from angel_orm_generator/test/migrations/has_map.sql rename to angel_orm_postgres/test/migrations/has_map.sql diff --git a/angel_orm_generator/test/migrations/leg.sql b/angel_orm_postgres/test/migrations/leg.sql similarity index 100% rename from angel_orm_generator/test/migrations/leg.sql rename to angel_orm_postgres/test/migrations/leg.sql diff --git a/angel_orm_generator/test/migrations/numba.sql b/angel_orm_postgres/test/migrations/numba.sql similarity index 100% rename from angel_orm_generator/test/migrations/numba.sql rename to angel_orm_postgres/test/migrations/numba.sql diff --git a/angel_orm_generator/test/migrations/role.sql b/angel_orm_postgres/test/migrations/role.sql similarity index 100% rename from angel_orm_generator/test/migrations/role.sql rename to angel_orm_postgres/test/migrations/role.sql diff --git a/angel_orm_generator/test/migrations/song.sql b/angel_orm_postgres/test/migrations/song.sql similarity index 100% rename from angel_orm_generator/test/migrations/song.sql rename to angel_orm_postgres/test/migrations/song.sql diff --git a/angel_orm_generator/test/migrations/tree.sql b/angel_orm_postgres/test/migrations/tree.sql similarity index 100% rename from angel_orm_generator/test/migrations/tree.sql rename to angel_orm_postgres/test/migrations/tree.sql diff --git a/angel_orm_generator/test/migrations/unorthodox.sql b/angel_orm_postgres/test/migrations/unorthodox.sql similarity index 100% rename from angel_orm_generator/test/migrations/unorthodox.sql rename to angel_orm_postgres/test/migrations/unorthodox.sql diff --git a/angel_orm_generator/test/migrations/user.sql b/angel_orm_postgres/test/migrations/user.sql similarity index 100% rename from angel_orm_generator/test/migrations/user.sql rename to angel_orm_postgres/test/migrations/user.sql diff --git a/angel_orm_generator/test/migrations/user_role.sql b/angel_orm_postgres/test/migrations/user_role.sql similarity index 100% rename from angel_orm_generator/test/migrations/user_role.sql rename to angel_orm_postgres/test/migrations/user_role.sql diff --git a/angel_orm_generator/test/migrations/weird_join.sql b/angel_orm_postgres/test/migrations/weird_join.sql similarity index 100% rename from angel_orm_generator/test/migrations/weird_join.sql rename to angel_orm_postgres/test/migrations/weird_join.sql diff --git a/angel_orm_generator/test/example/car_controller.dart b/angel_orm_test/example/car_controller.dart similarity index 90% rename from angel_orm_generator/test/example/car_controller.dart rename to angel_orm_test/example/car_controller.dart index 063491f5..536d0847 100644 --- a/angel_orm_generator/test/example/car_controller.dart +++ b/angel_orm_test/example/car_controller.dart @@ -1,7 +1,7 @@ import 'dart:async'; import 'package:angel_framework/angel_framework.dart'; import 'package:angel_orm/angel_orm.dart'; -import '../models/car.dart'; +import 'package:angel_orm_test/src/models/car.dart'; @Expose('/api/cars') class CarController extends Controller { diff --git a/angel_orm_test/lib/angel_orm_test.dart b/angel_orm_test/lib/angel_orm_test.dart index a037aa44..fec4592c 100644 --- a/angel_orm_test/lib/angel_orm_test.dart +++ b/angel_orm_test/lib/angel_orm_test.dart @@ -1,2 +1,8 @@ -export 'src/cases/cases.dart'; -export 'src/models/models.dart'; \ No newline at end of file +export 'src/belongs_to_test.dart'; +export 'src/edge_case_test.dart'; +export 'src/enum_and_nested_test.dart'; +export 'src/has_many_test.dart'; +export 'src/has_map_test.dart'; +export 'src/has_one_test.dart'; +export 'src/many_to_many_test.dart'; +export 'src/standalone_test.dart'; diff --git a/angel_orm_generator/test/belongs_to_test.dart b/angel_orm_test/lib/src/belongs_to_test.dart similarity index 93% rename from angel_orm_generator/test/belongs_to_test.dart rename to angel_orm_test/lib/src/belongs_to_test.dart index bb69b0e3..7b203b84 100644 --- a/angel_orm_generator/test/belongs_to_test.dart +++ b/angel_orm_test/lib/src/belongs_to_test.dart @@ -1,19 +1,19 @@ -/// Tests for @belongsTo... -library angel_orm_generator.test.book_test; - +import 'dart:async'; +import 'package:angel_orm/angel_orm.dart'; import 'package:test/test.dart'; import 'models/author.dart'; import 'models/book.dart'; -import 'common.dart'; -main() { - PostgresExecutor executor; +belongsToTests(FutureOr Function() createExecutor, + {FutureOr Function(QueryExecutor) close}) { + QueryExecutor executor; Author jkRowling; Author jameson; Book deathlyHallows; + close ??= (_) => null; setUp(() async { - executor = await connectToPostgres(['author', 'book']); + executor = await createExecutor(); // Insert an author var query = new AuthorQuery()..values.name = 'J.K. Rowling'; @@ -32,7 +32,7 @@ main() { deathlyHallows = await bookQuery.insert(executor); }); - tearDown(() => executor.close()); + tearDown(() => close(executor)); group('selects', () { test('select all', () async { diff --git a/angel_orm_test/lib/src/cases/cases.dart b/angel_orm_test/lib/src/cases/cases.dart deleted file mode 100644 index 237de063..00000000 --- a/angel_orm_test/lib/src/cases/cases.dart +++ /dev/null @@ -1,7 +0,0 @@ -import 'package:angel_orm/angel_orm.dart'; -import 'package:test/test.dart'; -import 'standalone.dart'; - -void ormTests(QueryExecutor Function() executor) { - group('standalone', standaloneTests(executor)); -} diff --git a/angel_orm_test/lib/src/cases/standalone.dart b/angel_orm_test/lib/src/cases/standalone.dart deleted file mode 100644 index 62b3af54..00000000 --- a/angel_orm_test/lib/src/cases/standalone.dart +++ /dev/null @@ -1,17 +0,0 @@ -import 'package:angel_orm/angel_orm.dart'; -import 'package:angel_orm_test/angel_orm_test.dart'; -import 'package:test/test.dart'; - -void Function() standaloneTests(QueryExecutor Function() executorFn) { - return () { - test('insert one', () async { - var executor = executorFn(); - var query = TodoQuery(); - query.values - ..isComplete = false - ..text = 'Clean your dirty room'; - var todo = await query.insert(executor); - print(todo.toJson()); - }); - }; -} diff --git a/angel_orm_generator/test/edge_case_test.dart b/angel_orm_test/lib/src/edge_case_test.dart similarity index 89% rename from angel_orm_generator/test/edge_case_test.dart rename to angel_orm_test/lib/src/edge_case_test.dart index 092297cc..00b7f910 100644 --- a/angel_orm_generator/test/edge_case_test.dart +++ b/angel_orm_test/lib/src/edge_case_test.dart @@ -1,15 +1,19 @@ +import 'dart:async'; +import 'package:angel_orm/angel_orm.dart'; import 'package:test/test.dart'; import 'models/unorthodox.dart'; -import 'common.dart'; -main() { - PostgresExecutor executor; +edgeCaseTests(FutureOr Function() createExecutor, + {FutureOr Function(QueryExecutor) close}) { + QueryExecutor executor; + close ??= (_) => null; setUp(() async { - executor = - await connectToPostgres(['unorthodox', 'weird_join', 'song', 'numba']); + executor = await createExecutor(); }); + tearDown(() => close(executor)); + test('can create object with no id', () async { var query = UnorthodoxQuery()..values.name = 'Hey'; var model = await query.insert(executor); diff --git a/angel_orm_generator/test/enum_and_nested_test.dart b/angel_orm_test/lib/src/enum_and_nested_test.dart similarity index 74% rename from angel_orm_generator/test/enum_and_nested_test.dart rename to angel_orm_test/lib/src/enum_and_nested_test.dart index b9658a93..9cced77b 100644 --- a/angel_orm_generator/test/enum_and_nested_test.dart +++ b/angel_orm_test/lib/src/enum_and_nested_test.dart @@ -1,12 +1,15 @@ +import 'dart:async'; +import 'package:angel_orm/angel_orm.dart'; import 'package:test/test.dart'; import 'models/has_car.dart'; -import 'common.dart'; -main() { - PostgresExecutor executor; +enumAndNestedTests(FutureOr Function() createExecutor, + {FutureOr Function(QueryExecutor) close}) { + QueryExecutor executor; + close ??= (_) => null; setUp(() async { - executor = await connectToPostgres(['has_car']); + executor = await createExecutor(); }); test('insert', () async { diff --git a/angel_orm_generator/test/has_many_test.dart b/angel_orm_test/lib/src/has_many_test.dart similarity index 84% rename from angel_orm_generator/test/has_many_test.dart rename to angel_orm_test/lib/src/has_many_test.dart index 1ec80892..511bf108 100644 --- a/angel_orm_generator/test/has_many_test.dart +++ b/angel_orm_test/lib/src/has_many_test.dart @@ -1,21 +1,26 @@ +import 'dart:async'; +import 'package:angel_orm/angel_orm.dart'; import 'package:test/test.dart'; import 'models/fruit.dart'; import 'models/tree.dart'; -import 'common.dart'; -main() { - PostgresExecutor executor; +hasManyTests(FutureOr Function() createExecutor, + {FutureOr Function(QueryExecutor) close}) { + QueryExecutor executor; Tree appleTree; int treeId; + close ??= (_) => null; setUp(() async { var query = new TreeQuery()..values.rings = 10; - executor = await connectToPostgres(['tree', 'fruit']); + executor = await createExecutor(); appleTree = await query.insert(executor); treeId = int.parse(appleTree.id); }); + tearDown(() => close(executor)); + test('list is empty if there is nothing', () { expect(appleTree.rings, 10); expect(appleTree.fruits, isEmpty); diff --git a/angel_orm_generator/test/has_map_test.dart b/angel_orm_test/lib/src/has_map_test.dart similarity index 88% rename from angel_orm_generator/test/has_map_test.dart rename to angel_orm_test/lib/src/has_map_test.dart index 4a7671b7..f61b8551 100644 --- a/angel_orm_generator/test/has_map_test.dart +++ b/angel_orm_test/lib/src/has_map_test.dart @@ -1,14 +1,19 @@ +import 'dart:async'; +import 'package:angel_orm/angel_orm.dart'; import 'package:test/test.dart'; import 'models/has_map.dart'; -import 'common.dart'; -main() { - PostgresExecutor executor; +hasMapTests(FutureOr Function() createExecutor, + {FutureOr Function(QueryExecutor) close}) { + QueryExecutor executor; + close ??= (_) => null; setUp(() async { - executor = await connectToPostgres(['has_map']); + executor = await createExecutor(); }); + tearDown(() => close(executor)); + test('insert', () async { var query = HasMapQuery(); query.values diff --git a/angel_orm_generator/test/has_one_test.dart b/angel_orm_test/lib/src/has_one_test.dart similarity index 89% rename from angel_orm_generator/test/has_one_test.dart rename to angel_orm_test/lib/src/has_one_test.dart index a1ecec57..33897b6a 100644 --- a/angel_orm_generator/test/has_one_test.dart +++ b/angel_orm_test/lib/src/has_one_test.dart @@ -1,21 +1,23 @@ -/// Tests for @hasOne... -library angel_orm_generator.test.has_one_test; - +import 'dart:async'; +import 'package:angel_orm/angel_orm.dart'; import 'package:test/test.dart'; import 'models/foot.dart'; import 'models/leg.dart'; -import 'common.dart'; -main() { - PostgresExecutor executor; +hasOneTests(FutureOr Function() createExecutor, + {FutureOr Function(QueryExecutor) close}) { + QueryExecutor executor; Leg originalLeg; + close ??= (_) => null; setUp(() async { - executor = await connectToPostgres(['leg', 'foot']); + executor = await createExecutor(); var query = new LegQuery()..values.name = 'Left'; originalLeg = await query.insert(executor); }); + tearDown(() => close(executor)); + test('sets to null if no child', () async { print(LegQuery().compile(Set())); var query = new LegQuery()..where.id.equals(int.parse(originalLeg.id)); diff --git a/angel_orm_generator/test/many_to_many_test.dart b/angel_orm_test/lib/src/many_to_many_test.dart similarity index 90% rename from angel_orm_generator/test/many_to_many_test.dart rename to angel_orm_test/lib/src/many_to_many_test.dart index 32b4d665..c5588b6f 100644 --- a/angel_orm_generator/test/many_to_many_test.dart +++ b/angel_orm_test/lib/src/many_to_many_test.dart @@ -1,15 +1,15 @@ -library angel_orm_generator.test; - import 'dart:async'; import 'dart:io'; +import 'package:angel_orm/angel_orm.dart'; import 'package:test/test.dart'; import 'models/user.dart'; -import 'common.dart'; -main() { - PostgresExecutor executor; +manyToManyTests(FutureOr Function() createExecutor, + {FutureOr Function(QueryExecutor) close}) { + QueryExecutor executor; Role canPub, canSub; User thosakwe; + close ??= (_) => null; Future dumpQuery(String query) async { if (Platform.environment.containsKey('STFU')) return; @@ -17,14 +17,14 @@ main() { print('=================================================='); print(' DUMPING QUERY'); print(query); - var rows = await executor.connection.query(query); + var rows = await executor.query(null, query, {}); print('\n${rows.length} row(s):'); rows.forEach((r) => print(' * $r')); print('==================================================\n\n'); } setUp(() async { - executor = await connectToPostgres(['user', 'role', 'user_role']); + executor = await createExecutor(); // await dumpQuery(""" // WITH roles as @@ -87,6 +87,8 @@ main() { print('==================================================\n\n'); }); + tearDown(() => close(executor)); + Future fetchThosakwe() async { var query = UserQuery()..where.id.equals(int.parse(thosakwe.id)); return await query.getOne(executor); diff --git a/angel_orm_generator/test/models/author.dart b/angel_orm_test/lib/src/models/author.dart similarity index 100% rename from angel_orm_generator/test/models/author.dart rename to angel_orm_test/lib/src/models/author.dart diff --git a/angel_orm_generator/test/models/author.g.dart b/angel_orm_test/lib/src/models/author.g.dart similarity index 100% rename from angel_orm_generator/test/models/author.g.dart rename to angel_orm_test/lib/src/models/author.g.dart diff --git a/angel_orm_generator/test/models/book.dart b/angel_orm_test/lib/src/models/book.dart similarity index 100% rename from angel_orm_generator/test/models/book.dart rename to angel_orm_test/lib/src/models/book.dart diff --git a/angel_orm_generator/test/models/book.g.dart b/angel_orm_test/lib/src/models/book.g.dart similarity index 100% rename from angel_orm_generator/test/models/book.g.dart rename to angel_orm_test/lib/src/models/book.g.dart diff --git a/angel_orm_generator/test/models/car.dart b/angel_orm_test/lib/src/models/car.dart similarity index 100% rename from angel_orm_generator/test/models/car.dart rename to angel_orm_test/lib/src/models/car.dart diff --git a/angel_orm_generator/test/models/car.g.dart b/angel_orm_test/lib/src/models/car.g.dart similarity index 100% rename from angel_orm_generator/test/models/car.g.dart rename to angel_orm_test/lib/src/models/car.g.dart diff --git a/angel_orm_generator/test/models/customer.dart b/angel_orm_test/lib/src/models/customer.dart similarity index 100% rename from angel_orm_generator/test/models/customer.dart rename to angel_orm_test/lib/src/models/customer.dart diff --git a/angel_orm_generator/test/models/customer.g.dart b/angel_orm_test/lib/src/models/customer.g.dart similarity index 100% rename from angel_orm_generator/test/models/customer.g.dart rename to angel_orm_test/lib/src/models/customer.g.dart diff --git a/angel_orm_generator/test/models/foot.dart b/angel_orm_test/lib/src/models/foot.dart similarity index 100% rename from angel_orm_generator/test/models/foot.dart rename to angel_orm_test/lib/src/models/foot.dart diff --git a/angel_orm_generator/test/models/foot.g.dart b/angel_orm_test/lib/src/models/foot.g.dart similarity index 100% rename from angel_orm_generator/test/models/foot.g.dart rename to angel_orm_test/lib/src/models/foot.g.dart diff --git a/angel_orm_generator/test/models/fruit.dart b/angel_orm_test/lib/src/models/fruit.dart similarity index 100% rename from angel_orm_generator/test/models/fruit.dart rename to angel_orm_test/lib/src/models/fruit.dart diff --git a/angel_orm_generator/test/models/fruit.g.dart b/angel_orm_test/lib/src/models/fruit.g.dart similarity index 100% rename from angel_orm_generator/test/models/fruit.g.dart rename to angel_orm_test/lib/src/models/fruit.g.dart diff --git a/angel_orm_generator/test/models/has_car.dart b/angel_orm_test/lib/src/models/has_car.dart similarity index 100% rename from angel_orm_generator/test/models/has_car.dart rename to angel_orm_test/lib/src/models/has_car.dart diff --git a/angel_orm_generator/test/models/has_car.g.dart b/angel_orm_test/lib/src/models/has_car.g.dart similarity index 100% rename from angel_orm_generator/test/models/has_car.g.dart rename to angel_orm_test/lib/src/models/has_car.g.dart diff --git a/angel_orm_generator/test/models/has_map.dart b/angel_orm_test/lib/src/models/has_map.dart similarity index 100% rename from angel_orm_generator/test/models/has_map.dart rename to angel_orm_test/lib/src/models/has_map.dart diff --git a/angel_orm_generator/test/models/has_map.g.dart b/angel_orm_test/lib/src/models/has_map.g.dart similarity index 100% rename from angel_orm_generator/test/models/has_map.g.dart rename to angel_orm_test/lib/src/models/has_map.g.dart diff --git a/angel_orm_generator/test/models/leg.dart b/angel_orm_test/lib/src/models/leg.dart similarity index 100% rename from angel_orm_generator/test/models/leg.dart rename to angel_orm_test/lib/src/models/leg.dart diff --git a/angel_orm_generator/test/models/leg.g.dart b/angel_orm_test/lib/src/models/leg.g.dart similarity index 100% rename from angel_orm_generator/test/models/leg.g.dart rename to angel_orm_test/lib/src/models/leg.g.dart diff --git a/angel_orm_test/lib/src/models/models.dart b/angel_orm_test/lib/src/models/models.dart deleted file mode 100644 index 7c8cc749..00000000 --- a/angel_orm_test/lib/src/models/models.dart +++ /dev/null @@ -1 +0,0 @@ -export 'todo.dart'; \ No newline at end of file diff --git a/angel_orm_generator/test/models/order.dart b/angel_orm_test/lib/src/models/order.dart similarity index 100% rename from angel_orm_generator/test/models/order.dart rename to angel_orm_test/lib/src/models/order.dart diff --git a/angel_orm_generator/test/models/order.g.dart b/angel_orm_test/lib/src/models/order.g.dart similarity index 100% rename from angel_orm_generator/test/models/order.g.dart rename to angel_orm_test/lib/src/models/order.g.dart diff --git a/angel_orm_test/lib/src/models/todo.dart b/angel_orm_test/lib/src/models/todo.dart deleted file mode 100644 index 126295df..00000000 --- a/angel_orm_test/lib/src/models/todo.dart +++ /dev/null @@ -1,12 +0,0 @@ -import 'package:angel_migration/angel_migration.dart'; -import 'package:angel_serialize/angel_serialize.dart'; -import 'package:angel_orm/angel_orm.dart'; -part 'todo.g.dart'; - -@serializable -@orm -abstract class _Todo extends Model { - String get text; - - bool get isComplete; -} diff --git a/angel_orm_test/lib/src/models/todo.g.dart b/angel_orm_test/lib/src/models/todo.g.dart deleted file mode 100644 index 357177ee..00000000 --- a/angel_orm_test/lib/src/models/todo.g.dart +++ /dev/null @@ -1,258 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -part of 'todo.dart'; - -// ************************************************************************** -// MigrationGenerator -// ************************************************************************** - -class TodoMigration extends Migration { - @override - up(Schema schema) { - schema.create('todos', (table) { - table.serial('id')..primaryKey(); - table.varChar('text'); - table.boolean('is_complete'); - table.timeStamp('created_at'); - table.timeStamp('updated_at'); - }); - } - - @override - down(Schema schema) { - schema.drop('todos'); - } -} - -// ************************************************************************** -// OrmGenerator -// ************************************************************************** - -class TodoQuery extends Query { - TodoQuery({Set trampoline}) { - trampoline ??= Set(); - trampoline.add(tableName); - _where = TodoQueryWhere(this); - } - - @override - final TodoQueryValues values = TodoQueryValues(); - - TodoQueryWhere _where; - - @override - get casts { - return {}; - } - - @override - get tableName { - return 'todos'; - } - - @override - get fields { - return const ['id', 'text', 'is_complete', 'created_at', 'updated_at']; - } - - @override - TodoQueryWhere get where { - return _where; - } - - @override - TodoQueryWhere newWhereClause() { - return TodoQueryWhere(this); - } - - static Todo parseRow(List row) { - if (row.every((x) => x == null)) return null; - var model = Todo( - id: row[0].toString(), - text: (row[1] as String), - isComplete: (row[2] as bool), - createdAt: (row[3] as DateTime), - updatedAt: (row[4] as DateTime)); - return model; - } - - @override - deserialize(List row) { - return parseRow(row); - } -} - -class TodoQueryWhere extends QueryWhere { - TodoQueryWhere(TodoQuery query) - : id = NumericSqlExpressionBuilder(query, 'id'), - text = StringSqlExpressionBuilder(query, 'text'), - isComplete = BooleanSqlExpressionBuilder(query, 'is_complete'), - createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'), - updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at'); - - final NumericSqlExpressionBuilder id; - - final StringSqlExpressionBuilder text; - - final BooleanSqlExpressionBuilder isComplete; - - final DateTimeSqlExpressionBuilder createdAt; - - final DateTimeSqlExpressionBuilder updatedAt; - - @override - get expressionBuilders { - return [id, text, isComplete, createdAt, updatedAt]; - } -} - -class TodoQueryValues extends MapQueryValues { - @override - get casts { - return {}; - } - - int get id { - return (values['id'] as int); - } - - set id(int value) => values['id'] = value; - String get text { - return (values['text'] as String); - } - - set text(String value) => values['text'] = value; - bool get isComplete { - return (values['is_complete'] as bool); - } - - set isComplete(bool value) => values['is_complete'] = value; - DateTime get createdAt { - return (values['created_at'] as DateTime); - } - - set createdAt(DateTime value) => values['created_at'] = value; - DateTime get updatedAt { - return (values['updated_at'] as DateTime); - } - - set updatedAt(DateTime value) => values['updated_at'] = value; - void copyFrom(Todo model) { - text = model.text; - isComplete = model.isComplete; - createdAt = model.createdAt; - updatedAt = model.updatedAt; - } -} - -// ************************************************************************** -// JsonModelGenerator -// ************************************************************************** - -@generatedSerializable -class Todo extends _Todo { - Todo({this.id, this.text, this.isComplete, this.createdAt, this.updatedAt}); - - @override - final String id; - - @override - final String text; - - @override - final bool isComplete; - - @override - final DateTime createdAt; - - @override - final DateTime updatedAt; - - Todo copyWith( - {String id, - String text, - bool isComplete, - DateTime createdAt, - DateTime updatedAt}) { - return new Todo( - id: id ?? this.id, - text: text ?? this.text, - isComplete: isComplete ?? this.isComplete, - createdAt: createdAt ?? this.createdAt, - updatedAt: updatedAt ?? this.updatedAt); - } - - bool operator ==(other) { - return other is _Todo && - other.id == id && - other.text == text && - other.isComplete == isComplete && - other.createdAt == createdAt && - other.updatedAt == updatedAt; - } - - @override - int get hashCode { - return hashObjects([id, text, isComplete, createdAt, updatedAt]); - } - - Map toJson() { - return TodoSerializer.toMap(this); - } -} - -// ************************************************************************** -// SerializerGenerator -// ************************************************************************** - -abstract class TodoSerializer { - static Todo fromMap(Map map) { - return new Todo( - id: map['id'] as String, - text: map['text'] as String, - isComplete: map['is_complete'] as bool, - createdAt: map['created_at'] != null - ? (map['created_at'] is DateTime - ? (map['created_at'] as DateTime) - : DateTime.parse(map['created_at'].toString())) - : null, - updatedAt: map['updated_at'] != null - ? (map['updated_at'] is DateTime - ? (map['updated_at'] as DateTime) - : DateTime.parse(map['updated_at'].toString())) - : null); - } - - static Map toMap(_Todo model) { - if (model == null) { - return null; - } - return { - 'id': model.id, - 'text': model.text, - 'is_complete': model.isComplete, - 'created_at': model.createdAt?.toIso8601String(), - 'updated_at': model.updatedAt?.toIso8601String() - }; - } -} - -abstract class TodoFields { - static const List allFields = [ - id, - text, - isComplete, - createdAt, - updatedAt - ]; - - static const String id = 'id'; - - static const String text = 'text'; - - static const String isComplete = 'is_complete'; - - static const String createdAt = 'created_at'; - - static const String updatedAt = 'updated_at'; -} diff --git a/angel_orm_generator/test/models/tree.dart b/angel_orm_test/lib/src/models/tree.dart similarity index 100% rename from angel_orm_generator/test/models/tree.dart rename to angel_orm_test/lib/src/models/tree.dart diff --git a/angel_orm_generator/test/models/tree.g.dart b/angel_orm_test/lib/src/models/tree.g.dart similarity index 100% rename from angel_orm_generator/test/models/tree.g.dart rename to angel_orm_test/lib/src/models/tree.g.dart diff --git a/angel_orm_generator/test/models/unorthodox.dart b/angel_orm_test/lib/src/models/unorthodox.dart similarity index 100% rename from angel_orm_generator/test/models/unorthodox.dart rename to angel_orm_test/lib/src/models/unorthodox.dart diff --git a/angel_orm_generator/test/models/unorthodox.g.dart b/angel_orm_test/lib/src/models/unorthodox.g.dart similarity index 100% rename from angel_orm_generator/test/models/unorthodox.g.dart rename to angel_orm_test/lib/src/models/unorthodox.g.dart diff --git a/angel_orm_generator/test/models/user.dart b/angel_orm_test/lib/src/models/user.dart similarity index 100% rename from angel_orm_generator/test/models/user.dart rename to angel_orm_test/lib/src/models/user.dart diff --git a/angel_orm_generator/test/models/user.g.dart b/angel_orm_test/lib/src/models/user.g.dart similarity index 100% rename from angel_orm_generator/test/models/user.g.dart rename to angel_orm_test/lib/src/models/user.g.dart diff --git a/angel_orm_generator/test/standalone_test.dart b/angel_orm_test/lib/src/standalone_test.dart similarity index 84% rename from angel_orm_generator/test/standalone_test.dart rename to angel_orm_test/lib/src/standalone_test.dart index 167ebd36..c70009a7 100644 --- a/angel_orm_generator/test/standalone_test.dart +++ b/angel_orm_test/lib/src/standalone_test.dart @@ -1,14 +1,13 @@ -/// Test for queries without relationships. -library angel_orm_generator.test.car_test; - +import 'dart:async'; import 'package:angel_orm/angel_orm.dart'; import 'package:test/test.dart'; import 'models/car.dart'; -import 'common.dart'; final DateTime y2k = new DateTime.utc(2000, 1, 1); -main() { +standaloneTests(FutureOr Function() createExecutor, + {FutureOr Function(QueryExecutor) close}) { + close ??= (_) => null; test('to where', () { var query = new CarQuery(); query.where @@ -35,15 +34,17 @@ main() { }); group('queries', () { - PostgresExecutor connection; + QueryExecutor executor; setUp(() async { - connection = await connectToPostgres(['car']); + executor = await createExecutor(); }); + tearDown(() => close(executor)); + group('selects', () { test('select all', () async { - var cars = await new CarQuery().get(connection); + var cars = await new CarQuery().get(executor); expect(cars, []); }); @@ -56,18 +57,16 @@ main() { ..make = 'Ferrarię±' ..description = 'Vroom vroom!' ..familyFriendly = false; - ferrari = await query.insert(connection); + ferrari = await query.insert(executor); }); - tearDown(() => connection.close()); - test('where clause is applied', () async { var query = new CarQuery()..where.familyFriendly.isTrue; - var cars = await query.get(connection); + var cars = await query.get(executor); expect(cars, isEmpty); var sportsCars = new CarQuery()..where.familyFriendly.isFalse; - cars = await sportsCars.get(connection); + cars = await sportsCars.get(executor); print(cars.map((c) => c.toJson())); var car = cars.first; @@ -83,7 +82,7 @@ main() { var query3 = new CarQuery()..where.description.equals('Submarine'); var union = query1.union(query2).unionAll(query3); print(union.compile(Set())); - var cars = await union.get(connection); + var cars = await union.get(executor); expect(cars, hasLength(1)); }); @@ -94,31 +93,31 @@ main() { ..familyFriendly.isTrue ..make.equals('Honda')); print(query.compile(Set())); - var cars = await query.get(connection); + var cars = await query.get(executor); expect(cars, hasLength(1)); }); test('limit obeyed', () async { var query = new CarQuery()..limit(0); print(query.compile(Set())); - var cars = await query.get(connection); + var cars = await query.get(executor); expect(cars, isEmpty); }); test('get one', () async { var id = int.parse(ferrari.id); var query = new CarQuery()..where.id.equals(id); - var car = await query.getOne(connection); + var car = await query.getOne(executor); expect(car, ferrari); }); test('delete one', () async { var id = int.parse(ferrari.id); var query = new CarQuery()..where.id.equals(id); - var car = await query.deleteOne(connection); + var car = await query.deleteOne(executor); expect(car.toJson(), ferrari.toJson()); - var cars = await new CarQuery().get(connection); + var cars = await new CarQuery().get(executor); expect(cars, isEmpty); }); @@ -128,7 +127,7 @@ main() { ..orWhere((w) => w.familyFriendly.isTrue); print(query.compile(Set(), preamble: 'DELETE FROM "cars"')); - var cars = await query.delete(connection); + var cars = await query.delete(executor); expect(cars, hasLength(1)); expect(cars.first.toJson(), ferrari.toJson()); }); @@ -137,7 +136,7 @@ main() { var query = new CarQuery() ..where.id.equals(int.parse(ferrari.id)) ..values.make = 'Hyundai'; - var cars = await query.update(connection); + var cars = await query.update(executor); expect(cars, hasLength(1)); expect(cars.first.make, 'Hyundai'); }); @@ -145,7 +144,7 @@ main() { test('update car', () async { var cloned = ferrari.copyWith(make: 'Angel'); var query = new CarQuery()..values.copyFrom(cloned); - var car = await query.updateOne(connection); + var car = await query.updateOne(executor); print(car.toJson()); expect(car.toJson(), cloned.toJson()); }); @@ -163,7 +162,7 @@ main() { ..recalledAt = recalledAt ..createdAt = now ..updatedAt = now; - var car = await query.insert(connection); + var car = await query.insert(executor); expect(car.id, isNotNull); expect(car.make, 'Honda'); expect(car.description, 'Hello'); @@ -180,7 +179,7 @@ main() { familyFriendly: true, recalledAt: recalledAt); var query = new CarQuery()..values.copyFrom(beetle); - var car = await query.insert(connection); + var car = await query.insert(executor); print(car.toJson()); expect(car.make, beetle.make); expect(car.description, beetle.description); diff --git a/angel_orm_test/pubspec.yaml b/angel_orm_test/pubspec.yaml index f567e7dc..d22b79a4 100644 --- a/angel_orm_test/pubspec.yaml +++ b/angel_orm_test/pubspec.yaml @@ -12,4 +12,5 @@ dependencies: dev_dependencies: angel_orm_generator: path: ../angel_orm_generator + angel_framework: ^2.0.0-alpha build_runner: ^1.0.0 \ No newline at end of file diff --git a/tool/.travis.sh b/tool/.travis.sh index 92cfa22a..f78af5ad 100644 --- a/tool/.travis.sh +++ b/tool/.travis.sh @@ -1,5 +1,18 @@ #!/usr/bin/env bash -cd angel_orm_generator -pub get -pub run build_runner build -POSTGRES_USERNAME="angel_orm" POSTGRES_PASSWORD="angel_orm" pub run test \ No newline at end of file +export POSTGRES_USERNAME="angel_orm" +export POSTGRES_PASSWORD="angel_orm" + + + +function angel_orm_test () { + cd $1; + pub get; + pub run test; + cd .. +} + +cd angel_orm_generator; +pub get; +pub run build_runner build; +cd ..; +angel_orm_test angel_orm_postgres \ No newline at end of file