public async Task EditGrade_WithExistentId_ShouldSuccessfullyEditGrade() { string errorMessagePrefix = "GradeService EditGradeAsync() does not work properly."; var context = NetBookDbContextInMemoryFactory.InitializeContext(); await this.SeedData(context); this.gradeService = new GradeService(context); var testId = context.Grades.First().Id; var editGrade = new GradeServiceModel { Id = testId, GradeValue = "3", Term = Term.Втори, IsTermGrade = false, }; var actualResult = await this.gradeService.EditGradeAsync(editGrade); Assert.True(actualResult, errorMessagePrefix); var editedGrade = context.Grades.First(); Assert.True(editedGrade.GradeValue == editGrade.GradeValue, errorMessagePrefix + " " + "GradeValue is not set properly."); Assert.True(editedGrade.Term == editGrade.Term, errorMessagePrefix + " " + "Term is not set properly."); Assert.True(editedGrade.IsTermGrade == editGrade.IsTermGrade, errorMessagePrefix + " " + "IsTermGrade is not set properly."); }
public async Task <bool> CreateGradeAsync(GradeServiceModel model) { var grade = Mapper.Map <Grade>(model); var student = await this.context.Students.Where(x => !x.IsDeleted).SingleOrDefaultAsync(s => s.Id == model.StudentId); if (student == null) { throw new ArgumentNullException(nameof(student)); } if (grade.IsTermGrade) { var termGradeFromContext = await this.context.Grades .Where(x => x.Term == grade.Term) .Where(x => x.SubjectId == grade.SubjectId) .Where(x => x.StudentId == grade.StudentId) .SingleOrDefaultAsync(g => g.IsTermGrade); if (termGradeFromContext != null) { return(false); } } student.Grades.Add(grade); this.context.Students.Update(student); await this.context.Grades.AddAsync(grade); int result = await this.context.SaveChangesAsync(); return(result > 0); }
public async Task CreateGrade_WithAnotherTermGrade_ShouldReturnFalse() { string errorMessagePrefix = "GradeService CreateGradeAsync() does not work properly."; var context = NetBookDbContextInMemoryFactory.InitializeContext(); await this.SeedData(context); this.gradeService = new GradeService(context); var studentId = "TestStudent"; var subjectId = "TestSubject"; var testGrade = new GradeServiceModel { GradeValue = "5", Term = Term.Първи, IsTermGrade = true, StudentId = studentId, SubjectId = subjectId, }; var actualResult = await this.gradeService.CreateGradeAsync(testGrade); Assert.True(!actualResult, errorMessagePrefix + " " + "Another term grade is added"); }
public async Task CreateGrade_WithCorrectData_ShouldSuccessfullyCreateGrade() { string errorMessagePrefix = "GradeService CreateGradeAsync() does not work properly."; var context = NetBookDbContextInMemoryFactory.InitializeContext(); await this.SeedData(context); this.gradeService = new GradeService(context); var student = context.Students.First(); var testGrade = new GradeServiceModel { Id = "TestCreateGrade", GradeValue = "5", Term = Term.Втори, IsTermGrade = false, Student = student.To <StudentServiceModel>(), StudentId = student.Id, }; var actualResult = await this.gradeService.CreateGradeAsync(testGrade); var grade = context.Grades.Find("TestCreateGrade"); Assert.True(actualResult, errorMessagePrefix); Assert.True(grade.GradeValue == testGrade.GradeValue, errorMessagePrefix + " " + "GradeValue is not set properly."); Assert.True(grade.Term == testGrade.Term, errorMessagePrefix + " " + "Term is not set properly."); Assert.True(grade.IsTermGrade == testGrade.IsTermGrade, errorMessagePrefix + " " + "IsTermGrade is not set properly."); Assert.True(grade.StudentId == testGrade.StudentId, errorMessagePrefix + " " + "StudentId is not set properly"); }
public async Task <bool> EditGradeAsync(GradeServiceModel model) { var grade = await this.context.Grades.Where(x => !x.IsDeleted).SingleOrDefaultAsync(g => g.Id == model.Id); if (grade == null) { throw new ArgumentNullException(nameof(grade)); } if (grade.GradeValue != model.GradeValue) { grade.GradeValue = model.GradeValue; grade.ModifiedOn = DateTime.UtcNow; } if (grade.Term != model.Term) { grade.Term = model.Term; grade.ModifiedOn = DateTime.UtcNow; } if (grade.IsTermGrade != model.IsTermGrade) { if (model.IsTermGrade) { var termGradeFromContext = await this.context.Grades .Where(x => x.Term == grade.Term) .Where(x => x.SubjectId == grade.SubjectId) .Where(x => x.StudentId == grade.StudentId) .SingleOrDefaultAsync(g => g.IsTermGrade); if (termGradeFromContext != null) { return(false); } } grade.IsTermGrade = model.IsTermGrade; grade.ModifiedOn = DateTime.UtcNow; } this.context.Grades.Update(grade); int result = await this.context.SaveChangesAsync(); return(result > 0); }
public async Task CreateGrade_WithNonExistentStudent_ShouldThrowArgumentNullException() { string errorMessagePrefix = "GradeService CreateGradeAsync() does not work properly."; var context = NetBookDbContextInMemoryFactory.InitializeContext(); await this.SeedData(context); this.gradeService = new GradeService(context); var testGrade = new GradeServiceModel(); await Assert.ThrowsAsync <ArgumentNullException>(async() => await this.gradeService.CreateGradeAsync(testGrade)); }
public async Task EditGrade_WithExistentIdAndWithAnotherTermGrade_ShouldReturnFalse() { string errorMessagePrefix = "GradeService EditGradeAsync() does not work properly."; var context = NetBookDbContextInMemoryFactory.InitializeContext(); await this.SeedData(context); this.gradeService = new GradeService(context); var testId = context.Grades.First().Id; var editGrade = new GradeServiceModel { Id = testId, GradeValue = "3", Term = Term.Първи, IsTermGrade = true, }; var actualResult = await this.gradeService.EditGradeAsync(editGrade); Assert.True(!actualResult, errorMessagePrefix + " " + "Can't change IsTermGrade to true when their is another Term Grade."); }