aboutsummaryrefslogtreecommitdiff
path: root/dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudServiceTest.cs
blob: ad0d34cea9edc4fedfff2ebbd5f8b5dde0964b0b (plain)
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
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"
        }));
    }
}