Skip to content

Commit

Permalink
add: replace
Browse files Browse the repository at this point in the history
wip add: DatabaseTupleRepository

wip #377
  • Loading branch information
zin- committed Aug 16, 2024
1 parent da69f31 commit 753e771
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
42 changes: 42 additions & 0 deletions integration_test/framework/database_tuple_repository_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,48 @@ void main() => group(
);
},
);

group(
'#replace',
() {
late TestObjectDatabaseTupleEntity savedFalseSample;
setUpAll(
() async {
final falseSample = TestObjectEntity(false);
final now = DateTime.now();

final dbA = await DatabaseFactory.open(_defDbTest);

await dbA.delete(_defTableTestObject);
savedFalseSample =
TestObjectDatabaseTupleEntity.fromMap(falseSample.toMap
..addAll({
defPkId.name: await dbA.insert(
_defTableTestObject,
falseSample.toMap
..addAll({defColCreatedAt.name: now})),
defColCreatedAt.name: now
}));
},
);

test(
': updated.',
() async {
final updatedAt = DateTime.now();
final updating = savedFalseSample.copiedWith(
a: () => true,
);

final updated = await repository.replace(updating,
updatedAt: updatedAt);

expect(updated.a, equals(updating.a));
expect(updated.updatedAt, equals(updatedAt));
},
);
},
);
},
);
},
Expand Down
21 changes: 21 additions & 0 deletions lib/framework/repository/database_tuple_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,25 @@ abstract class DatabaseTupleRepository<E extends Entity,
'limit': limit,
},
);

Future<Saved> replace(Saved savedEntity, {DateTime? updatedAt}) => v(
() async {
final entityMap = savedEntity.toMap;

entityMap[defColUpdatedAt.name] = updatedAt ?? DateTime.now();

final byId = Equals(defPkId.name, entityMap[defPkId.name]);
await (await _dbA).update(
_tableDefinition,
entityMap,
where: byId.where(),
whereArgs: byId.whereArgs(),
);

return pack(entityMap);
},
{
'savedEntity': savedEntity,
},
);
}
2 changes: 2 additions & 0 deletions lib/framework/repository/entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ abstract class EntityV1 {}
mixin Entity {
Map<String, dynamic> get toMap;

Entity copiedWith();

@override
String toString() => "${super.toString()}: $toMap";

Expand Down
26 changes: 26 additions & 0 deletions test/framework/repository/database_tuple_entity_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class TestObjectDatabaseTupleEntity extends TestObjectEntity
: super.fromMap(map) {
withMap(map);
}

@override
TestObjectDatabaseTupleEntity copiedWith({bool Function()? a}) =>
TestObjectDatabaseTupleEntity.fromMap(
toMap..addAll(super.copiedWith(a: a).toMap));
}

void main() => group(
Expand Down Expand Up @@ -63,5 +68,26 @@ void main() => group(
expect(testObject.toMap, map);
},
);

test(
'#copiedWith',
() {
final from = TestObjectDatabaseTupleEntity.fromMap({
TestObjectEntity.fieldNames[0]: false,
defPkId.name: 1,
defColCreatedAt.name: DateTime.now(),
defColUpdatedAt.name: DateTime.now(),
defColArchivedAt.name: DateTime.now()
});

final copied = from.copiedWith(a: () => true);

expect(copied.a, equals(true));
expect(copied.id, equals(from.id));
expect(copied.createdAt, equals(from.createdAt));
expect(copied.updatedAt, equals(from.updatedAt));
expect(copied.archivedAt, equals(from.archivedAt));
},
);
},
);
15 changes: 15 additions & 0 deletions test/framework/repository/entity_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class TestObjectEntity extends _TestObject with Entity {

@override
Map<String, dynamic> get toMap => {fieldNames[0]: a};

@override
TestObjectEntity copiedWith({bool Function()? a}) =>
TestObjectEntity(a == null ? this.a : a());
}

void main() => group(
Expand Down Expand Up @@ -70,5 +74,16 @@ void main() => group(
expect(testObjectA, equals(testObjectB));
},
);

test(
'#copiedWith',
() {
final from = TestObjectEntity(false);

final copied = from.copiedWith(a: () => true);

expect(copied.a, equals(true));
},
);
},
);

0 comments on commit 753e771

Please sign in to comment.