release_schedule/test/model/movie_manager_test.dart

203 lines
6.3 KiB
Dart
Raw Normal View History

2023-11-16 14:38:33 +01:00
import 'package:flutter_test/flutter_test.dart';
import 'package:release_schedule/api/movie_api.dart';
import 'package:release_schedule/model/dates.dart';
2023-11-16 14:38:33 +01:00
import 'package:release_schedule/model/local_movie_storage.dart';
import 'package:release_schedule/model/movie.dart';
import 'package:release_schedule/model/movie_manager.dart';
void main() {
group('MovieManager', () {
late MovieManager movieManager;
final theMatrix = MovieData()
..setNewDetails(
labels: [(text: 'The Matrix', language: 'en')],
releaseDates: [
DateWithPrecisionAndCountry(
DateTime(1999, 3, 31), DatePrecision.day, 'USA')
],
);
final theMatrixReloaded = MovieData()
..setNewDetails(
labels: [(text: 'The Matrix Reloaded', language: 'en')],
releaseDates: [
DateWithPrecisionAndCountry(
DateTime(2003, 5, 7), DatePrecision.day, 'USA')
],
);
2023-11-16 14:38:33 +01:00
setUp(() {
movieManager = MovieManager(
MovieApi(),
InMemoryMovieStorage(),
2023-11-16 14:38:33 +01:00
);
});
test('addMovies should add movies to the list', () {
final movies = [
MovieData()..updateWithNewIgnoringUserControlled(theMatrix),
MovieData()..updateWithNewIgnoringUserControlled(theMatrixReloaded),
2023-11-16 14:38:33 +01:00
];
movieManager.addMovies(movies);
expect(movieManager.movies, equals(movies));
});
test('addMovies should add new movies', () {
final movies = [
MovieData()..updateWithNewIgnoringUserControlled(theMatrix),
MovieData()..updateWithNewIgnoringUserControlled(theMatrixReloaded),
2023-11-16 14:38:33 +01:00
];
movieManager.addMovies(movies);
final newMovies = [
MovieData()
..setNewDetails(
labels: [(text: 'The Matrix Revolutions', language: 'en')],
releaseDates: [
DateWithPrecisionAndCountry(
DateTime(2003, 11, 5), DatePrecision.day, 'USA')
],
),
2023-11-16 14:38:33 +01:00
];
movieManager.addMovies(newMovies);
expect(movieManager.movies, equals(movies + newMovies));
2023-11-16 14:38:33 +01:00
});
test('addMovies should update existing movies', () {
final movies = [
MovieData()..updateWithNewIgnoringUserControlled(theMatrix),
MovieData()..updateWithNewIgnoringUserControlled(theMatrixReloaded),
];
movieManager.addMovies(movies);
final updatedMovie = MovieData()
..setNewDetails(
bookmarked: true,
genres: ['Action', 'Adventure'],
labels: [(text: 'The Matrix Reloaded', language: 'en')],
releaseDates: [
DateWithPrecisionAndCountry(
DateTime(2003, 5, 7), DatePrecision.day, 'USA')
],
);
movieManager.addMovies([updatedMovie]);
expect(movieManager.movies[1].genres, equals(updatedMovie.genres));
expect(movieManager.movies[1].bookmarked, equals(false));
});
test('addMovies should sort movies by their release dates', () {
2023-11-16 14:38:33 +01:00
final movies = [
MovieData()..updateWithNewIgnoringUserControlled(theMatrixReloaded),
MovieData()..updateWithNewIgnoringUserControlled(theMatrix),
2023-11-16 14:38:33 +01:00
];
movieManager.addMovies(movies);
expect(movieManager.movies, equals(movies.reversed.toList()));
2023-11-16 14:38:33 +01:00
});
test(
'addMovies should sort movies that have a less precise release date before movies with more precise release dates',
() {
final movies = [
MovieData()
..updateWithNewIgnoringUserControlled(theMatrixReloaded)
..setNewDetails(
releaseDates: [
DateWithPrecisionAndCountry(
DateTime(2003, 5, 7), DatePrecision.day, 'USA')
],
),
MovieData()
..updateWithNewIgnoringUserControlled(theMatrix)
..setNewDetails(
releaseDates: [
DateWithPrecisionAndCountry(
DateTime(2003, 5, 7), DatePrecision.month, 'USA')
],
),
];
movieManager.addMovies(movies);
expect(movieManager.movies, equals([...movies.reversed]));
});
2023-11-16 14:38:33 +01:00
test(
'when a movie is modified and it\'s date is changed the movies should be resorted',
() async {
final movies = [
MovieData()
..updateWithNewIgnoringUserControlled(theMatrixReloaded)
..setNewDetails(
releaseDates: [
DateWithPrecisionAndCountry(
DateTime(1998, 5, 7), DatePrecision.day, 'USA')
],
),
MovieData()..updateWithNewIgnoringUserControlled(theMatrix),
2023-11-16 14:38:33 +01:00
];
movieManager.addMovies(movies);
final movie = movieManager.movies.first;
movie.setNewDetails(
releaseDates: [
DateWithPrecisionAndCountry(DateTime(2003, 5, 7), DatePrecision.day,
'United States of America')
],
2023-11-16 14:38:33 +01:00
);
await Future.delayed(const Duration(milliseconds: 100));
expect(movieManager.movies, equals(movies.reversed.toList()));
2023-11-16 14:38:33 +01:00
});
test('removeMoviesWhere should remove movies from the list', () {
final movies = [
MovieData()..updateWithNewIgnoringUserControlled(theMatrix),
MovieData()..updateWithNewIgnoringUserControlled(theMatrixReloaded),
2023-11-16 14:38:33 +01:00
];
MovieData notRemoved = MovieData()
..setNewDetails(
labels: [
(text: 'Harry Potter and the Philosopher\'s Stone', language: 'en')
],
releaseDates: [
DateWithPrecisionAndCountry(
DateTime(2001, 11, 4), DatePrecision.day, 'UK')
],
);
2023-11-16 14:38:33 +01:00
movieManager.addMovies(movies + [notRemoved]);
2023-11-16 14:38:33 +01:00
movieManager.removeMoviesWhere(
(movie) => movie.title?.contains('Matrix') == true);
2023-11-16 14:38:33 +01:00
expect(movieManager.movies, equals([notRemoved]));
});
test("localSearch", () {
final movies = [
MovieData()..updateWithNewIgnoringUserControlled(theMatrix),
MovieData()..updateWithNewIgnoringUserControlled(theMatrixReloaded),
];
movieManager.addMovies(movies);
expect(movieManager.localSearch('Matrix'), equals(movies));
expect(movieManager.localSearch('Matrix Re'),
equals(movies.reversed.toList()));
expect(movieManager.localSearch('Matrix Reloaded'), equals([movies[1]]));
expect(movieManager.localSearch('Matrix Revolutions'), equals([]));
});
2023-11-16 14:38:33 +01:00
});
}