/// <summary> /// unit test for addition and deletion ip addresses /// </summary> /// <returns></returns> public async Task AddIpAddress() { var test = CreateTest("Test 1"); string userName = "******"; ApplicationUser user = new ApplicationUser() { Email = userName, UserName = userName }; await _userManager.CreateAsync(user); var applicationUser = await _userManager.FindByEmailAsync(user.Email); await _testRepository.CreateTestAsync(test, applicationUser.Id); var testIp = new TestIpAddress(); testIp.IpAddress = "127.0.0.1"; testIp.TestId = test.Id; await _trappistDbContext.TestIpAddresses.AddAsync(testIp); await _testRepository.UpdateTestByIdAsync(test); Assert.Equal(1, test.TestIpAddress.Count); await _testRepository.DeleteIpAddressAsync(testIp.Id); Assert.Equal(0, test.TestIpAddress.Count); }
public async Task <Test> DuplicateTest(int testId, Test newTest) { //Fetch categories present in that particular test and store them in a variable of type list var testCategoryList = _dbContext.TestCategory.Where(x => x.TestId == testId); // Fetch questions present in that particular test and store them in a variable of type list var testQuestionList = _dbContext.TestQuestion.Where(x => x.TestId == testId); var test = await _dbContext.Test.FindAsync(newTest.Id); //Fetch Ip Addresses in that particular test and store them in a variable of type list var testIpAddressList = await _dbContext.TestIpAddresses.Where(x => x.TestId == testId).ToListAsync(); if (testCategoryList.Any()) { var categoryList = new List <TestCategory>(); foreach (TestCategory categoryObject in testCategoryList) { var categoryObj = new TestCategory(); categoryObj.CategoryId = categoryObject.CategoryId; categoryObj.Test = test; categoryObj.TestId = test.Id; categoryList.Add(categoryObj); } await _dbContext.TestCategory.AddRangeAsync(categoryList); await _dbContext.SaveChangesAsync(); var questionList = new List <TestQuestion>(); if (testQuestionList.Any()) { foreach (TestQuestion questionObject in testQuestionList) { var questionObj = new TestQuestion(); questionObj.QuestionId = questionObject.QuestionId; questionObj.Test = test; questionObj.TestId = test.Id; questionList.Add(questionObj); } await _dbContext.TestQuestion.AddRangeAsync(questionList); await _dbContext.SaveChangesAsync(); } var ipAddressList = new List <TestIpAddress>(); if (testIpAddressList.Any()) { foreach (TestIpAddress testIpAddressObject in testIpAddressList) { var ipAddressObject = new TestIpAddress(); ipAddressObject.Test = test; ipAddressObject.TestId = test.Id; ipAddressObject.IpAddress = testIpAddressObject.IpAddress; ipAddressList.Add(ipAddressObject); } await _dbContext.TestIpAddresses.AddRangeAsync(ipAddressList); await _dbContext.SaveChangesAsync(); } } return(test); }
public async Task DuplicateTest() { var category1 = CreateCategory("Aptitude"); var questiontoCreate1 = "Question1"; var question1 = CreateSingleAnswerQuestion(category1, questiontoCreate1); var category2 = CreateCategory("Logical"); var questiontoCreate2 = "Question2"; var question2 = CreateSingleAnswerQuestion(category1, questiontoCreate2); var oldTest = CreateTest("Maths"); var testIp = new TestIpAddress(); testIp.IpAddress = "127.0.0.1"; testIp.TestId = oldTest.Id; string userName = "******"; ApplicationUser user = new ApplicationUser() { Email = userName, UserName = userName }; await _userManager.CreateAsync(user); var applicationUser = await _userManager.FindByEmailAsync(user.Email); await _testRepository.CreateTestAsync(oldTest, applicationUser.Id); var testCategoryObject1 = new TestCategory() { CategoryId = category1.Id, TestId = oldTest.Id, Test = oldTest }; var testCategoryObject2 = new TestCategory() { CategoryId = category2.Id, TestId = oldTest.Id, Test = oldTest }; var testCategoryList = new List <TestCategory>(); testCategoryList.Add(testCategoryObject1); testCategoryList.Add(testCategoryObject2); await _trappistDbContext.TestCategory.AddRangeAsync(testCategoryList); var testQuestionObject1 = new TestQuestion() { QuestionId = question1.Id, TestId = oldTest.Id, Test = oldTest, }; var testQuestionObject2 = new TestQuestion() { QuestionId = question2.Id, TestId = oldTest.Id, Test = oldTest, }; var testQuestionList = new List <TestQuestion>(); testQuestionList.Add(testQuestionObject1); testQuestionList.Add(testQuestionObject2); await _trappistDbContext.TestQuestion.AddRangeAsync(testQuestionList); var testIpAddressObject = new TestIpAddress() { Test = oldTest, TestId = oldTest.Id, IpAddress = testIp.IpAddress }; var testIpList = new List <TestIpAddress>(); testIpList.Add(testIpAddressObject); await _trappistDbContext.TestIpAddresses.AddRangeAsync(testIpList); var newTest = CreateTest("Maths_Copy"); await _testRepository.CreateTestAsync(newTest, applicationUser.Id); await _testRepository.DuplicateTest(oldTest.Id, newTest); var count = await _testRepository.SetTestCopiedNumberAsync(oldTest.TestName); Assert.Equal(4, _trappistDbContext.TestQuestion.Count()); Assert.Equal(4, _trappistDbContext.TestCategory.Count()); Assert.Equal(2, _trappistDbContext.TestIpAddresses.Count()); Assert.Equal(1, count); }