public void ThrowArgumentNullException_WhenSportshallNameIsNull() { //Arrange var dbContextMock = new Mock <ISportscardSystemDbContext>(); var mapperMock = new Mock <IMapper>(); var expectedSportshall = new Sportshall() { Name = null, IsDeleted = true }; var data = new List <Sportshall>() { new Sportshall() { Name = "Pulse", IsDeleted = false } }; var mockSet = new Mock <DbSet <Sportshall> >(); mockSet.SetupData(data); dbContextMock.Setup(x => x.Sportshalls).Returns(mockSet.Object); var sportshallService = new SportshallService(dbContextMock.Object, mapperMock.Object); //Act&Assert Assert.ThrowsException <ArgumentNullException>(() => sportshallService.DeleteSportshall(expectedSportshall.Name)); }
public void InvokeSaveChangesMethod_WhenSportshallExistAtDb() { //Arrange var dbContextMock = new Mock <ISportscardSystemDbContext>(); var mapperMock = new Mock <IMapper>(); var expectedSportshall = new Sportshall() { Name = "Pulse", IsDeleted = true }; var data = new List <Sportshall>() { new Sportshall() { Name = "Pulse", IsDeleted = false } }; var mockSet = new Mock <DbSet <Sportshall> >(); mockSet.SetupData(data); dbContextMock.Setup(x => x.Sportshalls).Returns(mockSet.Object); var sportshallService = new SportshallService(dbContextMock.Object, mapperMock.Object); //Act sportshallService.DeleteSportshall(expectedSportshall.Name); //Assert dbContextMock.Verify(x => x.SaveChanges(), Times.Once); Assert.AreEqual(expectedSportshall.IsDeleted, true); }
public void AddSportshallToDabatase_WhenInvokedWithValidParameterAndCompanyNotAlreadyAddedToDatabase() { //Arrange var dbContextMock = new Mock <ISportscardSystemDbContext>(); var mapperMock = new Mock <IMapper>(); var expectedSportshall = new Sportshall() { Name = "Sportshall4" }; var data = new List <Sportshall> { new Sportshall { Name = "Sportshall1" }, new Sportshall { Name = "Sportshall2" }, new Sportshall { Name = "Sportshall3" } }.AsQueryable(); var mockSet = new Mock <DbSet <Sportshall> >(); mockSet.As <IQueryable <Sportshall> >().Setup(m => m.Provider).Returns(data.Provider); mockSet.As <IQueryable <Sportshall> >().Setup(m => m.Expression).Returns(data.Expression); mockSet.As <IQueryable <Sportshall> >().Setup(m => m.ElementType).Returns(data.ElementType); mockSet.As <IQueryable <Sportshall> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator); mockSet.Setup(m => m.Add(It.IsAny <Sportshall>())); dbContextMock .Setup(x => x.Sportshalls) .Returns(mockSet.Object); var sportshallDto = new SportshallDto() { Name = "Sportshall4" }; mapperMock .Setup(x => x.Map <Sportshall>(sportshallDto)) .Returns(expectedSportshall); var sportshallService = new SportshallService(dbContextMock.Object, mapperMock.Object); //Act sportshallService.AddSportshall(sportshallDto); //Assert mockSet.Verify(x => x.Add(expectedSportshall), Times.Once); }
public void ThrowArgumentNullException_WhenThereIsNoSportshall() { //Arrange var dbContextMock = new Mock <ISportscardSystemDbContext>(); var mapperMock = new Mock <IMapper>(); var sportsHall = new Sportshall() { Id = new Guid("db97a0eb-9411-4f1d-9ead-3997e6271324"), Name = "Gym", IsDeleted = false }; var sport = new Sport() { Id = new Guid("db97a0eb-9411-4f1d-9ead-3997e6271323"), Name = "Gym", IsDeleted = false }; var visit = new Visit() { Id = new Guid("db97a0eb-9411-4f1d-9ead-3997e6271325"), IsDeleted = false, Sport = sport, Sportshall = sportsHall, CreatedOn = DateTime.Now.Date }; var sportshallVisits = new List <Visit>(); sportshallVisits.Add(visit); sportsHall.Visits = sportshallVisits; var data = new List <Sportshall>(); var mockSet = new Mock <DbSet <Sportshall> >(); mockSet.SetupData(data); dbContextMock.Setup(x => x.Sportshalls).Returns(mockSet.Object); var expectedSportshallDto = new SportshallDto() { Id = new Guid("db97a0eb-9411-4f1d-9ead-3997e6271324"), Name = "Gym", }; mapperMock.Setup(x => x.Map <SportshallDto>(sportsHall)).Returns(expectedSportshallDto); var sporthallService = new SportshallService(dbContextMock.Object, mapperMock.Object); //Act&&Assert Assert.ThrowsException <ArgumentNullException>(() => sporthallService.GetMostVisitedSportshall()); }
public ISportshallDto GetMostVisitedSportshall() { Sportshall sportshall = this.dbContext.Sportshalls?.Where(s => !s.IsDeleted) .OrderByDescending(c => c.Visits.Where(v => !v.IsDeleted).Count()) .FirstOrDefault(); Guard.WhenArgument(sportshall, "Most visited sportshall can not be null.").IsNull().Throw(); Guard.WhenArgument(sportshall.Visits.Where(v => !v.IsDeleted).Count(), "There no sportshall with visits.").IsLessThan(1).Throw(); ISportshallDto sportshallDto = this.mapper.Map <SportshallDto>(sportshall); Guard.WhenArgument(sportshallDto, "SportshallDto can not be null.").IsNull().Throw(); return(sportshallDto); }
public void ThrowsArgumentException_WhenSportshallWithTheSameNameAlreadyExists() { //Arrange var dbContextMock = new Mock <ISportscardSystemDbContext>(); var mapperMock = new Mock <IMapper>(); var expectedSportshall = new Sportshall() { Name = "Sportshall1" }; var data = new List <Sportshall> { new Sportshall { Name = "Sportshall1" }, new Sportshall { Name = "Sportshall2" }, new Sportshall { Name = "Sportshall3" } }; var mockSet = new Mock <DbSet <Sportshall> >(); mockSet.SetupData(data); mockSet.Setup(m => m.Add(It.IsAny <Sportshall>())); dbContextMock .Setup(x => x.Sportshalls) .Returns(mockSet.Object); var sportshallDto = new SportshallDto() { Name = "Sportshall1" }; mapperMock .Setup(x => x.Map <Sportshall>(sportshallDto)) .Returns(expectedSportshall); var sportshallService = new SportshallService(dbContextMock.Object, mapperMock.Object); //Act && Assert Assert.ThrowsException <ArgumentException>(() => sportshallService.AddSportshall(sportshallDto)); }
public void DeleteSportFromSportshall(string sport, string hallName) { Sportshall sportshall = this.dbContext.Sportshalls.FirstOrDefault(s => s.Name == hallName && s.IsDeleted != true); Guard.WhenArgument(sportshall, "No such sportshall.").IsNull().Throw(); Sport sportAtDb = this.dbContext.Sports.FirstOrDefault(s => s.Name == sport && !s.IsDeleted); Guard.WhenArgument(sportAtDb, "No such sport at database, please add it :-)").IsNull().Throw(); if (sportshall.Sports.Any(s => s.Name == sportAtDb.Name && s.IsDeleted == true)) { throw new ArgumentException($"We have already removed {sport} from {hallName}"); } else { sportshall.Sports.Remove(sportAtDb); this.dbContext.SaveChanges(); } }
public void AddSportToSportshall(string sport, string hallName) { Sportshall sportshall = this.dbContext.Sportshalls.FirstOrDefault(s => s.Name == hallName && s.IsDeleted != true); Guard.WhenArgument(sportshall, "No such sportshall.").IsNull().Throw(); Sport sportAtDb = this.dbContext.Sports.FirstOrDefault(s => s.Name == sport && !s.IsDeleted); Guard.WhenArgument(sportAtDb, "No such sport at database, please add it :-)").IsNull().Throw(); if (!(sportshall.Sports.Any(s => s.Name.ToLower() == sportAtDb.Name.ToLower() && !s.IsDeleted))) { //sportshall.Sports.Add(new Sport() { Id = sportAtDb.Id, Name = sportAtDb.Name }); sportshall.Sports.Add(sportAtDb); this.dbContext.SaveChanges(); } else { throw new ArgumentException($"There is already {sport} available at {hallName}"); } }
public void ReturnSporthallDto_WhenThereAreAnyVisits() { //Arrange var dbContextMock = new Mock <ISportscardSystemDbContext>(); var mapperMock = new Mock <IMapper>(); var sportsHall = new Sportshall() { Id = new Guid("db97a0eb-9411-4f1d-9ead-3997e6271324"), Name = "Gym", IsDeleted = false }; var sport = new Sport() { Id = new Guid("db97a0eb-9411-4f1d-9ead-3997e6271323"), Name = "Gym", IsDeleted = false }; var visit = new Visit() { Id = new Guid("db97a0eb-9411-4f1d-9ead-3997e6271325"), IsDeleted = false, Sport = sport, Sportshall = sportsHall, CreatedOn = DateTime.Now.Date }; var sportshallVisits = new List <Visit>(); sportshallVisits.Add(visit); sportsHall.Visits = sportshallVisits; var data = new List <Sportshall>() { new Sportshall() { Id = new Guid("db97a0eb-9411-4f1d-9ead-3997e6271321"), IsDeleted = false, Sports = new List <Sport>(), CreatedOn = DateTime.Now.Date } }; data.Add(sportsHall); var mockSet = new Mock <DbSet <Sportshall> >(); mockSet.SetupData(data); dbContextMock.Setup(x => x.Sportshalls).Returns(mockSet.Object); var expectedSportshallDto = new SportshallDto() { Id = new Guid("db97a0eb-9411-4f1d-9ead-3997e6271324"), Name = "Gym", }; mapperMock.Setup(x => x.Map <SportshallDto>(sportsHall)).Returns(expectedSportshallDto); var sporthallService = new SportshallService(dbContextMock.Object, mapperMock.Object); //Act var mostVisitedSportshall = sporthallService.GetMostVisitedSportshall(); //Assert Assert.AreEqual(expectedSportshallDto.Id, mostVisitedSportshall.Id); }
public void ReturnIEnumerableOfVisitDto_WhenCollectionIsNotNull() { //Arrange var dbContextMock = new Mock <ISportscardSystemDbContext>(); var mapperMock = new Mock <IMapper>(); var client = new Client() { Id = new Guid("db97a0eb-9411-4f1d-9ead-3997e6271324"), FirstName = "Georgi", LastName = "Georgiev", IsDeleted = false, Visits = new List <Visit>() }; var sport = new Sport() { Id = new Guid("db97a0eb-9411-4f1d-9ead-3997e6271323"), Name = "Gym", IsDeleted = false, }; var sportshall = new Sportshall() { Id = new Guid("db97a0eb-9411-4f1d-9ead-3997e6271322"), Name = "Topfit", IsDeleted = false, }; var data = new List <Visit> { new Visit { ClientId = client.Id, Client = client, SportId = sport.Id, Sport = sport, SportshallId = sportshall.Id, Sportshall = sportshall, CreatedOn = DateTime.Today.AddDays(-1) } }; var mockSet = new Mock <DbSet <Visit> >(); mockSet.SetupData(data); Mapper.Reset(); AutomapperConfiguration.Initialize(); dbContextMock .Setup(x => x.Visits) .Returns(mockSet.Object); var visitService = new VisitService(dbContextMock.Object, mapperMock.Object); //Act var visits = visitService.GetAllVisits(); //Assert Assert.AreEqual(data.Count, visits.Count()); Assert.IsNotNull(visits); Assert.IsInstanceOfType(visits, typeof(IEnumerable <IVisitViewDto>)); }