public async Task GetAll_HaveData_ReturnResultWithPagination() { // ===== Arrange ===== var dbName = Guid.NewGuid().ToString(); var context = BuildContext(dbName); var mapper = BuildMap(); var httpContext = new Mock <IHttpContextAccessor>(); var http = new DefaultHttpContext(); httpContext.Setup(_ => _.HttpContext).Returns(http); // Add Mockup data await Generate_ProductGroup_Data(context, mapper, httpContext.Object); // Prepare instance for testing var actContext = BuildContext(dbName); PaginationDto pagination = new PaginationDto { RecordsPerPage = 2 }; PaginationDto pagination2 = new PaginationDto { Page = 2, RecordsPerPage = 2 }; PaginationDto pagination3 = new PaginationDto { Page = 5, RecordsPerPage = 2 }; string filter = null; DataOrderDTO order = new DataOrderDTO(); // ===== Act ===== var service = new ProductGroupServices(actContext, mapper, httpContext.Object); var result1 = await service.GetAll(pagination, filter, order); var result2 = await service.GetAll(pagination2, filter, order); // ===== Assert ===== // Result 1 : Return data on 1st Page Assert.AreEqual(result1.IsSuccess, true); Assert.AreEqual(result1.Data[0].Id, 1); Assert.IsNotNull(result1.CurrentPage); Assert.IsNotNull(result1.PageIndex); // Result 2 : Return data on 2nd Page Assert.AreEqual(result2.IsSuccess, true); Assert.AreEqual(result2.Data[0].Id, 3); }
public async Task GetAll_ReachUnexistPage_ReturnResultWithPagination() { // ===== Arrange ===== var dbName = Guid.NewGuid().ToString(); var context = BuildContext(dbName); var mapper = BuildMap(); var httpContext = new Mock <IHttpContextAccessor>(); var http = new DefaultHttpContext(); httpContext.Setup(_ => _.HttpContext).Returns(http); // Add Mockup data await Generate_ProductGroup_Data(context, mapper, httpContext.Object); // Prepare instance for testing var actContext = BuildContext(dbName); PaginationDto pagination = new PaginationDto { Page = 5, RecordsPerPage = 2 }; string filter = null; DataOrderDTO order = new DataOrderDTO(); // ===== Act ===== var service = new ProductGroupServices(actContext, mapper, httpContext.Object); var result = await service.GetAll(pagination, filter, order); // ===== Assert ===== // Result : Check on Page Doesn't exist // Expected Exception }
public async Task GetAll_CanFilterByName_ReturnFilteredListOfProductGroup() { // ===== Arrange ===== var dbName = Guid.NewGuid().ToString(); var context = BuildContext(dbName); var mapper = BuildMap(); var httpContext = new Mock <IHttpContextAccessor>(); var http = new DefaultHttpContext(); httpContext.Setup(_ => _.HttpContext).Returns(http); // Add Mockup data await Generate_ProductGroup_Data(context, mapper, httpContext.Object); // Prepare Query var pagination = new PaginationDto(); string filter1 = "Test Product Group 3"; string filter2 = "G"; var order = new DataOrderDTO(); // New context var actContext = BuildContext(dbName); // ===== Act ===== var service = new ProductGroupServices(actContext, mapper, httpContext.Object); var result = await service.GetAll(pagination, filter1, order); var result2 = await service.GetAll(pagination, filter2, order); // ===== Assert ===== // Result 1 : Exact filter Assert.IsTrue(result.IsSuccess); Assert.AreEqual(result.Data[0].Name, filter1); Assert.AreEqual(result.Data.Count, 1); // Result 2 : Like cause filter Assert.IsTrue(result2.IsSuccess); Assert.IsTrue(result2.Data[0].Name.Contains(filter2)); Assert.IsTrue(result2.Data[result2.Data.Count - 1].Name.Contains(filter2)); Assert.AreEqual(result2.Data.Count, 5); }
public async Task GetAll_NoData_ReturnErrorMessage() { // ===== Arrange ===== var dbName = Guid.NewGuid().ToString(); var context = BuildContext(dbName); var mapper = BuildMap(); var httpContext = new Mock <IHttpContextAccessor>(); var http = new DefaultHttpContext(); httpContext.Setup(_ => _.HttpContext).Returns(http); var pagination = new PaginationDto(); var filter = ""; var order = new DataOrderDTO(); var expectEx = false; // ===== Act ===== long startTicks = Stopwatch.GetTimestamp(); try { var service = new ProductGroupServices(context, mapper, httpContext.Object); var result = await service.GetAll(pagination, filter, order); } catch (InvalidOperationException) { expectEx = true; } catch (Exception) { throw; } long endTicks = Stopwatch.GetTimestamp(); double elapsedSeconds = (endTicks - startTicks) * 1.0 / Stopwatch.Frequency; Console.WriteLine($"Start time : {startTicks}"); Console.WriteLine($"End time : {endTicks}"); Console.WriteLine($"Time Used : {elapsedSeconds}"); // ==== Assert ===== // Expected Exception Assert.IsTrue(expectEx); }
public async Task GetAll_FilterIsNotContainInDatabase_ReturnError() { // ===== Arrange ===== var dbName = Guid.NewGuid().ToString(); var context = BuildContext(dbName); var mapper = BuildMap(); var httpContext = new Mock <IHttpContextAccessor>(); var http = new DefaultHttpContext(); httpContext.Setup(_ => _.HttpContext).Returns(http); // Add Mockup data await Generate_ProductGroup_Data(context, mapper, httpContext.Object); // Prepare Query var pagination = new PaginationDto(); string filter = Guid.NewGuid().ToString(); var order = new DataOrderDTO(); // New context var actContext = BuildContext(dbName); // ===== Act ===== var service = new ProductGroupServices(actContext, mapper, httpContext.Object); var result = await service.GetAll(pagination, filter, order); // ===== Assert ===== // Result : Filter that not contain in Data // Expected Exception }