From 7d0cadbfc4b9fe3c71b8c9d459839f3987d673a7 Mon Sep 17 00:00:00 2001 From: zin- Date: Tue, 6 Aug 2024 00:20:49 +0900 Subject: [PATCH] add: Entity, DatabaseTupleEntity wip #377 --- .../repository/database_tuple_entity.dart | 24 +++++++ lib/framework/repository/entity.dart | 4 ++ .../database_tuple_entity_test.dart | 67 +++++++++++++++++++ test/framework/repository/entity_test.dart | 61 +++++++++++++++++ 4 files changed, 156 insertions(+) create mode 100644 test/framework/repository/database_tuple_entity_test.dart create mode 100644 test/framework/repository/entity_test.dart diff --git a/lib/framework/repository/database_tuple_entity.dart b/lib/framework/repository/database_tuple_entity.dart index b38d4ef7c..5d924921a 100644 --- a/lib/framework/repository/database_tuple_entity.dart +++ b/lib/framework/repository/database_tuple_entity.dart @@ -34,3 +34,27 @@ mixin SavedDatabaseTupleMixinV1 on EntityV1 { @override String toString() => "${super.toString()}${unpack()}"; } + +mixin DatabaseTupleEntity on Entity { + late PrimaryKey id; + late DateTime createdAt; + late DateTime? updatedAt; + late DateTime? archivedAt; + + DatabaseTupleEntity withMap(Map map) { + id = map[defPkId.name]; + createdAt = map[defColCreatedAt.name]; + updatedAt = map[defColUpdatedAt.name]; + archivedAt = map[defColArchivedAt.name]; + return this; + } + + @override + Map get toMap => super.toMap + ..addAll({ + defPkId.name: id, + defColCreatedAt.name: createdAt, + defColUpdatedAt.name: updatedAt, + defColArchivedAt.name: archivedAt, + }); +} diff --git a/lib/framework/repository/entity.dart b/lib/framework/repository/entity.dart index fa2312f2a..57c2e1f0c 100644 --- a/lib/framework/repository/entity.dart +++ b/lib/framework/repository/entity.dart @@ -7,6 +7,10 @@ abstract class EntityV2 {} abstract class EntityV1 {} + +mixin Entity { + Map get toMap; +} // memo // - view, domain, dataのそれぞれの領域で似た内容でも型が変わることになるはず // これをしっかりと定義したい diff --git a/test/framework/repository/database_tuple_entity_test.dart b/test/framework/repository/database_tuple_entity_test.dart new file mode 100644 index 000000000..c8e1a7b41 --- /dev/null +++ b/test/framework/repository/database_tuple_entity_test.dart @@ -0,0 +1,67 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:mem/databases/table_definitions/base.dart'; +import 'package:mem/framework/repository/database_tuple_entity.dart'; + +import 'entity_test.dart'; + +const _name = 'DatabaseTupleEntity test'; + +class _TestObjectDatabaseTupleEntity extends TestObjectEntity + with DatabaseTupleEntity { + _TestObjectDatabaseTupleEntity(super.a); + + _TestObjectDatabaseTupleEntity.fromMap(Map map) + : super.fromMap(map) { + withMap(map); + } +} + +void main() => group( + _name, + () { + test( + '#new', + () { + const a = false; + + final testObject = _TestObjectDatabaseTupleEntity(a); + + expect(testObject.a, equals(a)); + }, + ); + + test( + '#fromMap', + () { + final map = { + TestObjectEntity.fieldNames[0]: false, + defPkId.name: 1, + defColCreatedAt.name: DateTime.now(), + defColUpdatedAt.name: DateTime.now(), + defColArchivedAt.name: null + }; + + final testObject = _TestObjectDatabaseTupleEntity.fromMap(map); + + expect(testObject.a, map[TestObjectEntity.fieldNames[0]]); + }, + ); + + test( + '#toMap', + () { + final map = { + TestObjectEntity.fieldNames[0]: false, + defPkId.name: 1, + defColCreatedAt.name: DateTime.now(), + defColUpdatedAt.name: DateTime.now(), + defColArchivedAt.name: null + }; + + final testObject = _TestObjectDatabaseTupleEntity.fromMap(map); + + expect(testObject.toMap, map); + }, + ); + }, + ); diff --git a/test/framework/repository/entity_test.dart b/test/framework/repository/entity_test.dart new file mode 100644 index 000000000..1c798a0b0 --- /dev/null +++ b/test/framework/repository/entity_test.dart @@ -0,0 +1,61 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:mem/framework/repository/entity.dart'; + +const _name = 'Entity test'; + +class _TestObject { + final bool a; + + _TestObject(this.a); +} + +class TestObjectEntity extends _TestObject with Entity { + static List fieldNames = ['a']; + + TestObjectEntity(super.a); + + TestObjectEntity.fromMap(Map map) + : super(map[TestObjectEntity.fieldNames[0]]); + + @override + Map get toMap => {fieldNames[0]: a}; +} + +void main() => group( + _name, + () { + test( + '#new', + () { + const a = false; + + final testObject = TestObjectEntity(a); + + expect(testObject.a, equals(a)); + }, + ); + + test( + '#fromMap', + () { + final map = {TestObjectEntity.fieldNames[0]: false}; + + final testObject = TestObjectEntity.fromMap(map); + + expect(testObject.a, equals(map[TestObjectEntity.fieldNames[0]])); + }, + ); + + test( + '#toMap', + () { + const a = false; + + final testObject = TestObjectEntity(a); + + expect( + testObject.toMap, equals({TestObjectEntity.fieldNames[0]: a})); + }, + ); + }, + );