mirror of https://github.com/AMT-Cheif/drift.git
Migrate tests to companion
This commit is contained in:
parent
0616fb7082
commit
62c5106e7d
|
@ -45,8 +45,10 @@ class Database extends _$Database {
|
||||||
int get schemaVersion => 1;
|
int get schemaVersion => 1;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
// todo migrate
|
||||||
MigrationStrategy get migration => MigrationStrategy(onFinished: () async {
|
MigrationStrategy get migration => MigrationStrategy(onFinished: () async {
|
||||||
// populate data
|
// populate data
|
||||||
await into(categories).insert(Category(description: 'Sweets'));
|
await into(categories)
|
||||||
|
.insert(const CategoriesCompanion(description: Value('Sweets')));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ part of 'example.dart';
|
||||||
class Category extends DataClass implements Insertable<Category> {
|
class Category extends DataClass implements Insertable<Category> {
|
||||||
final int id;
|
final int id;
|
||||||
final String description;
|
final String description;
|
||||||
Category({this.id, this.description});
|
Category({@required this.id, this.description});
|
||||||
factory Category.fromData(Map<String, dynamic> data, GeneratedDatabase db,
|
factory Category.fromData(Map<String, dynamic> data, GeneratedDatabase db,
|
||||||
{String prefix}) {
|
{String prefix}) {
|
||||||
final effectivePrefix = prefix ?? '';
|
final effectivePrefix = prefix ?? '';
|
||||||
|
@ -41,10 +41,10 @@ class Category extends DataClass implements Insertable<Category> {
|
||||||
@override
|
@override
|
||||||
T createCompanion<T extends UpdateCompanion<Category>>(bool nullToAbsent) {
|
T createCompanion<T extends UpdateCompanion<Category>>(bool nullToAbsent) {
|
||||||
return CategoriesCompanion(
|
return CategoriesCompanion(
|
||||||
id: id == null && nullToAbsent ? const Value.absent() : Value.use(id),
|
id: id == null && nullToAbsent ? const Value.absent() : Value(id),
|
||||||
description: description == null && nullToAbsent
|
description: description == null && nullToAbsent
|
||||||
? const Value.absent()
|
? const Value.absent()
|
||||||
: Value.use(description),
|
: Value(description),
|
||||||
) as T;
|
) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,7 +162,11 @@ class Recipe extends DataClass implements Insertable<Recipe> {
|
||||||
final String title;
|
final String title;
|
||||||
final String instructions;
|
final String instructions;
|
||||||
final int category;
|
final int category;
|
||||||
Recipe({this.id, this.title, this.instructions, this.category});
|
Recipe(
|
||||||
|
{@required this.id,
|
||||||
|
@required this.title,
|
||||||
|
@required this.instructions,
|
||||||
|
this.category});
|
||||||
factory Recipe.fromData(Map<String, dynamic> data, GeneratedDatabase db,
|
factory Recipe.fromData(Map<String, dynamic> data, GeneratedDatabase db,
|
||||||
{String prefix}) {
|
{String prefix}) {
|
||||||
final effectivePrefix = prefix ?? '';
|
final effectivePrefix = prefix ?? '';
|
||||||
|
@ -201,16 +205,15 @@ class Recipe extends DataClass implements Insertable<Recipe> {
|
||||||
@override
|
@override
|
||||||
T createCompanion<T extends UpdateCompanion<Recipe>>(bool nullToAbsent) {
|
T createCompanion<T extends UpdateCompanion<Recipe>>(bool nullToAbsent) {
|
||||||
return RecipesCompanion(
|
return RecipesCompanion(
|
||||||
id: id == null && nullToAbsent ? const Value.absent() : Value.use(id),
|
id: id == null && nullToAbsent ? const Value.absent() : Value(id),
|
||||||
title: title == null && nullToAbsent
|
title:
|
||||||
? const Value.absent()
|
title == null && nullToAbsent ? const Value.absent() : Value(title),
|
||||||
: Value.use(title),
|
|
||||||
instructions: instructions == null && nullToAbsent
|
instructions: instructions == null && nullToAbsent
|
||||||
? const Value.absent()
|
? const Value.absent()
|
||||||
: Value.use(instructions),
|
: Value(instructions),
|
||||||
category: category == null && nullToAbsent
|
category: category == null && nullToAbsent
|
||||||
? const Value.absent()
|
? const Value.absent()
|
||||||
: Value.use(category),
|
: Value(category),
|
||||||
) as T;
|
) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -382,7 +385,8 @@ class Ingredient extends DataClass implements Insertable<Ingredient> {
|
||||||
final int id;
|
final int id;
|
||||||
final String name;
|
final String name;
|
||||||
final int caloriesPer100g;
|
final int caloriesPer100g;
|
||||||
Ingredient({this.id, this.name, this.caloriesPer100g});
|
Ingredient(
|
||||||
|
{@required this.id, @required this.name, @required this.caloriesPer100g});
|
||||||
factory Ingredient.fromData(Map<String, dynamic> data, GeneratedDatabase db,
|
factory Ingredient.fromData(Map<String, dynamic> data, GeneratedDatabase db,
|
||||||
{String prefix}) {
|
{String prefix}) {
|
||||||
final effectivePrefix = prefix ?? '';
|
final effectivePrefix = prefix ?? '';
|
||||||
|
@ -416,12 +420,11 @@ class Ingredient extends DataClass implements Insertable<Ingredient> {
|
||||||
@override
|
@override
|
||||||
T createCompanion<T extends UpdateCompanion<Ingredient>>(bool nullToAbsent) {
|
T createCompanion<T extends UpdateCompanion<Ingredient>>(bool nullToAbsent) {
|
||||||
return IngredientsCompanion(
|
return IngredientsCompanion(
|
||||||
id: id == null && nullToAbsent ? const Value.absent() : Value.use(id),
|
id: id == null && nullToAbsent ? const Value.absent() : Value(id),
|
||||||
name:
|
name: name == null && nullToAbsent ? const Value.absent() : Value(name),
|
||||||
name == null && nullToAbsent ? const Value.absent() : Value.use(name),
|
|
||||||
caloriesPer100g: caloriesPer100g == null && nullToAbsent
|
caloriesPer100g: caloriesPer100g == null && nullToAbsent
|
||||||
? const Value.absent()
|
? const Value.absent()
|
||||||
: Value.use(caloriesPer100g),
|
: Value(caloriesPer100g),
|
||||||
) as T;
|
) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -570,7 +573,10 @@ class IngredientInRecipe extends DataClass
|
||||||
final int recipe;
|
final int recipe;
|
||||||
final int ingredient;
|
final int ingredient;
|
||||||
final int amountInGrams;
|
final int amountInGrams;
|
||||||
IngredientInRecipe({this.recipe, this.ingredient, this.amountInGrams});
|
IngredientInRecipe(
|
||||||
|
{@required this.recipe,
|
||||||
|
@required this.ingredient,
|
||||||
|
@required this.amountInGrams});
|
||||||
factory IngredientInRecipe.fromData(
|
factory IngredientInRecipe.fromData(
|
||||||
Map<String, dynamic> data, GeneratedDatabase db,
|
Map<String, dynamic> data, GeneratedDatabase db,
|
||||||
{String prefix}) {
|
{String prefix}) {
|
||||||
|
@ -606,15 +612,14 @@ class IngredientInRecipe extends DataClass
|
||||||
T createCompanion<T extends UpdateCompanion<IngredientInRecipe>>(
|
T createCompanion<T extends UpdateCompanion<IngredientInRecipe>>(
|
||||||
bool nullToAbsent) {
|
bool nullToAbsent) {
|
||||||
return IngredientInRecipesCompanion(
|
return IngredientInRecipesCompanion(
|
||||||
recipe: recipe == null && nullToAbsent
|
recipe:
|
||||||
? const Value.absent()
|
recipe == null && nullToAbsent ? const Value.absent() : Value(recipe),
|
||||||
: Value.use(recipe),
|
|
||||||
ingredient: ingredient == null && nullToAbsent
|
ingredient: ingredient == null && nullToAbsent
|
||||||
? const Value.absent()
|
? const Value.absent()
|
||||||
: Value.use(ingredient),
|
: Value(ingredient),
|
||||||
amountInGrams: amountInGrams == null && nullToAbsent
|
amountInGrams: amountInGrams == null && nullToAbsent
|
||||||
? const Value.absent()
|
? const Value.absent()
|
||||||
: Value.use(amountInGrams),
|
: Value(amountInGrams),
|
||||||
) as T;
|
) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,9 @@ library moor;
|
||||||
// needed for the generated code that generates data classes with an Uint8List
|
// needed for the generated code that generates data classes with an Uint8List
|
||||||
// field.
|
// field.
|
||||||
export 'dart:typed_data' show Uint8List;
|
export 'dart:typed_data' show Uint8List;
|
||||||
|
// needed for generated code which provides an @required parameter hint where
|
||||||
|
// appropriate
|
||||||
|
export 'package:meta/meta.dart' show required;
|
||||||
|
|
||||||
export 'package:moor/src/dsl/table.dart';
|
export 'package:moor/src/dsl/table.dart';
|
||||||
export 'package:moor/src/dsl/columns.dart';
|
export 'package:moor/src/dsl/columns.dart';
|
||||||
|
|
|
@ -57,7 +57,7 @@ class Value<T> {
|
||||||
final bool present;
|
final bool present;
|
||||||
final T value;
|
final T value;
|
||||||
|
|
||||||
const Value.use(this.value) : present = true;
|
const Value(this.value) : present = true;
|
||||||
const Value.absent()
|
const Value.absent()
|
||||||
: value = null,
|
: value = null,
|
||||||
present = false;
|
present = false;
|
||||||
|
|
|
@ -14,7 +14,11 @@ class TodoEntry extends DataClass implements Insertable<TodoEntry> {
|
||||||
final DateTime targetDate;
|
final DateTime targetDate;
|
||||||
final int category;
|
final int category;
|
||||||
TodoEntry(
|
TodoEntry(
|
||||||
{this.id, this.title, this.content, this.targetDate, this.category});
|
{@required this.id,
|
||||||
|
this.title,
|
||||||
|
@required this.content,
|
||||||
|
this.targetDate,
|
||||||
|
this.category});
|
||||||
factory TodoEntry.fromData(Map<String, dynamic> data, GeneratedDatabase db,
|
factory TodoEntry.fromData(Map<String, dynamic> data, GeneratedDatabase db,
|
||||||
{String prefix}) {
|
{String prefix}) {
|
||||||
final effectivePrefix = prefix ?? '';
|
final effectivePrefix = prefix ?? '';
|
||||||
|
@ -58,19 +62,18 @@ class TodoEntry extends DataClass implements Insertable<TodoEntry> {
|
||||||
@override
|
@override
|
||||||
T createCompanion<T extends UpdateCompanion<TodoEntry>>(bool nullToAbsent) {
|
T createCompanion<T extends UpdateCompanion<TodoEntry>>(bool nullToAbsent) {
|
||||||
return TodosTableCompanion(
|
return TodosTableCompanion(
|
||||||
id: id == null && nullToAbsent ? const Value.absent() : Value.use(id),
|
id: id == null && nullToAbsent ? const Value.absent() : Value(id),
|
||||||
title: title == null && nullToAbsent
|
title:
|
||||||
? const Value.absent()
|
title == null && nullToAbsent ? const Value.absent() : Value(title),
|
||||||
: Value.use(title),
|
|
||||||
content: content == null && nullToAbsent
|
content: content == null && nullToAbsent
|
||||||
? const Value.absent()
|
? const Value.absent()
|
||||||
: Value.use(content),
|
: Value(content),
|
||||||
targetDate: targetDate == null && nullToAbsent
|
targetDate: targetDate == null && nullToAbsent
|
||||||
? const Value.absent()
|
? const Value.absent()
|
||||||
: Value.use(targetDate),
|
: Value(targetDate),
|
||||||
category: category == null && nullToAbsent
|
category: category == null && nullToAbsent
|
||||||
? const Value.absent()
|
? const Value.absent()
|
||||||
: Value.use(category),
|
: Value(category),
|
||||||
) as T;
|
) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -273,7 +276,7 @@ class $TodosTableTable extends TodosTable
|
||||||
class Category extends DataClass implements Insertable<Category> {
|
class Category extends DataClass implements Insertable<Category> {
|
||||||
final int id;
|
final int id;
|
||||||
final String description;
|
final String description;
|
||||||
Category({this.id, this.description});
|
Category({@required this.id, @required this.description});
|
||||||
factory Category.fromData(Map<String, dynamic> data, GeneratedDatabase db,
|
factory Category.fromData(Map<String, dynamic> data, GeneratedDatabase db,
|
||||||
{String prefix}) {
|
{String prefix}) {
|
||||||
final effectivePrefix = prefix ?? '';
|
final effectivePrefix = prefix ?? '';
|
||||||
|
@ -304,10 +307,10 @@ class Category extends DataClass implements Insertable<Category> {
|
||||||
@override
|
@override
|
||||||
T createCompanion<T extends UpdateCompanion<Category>>(bool nullToAbsent) {
|
T createCompanion<T extends UpdateCompanion<Category>>(bool nullToAbsent) {
|
||||||
return CategoriesCompanion(
|
return CategoriesCompanion(
|
||||||
id: id == null && nullToAbsent ? const Value.absent() : Value.use(id),
|
id: id == null && nullToAbsent ? const Value.absent() : Value(id),
|
||||||
description: description == null && nullToAbsent
|
description: description == null && nullToAbsent
|
||||||
? const Value.absent()
|
? const Value.absent()
|
||||||
: Value.use(description),
|
: Value(description),
|
||||||
) as T;
|
) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -424,11 +427,11 @@ class User extends DataClass implements Insertable<User> {
|
||||||
final Uint8List profilePicture;
|
final Uint8List profilePicture;
|
||||||
final DateTime creationTime;
|
final DateTime creationTime;
|
||||||
User(
|
User(
|
||||||
{this.id,
|
{@required this.id,
|
||||||
this.name,
|
@required this.name,
|
||||||
this.isAwesome,
|
@required this.isAwesome,
|
||||||
this.profilePicture,
|
@required this.profilePicture,
|
||||||
this.creationTime});
|
@required this.creationTime});
|
||||||
factory User.fromData(Map<String, dynamic> data, GeneratedDatabase db,
|
factory User.fromData(Map<String, dynamic> data, GeneratedDatabase db,
|
||||||
{String prefix}) {
|
{String prefix}) {
|
||||||
final effectivePrefix = prefix ?? '';
|
final effectivePrefix = prefix ?? '';
|
||||||
|
@ -473,18 +476,17 @@ class User extends DataClass implements Insertable<User> {
|
||||||
@override
|
@override
|
||||||
T createCompanion<T extends UpdateCompanion<User>>(bool nullToAbsent) {
|
T createCompanion<T extends UpdateCompanion<User>>(bool nullToAbsent) {
|
||||||
return UsersCompanion(
|
return UsersCompanion(
|
||||||
id: id == null && nullToAbsent ? const Value.absent() : Value.use(id),
|
id: id == null && nullToAbsent ? const Value.absent() : Value(id),
|
||||||
name:
|
name: name == null && nullToAbsent ? const Value.absent() : Value(name),
|
||||||
name == null && nullToAbsent ? const Value.absent() : Value.use(name),
|
|
||||||
isAwesome: isAwesome == null && nullToAbsent
|
isAwesome: isAwesome == null && nullToAbsent
|
||||||
? const Value.absent()
|
? const Value.absent()
|
||||||
: Value.use(isAwesome),
|
: Value(isAwesome),
|
||||||
profilePicture: profilePicture == null && nullToAbsent
|
profilePicture: profilePicture == null && nullToAbsent
|
||||||
? const Value.absent()
|
? const Value.absent()
|
||||||
: Value.use(profilePicture),
|
: Value(profilePicture),
|
||||||
creationTime: creationTime == null && nullToAbsent
|
creationTime: creationTime == null && nullToAbsent
|
||||||
? const Value.absent()
|
? const Value.absent()
|
||||||
: Value.use(creationTime),
|
: Value(creationTime),
|
||||||
) as T;
|
) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -690,7 +692,7 @@ class $UsersTable extends Users with TableInfo<$UsersTable, User> {
|
||||||
class SharedTodo extends DataClass implements Insertable<SharedTodo> {
|
class SharedTodo extends DataClass implements Insertable<SharedTodo> {
|
||||||
final int todo;
|
final int todo;
|
||||||
final int user;
|
final int user;
|
||||||
SharedTodo({this.todo, this.user});
|
SharedTodo({@required this.todo, @required this.user});
|
||||||
factory SharedTodo.fromData(Map<String, dynamic> data, GeneratedDatabase db,
|
factory SharedTodo.fromData(Map<String, dynamic> data, GeneratedDatabase db,
|
||||||
{String prefix}) {
|
{String prefix}) {
|
||||||
final effectivePrefix = prefix ?? '';
|
final effectivePrefix = prefix ?? '';
|
||||||
|
@ -719,10 +721,8 @@ class SharedTodo extends DataClass implements Insertable<SharedTodo> {
|
||||||
@override
|
@override
|
||||||
T createCompanion<T extends UpdateCompanion<SharedTodo>>(bool nullToAbsent) {
|
T createCompanion<T extends UpdateCompanion<SharedTodo>>(bool nullToAbsent) {
|
||||||
return SharedTodosCompanion(
|
return SharedTodosCompanion(
|
||||||
todo:
|
todo: todo == null && nullToAbsent ? const Value.absent() : Value(todo),
|
||||||
todo == null && nullToAbsent ? const Value.absent() : Value.use(todo),
|
user: user == null && nullToAbsent ? const Value.absent() : Value(user),
|
||||||
user:
|
|
||||||
user == null && nullToAbsent ? const Value.absent() : Value.use(user),
|
|
||||||
) as T;
|
) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -842,7 +842,7 @@ class TableWithoutPKData extends DataClass
|
||||||
implements Insertable<TableWithoutPKData> {
|
implements Insertable<TableWithoutPKData> {
|
||||||
final int notReallyAnId;
|
final int notReallyAnId;
|
||||||
final double someFloat;
|
final double someFloat;
|
||||||
TableWithoutPKData({this.notReallyAnId, this.someFloat});
|
TableWithoutPKData({@required this.notReallyAnId, @required this.someFloat});
|
||||||
factory TableWithoutPKData.fromData(
|
factory TableWithoutPKData.fromData(
|
||||||
Map<String, dynamic> data, GeneratedDatabase db,
|
Map<String, dynamic> data, GeneratedDatabase db,
|
||||||
{String prefix}) {
|
{String prefix}) {
|
||||||
|
@ -878,10 +878,10 @@ class TableWithoutPKData extends DataClass
|
||||||
return TableWithoutPKCompanion(
|
return TableWithoutPKCompanion(
|
||||||
notReallyAnId: notReallyAnId == null && nullToAbsent
|
notReallyAnId: notReallyAnId == null && nullToAbsent
|
||||||
? const Value.absent()
|
? const Value.absent()
|
||||||
: Value.use(notReallyAnId),
|
: Value(notReallyAnId),
|
||||||
someFloat: someFloat == null && nullToAbsent
|
someFloat: someFloat == null && nullToAbsent
|
||||||
? const Value.absent()
|
? const Value.absent()
|
||||||
: Value.use(someFloat),
|
: Value(someFloat),
|
||||||
) as T;
|
) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,13 +6,16 @@ import 'data/utils/mocks.dart';
|
||||||
|
|
||||||
// the content is set to non-null and the title must be between 4 and 16 chars
|
// the content is set to non-null and the title must be between 4 and 16 chars
|
||||||
// long
|
// long
|
||||||
final nullContent = const TodosTableCompanion(
|
const nullContent =
|
||||||
title: Value.use('Test'), content: Value.use(null));
|
TodosTableCompanion(title: Value('Test'), content: Value(null));
|
||||||
final absentContent = const TodosTableCompanion(
|
const absentContent =
|
||||||
title: Value.use('Test'), content: Value.absent());
|
TodosTableCompanion(title: Value('Test'), content: Value.absent());
|
||||||
final shortTitle = TodoEntry(title: 'A', content: 'content');
|
final shortTitle =
|
||||||
final longTitle = TodoEntry(title: 'A ${'very' * 5} long title', content: 'hi');
|
const TodosTableCompanion(title: Value('A'), content: Value('content'));
|
||||||
final valid = TodoEntry(title: 'Test', content: 'Some content');
|
final longTitle = TodosTableCompanion(
|
||||||
|
title: Value('A ${'very' * 5} long title'), content: const Value('hi'));
|
||||||
|
const valid =
|
||||||
|
TodosTableCompanion(title: Value('Test'), content: Value('Some content'));
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
TodoDb db;
|
TodoDb db;
|
||||||
|
|
|
@ -16,8 +16,8 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('generates insert statements', () async {
|
test('generates insert statements', () async {
|
||||||
await db.into(db.todosTable).insert(TodoEntry(
|
await db.into(db.todosTable).insert(const TodosTableCompanion(
|
||||||
content: 'Implement insert statements',
|
content: Value('Implement insert statements'),
|
||||||
));
|
));
|
||||||
|
|
||||||
verify(executor.runInsert('INSERT INTO todos (content) VALUES (?)',
|
verify(executor.runInsert('INSERT INTO todos (content) VALUES (?)',
|
||||||
|
@ -50,10 +50,10 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('runs bulk inserts', () async {
|
test('runs bulk inserts', () async {
|
||||||
await db.into(db.todosTable).insertAll([
|
await db.into(db.todosTable).insertAll(const [
|
||||||
TodoEntry(content: 'a'),
|
TodosTableCompanion(content: Value('a')),
|
||||||
TodoEntry(title: 'title', content: 'b'),
|
TodosTableCompanion(title: Value('title'), content: Value('b')),
|
||||||
TodoEntry(title: 'title', content: 'c'),
|
TodosTableCompanion(title: Value('title'), content: Value('c')),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
final insertSimple = 'INSERT INTO todos (content) VALUES (?)';
|
final insertSimple = 'INSERT INTO todos (content) VALUES (?)';
|
||||||
|
@ -73,10 +73,10 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('runs bulk inserts with OR REPLACE', () async {
|
test('runs bulk inserts with OR REPLACE', () async {
|
||||||
await db.into(db.todosTable).insertAll([
|
await db.into(db.todosTable).insertAll(const [
|
||||||
TodoEntry(content: 'a'),
|
TodosTableCompanion(content: Value('a')),
|
||||||
TodoEntry(title: 'title', content: 'b'),
|
TodosTableCompanion(title: Value('title'), content: Value('b')),
|
||||||
TodoEntry(title: 'title', content: 'c'),
|
TodosTableCompanion(title: Value('title'), content: Value('c')),
|
||||||
], orReplace: true);
|
], orReplace: true);
|
||||||
|
|
||||||
final insertSimple = 'INSERT OR REPLACE INTO todos (content) VALUES (?)';
|
final insertSimple = 'INSERT OR REPLACE INTO todos (content) VALUES (?)';
|
||||||
|
@ -97,10 +97,10 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('notifies stream queries on inserts', () async {
|
test('notifies stream queries on inserts', () async {
|
||||||
await db.into(db.users).insert(User(
|
await db.into(db.users).insert(UsersCompanion(
|
||||||
name: 'User McUserface',
|
name: const Value('User McUserface'),
|
||||||
isAwesome: true,
|
isAwesome: const Value(true),
|
||||||
profilePicture: Uint8List(0),
|
profilePicture: Value(Uint8List(0)),
|
||||||
));
|
));
|
||||||
|
|
||||||
verify(streamQueries.handleTableUpdates({db.users}));
|
verify(streamQueries.handleTableUpdates({db.users}));
|
||||||
|
@ -109,8 +109,9 @@ void main() {
|
||||||
test('enforces data integrity', () {
|
test('enforces data integrity', () {
|
||||||
expect(
|
expect(
|
||||||
db.into(db.todosTable).insert(
|
db.into(db.todosTable).insert(
|
||||||
TodoEntry(
|
const TodosTableCompanion(
|
||||||
content: null, // not declared as nullable in table definition
|
// not declared as nullable in table definition
|
||||||
|
content: Value(null),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
throwsA(const TypeMatcher<InvalidDataException>()),
|
throwsA(const TypeMatcher<InvalidDataException>()),
|
||||||
|
@ -120,7 +121,11 @@ void main() {
|
||||||
test('reports auto-increment id', () async {
|
test('reports auto-increment id', () async {
|
||||||
when(executor.runInsert(any, any)).thenAnswer((_) => Future.value(42));
|
when(executor.runInsert(any, any)).thenAnswer((_) => Future.value(42));
|
||||||
|
|
||||||
expect(db.into(db.todosTable).insert(TodoEntry(content: 'Bottom text')),
|
expect(
|
||||||
completion(42));
|
db
|
||||||
|
.into(db.todosTable)
|
||||||
|
.insert(const TodosTableCompanion(content: Value('Bottom text'))),
|
||||||
|
completion(42),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import 'package:test_api/test_api.dart';
|
import 'package:test_api/test_api.dart';
|
||||||
|
import 'package:moor/moor.dart';
|
||||||
|
|
||||||
import 'data/tables/todos.dart';
|
import 'data/tables/todos.dart';
|
||||||
import 'data/utils/mocks.dart';
|
import 'data/utils/mocks.dart';
|
||||||
|
@ -39,7 +40,9 @@ void main() {
|
||||||
.thenAnswer((_) => Future.value(2));
|
.thenAnswer((_) => Future.value(2));
|
||||||
|
|
||||||
await db.transaction((t) async {
|
await db.transaction((t) async {
|
||||||
await t.update(db.users).write(User(name: 'Updated name'));
|
await t
|
||||||
|
.update(db.users)
|
||||||
|
.write(const UsersCompanion(name: Value('Updated name')));
|
||||||
|
|
||||||
// Even though we just wrote to users, this only happened inside the
|
// Even though we just wrote to users, this only happened inside the
|
||||||
// transaction, so the top level stream queries should not be updated.
|
// transaction, so the top level stream queries should not be updated.
|
||||||
|
|
|
@ -19,9 +19,10 @@ void main() {
|
||||||
|
|
||||||
group('generates update statements', () {
|
group('generates update statements', () {
|
||||||
test('for entire table', () async {
|
test('for entire table', () async {
|
||||||
await db
|
await db.update(db.todosTable).write(const TodosTableCompanion(
|
||||||
.update(db.todosTable)
|
title: Value('Updated title'),
|
||||||
.write(TodoEntry(title: 'Updated title', category: 3));
|
category: Value(3),
|
||||||
|
));
|
||||||
|
|
||||||
verify(executor.runUpdate(
|
verify(executor.runUpdate(
|
||||||
'UPDATE todos SET title = ?, category = ?;', ['Updated title', 3]));
|
'UPDATE todos SET title = ?, category = ?;', ['Updated title', 3]));
|
||||||
|
@ -30,7 +31,7 @@ void main() {
|
||||||
test('with a WHERE clause', () async {
|
test('with a WHERE clause', () async {
|
||||||
await (db.update(db.todosTable)
|
await (db.update(db.todosTable)
|
||||||
..where((t) => t.id.isSmallerThanValue(50)))
|
..where((t) => t.id.isSmallerThanValue(50)))
|
||||||
.write(TodoEntry(title: 'Changed title'));
|
.write(const TodosTableCompanion(title: Value('Changed title')));
|
||||||
|
|
||||||
verify(executor.runUpdate(
|
verify(executor.runUpdate(
|
||||||
'UPDATE todos SET title = ? WHERE id < ?;', ['Changed title', 50]));
|
'UPDATE todos SET title = ? WHERE id < ?;', ['Changed title', 50]));
|
||||||
|
@ -55,7 +56,9 @@ void main() {
|
||||||
// The length of a title must be between 4 and 16 chars
|
// The length of a title must be between 4 and 16 chars
|
||||||
|
|
||||||
expect(() async {
|
expect(() async {
|
||||||
await db.update(db.todosTable).write(TodoEntry(title: 'lol'));
|
await db
|
||||||
|
.update(db.todosTable)
|
||||||
|
.write(const TodosTableCompanion(title: Value('lol')));
|
||||||
}, throwsA(const TypeMatcher<InvalidDataException>()));
|
}, throwsA(const TypeMatcher<InvalidDataException>()));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -63,8 +66,8 @@ void main() {
|
||||||
test('are issued when data was changed', () async {
|
test('are issued when data was changed', () async {
|
||||||
when(executor.runUpdate(any, any)).thenAnswer((_) => Future.value(3));
|
when(executor.runUpdate(any, any)).thenAnswer((_) => Future.value(3));
|
||||||
|
|
||||||
await db.update(db.todosTable).write(TodoEntry(
|
await db.update(db.todosTable).write(const TodosTableCompanion(
|
||||||
content: 'Updated content',
|
content: Value('Updated content'),
|
||||||
));
|
));
|
||||||
|
|
||||||
verify(streamQueries.handleTableUpdates({db.todosTable}));
|
verify(streamQueries.handleTableUpdates({db.todosTable}));
|
||||||
|
@ -73,7 +76,7 @@ void main() {
|
||||||
test('are not issued when no data was changed', () async {
|
test('are not issued when no data was changed', () async {
|
||||||
when(executor.runDelete(any, any)).thenAnswer((_) => Future.value(0));
|
when(executor.runDelete(any, any)).thenAnswer((_) => Future.value(0));
|
||||||
|
|
||||||
await db.update(db.todosTable).write(TodoEntry());
|
await db.update(db.todosTable).write(const TodosTableCompanion());
|
||||||
|
|
||||||
verifyNever(streamQueries.handleTableUpdates(any));
|
verifyNever(streamQueries.handleTableUpdates(any));
|
||||||
});
|
});
|
||||||
|
|
|
@ -52,16 +52,16 @@ class TodoEntry extends DataClass implements Insertable<TodoEntry> {
|
||||||
@override
|
@override
|
||||||
T createCompanion<T extends UpdateCompanion<TodoEntry>>(bool nullToAbsent) {
|
T createCompanion<T extends UpdateCompanion<TodoEntry>>(bool nullToAbsent) {
|
||||||
return TodosCompanion(
|
return TodosCompanion(
|
||||||
id: id == null && nullToAbsent ? const Value.absent() : Value.use(id),
|
id: id == null && nullToAbsent ? const Value.absent() : Value(id),
|
||||||
content: content == null && nullToAbsent
|
content: content == null && nullToAbsent
|
||||||
? const Value.absent()
|
? const Value.absent()
|
||||||
: Value.use(content),
|
: Value(content),
|
||||||
targetDate: targetDate == null && nullToAbsent
|
targetDate: targetDate == null && nullToAbsent
|
||||||
? const Value.absent()
|
? const Value.absent()
|
||||||
: Value.use(targetDate),
|
: Value(targetDate),
|
||||||
category: category == null && nullToAbsent
|
category: category == null && nullToAbsent
|
||||||
? const Value.absent()
|
? const Value.absent()
|
||||||
: Value.use(category),
|
: Value(category),
|
||||||
) as T;
|
) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -262,10 +262,10 @@ class Category extends DataClass implements Insertable<Category> {
|
||||||
@override
|
@override
|
||||||
T createCompanion<T extends UpdateCompanion<Category>>(bool nullToAbsent) {
|
T createCompanion<T extends UpdateCompanion<Category>>(bool nullToAbsent) {
|
||||||
return CategoriesCompanion(
|
return CategoriesCompanion(
|
||||||
id: id == null && nullToAbsent ? const Value.absent() : Value.use(id),
|
id: id == null && nullToAbsent ? const Value.absent() : Value(id),
|
||||||
description: description == null && nullToAbsent
|
description: description == null && nullToAbsent
|
||||||
? const Value.absent()
|
? const Value.absent()
|
||||||
: Value.use(description),
|
: Value(description),
|
||||||
) as T;
|
) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,9 +24,13 @@ class DataClassWriter {
|
||||||
buffer
|
buffer
|
||||||
..write(table.dartTypeName)
|
..write(table.dartTypeName)
|
||||||
..write('({')
|
..write('({')
|
||||||
..write(table.columns
|
..write(table.columns.map((column) {
|
||||||
.map((column) => 'this.${column.dartGetterName}')
|
if (column.nullable) {
|
||||||
.join(', '))
|
return 'this.${column.dartGetterName}';
|
||||||
|
} else {
|
||||||
|
return '@required this.${column.dartGetterName}';
|
||||||
|
}
|
||||||
|
}).join(', '))
|
||||||
..write('});');
|
..write('});');
|
||||||
|
|
||||||
// Also write parsing factory
|
// Also write parsing factory
|
||||||
|
|
Loading…
Reference in New Issue