Skip to content

Commit

Permalink
wip add: DatabaseTupleRepository
Browse files Browse the repository at this point in the history
wip #377
  • Loading branch information
zin- committed Aug 6, 2024
1 parent 078c371 commit fc991c9
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 6 deletions.
85 changes: 85 additions & 0 deletions integration_test/framework/database_tuple_repository_tests.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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/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();

test(
'#count',
() async {
final count = await repository.count();

expect(count, 0);
},
);

test(
'#receive',
() async {
final entity = TestObjectEntity(false);

final received = await repository.receive(entity);

expect(
received, equals(TestObjectDatabaseTupleEntity(entity.a)));
},
);
},
);
},
);
3 changes: 3 additions & 0 deletions integration_test/framework/framework_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'database_factory_tests.dart' as database_factory_tests;
import 'database_repository_tests.dart' as database_repository_tests;
import 'database_tuple_repository_tests_v1.dart'
as database_tuple_repository_tests_v1;
import 'database_tuple_repository_tests.dart'
as database_tuple_repository_tests;

const _name = "Framework test";

Expand All @@ -23,5 +25,6 @@ void main() => group(
database_repository_tests.main();

database_tuple_repository_tests_v1.main();
database_tuple_repository_tests.main();

Check failure on line 28 in integration_test/framework/framework_test.dart

View workflow job for this annotation

GitHub Actions / Report (Medium test (Windows) (framework))

integration_test/framework/framework_test.dart ► Framework test DatabaseTupleRepository tests operations ► #receive

Failed test found in: reports/test_report.log Error: LateInitializationError: Field 'id' has not been initialized.
Raw output
[D] TIME: 2024-08-06T00:59:33.361950 DatabaseTupleRepository.receive [start] :: {
  entity: Instance of 'TestObjectEntity': {a: false},
}
[D] TIME: 2024-08-06T00:59:33.361950 DatabaseAccessor.insert ** [AUTO DEBUG] ** [start] :: [
  test_object,
  {
    a: false,
    createdAt: 2024-08-06 00:59:33.361950,
  },
]
[D] TIME: 2024-08-06T00:59:33.364940 DatabaseAccessor.insert ** [AUTO DEBUG] ** [end] => [future] >> 1
[D] TIME: 2024-08-06T00:59:33.365948 DatabaseTupleRepository.receive [end] => [future] >> Instance of 'TestObjectDatabaseTupleEntity': {a: false, id: 1, createdAt: 2024-08-06 00:59:33.361950, updatedAt: null, archivedAt: null}
package:mem/framework/repository/database_tuple_entity.dart                                                     DatabaseTupleEntity.id
package:mem/framework/repository/database_tuple_entity.dart 55:21                                               DatabaseTupleEntity.toMap
package:mem/framework/repository/entity.dart 15:47                                                              Entity.toString
package:matcher                                                                                                 expect
package:flutter_test/src/widget_tester.dart 474:18                                                              expect
integration_test\framework\database_tuple_repository_tests.dart 78:17                                           main.<fn>.<fn>.<fn>
===== asynchronous gap ===========================
package:stream_channel                                                                                          _GuaranteeSink.add
c:/Users/RUNNER~1/AppData/Local/Temp/flutter_tools.e9667d55/flutter_test_listener.a1e8d3f4/listener.dart 53:22  main.<fn>
},
);
62 changes: 62 additions & 0 deletions lib/framework/repository/database_tuple_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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) => d(
() async {
final entityMap = entity.toMap;

entityMap[defColCreatedAt.name] = DateTime.now();

final id = await _databaseAccessor!.insert(
_tableDefinition,
entityMap,
);

entityMap[defPkId.name] = id;

return pack(entityMap);
},
{
'entity': entity,
},
);
}
3 changes: 3 additions & 0 deletions lib/framework/repository/entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ abstract class EntityV1 {}

mixin Entity {
Map<String, dynamic> get toMap;

@override
String toString() => "${super.toString()}: $toMap";
}
// memo
// - view, domain, dataのそれぞれの領域で似た内容でも型が変わることになるはず
Expand Down
2 changes: 2 additions & 0 deletions lib/framework/repository/repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ mixin Discarder<E extends EntityV2> on RepositoryV2<E> {
abstract class RepositoryV1<E extends EntityV1, Result> {
Future<Result> receive(E entity);
}

abstract class Repository<E extends Entity> {}
12 changes: 6 additions & 6 deletions test/framework/repository/database_tuple_entity_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import 'entity_test.dart';

const _name = 'DatabaseTupleEntity test';

class _TestObjectDatabaseTupleEntity extends TestObjectEntity
class TestObjectDatabaseTupleEntity extends TestObjectEntity
with DatabaseTupleEntity<int> {
_TestObjectDatabaseTupleEntity(super.a);
TestObjectDatabaseTupleEntity(super.a);

_TestObjectDatabaseTupleEntity.fromMap(Map<String, dynamic> map)
TestObjectDatabaseTupleEntity.fromMap(Map<String, dynamic> map)
: super.fromMap(map) {
withMap(map);
}
Expand All @@ -24,7 +24,7 @@ void main() => group(
() {
const a = false;

final testObject = _TestObjectDatabaseTupleEntity(a);
final testObject = TestObjectDatabaseTupleEntity(a);

expect(testObject.a, equals(a));
},
Expand All @@ -41,7 +41,7 @@ void main() => group(
defColArchivedAt.name: null
};

final testObject = _TestObjectDatabaseTupleEntity.fromMap(map);
final testObject = TestObjectDatabaseTupleEntity.fromMap(map);

expect(testObject.a, map[TestObjectEntity.fieldNames[0]]);
},
Expand All @@ -58,7 +58,7 @@ void main() => group(
defColArchivedAt.name: null
};

final testObject = _TestObjectDatabaseTupleEntity.fromMap(map);
final testObject = TestObjectDatabaseTupleEntity.fromMap(map);

expect(testObject.toMap, map);
},
Expand Down

0 comments on commit fc991c9

Please sign in to comment.