public async Task EditClass_WithCorrectData_ShouldSuccessfullyEditClass() { string errorMessagePrefix = "ClassService EditClassAsync() does not work properly."; var context = NetBookDbContextInMemoryFactory.InitializeContext(); await this.SeedData(context); this.classService = new ClassService(context); var testId = context.Classes.First().Id; var testEditClass = new ClassServiceModel { Id = testId, ClassNumber = 10, ClassLetter = ClassLetter.Б, SchoolYearStart = 2017, SchoolYearEnd = 2018, }; var actualResult = await this.classService.EditClassAsync(testEditClass); var editedClass = context.Classes.First(); Assert.True(actualResult, errorMessagePrefix); Assert.True(testEditClass.ClassNumber == editedClass.ClassNumber, errorMessagePrefix + " " + "ClassNumber is not set properly."); Assert.True(testEditClass.ClassLetter == editedClass.ClassLetter, errorMessagePrefix + " " + "ClassLetter is not set properly."); Assert.True(testEditClass.SchoolYearStart == editedClass.SchoolYearStart, errorMessagePrefix + " " + "SchoolYearStart is not set properly."); Assert.True(testEditClass.SchoolYearEnd == editedClass.SchoolYearEnd, errorMessagePrefix + " " + "SchoolYearEnd is not set properly."); }
public async Task CreateClass_WithNonExistentTeacher_ShouldThrowArgumentNullException() { var context = NetBookDbContextInMemoryFactory.InitializeContext(); await this.SeedData(context); this.classService = new ClassService(context); var testClass = new ClassServiceModel(); await Assert.ThrowsAsync <ArgumentNullException>(async() => await this.classService.CreateClassAsync(testClass)); }
public async Task <bool> EditClassAsync(ClassServiceModel model) { var classFromDb = await this.context.Classes.Where(x => !x.IsDeleted).SingleOrDefaultAsync(c => c.Id == model.Id); if (classFromDb == null) { throw new ArgumentNullException(nameof(classFromDb)); } if (classFromDb.ClassNumber != model.ClassNumber) { classFromDb.ClassNumber = model.ClassNumber; classFromDb.ModifiedOn = DateTime.UtcNow; } if (classFromDb.ClassLetter != model.ClassLetter) { classFromDb.ClassLetter = model.ClassLetter; classFromDb.ModifiedOn = DateTime.UtcNow; } if (classFromDb.SchoolYearStart != model.SchoolYearStart) { classFromDb.SchoolYearStart = model.SchoolYearStart; classFromDb.ModifiedOn = DateTime.UtcNow; } if (classFromDb.SchoolYearEnd != model.SchoolYearEnd) { classFromDb.SchoolYearEnd = model.SchoolYearEnd; classFromDb.ModifiedOn = DateTime.UtcNow; } if ((classFromDb.SchoolYearEnd - classFromDb.SchoolYearStart) != 1) { return(false); } this.context.Classes.Update(classFromDb); int result = await this.context.SaveChangesAsync(); return(result > 0); }
public async Task EditClass_WithIncorrectData_ShouldThrowArgumentNullException() { string errorMessagePrefix = "ClassService EditClassAsync() does not work properly."; var context = NetBookDbContextInMemoryFactory.InitializeContext(); await this.SeedData(context); this.classService = new ClassService(context); var testId = "Non_Existent"; var testClassModel = new ClassServiceModel { Id = testId }; await Assert.ThrowsAsync <ArgumentNullException>(async() => await this.classService.EditClassAsync(testClassModel)); }
public async Task <bool> CreateClassAsync(ClassServiceModel model) { var classToAdd = AutoMapper.Mapper.Map <Class>(model); var teacher = await this.context.Users.SingleOrDefaultAsync(x => x.Id == model.ClassTeacherId); if (teacher == null) { throw new ArgumentNullException(nameof(teacher)); } teacher.IsClassTeacher = true; teacher.ClassId = classToAdd.Id; this.context.Users.Update(teacher); await this.context.Classes.AddAsync(classToAdd); int result = await this.context.SaveChangesAsync(); return(result > 0); }
public async Task CreateClass_WithCorrectData_ShouldSuccessfullyCreateClass() { string errorMessagePrefix = "ClassService CreateClassAsync() does not work properly."; var context = NetBookDbContextInMemoryFactory.InitializeContext(); await this.SeedData(context); this.classService = new ClassService(context); var testTeacher = new NetBookUser { Id = "test", FullName = "TestTeacher", IsTeacher = false, }; await context.Users.AddAsync(testTeacher); await context.SaveChangesAsync(); var testClass = new ClassServiceModel { Id = "testClass", ClassTeacherId = testTeacher.Id, ClassTeacher = testTeacher.To <UserServiceModel>(), }; var actualResult = await this.classService.CreateClassAsync(testClass); var updatedTeacher = context.Users.Find("test"); var classId = updatedTeacher.ClassId; var isClassTeacher = updatedTeacher.IsClassTeacher; Assert.True(actualResult, errorMessagePrefix); Assert.True(isClassTeacher, errorMessagePrefix + " " + "IsClassTeacher is not set properly"); Assert.True(classId == "testClass", errorMessagePrefix + " " + "ClassId is not set properly to Teacher."); }
public async Task EditClass_WithSchoolYearDifferenceBiggerOrLessThan1_ShouldReturnFalse() { string errorMessagePrefix = "ClassService EditClassAsync() does not work properly."; var context = NetBookDbContextInMemoryFactory.InitializeContext(); await this.SeedData(context); this.classService = new ClassService(context); var testId = context.Classes.First().Id; var testEditClassLess = new ClassServiceModel { Id = testId, ClassNumber = 10, ClassLetter = ClassLetter.Б, SchoolYearStart = 2017, SchoolYearEnd = 2017, }; var testEditClassBigger = new ClassServiceModel { Id = testId, ClassNumber = 10, ClassLetter = ClassLetter.Б, SchoolYearStart = 2017, SchoolYearEnd = 2019, }; var actualResultLess = await this.classService.EditClassAsync(testEditClassLess); var actualResultBigger = await this.classService.EditClassAsync(testEditClassBigger); Assert.True(!actualResultLess, errorMessagePrefix + " " + "SchoolYearStart and SchoolYearEnd difference not checked properly."); Assert.True(!actualResultBigger, errorMessagePrefix + " " + "SchoolYearStart and SchoolYearEnd difference not checked properly."); }