using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using Moq; using webapp_italsort.DataItalsortGestionale; using webapp_italsort.Repositories.SharePoint; namespace library_spo_utils.test { [TestClass] public class NonComplianceSettingsRepositoryTest { private readonly NonComplianceSettingsRepository _sut; private readonly Mock> _factoryMock = new Mock>(); private readonly Mock _accessorMock = new Mock(); private Db_ItalsortGestionaleContext _context; public NonComplianceSettingsRepositoryTest() { _sut = new NonComplianceSettingsRepository(_factoryMock.Object); } [TestInitialize] public void Setup() { var httpContext = new DefaultHttpContext(); var fakeTenantId = "abcd"; httpContext.Request.Headers["Tenant-ID"] = fakeTenantId; _accessorMock.Setup(_ => _.HttpContext).Returns(httpContext); var options = new DbContextOptionsBuilder() .UseInMemoryDatabase("InMemoryTest") .Options; _context = new Db_ItalsortGestionaleContext(options, _accessorMock.Object); _factoryMock.Setup(x => x.CreateDbContext()).Returns(_context); } [TestMethod] [DataRow("codice-test")] public async Task GetCode_ShouldReturn_Instance(string code) { var record = new InventNonConformanceTable() { Code = code }; _context.InventNonConformanceTable.Add(record); await _context.SaveChangesAsync(); var result = _sut.GetByCode(code); Assert.IsNotNull(result); Assert.AreEqual(code, result.Code); } [TestMethod] [DataRow("codice", "NC23", "00012", "NC23-00012")] public async Task DefaultNonComplianceProject_Should_Return_ProjId(string code, string projId, string subProj,string projConcat) { var record = new InventNonConformanceTable() { Code = code, Proj = new ProjTable() { ProjId = projId, Name = "" }, SubProj = new SubProjTable() { SubProjId = subProj, } }; _context.InventNonConformanceTable.Add(record); await _context.SaveChangesAsync(); var projIdResult = _sut.DefaultNonComplianceProject(code); Assert.IsNotNull(projIdResult); Assert.AreEqual(projConcat, projIdResult); } } }