Skip to content

Commit

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

wip #377
  • Loading branch information
zin- committed Aug 15, 2024
1 parent 02363c0 commit f2721f4
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 11 deletions.
98 changes: 96 additions & 2 deletions integration_test/framework/database_tuple_repository_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import 'package:mem/framework/database/definition/column/boolean_column_definiti
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 'package:mem/framework/repository/extra_column.dart';
import 'package:mem/framework/repository/group_by.dart';
import 'package:mem/framework/repository/order_by.dart';

import '../../test/framework/repository/database_tuple_entity_test.dart';
import '../../test/framework/repository/entity_test.dart';
Expand All @@ -16,11 +20,12 @@ final _defTableTestObject =
TableDefinition('test_object', [_defColA, ...defColsBase]);
final _defDbTest = DatabaseDefinition('test_db', 1, [_defTableTestObject]);

class _TestObjectRepository extends DatabaseTupleRepository<TestObjectEntity> {
class _TestObjectRepository extends DatabaseTupleRepository<TestObjectEntity,
TestObjectDatabaseTupleEntity> {
_TestObjectRepository() : super(_defDbTest, _defTableTestObject);

@override
TestObjectEntity pack(Map<String, dynamic> map) =>
TestObjectDatabaseTupleEntity pack(Map<String, dynamic> map) =>
TestObjectDatabaseTupleEntity.fromMap(map);
}

Expand Down Expand Up @@ -83,6 +88,95 @@ void main() => group(
{defPkId.name: 1, defColCreatedAt.name: now})));
},
);

group(
'#ship',
() {
final falseSample = TestObjectEntity(false);
final trueSample = TestObjectEntity(true);
final now = DateTime.now();
final later = DateTime.now();

late int sampleId1;
late int sampleId2;
late int sampleId3;
setUpAll(
() async {
final dbA = await DatabaseFactory.open(_defDbTest);

sampleId1 = await dbA.insert(_defTableTestObject,
falseSample.toMap..addAll({defColCreatedAt.name: now}));
sampleId2 = await dbA.insert(_defTableTestObject,
trueSample.toMap..addAll({defColCreatedAt.name: now}));
sampleId3 = await dbA.insert(
_defTableTestObject,
falseSample.toMap
..addAll({defColCreatedAt.name: later}));
},
);

test(
': all.',
() async {
final shipped = await repository.ship();

expect(shipped, hasLength(3));
expect(shipped[0].id, equals(sampleId1));
expect(shipped[0].createdAt, equals(now));
},
);
test(
': condition.',
() async {
final shipped = await repository.ship(
condition: Equals(_defColA.name, false),
);

expect(shipped, hasLength(2));
},
);
test(
': groupBy.',
() async {
final shipped = await repository.ship(
groupBy: GroupBy([_defColA],
extraColumns: [Max(defColCreatedAt)]));

expect(shipped, hasLength(2));
expect(shipped[0].id, equals(sampleId3));
expect(shipped[0].createdAt, equals(later));
},
);
test(
': orderBy.',
() async {
final shipped =
await repository.ship(orderBy: [Descending(defPkId)]);

expect(shipped, hasLength(3));
expect(shipped[0].id, sampleId3);
},
);
test(
': offset.',
() async {
final shipped = await repository.ship(offset: 1);

expect(shipped, hasLength(2));
expect(shipped[0].id, sampleId2);
},
);
test(
': limit.',
() async {
final shipped = await repository.ship(limit: 2);

expect(shipped, hasLength(2));
expect(shipped[1].id, sampleId2);
},
);
},
);
},
);
},
Expand Down
49 changes: 40 additions & 9 deletions lib/framework/repository/database_tuple_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ 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/database_tuple_entity.dart';
import 'package:mem/framework/repository/entity.dart';
import 'package:mem/framework/repository/group_by.dart';
import 'package:mem/framework/repository/order_by.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> {
abstract class DatabaseTupleRepository<E extends Entity,
Saved extends DatabaseTupleEntity> extends Repository<E> {
final DatabaseDefinition _databaseDefinition;
final TableDefinition _tableDefinition;

// FIXME DatabaseDefinitionの中にTableDefinitionがあるのでEから取得できるのでは?
DatabaseTupleRepository(this._databaseDefinition, this._tableDefinition);

DatabaseAccessor? _databaseAccessor;
Expand All @@ -38,15 +38,15 @@ abstract class DatabaseTupleRepository<E extends Entity> extends Repository<E> {
},
);

E pack(Map<String, dynamic> map);
Saved pack(Map<String, dynamic> map);

Future<E> receive(E entity, {DateTime? createdAt}) => v(
Future<Saved> receive(E entity, {DateTime? createdAt}) => v(
() async {
final entityMap = entity.toMap;

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

final id = await _databaseAccessor!.insert(
final id = await (await _dbA).insert(
_tableDefinition,
entityMap,
);
Expand All @@ -60,4 +60,35 @@ abstract class DatabaseTupleRepository<E extends Entity> extends Repository<E> {
'createdAt': createdAt,
},
);

Future<List<Saved>> ship({
Condition? condition,
GroupBy? groupBy,
List<OrderBy>? orderBy,
int? offset,
int? limit,
}) =>
v(
() async => (await (await _dbA).select(
_tableDefinition,
groupBy: groupBy?.toQuery,
extraColumns: groupBy?.toExtraColumns,
where: condition?.where(),
whereArgs: condition?.whereArgs(),
orderBy: orderBy?.isEmpty != false
? null
: orderBy?.map((e) => e.toQuery()).join(", "),
offset: offset,
limit: limit,
))
.map((e) => pack(e))
.toList(),
{
'condition': condition,
'groupBy': groupBy,
'orderBy': orderBy,
'offset': offset,
'limit': limit,
},
);
}

0 comments on commit f2721f4

Please sign in to comment.