Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test case for change rest duration #1129

Merged
merged 3 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class ChangeRestDuration {
PillSheetModifiedHistoryServiceActionFactory.createChangedRestDurationBeginDateAction(
pillSheetGroupID: pillSheetGroup.id,
before: fromRestDurationPillSheet,
after: toRestDurationPillSheet,
after: updatedToRestDurationPillSheet,
beforeRestDuration: fromRestDuration,
afterRestDuration: toRestDuration,
beforePillSheetGroup: pillSheetGroup,
Expand All @@ -269,7 +269,7 @@ class ChangeRestDuration {
PillSheetModifiedHistoryServiceActionFactory.createChangedRestDurationAction(
pillSheetGroupID: pillSheetGroup.id,
before: fromRestDurationPillSheet,
after: toRestDurationPillSheet,
after: updatedToRestDurationPillSheet,
beforeRestDuration: fromRestDuration,
afterRestDuration: toRestDuration,
beforePillSheetGroup: pillSheetGroup,
Expand Down
168 changes: 168 additions & 0 deletions test/features/record/components/supports/button/provider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,172 @@ void main() {
verify(batchSetPillSheetModifiedHistory(batch, history)).called(1);
});
});

group("#changeRestDurationBeginDate", () {
test("group has only one pill sheet", () async {
var mockTodayRepository = MockTodayService();
final mockToday = DateTime.parse("2020-09-19");
todayRepository = mockTodayRepository;
when(mockTodayRepository.now()).thenReturn(mockToday);

final mockIDGenerator = MockFirestoreIDGenerator();
when(mockIDGenerator.call()).thenReturn("rest_duration_id");
firestoreIDGenerator = mockIDGenerator;
final beforeRestDuration = RestDuration(
id: "rest_duration_id",
beginDate: now(),
createdDate: now(),
endDate: null,
);
final afterRestDuration = RestDuration(
id: "rest_duration_id",
beginDate: now().subtract(const Duration(days: 1)),
createdDate: now(),
endDate: null,
);

final batchFactory = MockBatchFactory();
final batch = MockWriteBatch();
when(batchFactory.batch()).thenReturn(batch);

final pillSheet = PillSheet(
id: "pill_sheet_id_1",
typeInfo: PillSheetType.pillsheet_28_0.typeInfo,
lastTakenDate: null,
beginingDate: now(),
createdAt: now(),
restDurations: [beforeRestDuration],
);
final updatedPillSheet = pillSheet.copyWith(
restDurations: [afterRestDuration],
);

final pillSheetGroup = PillSheetGroup(
id: "group_id",
pillSheetIDs: ["pill_sheet_id_1"].toList(),
pillSheets: [pillSheet],
createdAt: now(),
);
final updatedPillSheetGroup = PillSheetGroup(
id: "group_id",
pillSheetIDs: ["pill_sheet_id_1"].toList(),
pillSheets: [updatedPillSheet],
createdAt: now(),
);
final batchSetPillSheetGroup = MockBatchSetPillSheetGroup();
when(batchSetPillSheetGroup(batch, updatedPillSheetGroup)).thenReturn(updatedPillSheetGroup.copyWith(id: "group_id"));

final history = PillSheetModifiedHistoryServiceActionFactory.createChangedRestDurationBeginDateAction(
pillSheetGroupID: "group_id",
before: pillSheet,
after: updatedPillSheet,
beforeRestDuration: beforeRestDuration,
afterRestDuration: afterRestDuration,
beforePillSheetGroup: pillSheetGroup,
afterPillSheetGroup: updatedPillSheetGroup,
);
final batchSetPillSheetModifiedHistory = MockBatchSetPillSheetModifiedHistory();
when(batchSetPillSheetModifiedHistory(batch, history)).thenReturn(null);

final beginRestDuration = ChangeRestDuration(
actionType: PillSheetModifiedActionType.changedRestDurationBeginDate,
batchFactory: batchFactory,
batchSetPillSheetGroup: batchSetPillSheetGroup,
batchSetPillSheetModifiedHistory: batchSetPillSheetModifiedHistory);
await beginRestDuration.call(
fromRestDuration: beforeRestDuration,
toRestDuration: afterRestDuration,
pillSheetGroup: pillSheetGroup,
);

verify(batchFactory.batch()).called(1);

verify(batchSetPillSheetGroup(batch, updatedPillSheetGroup)).called(1);
verify(batchSetPillSheetModifiedHistory(batch, history)).called(1);
});
});

group("#changeRestDuration", () {
test("group has only one pill sheet", () async {
var mockTodayRepository = MockTodayService();
final mockToday = DateTime.parse("2020-09-19");
todayRepository = mockTodayRepository;
when(mockTodayRepository.now()).thenReturn(mockToday);

final mockIDGenerator = MockFirestoreIDGenerator();
when(mockIDGenerator.call()).thenReturn("rest_duration_id");
firestoreIDGenerator = mockIDGenerator;
final beforeRestDuration = RestDuration(
id: "rest_duration_id",
beginDate: now(),
createdDate: now(),
endDate: now(),
);
final afterRestDuration = RestDuration(
id: "rest_duration_id",
beginDate: now().subtract(const Duration(days: 2)),
createdDate: now(),
endDate: now().subtract(const Duration(days: 1)),
);

final batchFactory = MockBatchFactory();
final batch = MockWriteBatch();
when(batchFactory.batch()).thenReturn(batch);

final pillSheet = PillSheet(
id: "pill_sheet_id_1",
typeInfo: PillSheetType.pillsheet_28_0.typeInfo,
lastTakenDate: null,
beginingDate: now(),
createdAt: now(),
restDurations: [beforeRestDuration],
);
final updatedPillSheet = pillSheet.copyWith(
restDurations: [afterRestDuration],
);

final pillSheetGroup = PillSheetGroup(
id: "group_id",
pillSheetIDs: ["pill_sheet_id_1"].toList(),
pillSheets: [pillSheet],
createdAt: now(),
);
final updatedPillSheetGroup = PillSheetGroup(
id: "group_id",
pillSheetIDs: ["pill_sheet_id_1"].toList(),
pillSheets: [updatedPillSheet],
createdAt: now(),
);
final batchSetPillSheetGroup = MockBatchSetPillSheetGroup();
when(batchSetPillSheetGroup(batch, updatedPillSheetGroup)).thenReturn(updatedPillSheetGroup.copyWith(id: "group_id"));

final history = PillSheetModifiedHistoryServiceActionFactory.createChangedRestDurationAction(
pillSheetGroupID: "group_id",
before: pillSheet,
after: updatedPillSheet,
beforeRestDuration: beforeRestDuration,
afterRestDuration: afterRestDuration,
beforePillSheetGroup: pillSheetGroup,
afterPillSheetGroup: updatedPillSheetGroup,
);
final batchSetPillSheetModifiedHistory = MockBatchSetPillSheetModifiedHistory();
when(batchSetPillSheetModifiedHistory(batch, history)).thenReturn(null);

final beginRestDuration = ChangeRestDuration(
actionType: PillSheetModifiedActionType.changedRestDuration,
batchFactory: batchFactory,
batchSetPillSheetGroup: batchSetPillSheetGroup,
batchSetPillSheetModifiedHistory: batchSetPillSheetModifiedHistory);
await beginRestDuration.call(
fromRestDuration: beforeRestDuration,
toRestDuration: afterRestDuration,
pillSheetGroup: pillSheetGroup,
);

verify(batchFactory.batch()).called(1);

verify(batchSetPillSheetGroup(batch, updatedPillSheetGroup)).called(1);
verify(batchSetPillSheetModifiedHistory(batch, history)).called(1);
});
});
}
Loading