-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip add: DatabaseTupleRepository wip #377
- Loading branch information
Showing
7 changed files
with
237 additions
and
6 deletions.
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
integration_test/framework/database_tuple_repository_tests.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mem/databases/table_definitions/base.dart'; | ||
import 'package:mem/framework/database/definition/column/boolean_column_definition.dart'; | ||
import 'package:mem/framework/database/definition/database_definition.dart'; | ||
import 'package:mem/framework/database/definition/table_definition.dart'; | ||
import 'package:mem/framework/database/factory.dart'; | ||
import 'package:mem/framework/repository/condition/conditions.dart'; | ||
import 'package:mem/framework/repository/database_tuple_repository.dart'; | ||
|
||
import '../../test/framework/repository/database_tuple_entity_test.dart'; | ||
import '../../test/framework/repository/entity_test.dart'; | ||
|
||
const _name = "DatabaseTupleRepository tests"; | ||
|
||
final _defColA = BooleanColumnDefinition(TestObjectEntity.fieldNames[0]); | ||
final _defTableTestObject = | ||
TableDefinition('test_object', [_defColA, ...defColsBase]); | ||
final _defDbTest = DatabaseDefinition('test_db', 1, [_defTableTestObject]); | ||
|
||
class _TestObjectRepository extends DatabaseTupleRepository<TestObjectEntity> { | ||
_TestObjectRepository() : super(_defDbTest, _defTableTestObject); | ||
|
||
@override | ||
TestObjectEntity pack(Map<String, dynamic> map) => | ||
TestObjectDatabaseTupleEntity.fromMap(map); | ||
} | ||
|
||
void main() => group( | ||
_name, | ||
() { | ||
late String databasePath; | ||
setUpAll( | ||
() async { | ||
databasePath = | ||
await DatabaseFactory.buildDatabasePath(_defDbTest.name); | ||
|
||
await DatabaseFactory | ||
// ignore: deprecated_member_use_from_same_package | ||
.nativeFactory | ||
.deleteDatabase(databasePath); | ||
}, | ||
); | ||
|
||
test( | ||
'#new', | ||
() async { | ||
_TestObjectRepository(); | ||
|
||
expect( | ||
await DatabaseFactory | ||
// ignore: deprecated_member_use_from_same_package | ||
.nativeFactory | ||
.databaseExists(databasePath), | ||
false); | ||
}, | ||
); | ||
|
||
group( | ||
'operations', | ||
() { | ||
final repository = _TestObjectRepository(); | ||
|
||
group( | ||
'#count', | ||
() { | ||
setUpAll( | ||
() async { | ||
final falseSample = TestObjectEntity(false); | ||
final trueSample = TestObjectEntity(true); | ||
final now = DateTime.now(); | ||
|
||
final dbA = await DatabaseFactory.open(_defDbTest); | ||
|
||
await dbA.delete(_defTableTestObject); | ||
await dbA.insert(_defTableTestObject, | ||
falseSample.toMap..addAll({defColCreatedAt.name: now})); | ||
await dbA.insert(_defTableTestObject, | ||
falseSample.toMap..addAll({defColCreatedAt.name: now})); | ||
await dbA.insert(_defTableTestObject, | ||
trueSample.toMap..addAll({defColCreatedAt.name: now})); | ||
}, | ||
); | ||
|
||
test( | ||
': all.', | ||
() async { | ||
final count = await repository.count(); | ||
|
||
expect(count, 3); | ||
}, | ||
); | ||
test( | ||
': condition.', | ||
() async { | ||
final count = await repository.count( | ||
condition: Equals(_defColA.name, false)); | ||
|
||
expect(count, 2); | ||
}, | ||
); | ||
}, | ||
); | ||
|
||
group( | ||
'#receive', | ||
() { | ||
setUpAll( | ||
() async { | ||
final dbA = await DatabaseFactory.open(_defDbTest); | ||
|
||
await dbA.delete(_defTableTestObject); | ||
}, | ||
); | ||
|
||
test( | ||
': received.', | ||
() async { | ||
final now = DateTime.now(); | ||
final entity = TestObjectEntity(false); | ||
|
||
final received = | ||
await repository.receive(entity, createdAt: now); | ||
|
||
expect( | ||
received, | ||
equals(TestObjectDatabaseTupleEntity(entity.a).withMap( | ||
{defPkId.name: 1, defColCreatedAt.name: now}))); | ||
}, | ||
); | ||
}, | ||
); | ||
}, | ||
); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import 'package:mem/databases/table_definitions/base.dart'; | ||
import 'package:mem/framework/database/accessor.dart'; | ||
import 'package:mem/framework/database/definition/database_definition.dart'; | ||
import 'package:mem/framework/database/definition/table_definition.dart'; | ||
import 'package:mem/framework/repository/condition/conditions.dart'; | ||
import 'package:mem/framework/repository/database_repository.dart'; | ||
import 'package:mem/framework/repository/entity.dart'; | ||
import 'package:mem/framework/repository/repository.dart'; | ||
import 'package:mem/logger/log_service.dart'; | ||
|
||
// FIXME byIdの引数の型のためにSavedEntityの型以外にIが必要になっている | ||
// Rにidの型情報が含まれているのに改めて渡す必要があるのはおかしい | ||
// DatabaseTupleに型情報を付与することでズレは発生しなくなった | ||
// ただ、これだと未保存のDatabaseTupleが | ||
// FIXME SavedEntityはSavedDatabaseTupleをmixinしている必要があるが型制約を定義できていない | ||
abstract class DatabaseTupleRepository<E extends Entity> extends Repository<E> { | ||
final DatabaseDefinition _databaseDefinition; | ||
final TableDefinition _tableDefinition; | ||
|
||
DatabaseTupleRepository(this._databaseDefinition, this._tableDefinition); | ||
|
||
DatabaseAccessor? _databaseAccessor; | ||
|
||
late final Future<DatabaseAccessor> _dbA = (() async => _databaseAccessor ??= | ||
await DatabaseRepository().receive(_databaseDefinition))(); | ||
|
||
Future<int> count({ | ||
Condition? condition, | ||
}) => | ||
v( | ||
() async => (await _dbA).count( | ||
_tableDefinition, | ||
where: condition?.where(), | ||
whereArgs: condition?.whereArgs(), | ||
), | ||
{ | ||
'condition': condition, | ||
}, | ||
); | ||
|
||
E pack(Map<String, dynamic> map); | ||
|
||
Future<E> receive(E entity, {DateTime? createdAt}) => v( | ||
() async { | ||
final entityMap = entity.toMap; | ||
|
||
entityMap[defColCreatedAt.name] = createdAt ?? DateTime.now(); | ||
|
||
final id = await _databaseAccessor!.insert( | ||
_tableDefinition, | ||
entityMap, | ||
); | ||
|
||
entityMap[defPkId.name] = id; | ||
|
||
return pack(entityMap); | ||
}, | ||
{ | ||
'entity': entity, | ||
'createdAt': createdAt, | ||
}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters