-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathAdminEventRepositoryTests.cs
185 lines (146 loc) · 6.46 KB
/
AdminEventRepositoryTests.cs
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
using Azure;
using Azure.Data.Tables;
using AzureOpenAIProxy.ApiApp.Configurations;
using AzureOpenAIProxy.ApiApp.Models;
using AzureOpenAIProxy.ApiApp.Repositories;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
namespace AzureOpenAIProxy.ApiApp.Tests.Repositories;
public class AdminEventRepositoryTests
{
[Fact]
public void Given_ServiceCollection_When_AddAdminEventRepository_Invoked_Then_It_Should_Contain_AdminEventRepository()
{
// Arrange
var services = new ServiceCollection();
// Act
services.AddAdminEventRepository();
// Assert
services.SingleOrDefault(p => p.ServiceType == typeof(IAdminEventRepository)).Should().NotBeNull();
}
[Fact]
public void Given_Null_TableServiceClient_When_Creating_AdminEventRepository_Then_It_Should_Throw_Exception()
{
// Arrange
var settings = Substitute.For<StorageAccountSettings>();
var tableServiceClient = default(TableServiceClient);
// Act
Action action = () => new AdminEventRepository(tableServiceClient, settings);
// Assert
action.Should().Throw<ArgumentNullException>();
}
[Fact]
public void Given_Null_StorageAccountSettings_When_Creating_AdminEventRepository_Then_It_Should_Throw_Exception()
{
// Arrange
var settings = default(StorageAccountSettings);
var tableServiceClient = Substitute.For<TableServiceClient>();
// Act
Action action = () => new AdminEventRepository(tableServiceClient, settings);
// Assert
action.Should().Throw<ArgumentNullException>();
}
[Fact]
public async Task Given_Instance_When_CreateEvent_Invoked_Then_It_Should_Return_Same_Instance()
{
// Arrange
var settings = Substitute.For<StorageAccountSettings>();
var tableServiceClient = Substitute.For<TableServiceClient>();
var eventDetails = new AdminEventDetails();
var repository = new AdminEventRepository(tableServiceClient, settings);
// Act
var result = await repository.CreateEvent(eventDetails);
// Assert
result.Should().BeSameAs(eventDetails);
}
[Fact]
public async Task Given_Failure_In_Add_Entity_When_CreateEvent_Invoked_Then_It_Should_Throw_Exception()
{
// Arrange
var settings = Substitute.For<StorageAccountSettings>();
var tableServiceClient = Substitute.For<TableServiceClient>();
var repository = new AdminEventRepository(tableServiceClient, settings);
var eventDetails = new AdminEventDetails();
var exception = new InvalidOperationException("Invalid Operation Error : check duplicate, null or empty values");
var tableClient = Substitute.For<TableClient>();
tableServiceClient.GetTableClient(Arg.Any<string>()).Returns(tableClient);
tableClient.AddEntityAsync<AdminEventDetails>(Arg.Any<AdminEventDetails>())
.ThrowsAsync(exception);
// Act
Func<Task> func = () => repository.CreateEvent(eventDetails);
// Assert
await func.Should().ThrowAsync<InvalidOperationException>();
}
[Fact]
public void Given_Instance_When_GetEvents_Invoked_Then_It_Should_Throw_Exception()
{
// Arrange
var settings = Substitute.For<StorageAccountSettings>();
var tableServiceClient = Substitute.For<TableServiceClient>();
var repository = new AdminEventRepository(tableServiceClient, settings);
// Act
Func<Task> func = async () => await repository.GetEvents();
// Assert
func.Should().ThrowAsync<NotImplementedException>();
}
[Theory]
[InlineData(404)]
[InlineData(500)]
public async Task Given_Failure_In_Get_Entity_When_GetEvent_Invoked_Then_It_Should_Throw_Exception(int statusCode)
{
// Arrange
var settings = Substitute.For<StorageAccountSettings>();
var tableServiceClient = Substitute.For<TableServiceClient>();
var eventId = Guid.NewGuid();
var repository = new AdminEventRepository(tableServiceClient, settings);
var exception = new RequestFailedException(statusCode, "Request Error", default, default);
var tableClient = Substitute.For<TableClient>();
tableServiceClient.GetTableClient(Arg.Any<string>()).Returns(tableClient);
tableClient.GetEntityAsync<AdminEventDetails>(Arg.Any<string>(), Arg.Any<string>())
.ThrowsAsync(exception);
// Act
Func<Task> func = () => repository.GetEvent(eventId);
// Assert
var assertion = await func.Should().ThrowAsync<RequestFailedException>();
assertion.Which.Status.Should().Be(statusCode);
}
[Theory]
[InlineData("c355cc28-d847-4637-aad9-2f03d39aa51f", "event-details")]
public async Task Given_Exist_EventId_When_GetEvent_Invoked_Then_It_Should_Return_AdminEventDetails(string eventId, string partitionKey)
{
// Arrange
var settings = Substitute.For<StorageAccountSettings>();
var tableServiceClient = Substitute.For<TableServiceClient>();
var repository = new AdminEventRepository(tableServiceClient, settings);
var eventDetails = new AdminEventDetails
{
RowKey = eventId,
PartitionKey = partitionKey
};
var response = Response.FromValue(eventDetails, Substitute.For<Response>());
var tableClient = Substitute.For<TableClient>();
tableServiceClient.GetTableClient(Arg.Any<string>()).Returns(tableClient);
tableClient.GetEntityAsync<AdminEventDetails>(partitionKey, eventId)
.Returns(Task.FromResult(response));
// Act
var result = await repository.GetEvent(Guid.Parse(eventId));
// Assert
result.Should().BeEquivalentTo(eventDetails);
}
[Fact]
public void Given_Instance_When_UpdateEvent_Invoked_Then_It_Should_Throw_Exception()
{
// Arrange
var settings = Substitute.For<StorageAccountSettings>();
var tableServiceClient = Substitute.For<TableServiceClient>();
var eventId = Guid.NewGuid();
var eventDetails = new AdminEventDetails();
var repository = new AdminEventRepository(tableServiceClient, settings);
// Act
Func<Task> func = async () => await repository.UpdateEvent(eventId, eventDetails);
// Assert
func.Should().ThrowAsync<NotImplementedException>();
}
}