mercredi 8 juillet 2020

Unit test of INSERT operation in DB

I have a some code for adding in my DB. It works fine, but now I need develop UNIT test for it. I have not experience with it. I'll be happy If you will give me recommendation or advice.

TESTDATAGENERATE class (here methods for tests)

public class TestDataGenerator
    {
        private readonly string _dataFilePath;
        private readonly Dictionary<string, List<object>> _jsonData;

        public TestDataGenerator(string dataFilePath)
        {
            _dataFilePath = dataFilePath;
            _jsonData = new Dictionary<string, List<object>>();
        }

        public void LoadData(string key)
        {
            if (!_jsonData.ContainsKey(key))
                _jsonData.Add(key, JsonUtils.GetData(_dataFilePath, key).ToList());
        }

        public List<TEntity> GetTestData<TEntity>(string mainKey, string subKey)
        {
            var data = JsonConvert.DeserializeObject<List<TEntity>>(JObject.Parse(_jsonData[mainKey][0].ToString())[subKey].ToString());

            return data;
        }
    }

Interface with Insert method

   public interface ITownRepository
    {
        Task<Town> InsertTownName(string townName);
    }

Implement of this Interface

    public class TownRepository : ITownRepository
    {
        protected readonly IConnectionFactory _connectionFactory;
        private readonly AppSettings _appSettings;

        public TownRepository( IConnectionFactory connectionFactory
                              , IOptions<AppSettings> appSettings)
        {
            _connectionFactory = connectionFactory;
            _appSettings = appSettings.Value;
        }

        public async Task<Town> InsertTownName(string townName)
        {

            using var connection = _connectionFactory.GetConnection();
            
            var query = File.ReadAllText(_appSettings.SQlScriptsPath + "/AddTown.sql");
     
            return await connection.QueryFirstOrDefaultAsync<Town>(query, new { townName = townName });
        }
    }

I have example for SEARCH query and I need develop from it my unit test with INSERT query It's example with SEARCH

    public partial class ClientRepositoryTests
    {
        private class TestBuilder
        {
            public Mock<IConnectionFactory> MockConnectionFactory { get; set; }

            private readonly IOptions<AppSettings> _appSettingsOptions;

            public InMemoryDatabase _memoryDatabase;

            public TestDataGenerator TestDataGenerator;

            private const string _jsonDataPath = @"../../../TestData/Client/ClientTestData.json";

            private string _jsonDataKey;

            public TestBuilder(string jsonDataKey)
            {
                MockConnectionFactory = new Mock<IConnectionFactory>();

                _appSettingsOptions = MockSetup.GetAppSettingsOptions();

                _memoryDatabase = new InMemoryDatabase();

                MockConnectionFactory.Setup(c => c.GetConnection()).Returns(_memoryDatabase.OpenConnection());

                TestDataGenerator = new TestDataGenerator(_jsonDataPath);

                _jsonDataKey = jsonDataKey;

                TestDataGenerator.LoadData(_jsonDataKey);
            }

            public IClientRepository Build()
            {
                return new ClientRepository(MockConnectionFactory.Object
                                          , _appSettingsOptions);
            }

            public TestBuilder SetupClients()
            {
                var clients = TestDataGenerator.GetTestData<Client>(_jsonDataKey, "Clients");

                _memoryDatabase.Insert(clients);

                return this;
            }

            public TestBuilder SetupProperties()
            {
                var properties = TestDataGenerator.GetTestData<Client>(_jsonDataKey, "Properties");

                _memoryDatabase.Insert("property_published_v", properties);

                return this;
            }
        }
    }

And here is a test of search query

  public partial class ClientRepositoryTests
    {
        [Theory]
        [InlineData("Data")]
        public async Task GetClientBySearchCriteria_InValidInput_ReturnZeroRecords(string jsonDataKey)
        {
            var searchParameter = "5";

            var builder = new TestBuilder(jsonDataKey).SetupClients()
                                                      .SetupProperties();

            var repository = builder.Build();

            var result = await repository.GetClientBySearchCriteria(searchParameter);

            Assert.NotNull(result);
            Assert.Equal(0, result.Count);
        }

I tried to repeat it, but I have a problem, because I don't have experience and understanding how it works, I'll be grateful for your advice and help My decision

    public partial class TownRepositoryTests
    {
        private class TestBuilder
        {
            public Mock<IConnectionFactory> MockConnectionFactory { get; set; }

            private readonly IOptions<AppSettings> _appSettingsOptions;

            public InMemoryDatabase _memoryDatabase;

            public TestDataGenerator TestDataGenerator;

            private const string _jsonDataPath = @"../../../TestData/Town/TownTestData.json";

            private string _jsonDataKey;

            public TestBuilder(string jsonDataKey)
            {
                MockConnectionFactory = new Mock<IConnectionFactory>();

                _appSettingsOptions = MockSetup.GetAppSettingsOptions();

                _memoryDatabase = new InMemoryDatabase();

                MockConnectionFactory.Setup(c => c.GetConnection()).Returns(_memoryDatabase.OpenConnection());

                TestDataGenerator = new TestDataGenerator(_jsonDataPath);

                _jsonDataKey = jsonDataKey;

                TestDataGenerator.LoadData(_jsonDataKey);
            }

            public ITownRepository Build()
            {
                return new TownRepository(MockConnectionFactory.Object
                                          , _appSettingsOptions);
            }

            public TestBuilder InsertTown()
            {
                //What I need to do here?
            }
        }
    }

First that I think to change in a InsertTown method I need call(before I'll do it, I need add it in TestDataGenerator) new method from TestDataGenerator, right? If it is correct, from what I need start, because I don't understand their logic, could I add insert query in TestDataGenerator?

Okay, after that I'll start to work with Tests. Could I add INSERT query here or I need add it in different place? If not, what I need to do in this part?

    public partial class TownRepositoryTests
    {
        [Theory]
        [InlineData("Data")]
        public async Task InsertTownName(string jsonDataKey)
        {
           ??
        }
    }

Aucun commentaire:

Enregistrer un commentaire