-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEfMovieRepositoryTests.cs
More file actions
186 lines (159 loc) · 7.79 KB
/
Copy pathEfMovieRepositoryTests.cs
File metadata and controls
186 lines (159 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using Microsoft.EntityFrameworkCore;
using SmartMovieCatalog.Domain.Movies;
using SmartMovieCatalog.Infrastructure.Persistence;
namespace SmartMovieCatalog.Infrastructure.Tests.Persistence;
public sealed class EfMovieRepositoryTests
{
[Fact]
public async Task AddAsync_SavesMovieWithGenres()
{
await using SmartMovieCatalogDbContext dbContext = CreateDbContext();
EfMovieRepository repository = new(dbContext);
Genre genre = Genre.Create(GenreId.New(), " Drama ", 18);
Movie movie = Movie.Create(
MovieId.New(),
" Central do Brasil ",
" Central Station ",
1998,
" br ",
" pt-BR ",
[genre],
" Walter Salles ",
" Synopsis ",
110,
" 12 ",
666,
"/p/example-card.jpg",
new DateTimeOffset(2026, 5, 4, 12, 0, 0, TimeSpan.Zero));
await repository.AddAsync(movie, CancellationToken.None);
await repository.SaveChangesAsync(CancellationToken.None);
Movie persistedMovie = await dbContext.Movies
.Include(savedMovie => savedMovie.Genres)
.SingleAsync(savedMovie => savedMovie.Id == movie.Id);
Assert.Equal("Central do Brasil", persistedMovie.Title);
Assert.Equal("BR", persistedMovie.CountryCode);
Assert.Equal(666, persistedMovie.ExternalId);
Assert.Equal("/p/example-card.jpg", persistedMovie.Image);
Assert.Contains(persistedMovie.Genres, movieGenre => movieGenre.Genre.Name == "Drama");
Assert.Single(dbContext.Genres);
Assert.Single(dbContext.Set<MovieGenre>());
}
[Fact]
public async Task AddAsync_SavesTwoMoviesWithSharedGenre()
{
await using SmartMovieCatalogDbContext dbContext = CreateDbContext();
EfMovieRepository repository = new(dbContext);
Genre genre = Genre.Create(GenreId.New(), "Drama", externalId: null);
Movie firstMovie = CreateMovie("Central do Brasil", genre);
Movie secondMovie = CreateMovie("Ainda Estou Aqui", genre);
await repository.AddAsync(firstMovie, CancellationToken.None);
await repository.AddAsync(secondMovie, CancellationToken.None);
await repository.SaveChangesAsync(CancellationToken.None);
Assert.Single(dbContext.Genres);
Assert.Equal(2, await dbContext.Set<MovieGenre>().CountAsync());
}
[Fact]
public async Task ListAsync_WithQueryAndPaging_ReturnsMatchingMovies()
{
await using SmartMovieCatalogDbContext dbContext = CreateDbContext();
EfMovieRepository repository = new(dbContext);
Genre genre = Genre.Create(GenreId.New(), "Drama", externalId: null);
Movie firstMovie = CreateMovie("Central do Brasil", genre);
Movie secondMovie = CreateMovie("Ainda Estou Aqui", genre);
await repository.AddAsync(firstMovie, CancellationToken.None);
await repository.AddAsync(secondMovie, CancellationToken.None);
await repository.SaveChangesAsync(CancellationToken.None);
SmartMovieCatalog.Application.Abstractions.Persistence.PagedResult<Movie> result =
await repository.ListAsync("central", 1, 12, CancellationToken.None);
Movie movie = Assert.Single(result.Items);
Assert.Equal(firstMovie.Id, movie.Id);
Assert.Equal(1, result.TotalCount);
Assert.Equal(1, result.TotalPages);
Assert.Contains(movie.Genres, movieGenre => movieGenre.Genre.Name == "Drama");
}
[Fact]
public async Task ListAsync_WithQueryMatchingOriginalTitle_ReturnsMatchingMovies()
{
await using SmartMovieCatalogDbContext dbContext = CreateDbContext();
EfMovieRepository repository = new(dbContext);
Genre genre = Genre.Create(GenreId.New(), "Drama", externalId: null);
Movie movieWithOriginalTitleMatch = CreateMovie("Central do Brasil", genre, originalTitle: "Central Station");
Movie movieWithoutMatch = CreateMovie("Cidade de Deus", genre, originalTitle: "Cidade de Deus");
await repository.AddAsync(movieWithOriginalTitleMatch, CancellationToken.None);
await repository.AddAsync(movieWithoutMatch, CancellationToken.None);
await repository.SaveChangesAsync(CancellationToken.None);
SmartMovieCatalog.Application.Abstractions.Persistence.PagedResult<Movie> result =
await repository.ListAsync("station", 1, 12, CancellationToken.None);
Movie movie = Assert.Single(result.Items);
Assert.Equal(movieWithOriginalTitleMatch.Id, movie.Id);
Assert.Equal(1, result.TotalCount);
}
[Fact]
public async Task ListAsync_WithCaseInsensitiveQuery_ReturnsMatchingMovies()
{
await using SmartMovieCatalogDbContext dbContext = CreateDbContext();
EfMovieRepository repository = new(dbContext);
Genre genre = Genre.Create(GenreId.New(), "Drama", externalId: null);
Movie movie = CreateMovie("Central do Brasil", genre);
await repository.AddAsync(movie, CancellationToken.None);
await repository.SaveChangesAsync(CancellationToken.None);
SmartMovieCatalog.Application.Abstractions.Persistence.PagedResult<Movie> result =
await repository.ListAsync("CeNtRaL", 1, 12, CancellationToken.None);
Assert.Single(result.Items);
Assert.Equal(movie.Id, result.Items.Single().Id);
}
[Fact]
public async Task GetByIdAsync_WithExistingMovie_ReturnsMovieWithGenres()
{
await using SmartMovieCatalogDbContext dbContext = CreateDbContext();
EfMovieRepository repository = new(dbContext);
Genre genre = Genre.Create(GenreId.New(), "Drama", externalId: null);
Movie movie = CreateMovie("Central do Brasil", genre);
await repository.AddAsync(movie, CancellationToken.None);
await repository.SaveChangesAsync(CancellationToken.None);
Movie? result = await repository.GetByIdAsync(movie.Id, CancellationToken.None);
Assert.NotNull(result);
Assert.Equal(movie.Id, result.Id);
Assert.Contains(result.Genres, movieGenre => movieGenre.Genre.Name == "Drama");
}
[Fact]
public void Model_MapsGenreExternalIdAsUniqueAndMovieGenresAsAssociation()
{
using SmartMovieCatalogDbContext dbContext = CreateDbContext();
Microsoft.EntityFrameworkCore.Metadata.IEntityType genreEntity = dbContext.Model.FindEntityType(typeof(Genre))!;
Assert.Contains(
genreEntity.GetIndexes(),
index => index.IsUnique &&
index.Properties.Count == 1 &&
index.Properties.Single().Name == nameof(Genre.ExternalId));
Microsoft.EntityFrameworkCore.Metadata.IEntityType movieGenreEntity = dbContext.Model.FindEntityType(typeof(MovieGenre))!;
Assert.Null(movieGenreEntity.FindProperty("Name"));
Assert.NotNull(movieGenreEntity.FindProperty(nameof(MovieGenre.MovieId)));
Assert.NotNull(movieGenreEntity.FindProperty(nameof(MovieGenre.GenreId)));
}
private static Movie CreateMovie(string title, Genre genre, string? originalTitle = null)
{
return Movie.Create(
MovieId.New(),
title,
originalTitle: originalTitle,
2024,
"BR",
"pt-BR",
[genre],
director: null,
synopsis: null,
durationMinutes: null,
ageRating: null,
externalId: null,
image: null,
new DateTimeOffset(2026, 5, 4, 12, 0, 0, TimeSpan.Zero));
}
private static SmartMovieCatalogDbContext CreateDbContext()
{
DbContextOptions<SmartMovieCatalogDbContext> options = new DbContextOptionsBuilder<SmartMovieCatalogDbContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;
return new SmartMovieCatalogDbContext(options);
}
}