Move snippet test to docs package

This commit is contained in:
Simon Binder 2022-08-15 19:55:06 +02:00
parent d43f8623e4
commit 74ab9d4f80
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
7 changed files with 130 additions and 145 deletions

View File

@ -24,9 +24,13 @@ jobs:
- name: Analyze Dart sources - name: Analyze Dart sources
working-directory: docs working-directory: docs
run: | run: |
dart format -o none --set-exit-if-changed .
dart analyze --fatal-infos --fatal-warnings dart analyze --fatal-infos --fatal-warnings
dart run drift_dev analyze dart run drift_dev analyze
- name: Run tests
working-directory: docs
run: dart test
- name: Deploy to netlify (Branch) - name: Deploy to netlify (Branch)
if: ${{ github.event_name == 'push' }} if: ${{ github.event_name == 'push' }}
uses: nwtgck/actions-netlify@v1.2 uses: nwtgck/actions-netlify@v1.2

View File

@ -39,6 +39,7 @@ targets:
enabled: true enabled: true
sources: sources:
- lib/** - lib/**
- test/generated/**
prepare: prepare:
dependencies: [":source_gen"] dependencies: [":source_gen"]

View File

@ -40,6 +40,7 @@ dev_dependencies:
shelf: ^1.2.0 shelf: ^1.2.0
shelf_static: ^1.1.0 shelf_static: ^1.1.0
source_span: ^1.9.1 source_span: ^1.9.1
test: ^1.18.0
sqlparser: sqlparser:

View File

@ -0,0 +1,21 @@
import 'package:drift/drift.dart';
part 'database.g.dart';
class Users extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get name => text()();
DateTimeColumn get createdAt =>
dateTime().nullable().withDefault(currentDateAndTime)();
}
@DriftDatabase(tables: [Users])
class Database extends _$Database {
Database.connect(DatabaseConnection c) : super.connect(c);
@override
int get schemaVersion => 1;
@override
DriftDatabaseOptions options = DriftDatabaseOptions();
}

103
docs/test/snippet_test.dart Normal file
View File

@ -0,0 +1,103 @@
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:drift_docs/snippets/migrations/datetime_conversion.dart';
import 'package:test/test.dart';
import 'generated/database.dart';
/// Test for some snippets embedded on https://drift.simonbinder.eu to make sure
/// that they are still up to date and work as intended with the latest drift
/// version.
void main() {
group('changing datetime format', () {
test('unix timestamp to text', () async {
final db = Database.connect(DatabaseConnection(NativeDatabase.memory()));
addTearDown(db.close);
final time = DateTime.fromMillisecondsSinceEpoch(
// Drop milliseconds which are not supported by the old format
1000 * (DateTime.now().millisecondsSinceEpoch ~/ 1000),
);
// The database is currently using unix timstamps. Let's add some rows in
// that format:
await db.users.insertOne(
UsersCompanion.insert(name: 'name', createdAt: Value(time)));
await db.users.insertOne(
UsersCompanion.insert(name: 'name2', createdAt: Value(null)));
// Run conversion from unix timestamps to text
await db.migrateFromUnixTimestampsToText(db.createMigrator());
// Check that the values are still there!
final rows = await (db.select(db.users)
..orderBy([(row) => OrderingTerm.asc(row.rowId)]))
.get();
expect(rows, [
User(id: 1, name: 'name', createdAt: time),
const User(id: 2, name: 'name2', createdAt: null),
]);
});
test('text to unix timestamp', () async {
// First, create all tables using text as datetime
final db = Database.connect(DatabaseConnection(NativeDatabase.memory()));
db.options = const DriftDatabaseOptions(storeDateTimeAsText: true);
addTearDown(db.close);
final time = DateTime.fromMillisecondsSinceEpoch(
1000 * (DateTime.now().millisecondsSinceEpoch ~/ 1000));
// Add rows, storing date time as text
await db.users.insertOne(
UsersCompanion.insert(name: 'name', createdAt: Value(time)));
await db.users.insertOne(
UsersCompanion.insert(name: 'name2', createdAt: Value(null)));
// Next, migrate back to unix timestamps
db.options = const DriftDatabaseOptions(storeDateTimeAsText: false);
final migrator = db.createMigrator();
await db.migrateFromTextDateTimesToUnixTimestamps(migrator);
final rows = await (db.select(db.users)
..orderBy([(row) => OrderingTerm.asc(row.rowId)]))
.get();
expect(rows, [
User(id: 1, name: 'name', createdAt: time),
const User(id: 2, name: 'name2', createdAt: null),
]);
});
test('text to unix timestamp, support old sqlite', () async {
// First, create all tables using text as datetime
final db = Database.connect(DatabaseConnection(NativeDatabase.memory()));
db.options = const DriftDatabaseOptions(storeDateTimeAsText: true);
addTearDown(db.close);
final time = DateTime.fromMillisecondsSinceEpoch(
1000 * (DateTime.now().millisecondsSinceEpoch ~/ 1000));
// Add rows, storing date time as text
await db.users.insertOne(
UsersCompanion.insert(name: 'name', createdAt: Value(time)));
await db.users.insertOne(
UsersCompanion.insert(name: 'name2', createdAt: Value(null)));
// Next, migrate back to unix timestamps
db.options = const DriftDatabaseOptions(storeDateTimeAsText: false);
final migrator = db.createMigrator();
await db.migrateFromTextDateTimesToUnixTimestampsPre338(migrator);
final rows = await (db.select(db.users)
..orderBy([(row) => OrderingTerm.asc(row.rowId)]))
.get();
expect(rows, [
User(id: 1, name: 'name', createdAt: time),
const User(id: 2, name: 'name2', createdAt: null),
]);
});
});
}

View File

@ -24,8 +24,6 @@ dev_dependencies:
drift_dev: any drift_dev: any
drift_testcases: drift_testcases:
path: ../extras/integration_tests/drift_testcases path: ../extras/integration_tests/drift_testcases
drift_docs:
path: ../docs/
http: ^0.13.4 http: ^0.13.4
lints: ^2.0.0 lints: ^2.0.0
uuid: ^3.0.0 uuid: ^3.0.0

View File

@ -1,143 +0,0 @@
import 'package:drift/drift.dart';
import 'package:drift_docs/snippets/migrations/datetime_conversion.dart';
import 'package:test/test.dart';
import 'generated/custom_tables.dart';
import 'generated/todos.dart';
import 'test_utils/test_utils.dart';
/// Test for some snippets embedded on https://drift.simonbinder.eu to make sure
/// that they are still up to date and work as intended with the latest drift
/// version.
void main() {
group('changing datetime format', () {
test('unix timestamp to text', () async {
// Note: CustomTablesDb has been compiled to use datetimes as text by
// default.
final db = CustomTablesDb.connect(testInMemoryDatabase());
addTearDown(db.close);
final time = DateTime.fromMillisecondsSinceEpoch(
1000 * (DateTime.now().millisecondsSinceEpoch ~/ 1000),
isUtc: true,
);
// Re-create the `mytable` table (the only one using dates) to use unix
// timestamps
await db.customStatement('DROP TABLE mytable');
await db.customStatement('''
CREATE TABLE mytable (
someid INTEGER NOT NULL,
sometext TEXT,
is_inserting BOOLEAN,
somedate INTEGER,
PRIMARY KEY (someid DESC)
);
''');
await db.customStatement('''
INSERT INTO mytable (someid) VALUES (1); -- nullable value
INSERT INTO mytable (someid, somedate) VALUES (2, ${time.millisecondsSinceEpoch ~/ 1000});
''');
// Run conversion from unix timestamps to text
await db.migrateFromUnixTimestampsToText(db.createMigrator());
// Check that the values are still there!
final rows = await (db.select(db.mytable)
..orderBy([(row) => OrderingTerm.asc(row.someid)]))
.get();
expect(rows, [
const MytableData(someid: 1),
MytableData(someid: 2, somedate: time),
]);
});
test('text to unix timestamp', () async {
// First, create all tables using text as datetime
final db = TodoDb.connect(testInMemoryDatabase());
db.options = const DriftDatabaseOptions(storeDateTimeAsText: true);
addTearDown(db.close);
final time = DateTime.fromMillisecondsSinceEpoch(
1000 * (DateTime.now().millisecondsSinceEpoch ~/ 1000));
await db.into(db.users).insert(
UsersCompanion.insert(
name: 'Some user',
profilePicture: Uint8List(0),
creationTime: Value(time),
),
);
await db.into(db.todosTable).insert(TodosTableCompanion.insert(
content: 'with null date',
));
await db.into(db.todosTable).insert(TodosTableCompanion.insert(
content: 'with due date',
targetDate: Value(time),
));
// Next, migrate back to unix timestamps
db.options = const DriftDatabaseOptions(storeDateTimeAsText: false);
final migrator = db.createMigrator();
await migrator.drop(db.categoryTodoCountView);
await migrator.drop(db.todoWithCategoryView);
await db.migrateFromTextDateTimesToUnixTimestamps(migrator);
await migrator.create(db.categoryTodoCountView);
await migrator.create(db.todoWithCategoryView);
expect(await db.users.select().getSingle(),
isA<User>().having((e) => e.creationTime, 'creationTime', time));
expect(await db.todosTable.select().get(), [
const TodoEntry(id: 1, content: 'with null date'),
TodoEntry(id: 2, content: 'with due date', targetDate: time),
]);
});
test('text to unix timestamp, support old sqlite', () async {
// First, create all tables using text as datetime
final db = TodoDb.connect(testInMemoryDatabase());
db.options = const DriftDatabaseOptions(storeDateTimeAsText: true);
addTearDown(db.close);
final time = DateTime.fromMillisecondsSinceEpoch(
1000 * (DateTime.now().millisecondsSinceEpoch ~/ 1000));
await db.into(db.users).insert(
UsersCompanion.insert(
name: 'Some user',
profilePicture: Uint8List(0),
creationTime: Value(time),
),
);
await db.into(db.todosTable).insert(TodosTableCompanion.insert(
content: 'with null date',
));
await db.into(db.todosTable).insert(TodosTableCompanion.insert(
content: 'with due date',
targetDate: Value(time),
));
// Next, migrate back to unix timestamps
db.options = const DriftDatabaseOptions(storeDateTimeAsText: false);
final migrator = db.createMigrator();
await migrator.drop(db.categoryTodoCountView);
await migrator.drop(db.todoWithCategoryView);
await db.migrateFromTextDateTimesToUnixTimestampsPre338(migrator);
await migrator.create(db.categoryTodoCountView);
await migrator.create(db.todoWithCategoryView);
expect(await db.users.select().getSingle(),
isA<User>().having((e) => e.creationTime, 'creationTime', time));
expect(await db.todosTable.select().get(), [
const TodoEntry(id: 1, content: 'with null date'),
TodoEntry(id: 2, content: 'with due date', targetDate: time),
]);
});
});
}