-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDepartmentRepositoryAsyncTests.cs
More file actions
72 lines (57 loc) · 2.72 KB
/
Copy pathDepartmentRepositoryAsyncTests.cs
File metadata and controls
72 lines (57 loc) · 2.72 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
namespace TalentManagementAPI.Infrastructure.Tests.Repositories
{
public class DepartmentRepositoryAsyncTests : IDisposable
{
private readonly ApplicationDbContext _context;
private readonly DepartmentRepositoryAsync _repository;
public DepartmentRepositoryAsyncTests()
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;
var dateTime = new DateTimeService();
var loggerFactory = LoggerFactory.Create(builder => { });
_context = new ApplicationDbContext(options, dateTime, loggerFactory);
_context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.TrackAll;
var dataShaper = new DataShapeHelper<Department>();
_repository = new DepartmentRepositoryAsync(_context, dataShaper);
}
[Fact]
public async Task GetDepartmentResponseAsync_ShouldReturnShapedData()
{
_context.Departments.Add(new Department { Id = Guid.NewGuid(), Name = new DepartmentName("Engineering") });
await _context.SaveChangesAsync();
var query = new GetDepartmentsQuery { Fields = "Id,Name", PageNumber = 1, PageSize = 5 };
var (data, count) = await _repository.GetDepartmentResponseAsync(query);
data.Should().HaveCount(1);
data.First()["Name"].Should().BeOfType<DepartmentName>().Which.Value.Should().Be("Engineering");
count.RecordsFiltered.Should().Be(1);
count.RecordsTotal.Should().Be(1);
}
[Fact]
public async Task UpdateAsync_ShouldPersistChanges()
{
var department = new Department { Id = Guid.NewGuid(), Name = new DepartmentName("Ops") };
_context.Departments.Add(department);
await _context.SaveChangesAsync();
department.Name = new DepartmentName("Operations");
await _repository.UpdateAsync(department);
var updated = await _context.Departments.FindAsync(department.Id);
updated!.Name.Value.Should().Be("Operations");
}
[Fact]
public async Task DeleteAsync_ShouldRemoveDepartment()
{
var department = new Department { Id = Guid.NewGuid(), Name = new DepartmentName("Temp") };
_context.Departments.Add(department);
await _context.SaveChangesAsync();
await _repository.DeleteAsync(department);
(await _context.Departments.FindAsync(department.Id)).Should().BeNull();
}
public void Dispose()
{
_context.Database.EnsureDeleted();
_context.Dispose();
}
}
}