using LCU.Library.Internal.CoreApiWrapperLib.CoreDbUtilities; using LCU.Library.Internal.CoreApiWrapperLib.DataAccess; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.MemberAgreements; using Moq; using NUnit.Framework; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace LCU.Api.Internal.CoreApiWrapperTest.DataAccess { public class AgreeDaoTest { private AgreeDao _agreeDao; private Mock _mockCoreDbExecution; [SetUp] public void SetUp() { _mockCoreDbExecution = new Mock(); _agreeDao = new AgreeDao(_mockCoreDbExecution.Object); } [Test] public async Task FindAgreeUsersByAccountNumbers_OK() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny>())) .ReturnsAsync(new List() { new() { AccountNumber = 20001, AgreeNumber = 2000, UserId = "12000" } }).Verifiable(); var testAccountNumbers = new List() { "20001" }; var results = await _agreeDao.FindAgreeUsersByAccountNumbers(testAccountNumbers); Assert.NotNull(results); var firstResult = results.FirstOrDefault(); Assert.AreEqual(20001, firstResult.AccountNumber); Assert.AreEqual(2000, firstResult.AgreeNumber); Assert.AreEqual("12000", firstResult.UserId); _mockCoreDbExecution.Verify(x => x.ExecuteQuery>( It.Is(str => str.Contains(":param0")), It.IsAny>())); } [Test] public void FindByUserId_Exception() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny>())) .ThrowsAsync(new Exception("Ah oh Error")); var testAccountNumbers = new List() { "20001" }; Assert.ThrowsAsync(async () => await _agreeDao.FindAgreeUsersByAccountNumbers(testAccountNumbers)); _mockCoreDbExecution.Verify(x => x.ExecuteQuery>( It.Is(str => str.Contains(":param0")), It.IsAny>())); } [Test] public async Task FindAgreeUserByPersonNumber_OK() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { "5436" }).Verifiable(); var result = await _agreeDao.FindAgreeUserByPersonNumber(12355L); Assert.NotNull(result); Assert.AreEqual("5436", result.FirstOrDefault()); _mockCoreDbExecution.Verify(); } [Test] public void FindAgreeUserByPersonNumber_Exception() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ThrowsAsync(new Exception("Error thrown from unit test")); Assert.ThrowsAsync(async () => await _agreeDao.FindAgreeUserByPersonNumber(123L)); _mockCoreDbExecution.Verify(); } [Test] public async Task FindAgreeUserByOrganizationNumber_OK() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { "5436" }).Verifiable(); var result = await _agreeDao.FindAgreeUserByOrganizationNumber(12355L); Assert.NotNull(result); Assert.AreEqual("5436", result); _mockCoreDbExecution.Verify(); } [Test] public void FindAgreeUserByOrganizationNumber_Exception() { _mockCoreDbExecution .Setup(x => x.ExecuteQuery(It.IsAny(), It.IsAny())) .ThrowsAsync(new Exception("Error thrown from unit test")); Assert.ThrowsAsync(async () => await _agreeDao.FindAgreeUserByOrganizationNumber(123L)); _mockCoreDbExecution.Verify(); } } }