2023-11-16 14:38:33 +01:00
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import 'package:release_schedule/api/movie_api.dart';
|
2024-01-09 16:32:53 +01:00
|
|
|
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;
|
|
|
|
|
2024-01-10 23:50:43 +01:00
|
|
|
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(),
|
2024-01-09 21:02:18 +01:00
|
|
|
InMemoryMovieStorage(),
|
2023-11-16 14:38:33 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('addMovies should add movies to the list', () {
|
|
|
|
final movies = [
|
2024-01-10 23:50:43 +01:00
|
|
|
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 = [
|
2024-01-10 23:50:43 +01:00
|
|
|
MovieData()..updateWithNewIgnoringUserControlled(theMatrix),
|
|
|
|
MovieData()..updateWithNewIgnoringUserControlled(theMatrixReloaded),
|
2023-11-16 14:38:33 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
movieManager.addMovies(movies);
|
|
|
|
|
|
|
|
final newMovies = [
|
2024-01-10 23:50:43 +01:00
|
|
|
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);
|
|
|
|
|
2024-01-10 23:50:43 +01:00
|
|
|
expect(movieManager.movies, equals(movies + newMovies));
|
2023-11-16 14:38:33 +01:00
|
|
|
});
|
|
|
|
|
2024-01-09 21:02:18 +01:00
|
|
|
test('addMovies should update existing movies', () {
|
|
|
|
final movies = [
|
2024-01-10 23:50:43 +01:00
|
|
|
MovieData()..updateWithNewIgnoringUserControlled(theMatrix),
|
|
|
|
MovieData()..updateWithNewIgnoringUserControlled(theMatrixReloaded),
|
2024-01-09 21:02:18 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
movieManager.addMovies(movies);
|
|
|
|
|
2024-01-10 23:50:43 +01:00
|
|
|
final updatedMovie = MovieData()
|
|
|
|
..setNewDetails(
|
2024-01-09 21:02:18 +01:00
|
|
|
bookmarked: true,
|
|
|
|
genres: ['Action', 'Adventure'],
|
2024-01-10 23:50:43 +01:00
|
|
|
labels: [(text: 'The Matrix Reloaded', language: 'en')],
|
|
|
|
releaseDates: [
|
|
|
|
DateWithPrecisionAndCountry(
|
|
|
|
DateTime(2003, 5, 7), DatePrecision.day, 'USA')
|
|
|
|
],
|
2024-01-09 21:02:18 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
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 = [
|
2024-01-10 23:50:43 +01:00
|
|
|
MovieData()..updateWithNewIgnoringUserControlled(theMatrixReloaded),
|
|
|
|
MovieData()..updateWithNewIgnoringUserControlled(theMatrix),
|
2023-11-16 14:38:33 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
movieManager.addMovies(movies);
|
|
|
|
|
2024-01-10 23:50:43 +01:00
|
|
|
expect(movieManager.movies, equals(movies.reversed.toList()));
|
2023-11-16 14:38:33 +01:00
|
|
|
});
|
|
|
|
|
2024-01-09 21:02:18 +01:00
|
|
|
test(
|
|
|
|
'addMovies should sort movies that have a less precise release date before movies with more precise release dates',
|
|
|
|
() {
|
|
|
|
final movies = [
|
2024-01-10 23:50:43 +01:00
|
|
|
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')
|
|
|
|
],
|
|
|
|
),
|
2024-01-09 21:02:18 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
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 = [
|
2024-01-10 23:50:43 +01:00
|
|
|
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;
|
2024-01-10 23:50:43 +01:00
|
|
|
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));
|
|
|
|
|
2024-01-10 23:50:43 +01:00
|
|
|
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 = [
|
2024-01-10 23:50:43 +01:00
|
|
|
MovieData()..updateWithNewIgnoringUserControlled(theMatrix),
|
|
|
|
MovieData()..updateWithNewIgnoringUserControlled(theMatrixReloaded),
|
2023-11-16 14:38:33 +01:00
|
|
|
];
|
2024-01-10 23:50:43 +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
|
|
|
|
2024-01-10 23:50:43 +01:00
|
|
|
movieManager.addMovies(movies + [notRemoved]);
|
2023-11-16 14:38:33 +01:00
|
|
|
|
2024-01-10 23:50:43 +01:00
|
|
|
movieManager.removeMoviesWhere(
|
|
|
|
(movie) => movie.title?.contains('Matrix') == true);
|
2023-11-16 14:38:33 +01:00
|
|
|
|
|
|
|
expect(movieManager.movies, equals([notRemoved]));
|
|
|
|
});
|
2024-01-09 21:02:18 +01:00
|
|
|
|
|
|
|
test("localSearch", () {
|
|
|
|
final movies = [
|
2024-01-10 23:50:43 +01:00
|
|
|
MovieData()..updateWithNewIgnoringUserControlled(theMatrix),
|
|
|
|
MovieData()..updateWithNewIgnoringUserControlled(theMatrixReloaded),
|
2024-01-09 21:02:18 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|