-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPositionRepositoryAsyncTests.cs
More file actions
124 lines (105 loc) · 4.57 KB
/
PositionRepositoryAsyncTests.cs
File metadata and controls
124 lines (105 loc) · 4.57 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
namespace TalentManagementAPI.Infrastructure.Tests.Repositories
{
public class PositionRepositoryAsyncTests : IDisposable
{
private readonly ApplicationDbContext _context;
private readonly PositionRepositoryAsync _repository;
public PositionRepositoryAsyncTests()
{
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<Position>();
var mockService = new Mock<IMockService>();
_repository = new PositionRepositoryAsync(_context, dataShaper, mockService.Object);
}
[Fact]
public async Task IsUniquePositionNumberAsync_ShouldDetectExistingNumber()
{
var position = new Position
{
Id = Guid.NewGuid(),
PositionNumber = "ENG-01",
PositionTitle = new PositionTitle("Engineer"),
PositionDescription = "Builds things",
DepartmentId = Guid.NewGuid(),
SalaryRangeId = Guid.NewGuid()
};
_context.Positions.Add(position);
await _context.SaveChangesAsync();
(await _repository.IsUniquePositionNumberAsync("ENG-99")).Should().BeTrue();
(await _repository.IsUniquePositionNumberAsync("ENG-01")).Should().BeFalse();
}
[Fact]
public async Task GetPositionReponseAsync_ShouldShapeDataAndReturnRecordCounts()
{
_context.Positions.Add(new Position
{
Id = Guid.NewGuid(),
PositionNumber = "QA-01",
PositionTitle = new PositionTitle("QA Engineer"),
PositionDescription = "Tests things",
DepartmentId = Guid.NewGuid(),
SalaryRangeId = Guid.NewGuid(),
Department = new Department { Id = Guid.NewGuid(), Name = new DepartmentName("Engineering") },
SalaryRange = new SalaryRange { Id = Guid.NewGuid(), MinSalary = 1, MaxSalary = 2 }
});
await _context.SaveChangesAsync();
var query = new GetPositionsQuery
{
Fields = "Id,PositionTitle",
PageNumber = 1,
PageSize = 10
};
var (data, count) = await _repository.GetPositionReponseAsync(query);
data.Should().HaveCount(1);
data.First()["PositionTitle"].Should().BeOfType<PositionTitle>().Which.Value.Should().Be("QA Engineer");
count.Should().BeEquivalentTo(new RecordsCount { RecordsFiltered = 1, RecordsTotal = 1 });
}
[Fact]
public async Task UpdateAsync_ShouldPersistChanges()
{
var position = new Position
{
Id = Guid.NewGuid(),
PositionNumber = "ENG-02",
PositionTitle = new PositionTitle("Engineer"),
PositionDescription = "Builds",
DepartmentId = Guid.NewGuid(),
SalaryRangeId = Guid.NewGuid()
};
_context.Positions.Add(position);
await _context.SaveChangesAsync();
position.PositionTitle = new PositionTitle("Senior Engineer");
await _repository.UpdateAsync(position);
var updated = await _context.Positions.FindAsync(position.Id);
updated!.PositionTitle.Value.Should().Be("Senior Engineer");
}
[Fact]
public async Task DeleteAsync_ShouldRemoveEntity()
{
var position = new Position
{
Id = Guid.NewGuid(),
PositionNumber = "ENG-03",
PositionTitle = new PositionTitle("Engineer"),
PositionDescription = "Builds",
DepartmentId = Guid.NewGuid(),
SalaryRangeId = Guid.NewGuid()
};
_context.Positions.Add(position);
await _context.SaveChangesAsync();
await _repository.DeleteAsync(position);
(await _context.Positions.FindAsync(position.Id)).Should().BeNull();
}
public void Dispose()
{
_context.Database.EnsureDeleted();
_context.Dispose();
}
}
}