Skip to content

Commit

Permalink
add: Entity, DatabaseTupleEntity
Browse files Browse the repository at this point in the history
wip #377
  • Loading branch information
zin- committed Aug 5, 2024
1 parent c747f69 commit 7d0cadb
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/framework/repository/database_tuple_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,27 @@ mixin SavedDatabaseTupleMixinV1<T> on EntityV1 {
@override
String toString() => "${super.toString()}${unpack()}";
}

mixin DatabaseTupleEntity<PrimaryKey> on Entity {
late PrimaryKey id;
late DateTime createdAt;
late DateTime? updatedAt;
late DateTime? archivedAt;

DatabaseTupleEntity<PrimaryKey> withMap(Map<String, dynamic> map) {
id = map[defPkId.name];
createdAt = map[defColCreatedAt.name];
updatedAt = map[defColUpdatedAt.name];
archivedAt = map[defColArchivedAt.name];
return this;
}

@override
Map<String, dynamic> get toMap => super.toMap
..addAll({
defPkId.name: id,
defColCreatedAt.name: createdAt,
defColUpdatedAt.name: updatedAt,
defColArchivedAt.name: archivedAt,
});
}
4 changes: 4 additions & 0 deletions lib/framework/repository/entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
abstract class EntityV2 {}

abstract class EntityV1 {}

mixin Entity {
Map<String, dynamic> get toMap;
}
// memo
// - view, domain, dataのそれぞれの領域で似た内容でも型が変わることになるはず
// これをしっかりと定義したい
Expand Down
67 changes: 67 additions & 0 deletions test/framework/repository/database_tuple_entity_test.dart
Original file line number Diff line number Diff line change
@@ -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<int> {
_TestObjectDatabaseTupleEntity(super.a);

_TestObjectDatabaseTupleEntity.fromMap(Map<String, dynamic> 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);
},
);
},
);
61 changes: 61 additions & 0 deletions test/framework/repository/entity_test.dart
Original file line number Diff line number Diff line change
@@ -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<String> fieldNames = ['a'];

TestObjectEntity(super.a);

TestObjectEntity.fromMap(Map<String, dynamic> map)
: super(map[TestObjectEntity.fieldNames[0]]);

@override
Map<String, dynamic> 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}));
},
);
},
);

0 comments on commit 7d0cadb

Please sign in to comment.