public void Add_ShouldAddMarkGeneralDataPoint() { // Arrange var context = GetContext(TestData.markGeneralDataPoints); var repo = new SqlMarkGeneralDataPointRepo(context); int markId = _rnd.Next(1, TestData.marks.Count()); int sectionId = _rnd.Next(1, TestData.generalDataSections.Count()); var markGeneralDataPoint = new MarkGeneralDataPoint { Mark = TestData.marks.SingleOrDefault(v => v.Id == markId), Section = TestData.generalDataSections.SingleOrDefault( v => v.Id == sectionId), Text = "NewCreate", OrderNum = 3, }; // Act repo.Add(markGeneralDataPoint); // Assert Assert.NotNull(repo.GetById(markGeneralDataPoint.Id)); context.Database.EnsureDeleted(); context.Dispose(); }
public void Create( MarkGeneralDataPoint markGeneralDataPoint, int markId, int sectionId) { if (markGeneralDataPoint == null) { throw new ArgumentNullException(nameof(markGeneralDataPoint)); } var foundSection = _generalDataSectionRepo.GetById(sectionId); if (foundSection == null) { throw new ArgumentNullException(nameof(foundSection)); } var foundMark = _markRepo.GetById(markId); if (foundMark == null) { throw new ArgumentNullException(nameof(foundMark)); } var uniqueConstraintViolationCheck = _repository.GetByUniqueKey( markId, sectionId, markGeneralDataPoint.Text); if (uniqueConstraintViolationCheck != null) { throw new ConflictException(uniqueConstraintViolationCheck.Id.ToString()); } markGeneralDataPoint.Section = foundSection; markGeneralDataPoint.Mark = foundMark; var currentPoints = _repository.GetAllByMarkAndSectionId(markId, sectionId); if (currentPoints.Count() == 0) { markGeneralDataPoint.OrderNum = 1; } else { markGeneralDataPoint.OrderNum = (Int16)(currentPoints.Max(v => v.OrderNum) + 1); } _repository.Add(markGeneralDataPoint); foundMark.EditedDate = DateTime.Now; _markRepo.Update(foundMark); }
public void Create_ShouldFailWithConflict_WhenConflictValues() { // Arrange int conflictMarkId = _markGeneralDataPoints[0].Mark.Id; int conflictSectionId = _markGeneralDataPoints[0].Section.Id; var newMarkGeneralDataPoint = new MarkGeneralDataPoint { Text = _markGeneralDataPoints[0].Text, }; // Act & Assert Assert.Throws <ConflictException>(() => _service.Create(newMarkGeneralDataPoint, conflictMarkId, conflictSectionId)); _repository.Verify(mock => mock.Add(It.IsAny <MarkGeneralDataPoint>()), Times.Never); }
public void Create_ShouldFailWithNull_WhenWrongValues() { // Arrange int markId = _rnd.Next(1, TestData.marks.Count()); int sectionId = _rnd.Next(1, TestData.generalDataSections.Count()); var newmarkGeneralDataPoint = new MarkGeneralDataPoint { Text = "NewCreate", }; // Act & Assert Assert.Throws <ArgumentNullException>(() => _service.Create(null, markId, sectionId)); Assert.Throws <ArgumentNullException>(() => _service.Create(newmarkGeneralDataPoint, markId, 999)); _repository.Verify(mock => mock.Add(It.IsAny <MarkGeneralDataPoint>()), Times.Never); }
public void Create_ShouldCreateMarkGeneralDataPoint() { // Arrange int markId = _rnd.Next(1, TestData.marks.Count()); int sectionId = _rnd.Next(1, TestData.generalDataSections.Count()); var newMarkGeneralDataPoint = new MarkGeneralDataPoint { Text = "NewCreate", }; // Act _service.Create(newMarkGeneralDataPoint, markId, sectionId); // Assert _repository.Verify(mock => mock.Add(It.IsAny <MarkGeneralDataPoint>()), Times.Once); Assert.NotNull(newMarkGeneralDataPoint.Mark); }
public IEnumerable <MarkGeneralDataPoint> UpdateAllByPointIds( int userId, int markId, int sectionId, List <int> pointIds) { if (pointIds == null) { throw new ArgumentNullException(nameof(pointIds)); } var foundMark = _markRepo.GetById(markId); if (foundMark == null) { throw new ArgumentNullException(nameof(foundMark)); } var foundSection = _generalDataSectionRepo.GetById(sectionId); if (foundSection == null) { throw new ArgumentNullException(nameof(foundSection)); } var currentPoints = _repository.GetAllByMarkAndSectionId( markId, sectionId).ToList(); var generalDataPoints = new List <GeneralDataPoint> { }; foreach (var id in pointIds) { var generalDataPoint = _generalDataPointRepo.GetById(id); if (generalDataPoint == null) { throw new ArgumentNullException(nameof(generalDataPoint)); } generalDataPoints.Add(generalDataPoint); } var allUserPoints = _generalDataPointRepo.GetAllByUserAndSectionId(userId, sectionId); foreach (var userPoint in allUserPoints) { if (!pointIds.Contains(userPoint.Id)) { if (currentPoints.Select(v => v.Text).Contains(userPoint.Text)) { var point = currentPoints.SingleOrDefault(v => v.Text == userPoint.Text); _repository.Delete( currentPoints.SingleOrDefault(v => v.Text == userPoint.Text)); currentPoints.Remove(point); } } } foreach (var p in generalDataPoints.OrderBy(v => v.OrderNum)) { var uniqueConstraintCheck = _repository.GetByUniqueKey( markId, sectionId, p.Text); if (uniqueConstraintCheck == null) { var markGeneralDataPoint = new MarkGeneralDataPoint { Mark = foundMark, Section = foundSection, Text = p.Text, }; if (currentPoints.Count() == 0) { markGeneralDataPoint.OrderNum = 1; } else { markGeneralDataPoint.OrderNum = (Int16)(currentPoints.Max(v => v.OrderNum) + 1); } _repository.Add(markGeneralDataPoint); currentPoints.Add(markGeneralDataPoint); } } short num = 1; foreach (var p in currentPoints) { p.OrderNum = num; _repository.Update(p); num += 1; } foundMark.EditedDate = DateTime.Now; _markRepo.Update(foundMark); return(currentPoints); }
public void Delete(MarkGeneralDataPoint MarkgeneralDataPoint) { _context.MarkGeneralDataPoints.Remove(MarkgeneralDataPoint); _context.SaveChanges(); }
public void Update(MarkGeneralDataPoint MarkgeneralDataPoint) { _context.Entry(MarkgeneralDataPoint).State = EntityState.Modified; _context.SaveChanges(); }
public void Add(MarkGeneralDataPoint MarkgeneralDataPoint) { _context.MarkGeneralDataPoints.Add(MarkgeneralDataPoint); _context.SaveChanges(); }