aboutsummaryrefslogtreecommitdiff
path: root/dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudServiceTest.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2024-11-11 01:12:29 +0800
committerYuqian Yang <crupest@crupest.life>2024-12-19 21:42:01 +0800
commitf9aa02ec1a4c24e80a206857d4f68198bb027bb4 (patch)
tree5994f0a62733b13f9f330e3515260ae20dc4a0bd /dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudServiceTest.cs
parent7b4d49e4bbdff6ddf1f8f7e937130e700024d5e9 (diff)
downloadcrupest-f9aa02ec1a4c24e80a206857d4f68198bb027bb4.tar.gz
crupest-f9aa02ec1a4c24e80a206857d4f68198bb027bb4.tar.bz2
crupest-f9aa02ec1a4c24e80a206857d4f68198bb027bb4.zip
HALF WORK: 2024.12.19
Re-organize file structure.
Diffstat (limited to 'dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudServiceTest.cs')
-rw-r--r--dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudServiceTest.cs77
1 files changed, 77 insertions, 0 deletions
diff --git a/dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudServiceTest.cs b/dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudServiceTest.cs
new file mode 100644
index 0000000..ad0d34c
--- /dev/null
+++ b/dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudServiceTest.cs
@@ -0,0 +1,77 @@
+using CrupestApi.Commons.Crud.Migrations;
+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, new SqliteDatabaseMigrator(), NullLoggerFactory.Instance);
+ }
+
+ [Fact]
+ public void CrudTest()
+ {
+ var key = _crudService.Create(new TestEntity()
+ {
+ Name = "crupest",
+ Age = 18,
+ });
+
+ Assert.Equal("crupest", key);
+
+ var entity = _crudService.GetByKey(key);
+ Assert.Equal("crupest", entity.Name);
+ Assert.Equal(18, entity.Age);
+ Assert.Null(entity.Height);
+ Assert.NotEmpty(entity.Secret);
+
+ var list = _crudService.GetAll();
+ entity = Assert.Single(list);
+ Assert.Equal("crupest", entity.Name);
+ Assert.Equal(18, entity.Age);
+ Assert.Null(entity.Height);
+ Assert.NotEmpty(entity.Secret);
+
+ var count = _crudService.GetCount();
+ Assert.Equal(1, count);
+
+ _crudService.UpdateByKey(key, new TestEntity()
+ {
+ Name = "crupest2.0",
+ Age = 22,
+ Height = 180,
+ });
+
+ entity = _crudService.GetByKey("crupest2.0");
+ Assert.Equal("crupest2.0", entity.Name);
+ Assert.Equal(22, entity.Age);
+ Assert.Equal(180, entity.Height);
+ Assert.NotEmpty(entity.Secret);
+
+ _crudService.DeleteByKey("crupest2.0");
+
+ count = _crudService.GetCount();
+ Assert.Equal(0, count);
+ }
+
+ [Fact]
+ public void EntityNotExistTest()
+ {
+ Assert.Throws<EntityNotExistException>(() => _crudService.GetByKey("KeyNotExist"));
+ Assert.Throws<EntityNotExistException>(() => _crudService.UpdateByKey("KeyNotExist", new TestEntity
+ {
+ Name = "crupest"
+ }));
+ }
+}