Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run real database tests on development machine and CI #252

Merged
merged 11 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ jobs:
run: flutter packages get
working-directory: floor

- name: Install SQLite
run: sudo apt-get -y install sqlite3 libsqlite3-dev

- name: Analyze
run: flutter analyze
working-directory: floor
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Exclude generated Dart files
*.g.dart
!floor/test/integration/*.g.dart

# Miscellaneous
*.class
Expand Down
75 changes: 74 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This package is still in an early phase and the API will likely change.
1. [In-Memory Database](#in-memory-database)
1. [Callback](#callback)
1. [Ignore Fields](#ignore-fields)
1. [Testing](#testing)
1. [Examples](#examples)
1. [Naming](#naming)
1. [Bugs and Feedback](#bugs-and-feedback)
Expand Down Expand Up @@ -465,15 +466,87 @@ In case further fields should be ignored, the `@ignore` annotation should be use
class Person {
@primaryKey
final int id;

final String name;

@ignore
String nickname;

Person(this.id, this.name);
}
```

## Testing
In order to run database tests on your development machine without the need to deploy the code to an actual device, the setup has to be configured as shown in the following.
For more test references, check out the [project's tests](https://github.com/vitusortner/floor/tree/develop/floor/integration).

#### pubspec.yaml
```yaml
dependencies:
flutter:
sdk: flutter
floor: ^0.11.0

dev_dependencies:
floor_generator: ^0.11.0
build_runner: ^1.7.3

# additional dependencies
flutter_test:
sdk: flutter
sqflite_ffi_test:
git:
url: git://github.com/tekartik/sqflite_more
ref: dart2
path: sqflite_ffi_test
```

#### database_test.dart
```dart
import 'package:floor/floor.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:matcher/matcher.dart';
import 'package:sqflite/sqflite.dart';
import 'package:sqflite_ffi_test/sqflite_ffi_test.dart';

// your imports follow here
import 'dao/person_dao.dart';
import 'database.dart';
import 'model/person.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
sqfliteFfiTestInit();

group('database tests', () {
TestDatabase database;
PersonDao personDao;

setUp(() async {
database = await $FloorTestDatabase
.inMemoryDatabaseBuilder()
.build();
personDao = database.personDao;
});

tearDown(() async {
await database.close();
});

test('insert person', () async {
final person = Person(1, 'Simon');
await personDao.insertPerson(person);

final actual = await personDao.findPersonById(person.id);

expect(actual, equals(person));
});
}
}
```

## Examples
For further examples take a look at the [example](https://github.com/vitusortner/floor/tree/develop/example) and [floor_test](https://github.com/vitusortner/floor/tree/develop/floor_test) directories.
For further examples take a look at the [example](https://github.com/vitusortner/floor/tree/develop/example) and [test](https://github.com/vitusortner/floor/tree/develop/floor/integration) directories.

## Naming
*Floor - the bottom layer of a [Room](https://developer.android.com/topic/libraries/architecture/room).*
Expand Down
75 changes: 74 additions & 1 deletion floor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This package is still in an early phase and the API will likely change.
1. [In-Memory Database](#in-memory-database)
1. [Callback](#callback)
1. [Ignore Fields](#ignore-fields)
1. [Testing](#testing)
1. [Examples](#examples)
1. [Naming](#naming)
1. [Bugs and Feedback](#bugs-and-feedback)
Expand Down Expand Up @@ -465,15 +466,87 @@ In case further fields should be ignored, the `@ignore` annotation should be use
class Person {
@primaryKey
final int id;

final String name;

@ignore
String nickname;

Person(this.id, this.name);
}
```

## Testing
In order to run database tests on your development machine without the need to deploy the code to an actual device, the setup has to be configured as shown in the following.
For more test references, check out the [project's tests](https://github.com/vitusortner/floor/tree/develop/floor/integration).

#### pubspec.yaml
```yaml
dependencies:
flutter:
sdk: flutter
floor: ^0.11.0

dev_dependencies:
floor_generator: ^0.11.0
build_runner: ^1.7.3

# additional dependencies
flutter_test:
sdk: flutter
sqflite_ffi_test:
git:
url: git://github.com/tekartik/sqflite_more
ref: dart2
path: sqflite_ffi_test
```

#### database_test.dart
```dart
import 'package:floor/floor.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:matcher/matcher.dart';
import 'package:sqflite/sqflite.dart';
import 'package:sqflite_ffi_test/sqflite_ffi_test.dart';

// your imports follow here
import 'dao/person_dao.dart';
import 'database.dart';
import 'model/person.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
sqfliteFfiTestInit();

group('database tests', () {
TestDatabase database;
PersonDao personDao;

setUp(() async {
database = await $FloorTestDatabase
.inMemoryDatabaseBuilder()
.build();
personDao = database.personDao;
});

tearDown(() async {
await database.close();
});

test('insert person', () async {
final person = Person(1, 'Simon');
await personDao.insertPerson(person);

final actual = await personDao.findPersonById(person.id);

expect(actual, equals(person));
});
}
}
```

## Examples
For further examples take a look at the [example](https://github.com/vitusortner/floor/tree/develop/example) and [floor_test](https://github.com/vitusortner/floor/tree/develop/floor_test) directories.
For further examples take a look at the [example](https://github.com/vitusortner/floor/tree/develop/example) and [test](https://github.com/vitusortner/floor/tree/develop/floor/integration) directories.

## Naming
*Floor - the bottom layer of a [Room](https://developer.android.com/topic/libraries/architecture/room).*
Expand Down
8 changes: 8 additions & 0 deletions floor/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ dev_dependencies:
mockito: ^4.1.1
flutter_test:
sdk: flutter
floor_generator:
path: ../floor_generator/
build_runner: ^1.7.3
sqflite_ffi_test:
git:
url: git://github.com/tekartik/sqflite_more
ref: dart2
path: sqflite_ffi_test
File renamed without changes.
Loading