2023-11-16 14:38:33 +01:00
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import 'package:release_schedule/model/movie.dart';
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
group('MovieData', () {
|
|
|
|
test('updateWithNew() updates all fields', () {
|
2023-11-17 15:37:08 +01:00
|
|
|
final movie1 = MovieData(
|
|
|
|
'Title 1',
|
|
|
|
DateWithPrecisionAndCountry(
|
|
|
|
DateTime(2023, 1, 1), DatePrecision.day, 'US'));
|
|
|
|
final movie2 = MovieData(
|
|
|
|
'Title 2',
|
|
|
|
DateWithPrecisionAndCountry(
|
|
|
|
DateTime(2023, 1, 1), DatePrecision.day, 'UK'));
|
2023-11-16 14:38:33 +01:00
|
|
|
movie2.setDetails(releaseDates: [
|
2023-11-17 15:37:08 +01:00
|
|
|
DateWithPrecisionAndCountry(
|
|
|
|
DateTime(2023, 1, 1), DatePrecision.day, 'US')
|
2023-11-16 14:38:33 +01:00
|
|
|
], genres: [
|
|
|
|
'Action',
|
|
|
|
'Adventure'
|
|
|
|
], titles: [
|
|
|
|
(title: 'Title 2', language: 'en')
|
|
|
|
], reviews: [
|
2023-11-17 15:37:08 +01:00
|
|
|
Review('8.5', 'John Doe', DateTime(2023, 1, 1), 100)
|
2023-11-16 14:38:33 +01:00
|
|
|
]);
|
|
|
|
movie1.updateWithNew(movie2);
|
|
|
|
expect(movie1.title, equals('Title 2'));
|
|
|
|
expect(movie1.releaseDate.country, equals('UK'));
|
|
|
|
expect(movie1.releaseDates!.length, equals(1));
|
|
|
|
expect(movie1.releaseDates![0].country, equals('US'));
|
|
|
|
expect(movie1.genres!.length, equals(2));
|
|
|
|
expect(movie1.genres![0], equals('Action'));
|
|
|
|
expect(movie1.genres![1], equals('Adventure'));
|
|
|
|
expect(movie1.titles!.length, equals(1));
|
|
|
|
expect(movie1.titles![0].title, equals('Title 2'));
|
|
|
|
expect(movie1.titles![0].language, equals('en'));
|
|
|
|
expect(movie1.reviews!.length, equals(1));
|
|
|
|
expect(movie1.reviews![0].score, equals('8.5'));
|
|
|
|
expect(movie1.reviews![0].by, equals('John Doe'));
|
|
|
|
expect(movie1.reviews![0].count, equals(100));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('same() returns true for same title and release date', () {
|
|
|
|
final movie1 = MovieData(
|
|
|
|
'Title 1',
|
|
|
|
DateWithPrecisionAndCountry(
|
|
|
|
DateTime(2023, 1, 1), DatePrecision.day, 'US'));
|
|
|
|
final movie2 = MovieData(
|
|
|
|
'Title 1',
|
|
|
|
DateWithPrecisionAndCountry(
|
|
|
|
DateTime(2023, 1, 1), DatePrecision.day, 'US'));
|
|
|
|
expect(movie1.same(movie2), isTrue);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('same() returns false for different title', () {
|
|
|
|
final movie1 = MovieData(
|
|
|
|
'Title 1',
|
|
|
|
DateWithPrecisionAndCountry(
|
|
|
|
DateTime(2023, 1, 1), DatePrecision.day, 'US'));
|
|
|
|
final movie2 = MovieData(
|
|
|
|
'Title 2',
|
|
|
|
DateWithPrecisionAndCountry(
|
|
|
|
DateTime(2023, 1, 1), DatePrecision.day, 'US'));
|
|
|
|
expect(movie1.same(movie2), isFalse);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('same() returns false for different release date', () {
|
|
|
|
final movie1 = MovieData(
|
|
|
|
'Title 1',
|
|
|
|
DateWithPrecisionAndCountry(
|
|
|
|
DateTime(2023, 1, 1), DatePrecision.day, 'US'));
|
|
|
|
final movie2 = MovieData(
|
|
|
|
'Title 1',
|
|
|
|
DateWithPrecisionAndCountry(
|
|
|
|
DateTime(2023, 1, 2), DatePrecision.day, 'US'));
|
|
|
|
expect(movie1.same(movie2), isFalse);
|
|
|
|
});
|
|
|
|
test('can be encoded to JSON and back', () {
|
2023-11-17 15:37:08 +01:00
|
|
|
final movie = MovieData(
|
|
|
|
'Title 1',
|
|
|
|
DateWithPrecisionAndCountry(
|
|
|
|
DateTime(2023, 1, 1), DatePrecision.day, 'US'));
|
2023-11-16 14:38:33 +01:00
|
|
|
movie.setDetails(releaseDates: [
|
2023-11-17 15:37:08 +01:00
|
|
|
DateWithPrecisionAndCountry(
|
|
|
|
DateTime(2023, 1, 1), DatePrecision.day, 'US')
|
2023-11-16 14:38:33 +01:00
|
|
|
], genres: [
|
|
|
|
'Action',
|
|
|
|
'Adventure'
|
|
|
|
], titles: [
|
|
|
|
(title: 'Title 2', language: 'en')
|
|
|
|
], reviews: [
|
2023-11-17 15:37:08 +01:00
|
|
|
Review('8.5', 'John Doe', DateTime(2023, 1, 1), 100)
|
2023-11-16 14:38:33 +01:00
|
|
|
]);
|
|
|
|
final json = movie.toJsonEncodable();
|
|
|
|
final movie2 = MovieData.fromJsonEncodable(json);
|
|
|
|
expect(movie2.title, equals('Title 1'));
|
|
|
|
expect(movie2.releaseDate.country, equals('US'));
|
|
|
|
expect(movie2.releaseDates!.length, equals(1));
|
|
|
|
expect(movie2.releaseDates![0].country, equals('US'));
|
|
|
|
expect(movie2.genres!.length, equals(2));
|
|
|
|
expect(movie2.genres![0], equals('Action'));
|
|
|
|
expect(movie2.genres![1], equals('Adventure'));
|
|
|
|
expect(movie2.titles!.length, equals(1));
|
|
|
|
expect(movie2.titles![0].title, equals('Title 2'));
|
|
|
|
expect(movie2.titles![0].language, equals('en'));
|
|
|
|
expect(movie2.reviews!.length, equals(1));
|
|
|
|
expect(movie2.reviews![0].score, equals('8.5'));
|
|
|
|
expect(movie2.reviews![0].by, equals('John Doe'));
|
|
|
|
expect(movie2.reviews![0].count, equals(100));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('toString()', () {
|
2023-11-17 15:37:08 +01:00
|
|
|
final movie = MovieData(
|
|
|
|
'Title 1',
|
|
|
|
DateWithPrecisionAndCountry(
|
|
|
|
DateTime(2023, 1, 1), DatePrecision.day, 'US'));
|
2023-11-16 14:38:33 +01:00
|
|
|
movie.setDetails(releaseDates: [
|
2023-11-17 15:37:08 +01:00
|
|
|
DateWithPrecisionAndCountry(
|
|
|
|
DateTime(2023, 1, 1), DatePrecision.day, 'US')
|
2023-11-16 14:38:33 +01:00
|
|
|
], genres: [
|
|
|
|
'Action',
|
|
|
|
'Adventure'
|
|
|
|
], titles: [
|
|
|
|
(title: 'Title 2', language: 'en')
|
|
|
|
], reviews: [
|
2023-11-17 15:37:08 +01:00
|
|
|
Review('8.5', 'John Doe', DateTime(2023, 1, 1), 100)
|
2023-11-16 14:38:33 +01:00
|
|
|
]);
|
|
|
|
expect(movie.toString(),
|
2023-11-17 15:37:08 +01:00
|
|
|
equals('Title 1 (January 1, 2023 (US); Action, Adventure)'));
|
2023-11-16 14:38:33 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group('DateWithPrecisionAndCountry', () {
|
|
|
|
test('can be encoded to JSON and back', () {
|
2023-11-17 15:37:08 +01:00
|
|
|
final date = DateWithPrecisionAndCountry(
|
|
|
|
DateTime(2023, 1, 1), DatePrecision.day, 'US');
|
2023-11-16 14:38:33 +01:00
|
|
|
final json = date.toJsonEncodable();
|
|
|
|
final date2 = DateWithPrecisionAndCountry.fromJsonEncodable(json);
|
|
|
|
expect(date2.date, equals(date.date));
|
|
|
|
expect(date2.precision, equals(date.precision));
|
|
|
|
expect(date2.country, equals(date.country));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('toString()', () {
|
|
|
|
final date = DateWithPrecisionAndCountry(
|
|
|
|
DateTime(2023, 1, 1), DatePrecision.day, 'US');
|
|
|
|
expect(date.toString(), equals('January 1, 2023 (US)'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('toString() with month precision', () {
|
|
|
|
final date = DateWithPrecisionAndCountry(
|
|
|
|
DateTime(2023, 1, 1), DatePrecision.month, 'US');
|
|
|
|
expect(date.toString(), equals('January 2023 (US)'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('toString() with year precision', () {
|
|
|
|
final date = DateWithPrecisionAndCountry(
|
|
|
|
DateTime(2023, 1, 1), DatePrecision.year, 'US');
|
|
|
|
expect(date.toString(), equals('2023 (US)'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('toString() with decade precision', () {
|
|
|
|
final date = DateWithPrecisionAndCountry(
|
|
|
|
DateTime(2023, 1, 1), DatePrecision.decade, 'US');
|
|
|
|
expect(date.toString(), equals('2020s (US)'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|