aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests
diff options
context:
space:
mode:
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests')
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudServiceTest.cs32
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudTestBase.cs74
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/TestEntity.cs10
3 files changed, 41 insertions, 75 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudServiceTest.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudServiceTest.cs
new file mode 100644
index 0000000..0c42c67
--- /dev/null
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudServiceTest.cs
@@ -0,0 +1,32 @@
+using Microsoft.Extensions.Logging.Abstractions;
+
+namespace CrupestApi.Commons.Crud.Tests;
+
+public class CrudServiceTest
+{
+ private readonly SqliteMemoryConnectionFactory _memoryConnectionFactory = new SqliteMemoryConnectionFactory();
+
+ private readonly CrudService<TestEntity> _crudService;
+
+ public CrudServiceTest()
+ {
+ var columnTypeProvider = new ColumnTypeProvider();
+ var tableInfoFactory = new TableInfoFactory(columnTypeProvider, NullLoggerFactory.Instance);
+ var dbConnectionFactory = new SqliteMemoryConnectionFactory();
+
+ _crudService = new CrudService<TestEntity>(
+ tableInfoFactory, dbConnectionFactory, NullLoggerFactory.Instance);
+ }
+
+ [Fact]
+ public void CrudTest()
+ {
+ _crudService.Create(new TestEntity()
+ {
+ Name = "crupest",
+ Age = 18,
+ });
+ }
+
+
+}
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudTestBase.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudTestBase.cs
deleted file mode 100644
index 98c0dfd..0000000
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudTestBase.cs
+++ /dev/null
@@ -1,74 +0,0 @@
-using System.Net;
-using CrupestApi.Commons.Secrets;
-using Microsoft.AspNetCore.TestHost;
-
-namespace CrupestApi.Commons.Crud.Tests;
-
-public abstract class CrudTestBase<TEntity> : IAsyncDisposable where TEntity : class
-{
- protected readonly WebApplication _app;
-
- protected readonly string _path;
- protected readonly string? _authKey;
-
- protected readonly HttpClient _client;
-
- public CrudTestBase(string path, string? authKey = null)
- {
- _path = path;
- _authKey = authKey;
-
- var builder = WebApplication.CreateBuilder();
- builder.WebHost.UseTestServer();
- builder.Services.AddCrud<TEntity>();
- ConfigureApplication(builder);
- _app = builder.Build();
-
- if (authKey is not null)
- {
- using (var scope = _app.Services.CreateScope())
- {
- var secretService = scope.ServiceProvider.GetRequiredService<ISecretService>();
- secretService.CreateTestSecret(authKey, "test-secret");
- }
- }
-
- _client = CreateHttpClient();
- }
-
- protected abstract void ConfigureApplication(WebApplicationBuilder builder);
-
- public virtual async ValueTask DisposeAsync()
- {
- await _app.DisposeAsync();
- }
-
- public TestServer GetTestServer()
- {
- return _app.GetTestServer();
- }
-
- public HttpClient CreateHttpClient()
- {
- return GetTestServer().CreateClient();
- }
-
- public async Task TestAuth()
- {
- if (_authKey is null)
- {
- return;
- }
-
- {
- using var response = await _client.GetAsync(_path);
- Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
- }
-
- {
- var entity = Activator.CreateInstance<TEntity>();
- using var response = await _client.PostAsJsonAsync(_path, entity);
- Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
- }
- }
-}
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/TestEntity.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/TestEntity.cs
index ca84d5a..7cc19ed 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/TestEntity.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/TestEntity.cs
@@ -2,7 +2,7 @@ namespace CrupestApi.Commons.Crud.Tests;
public class TestEntity
{
- [Column(NotNull = true)]
+ [Column(ActAsKey = true, NotNull = true)]
public string Name { get; set; } = default!;
[Column(NotNull = true)]
@@ -11,5 +11,13 @@ public class TestEntity
[Column]
public float? Height { get; set; }
+ [Column(Generated = true, NotNull = true, NoUpdate = true)]
+ public string Secret { get; set; } = default!;
+
+ public static string SecretDefaultValueGenerator()
+ {
+ return "secret";
+ }
+
public string NonColumn { get; set; } = "Not A Column";
}