Skip to content

Commit

Permalink
Merge b0bdb5b into 02c0be1
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 committed Jan 10, 2023
2 parents 02c0be1 + b0bdb5b commit fa4c912
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 29 deletions.
10 changes: 4 additions & 6 deletions lib/src/model/booking/recurring_booking.dart
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,10 @@ class RecurringBooking extends Booking {
cabin: cabin ?? this.cabin,
periodicity: periodicity ?? this.periodicity,
repeatEvery: repeatEvery ?? this.repeatEvery,
recurringEndDate: recurringEndDate != null && occurrences == null
? recurringEndDate
: _recurringEndDate,
occurrences: occurrences != null && recurringEndDate == null
? occurrences
: _occurrences,
recurringEndDate: recurringEndDate ??
(occurrences != null ? null : _recurringEndDate),
occurrences:
occurrences ?? (recurringEndDate != null ? null : _occurrences),
);

@override
Expand Down
7 changes: 6 additions & 1 deletion lib/src/model/cabin/cabin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,18 @@ class Cabin extends Item {

@override
Cabin copyWith({
String? id,
int? number,
CabinElements? elements,
Set<SingleBooking>? bookings,
Set<RecurringBooking>? recurringBookings,
}) =>
Cabin(
id: id,
id: id ?? this.id,
number: number ?? this.number,
elements: elements ?? this.elements,
bookings: bookings ?? this.bookings,
recurringBookings: recurringBookings ?? this.recurringBookings,
);

@override
Expand Down
79 changes: 79 additions & 0 deletions test/model/booking/recurring_booking_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,84 @@ void main() {
},
);
});

group('.copyWith()', () {
test('should return a new copy of this RecurringBooking', () {
final booking = RecurringBooking(
startDate: DateTime.utc(2022, 12, 4, 9),
endDate: DateTime.utc(2022, 12, 4, 10, 30),
description: 'Booked slot',
isLocked: true,
periodicity: Periodicity.daily,
repeatEvery: 2,
occurrences: 4,
);
expect(booking, booking.copyWith());
expect(identical(booking, booking.copyWith()), isFalse);
expect(identical(booking.copyWith(), booking.copyWith()), isFalse);
});

test(
'should return a new copy of this RecurringBooking with overridden '
'properties (recurringEndDate to occurrences)',
() {
final booking = RecurringBooking(
startDate: DateTime(2022, 12, 4, 9),
endDate: DateTime(2022, 12, 4, 10, 30),
description: 'Booked slot',
recurringEndDate: DateTime(2022, 12, 31),
);
final copiedBooking = booking.copyWith(
id: 'copied-booking',
startDate: DateTime.utc(2023, 1, 1, 9),
endDate: DateTime.utc(2023, 1, 1, 10),
description: 'John Appleseed',
isLocked: true,
periodicity: Periodicity.daily,
repeatEvery: 3,
occurrences: 6,
);
expect(copiedBooking.id, 'copied-booking');
expect(copiedBooking.startDate, DateTime.utc(2023, 1, 1, 9));
expect(copiedBooking.endDate, DateTime.utc(2023, 1, 1, 10));
expect(copiedBooking.description, 'John Appleseed');
expect(copiedBooking.isLocked, isTrue);
expect(copiedBooking.periodicity, Periodicity.daily);
expect(copiedBooking.repeatEvery, 3);
expect(copiedBooking.occurrences, 6);
},
);

test(
'should return a new copy of this RecurringBooking with overridden '
'properties (occurrences to recurringEndDate)',
() {
final booking = RecurringBooking(
startDate: DateTime(2022, 12, 4, 9),
endDate: DateTime(2022, 12, 4, 10, 30),
description: 'Booked slot',
occurrences: 6,
);
final copiedBooking = booking.copyWith(
id: 'copied-booking',
startDate: DateTime.utc(2023, 1, 1, 9),
endDate: DateTime.utc(2023, 1, 1, 10),
description: 'John Appleseed',
isLocked: true,
periodicity: Periodicity.daily,
repeatEvery: 3,
recurringEndDate: DateTime.utc(2022, 12, 31),
);
expect(copiedBooking.id, 'copied-booking');
expect(copiedBooking.startDate, DateTime.utc(2023, 1, 1, 9));
expect(copiedBooking.endDate, DateTime.utc(2023, 1, 1, 10));
expect(copiedBooking.description, 'John Appleseed');
expect(copiedBooking.isLocked, isTrue);
expect(copiedBooking.periodicity, Periodicity.daily);
expect(copiedBooking.repeatEvery, 3);
expect(copiedBooking.recurringEndDate, DateTime.utc(2022, 12, 31));
},
);
});
});
}
39 changes: 39 additions & 0 deletions test/model/booking/single_booking_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,45 @@ void main() {
);
});

group('.copyWith()', () {
test('should return a new copy of this SingleBooking', () {
final booking = SingleBooking(
startDate: DateTime(2022, 12, 4, 9),
endDate: DateTime(2022, 12, 4, 10, 30),
description: 'Booked slot',
isLocked: true,
);
expect(booking, booking.copyWith());
expect(identical(booking, booking.copyWith()), isFalse);
expect(identical(booking.copyWith(), booking.copyWith()), isFalse);
});

test(
'should return a new copy of this SingleBooking with overridden '
'properties',
() {
final booking = SingleBooking(
startDate: DateTime.utc(2022, 12, 4, 9),
endDate: DateTime.utc(2022, 12, 4, 10, 30),
description: 'Booked slot',
isLocked: true,
);
final copiedBooking = booking.copyWith(
id: 'copied-booking',
startDate: DateTime(2023, 1, 1, 9),
endDate: DateTime(2023, 1, 1, 10),
description: 'John Appleseed',
isLocked: false,
);
expect(copiedBooking.id, 'copied-booking');
expect(copiedBooking.startDate, DateTime(2023, 1, 1, 9));
expect(copiedBooking.endDate, DateTime(2023, 1, 1, 10));
expect(copiedBooking.description, 'John Appleseed');
expect(copiedBooking.isLocked, isFalse);
},
);
});

group('.toString()', () {
test('should return a string representation of a SingleBooking', () {
final booking = SingleBooking(
Expand Down
74 changes: 71 additions & 3 deletions test/model/cabin/cabin_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ void main() {
'b': <Map<String, dynamic>>[],
'rb': <Map<String, dynamic>>[],
};
const pianos = [Piano(brand: 'Yamaha', model: 'C5')];
final cabin = Cabin.fromJson(rawCabin);
expect(
cabin,
Cabin(
id: 'cabin-id',
number: 1,
elements: CabinElements(
pianos: pianos,
pianos: const [Piano(brand: 'Yamaha', model: 'C5')],
lecterns: 2,
tables: 1,
),
Expand All @@ -45,7 +44,7 @@ void main() {
test(
'should return a JSON object representation of this Cabin',
() {
final rawCabin = {
const rawCabin = {
'id': 'cabin-id',
'cd': '1969-07-20T20:18:04.000Z',
'md': '1969-07-20T20:18:04.000Z',
Expand All @@ -66,5 +65,74 @@ void main() {
},
);
});

group('.copyWith()', () {
test('should return a new copy of this Cabin', () {
final cabin = Cabin(
id: 'cabin-id',
number: 1,
elements: CabinElements(
pianos: const [Piano(brand: 'Yamaha', model: 'C5')],
lecterns: 2,
tables: 1,
),
bookings: {
SingleBooking(id: 'booking-id'),
},
recurringBookings: {
RecurringBooking(id: 'recurring-booking-id', occurrences: 1),
},
);
expect(cabin, cabin.copyWith());
expect(identical(cabin, cabin.copyWith()), isFalse);
expect(identical(cabin.copyWith(), cabin.copyWith()), isFalse);
});

test(
'should return a new copy of this Cabin with overridden '
'properties',
() {
final cabin = Cabin(
id: 'cabin-id',
number: 1,
elements: CabinElements(
pianos: const [Piano(brand: 'Yamaha', model: 'C5')],
lecterns: 2,
tables: 1,
),
bookings: {
SingleBooking(id: 'booking-id'),
},
recurringBookings: {
RecurringBooking(id: 'recurring-booking-id', occurrences: 1),
},
);
final newCabinElements = CabinElements(
pianos: const [Piano(brand: 'Bösendorfer', model: 'Imperial')],
lecterns: 1,
tables: 3,
);
final newBookings = {
SingleBooking(id: 'booking-id-1'),
SingleBooking(id: 'booking-id-2'),
};
final newRecurringBookings = {
RecurringBooking(id: 'recurring-booking-id-1', occurrences: 2),
};
final copiedCabin = cabin.copyWith(
id: 'copied-cabin',
number: 2,
elements: newCabinElements,
bookings: newBookings,
recurringBookings: newRecurringBookings,
);
expect(copiedCabin.id, 'copied-cabin');
expect(copiedCabin.number, 2);
expect(copiedCabin.elements, newCabinElements);
expect(copiedCabin.bookings, newBookings);
expect(copiedCabin.recurringBookings, newRecurringBookings);
},
);
});
});
}
5 changes: 2 additions & 3 deletions test/model/date/date_range_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ void main() {
test('should create a copy of this DateRange', () {
final dateRange = DateRange.today();
expect(dateRange, dateRange.copyWith());

const infiniteDateRange = DateRange.infinite;
expect(infiniteDateRange, infiniteDateRange.copyWith());
expect(identical(dateRange, dateRange.copyWith()), isFalse);
expect(DateRange.infinite, DateRange.infinite.copyWith());
});

test('should create a copy of this DateRange replacing old values', () {
Expand Down
Loading

0 comments on commit fa4c912

Please sign in to comment.