mirror of https://github.com/AMT-Cheif/drift.git
more tests
This commit is contained in:
parent
cdd11c8e40
commit
327244b607
|
@ -152,6 +152,7 @@ class TableManagerState<
|
|||
if (joins.isEmpty && targetColumns == null) {
|
||||
final simpleStatement =
|
||||
db.select(_tableAsTableInfo, distinct: distinct ?? false);
|
||||
|
||||
// Apply the expression to the statement
|
||||
if (filter != null) {
|
||||
simpleStatement.where((_) => filter!);
|
||||
|
@ -249,7 +250,12 @@ class TableManagerState<
|
|||
final query = existsQuery(statement);
|
||||
final existsStatement = db.selectOnly(_tableAsTableInfo)
|
||||
..addColumns([query]);
|
||||
return (await existsStatement.map((p0) => p0.read(query)).getSingle())!;
|
||||
return (await existsStatement
|
||||
.map((p0) => p0.read(query))
|
||||
.get()
|
||||
.then((value) {
|
||||
return value.firstOrNull ?? false;
|
||||
}));
|
||||
}
|
||||
|
||||
/// Build a delete statement based on the manager state
|
||||
|
|
|
@ -23,7 +23,7 @@ class TodosTable extends Table with AutoIncrement {
|
|||
TextColumn get content => text()();
|
||||
@JsonKey('target_date')
|
||||
DateTimeColumn get targetDate => dateTime().nullable().unique()();
|
||||
|
||||
@ReferenceName("todos")
|
||||
IntColumn get category => integer().references(Categories, #id).nullable()();
|
||||
|
||||
TextColumn get status => textEnum<TodoStatus>().nullable()();
|
||||
|
|
|
@ -2510,7 +2510,7 @@ class $$CategoriesTableFilterComposer
|
|||
get priority => ColumnWithTypeConverterFilters($table.priority);
|
||||
ColumnFilters<String> get descriptionInUpperCase =>
|
||||
ColumnFilters($table.descriptionInUpperCase);
|
||||
ComposableFilter todosTableRefs(
|
||||
ComposableFilter todos(
|
||||
ComposableFilter Function($$TodosTableTableFilterComposer f) f) {
|
||||
return $composeWithJoins(
|
||||
$db: $db,
|
||||
|
|
|
@ -519,5 +519,23 @@ void main() {
|
|||
))
|
||||
.count(),
|
||||
completion(4));
|
||||
|
||||
// Use backreference
|
||||
expect(
|
||||
db.managers.categories
|
||||
.filter((f) => f.todos((f) => f.title.equals("Math Homework")))
|
||||
.getSingle()
|
||||
.then((value) => value.description),
|
||||
completion("School"));
|
||||
|
||||
// Nested backreference
|
||||
expect(
|
||||
db.managers.categories
|
||||
.filter((f) => f.todos((f) => f.category(
|
||||
(f) => f.todos((f) => f.title.equals("Math Homework")))))
|
||||
.distict()
|
||||
.getSingle()
|
||||
.then((value) => value.description),
|
||||
completion("School"));
|
||||
});
|
||||
}
|
||||
|
|
|
@ -0,0 +1,91 @@
|
|||
import 'package:drift/drift.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../generated/todos.dart';
|
||||
import '../test_utils/test_utils.dart';
|
||||
|
||||
void main() {
|
||||
late TodoDb db;
|
||||
|
||||
setUp(() {
|
||||
db = TodoDb(testInMemoryDatabase());
|
||||
});
|
||||
|
||||
tearDown(() => db.close());
|
||||
|
||||
test('manager - order', () async {
|
||||
await db.managers.tableWithEveryColumnType.create((o) => o(
|
||||
id: Value(RowId(1)),
|
||||
aText: Value("Get that math homework done"),
|
||||
anIntEnum: Value(TodoStatus.open),
|
||||
aReal: Value(5.0),
|
||||
aDateTime: Value(DateTime.now().add(Duration(days: 1)))));
|
||||
await db.managers.tableWithEveryColumnType.create((o) => o(
|
||||
aText: Value("Get that math homework done"),
|
||||
anIntEnum: Value(TodoStatus.open),
|
||||
aDateTime: Value(DateTime.now().add(Duration(days: 2)))));
|
||||
await db.managers.tableWithEveryColumnType.create((o) => o(
|
||||
aText: Value("Get that math homework done"),
|
||||
anIntEnum: Value(TodoStatus.open),
|
||||
aReal: Value(3.0),
|
||||
aDateTime: Value(DateTime.now().add(Duration(days: 3)))));
|
||||
|
||||
// Equals
|
||||
expect(
|
||||
db.managers.tableWithEveryColumnType
|
||||
.orderBy((o) => o.aDateTime.desc())
|
||||
.get()
|
||||
.then((value) => value[0].id),
|
||||
completion(3));
|
||||
expect(
|
||||
db.managers.tableWithEveryColumnType
|
||||
.orderBy((o) => o.aDateTime.asc())
|
||||
.get()
|
||||
.then((value) => value[0].id),
|
||||
completion(1));
|
||||
});
|
||||
|
||||
test('manager - order related', () async {
|
||||
final schoolCategoryId = await db.managers.categories.create((o) =>
|
||||
o(priority: Value(CategoryPriority.high), description: "School"));
|
||||
final workCategoryId = await db.managers.categories.create(
|
||||
(o) => o(priority: Value(CategoryPriority.low), description: "Work"));
|
||||
|
||||
await db.managers.todosTable.create((o) => o(
|
||||
id: Value(RowId(1)),
|
||||
content: "Get that english homework done",
|
||||
title: Value("English Homework"),
|
||||
category: Value(workCategoryId),
|
||||
status: Value(TodoStatus.open),
|
||||
targetDate: Value(DateTime.now().add(Duration(days: 1, seconds: 15)))));
|
||||
await db.managers.todosTable.create((o) => o(
|
||||
id: Value(RowId(2)),
|
||||
content: "Finish that Book report",
|
||||
title: Value("Book Report"),
|
||||
category: Value(workCategoryId),
|
||||
status: Value(TodoStatus.done),
|
||||
targetDate:
|
||||
Value(DateTime.now().subtract(Duration(days: 2, seconds: 15)))));
|
||||
await db.managers.todosTable.create((o) => o(
|
||||
id: Value(RowId(3)),
|
||||
content: "Get that math homework done",
|
||||
title: Value("Math Homework"),
|
||||
category: Value(schoolCategoryId),
|
||||
status: Value(TodoStatus.open),
|
||||
targetDate: Value(DateTime.now().add(Duration(days: 1, seconds: 10)))));
|
||||
await db.managers.todosTable.create((o) => o(
|
||||
id: Value(RowId(4)),
|
||||
content: "Finish that report",
|
||||
title: Value("Report"),
|
||||
category: Value(schoolCategoryId),
|
||||
status: Value(TodoStatus.workInProgress),
|
||||
targetDate: Value(DateTime.now().add(Duration(days: 2, seconds: 10)))));
|
||||
// Order by related
|
||||
expect(
|
||||
db.managers.todosTable
|
||||
.orderBy((o) => o.category((o) => o.id.asc()))
|
||||
.get()
|
||||
.then((value) => value.map((e) => e.id).toList()),
|
||||
completion([3, 4, 1, 2]));
|
||||
});
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
import 'package:drift/drift.dart';
|
||||
import 'package:test/expect.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../generated/todos.dart';
|
||||
import '../test_utils/test_utils.dart';
|
||||
|
||||
void main() {
|
||||
late TodoDb db;
|
||||
|
||||
setUp(() {
|
||||
db = TodoDb(testInMemoryDatabase());
|
||||
});
|
||||
|
||||
tearDown(() => db.close());
|
||||
|
||||
test('processed manager', () async {
|
||||
await db.managers.tableWithEveryColumnType.create((o) => o(
|
||||
aText: Value("Get that math homework done"),
|
||||
anIntEnum: Value(TodoStatus.open),
|
||||
aReal: Value(5.0),
|
||||
aDateTime: Value(DateTime.now().add(Duration(days: 1)))));
|
||||
await db.managers.tableWithEveryColumnType.create((o) => o(
|
||||
aText: Value("Get that math homework done"),
|
||||
anIntEnum: Value(TodoStatus.open),
|
||||
aDateTime: Value(DateTime.now().add(Duration(days: 2)))));
|
||||
await db.managers.tableWithEveryColumnType.create((o) => o(
|
||||
aText: Value("Get that math homework done"),
|
||||
anIntEnum: Value(TodoStatus.open),
|
||||
aReal: Value(3.0),
|
||||
aDateTime: Value(DateTime.now().add(Duration(days: 3)))));
|
||||
// Test count
|
||||
expect(db.managers.tableWithEveryColumnType.all().count(), completion(3));
|
||||
// Test get
|
||||
expect(
|
||||
db.managers.tableWithEveryColumnType
|
||||
.all()
|
||||
.get()
|
||||
.then((value) => value.length),
|
||||
completion(3));
|
||||
// Test getSingle with limit
|
||||
expect(
|
||||
db.managers.tableWithEveryColumnType
|
||||
.all()
|
||||
.limit(1, offset: 1)
|
||||
.getSingle()
|
||||
.then((value) => value.id),
|
||||
completion(2));
|
||||
// Test filtered delete
|
||||
expect(
|
||||
db.managers.tableWithEveryColumnType
|
||||
.filter((f) => f.id(RowId(2)))
|
||||
.delete(),
|
||||
completion(1));
|
||||
|
||||
// Test filtered update
|
||||
expect(
|
||||
db.managers.tableWithEveryColumnType
|
||||
.filter((f) => f.id(RowId(1)))
|
||||
.update((o) => o(aReal: Value(10.0))),
|
||||
completion(1));
|
||||
expect(
|
||||
db.managers.tableWithEveryColumnType
|
||||
.filter((f) => f.id(RowId(1)))
|
||||
.getSingle()
|
||||
.then((value) => value.aReal),
|
||||
completion(10.0));
|
||||
// Test filtered exists
|
||||
expect(
|
||||
db.managers.tableWithEveryColumnType
|
||||
.filter((f) => f.id(RowId(1)))
|
||||
.exists(),
|
||||
completion(true));
|
||||
|
||||
// Test filtered count
|
||||
expect(
|
||||
db.managers.tableWithEveryColumnType
|
||||
.filter((f) => f.id(RowId(1)))
|
||||
.count(),
|
||||
completion(1));
|
||||
// Test delte all
|
||||
expect(db.managers.tableWithEveryColumnType.delete(), completion(2));
|
||||
// Test exists - false
|
||||
expect(
|
||||
db.managers.tableWithEveryColumnType
|
||||
.filter((f) => f.id(RowId(1)))
|
||||
.exists(),
|
||||
completion(false));
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue