Move all tests to angel_orm_test
This commit is contained in:
parent
97e146a80e
commit
260580ae00
62 changed files with 159 additions and 452 deletions
|
@ -1,67 +0,0 @@
|
|||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'package:angel_orm/angel_orm.dart';
|
||||
import 'package:postgres/postgres.dart';
|
||||
|
||||
Future<PostgresExecutor> connectToPostgres(Iterable<String> 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<List<List>> query(
|
||||
String tableName, String query, Map<String, dynamic> substitutionValues,
|
||||
[List<String> 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<T> transaction<T>(FutureOr<T> 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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
});
|
||||
}
|
||||
|
|
25
angel_orm_postgres/test/common.dart
Normal file
25
angel_orm_postgres/test/common.dart
Normal file
|
@ -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<QueryExecutor> Function() pg(Iterable<String> schemas) {
|
||||
return () => connectToPostgres(schemas);
|
||||
}
|
||||
|
||||
Future<void> closePg(QueryExecutor executor) =>
|
||||
(executor as PostgreSqlExecutor).close();
|
||||
|
||||
Future<PostgreSqlExecutor> connectToPostgres(Iterable<String> 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);
|
||||
}
|
|
@ -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 {
|
|
@ -1,2 +1,8 @@
|
|||
export 'src/cases/cases.dart';
|
||||
export 'src/models/models.dart';
|
||||
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';
|
||||
|
|
|
@ -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<QueryExecutor> Function() createExecutor,
|
||||
{FutureOr<void> 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 {
|
|
@ -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));
|
||||
}
|
|
@ -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());
|
||||
});
|
||||
};
|
||||
}
|
|
@ -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<QueryExecutor> Function() createExecutor,
|
||||
{FutureOr<void> 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);
|
|
@ -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<QueryExecutor> Function() createExecutor,
|
||||
{FutureOr<void> Function(QueryExecutor) close}) {
|
||||
QueryExecutor executor;
|
||||
close ??= (_) => null;
|
||||
|
||||
setUp(() async {
|
||||
executor = await connectToPostgres(['has_car']);
|
||||
executor = await createExecutor();
|
||||
});
|
||||
|
||||
test('insert', () async {
|
|
@ -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<QueryExecutor> Function() createExecutor,
|
||||
{FutureOr<void> 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);
|
|
@ -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<QueryExecutor> Function() createExecutor,
|
||||
{FutureOr<void> 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
|
|
@ -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<QueryExecutor> Function() createExecutor,
|
||||
{FutureOr<void> 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));
|
|
@ -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<QueryExecutor> Function() createExecutor,
|
||||
{FutureOr<void> Function(QueryExecutor) close}) {
|
||||
QueryExecutor executor;
|
||||
Role canPub, canSub;
|
||||
User thosakwe;
|
||||
close ??= (_) => null;
|
||||
|
||||
Future<void> 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<User> fetchThosakwe() async {
|
||||
var query = UserQuery()..where.id.equals(int.parse(thosakwe.id));
|
||||
return await query.getOne(executor);
|
|
@ -1 +0,0 @@
|
|||
export 'todo.dart';
|
|
@ -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;
|
||||
}
|
|
@ -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<Todo, TodoQueryWhere> {
|
||||
TodoQuery({Set<String> 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<int>(query, 'id'),
|
||||
text = StringSqlExpressionBuilder(query, 'text'),
|
||||
isComplete = BooleanSqlExpressionBuilder(query, 'is_complete'),
|
||||
createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'),
|
||||
updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at');
|
||||
|
||||
final NumericSqlExpressionBuilder<int> 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<String, dynamic> 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<String, dynamic> 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<String> allFields = <String>[
|
||||
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';
|
||||
}
|
|
@ -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<QueryExecutor> Function() createExecutor,
|
||||
{FutureOr<void> 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);
|
|
@ -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
|
|
@ -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
|
||||
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
|
Loading…
Reference in a new issue