Ejemplo n.º 1
0
        public async Task CreateInstanceOfTypeBan()
        {
            //Arrange
            var options    = TestUtilities.GetOptions(nameof(CreateInstanceOfTypeBan));
            var mapperMock = new Mock <IDtoMapper <User, UserDto> >();
            var testGuid   = Guid.NewGuid();
            var user       = new User {
                Id = testGuid, UserName = "******"
            };

            using (var actContext = new CWContext(options))
            {
                //Act
                //Guid id, string description, int period
                var sut = new BanService(actContext, mapperMock.Object);
                await actContext.Users.AddAsync(user);

                await actContext.SaveChangesAsync();

                await sut.CreateAsync(testGuid, "testDescription", 3);

                await actContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Assert
                var ban = assertContext.Bans.FirstOrDefault(b => b.User == user);
                Assert.IsInstanceOfType(ban, typeof(Ban));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create(Schedule schedule)
        {
            var r = _context.Routes.Include(x => x.CityFromNavigation)
                    .Include(x => x.CityToNavigation)
                    .FirstOrDefault(x => x.RouteId == schedule.RouteId);
            var dist = CalcDist(r.CityFromNavigation, r.CityToNavigation);

            schedule.EndDateTime = CalcTime(schedule, dist);
            if (ModelState.IsValid)
            {
                schedule.DateAdded   = DateTime.Now;
                schedule.DateUpdated = DateTime.Now;
                _context.Add(schedule);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["Route"] = new SelectList(_context.Routes
                                               .Include(x => x.CityFromNavigation)
                                               .Include(x => x.CityToNavigation), "RouteId", "RouteName", schedule.RouteId);
            ViewData["Transport"] = new SelectList(_context.Transports
                                                   .Include(x => x.Brand), "TransportId", "TransportName", schedule.TransportId);
            ViewData["Worker"] = new SelectList(_context.Workers.Include(x => x.Role)
                                                .Where(x => x.Role.Role1.Contains("Driver")), "WorkerId", "WorkerFullName", schedule.WorkerId);

            return(View(schedule));
        }
Ejemplo n.º 3
0
        public async Task SetCorrectInputValues()
        {
            //Arrange
            var options    = TestUtilities.GetOptions(nameof(SetCorrectInputValues));
            var mapperMock = new Mock <IDtoMapper <User, UserDto> >();
            var testGuid   = Guid.NewGuid();
            var user       = new User {
                Id = testGuid, UserName = "******"
            };

            using (var actContext = new CWContext(options))
            {
                //Act
                var sut = new BanService(actContext, mapperMock.Object);
                await actContext.Users.AddAsync(user);

                await actContext.SaveChangesAsync();

                await sut.CreateAsync(testGuid, "description", 3);

                await actContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Assert
                var ban = assertContext.Bans.Include(b => b.User).FirstOrDefault(b => b.User.UserName == "Pesho");
                Assert.AreEqual("Pesho", ban.User.UserName);
                Assert.AreEqual("description", ban.Description);
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> CreateBrand(Brand brand)
        {
            if (ModelState.IsValid)
            {
                brand.DateAdded   = DateTime.Now;
                brand.DateUpdated = DateTime.Now;
                _context.Add(brand);
                await _context.SaveChangesAsync();

                //return RedirectToAction(nameof(Index));
                return(RedirectToAction("Create"));
            }
            return(View(brand));
        }
        public async Task ReturnCorrectCountOfDtos()
        {
            //Arrange
            var options            = TestUtilities.GetOptions(nameof(ReturnCorrectCountOfDtos));
            var mapperMock         = new Mock <IDtoMapper <Bar, BarDto> >();
            var searchMapperMock   = new Mock <IDtoMapper <Bar, SearchBarDto> >();
            var cocktailMapperMock = new Mock <IDtoMapper <Cocktail, CocktailDto> >();
            var testGuid           = Guid.NewGuid();
            var testGuid2          = Guid.NewGuid();
            var testGuid3          = Guid.NewGuid();

            var bar1 = new Bar
            {
                Id   = testGuid,
                Name = "testBar1",
            };
            var bar2 = new Bar
            {
                Id   = testGuid2,
                Name = "testBar2",
            };
            var bar3 = new Bar
            {
                Id   = testGuid3,
                Name = "testBar3",
            };

            var list = new List <BarDto>()
            {
                new BarDto {
                    Id = testGuid, Name = "testBar1"
                }, new BarDto {
                    Id = testGuid2, Name = "testBar2"
                }, new BarDto {
                    Id = testGuid3, Name = "testBar3"
                }
            };

            mapperMock.Setup(x => x.MapFrom(It.IsAny <ICollection <Bar> >())).Returns(list);

            using (var arrangeContext = new CWContext(options))
            {
                await arrangeContext.Bars.AddAsync(bar1);

                await arrangeContext.Bars.AddAsync(bar2);

                await arrangeContext.Bars.AddAsync(bar3);

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Act & Assert
                var sut    = new BarService(assertContext, mapperMock.Object, searchMapperMock.Object, cocktailMapperMock.Object);
                var result = await sut.GetTopBarsAsync(3);

                Assert.AreEqual(3, result.Count());
            }
        }
Ejemplo n.º 6
0
        public async Task ThrowWhen_NoBarFound()
        {
            //Arrange
            var options            = TestUtilities.GetOptions(nameof(ThrowWhen_NoBarFound));
            var mapperMock         = new Mock <IDtoMapper <Bar, BarDto> >();
            var searchMapperMock   = new Mock <IDtoMapper <Bar, SearchBarDto> >();
            var cocktailMapperMock = new Mock <IDtoMapper <Cocktail, CocktailDto> >();
            var testGuid           = Guid.NewGuid();
            var testGuid2          = Guid.NewGuid();

            var bar = new Bar
            {
                Id      = testGuid,
                Name    = "testBar",
                Info    = "testInfo",
                Address = "testAddress",
                Phone   = "111-333-666",
            };

            mapperMock.Setup(x => x.MapFrom(It.IsAny <Bar>())).Returns(It.IsAny <BarDto>);

            using (var arrangeContext = new CWContext(options))
            {
                await arrangeContext.Bars.AddAsync(bar);

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                var sut = new BarService(assertContext, mapperMock.Object, searchMapperMock.Object, cocktailMapperMock.Object);
                await Assert.ThrowsExceptionAsync <BusinessLogicException>(() => sut.EditAsync(testGuid2, "newTestName", "newTestInfo", "newTestAddress", "111-333-6667"));
            }
        }
        public async Task UpdatePropertiesWhen_ValuesAreValid()
        {
            //Arrange
            var options          = TestUtilities.GetOptions(nameof(UpdatePropertiesWhen_ValuesAreValid));
            var mapperMock       = new Mock <IDtoMapper <Ingredient, IngredientDto> >();
            var ingTestGuid      = Guid.NewGuid();
            var cocktailTestGuid = Guid.NewGuid();

            var ingredient = new Ingredient
            {
                Id        = ingTestGuid,
                Name      = "djodjan",
                IsDeleted = false,
            };

            using (var assertContext = new CWContext(options))
            {
                //Act & Assert
                await assertContext.Ingredients.AddAsync(ingredient);

                await assertContext.SaveChangesAsync();

                var sut    = new IngredientService(assertContext, mapperMock.Object);
                var result = await sut.DeleteAsync(ingTestGuid);

                Assert.AreEqual(true, ingredient.IsDeleted);
            }
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> Create(Worker worker)
        {
            if (ModelState.IsValid)
            {
                worker.DateAdded   = DateTime.Now;
                worker.DateUpdated = DateTime.Now;
                //worker.WorkerId = _context.Workers.Count()+1;
                worker.WorkerId = _context.Workers.Max(x => x.WorkerId) + 1;
                _context.Workers.Add(worker);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["RoleId"] = new SelectList(_context.Roles.Where(x => x.Role1 != "Admin"), "RoleId", "Role1", worker.RoleId);
            return(View(worker));
        }
        public async Task ReturnNullWhen_IngredientIdIsNull()
        {
            //Arrange
            var options    = TestUtilities.GetOptions(nameof(ReturnNullWhen_IngredientIdIsNull));
            var mapperMock = new Mock <IDtoMapper <Ingredient, IngredientDto> >();
            var testGuid   = new Guid("b9653c65-2311-4d57-a95b-6522d7bc88f3");

            var entity = new Ingredient
            {
                Id        = testGuid,
                Name      = "djodjan4",
                IsDeleted = false
            };

            using (var actContext = new CWContext(options))
            {
                //Act
                await actContext.Ingredients.AddAsync(entity);

                await actContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Assert
                var sut        = new IngredientService(assertContext, mapperMock.Object);
                var ingredient = await sut.GetIngredientAsync(null);

                Assert.IsNull(ingredient);
            }
        }
Ejemplo n.º 10
0
        public async Task ThrowWhen_BarNotFound()
        {
            //Arrange
            var options            = TestUtilities.GetOptions(nameof(ThrowWhen_BarNotFound));
            var mapperMock         = new Mock <IDtoMapper <Bar, BarDto> >();
            var searchMapperMock   = new Mock <IDtoMapper <Bar, SearchBarDto> >();
            var cocktailMapperMock = new Mock <IDtoMapper <Cocktail, CocktailDto> >();
            var testGuid           = Guid.NewGuid();
            var testGuid2          = Guid.NewGuid();

            mapperMock.Setup(x => x.MapFrom(It.IsAny <Bar>())).Returns(It.IsAny <BarDto>);

            var bar1 = new Bar
            {
                Id   = testGuid,
                Name = "testBar1",
            };

            using (var arrangeContext = new CWContext(options))
            {
                await arrangeContext.Bars.AddAsync(bar1);

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Act & Assert
                var sut = new BarService(assertContext, mapperMock.Object, searchMapperMock.Object, cocktailMapperMock.Object);
                await Assert.ThrowsExceptionAsync <BusinessLogicException>(() => sut.GetBarAsync(testGuid2));
            }
        }
        public async Task CorrectlyRemoveCocktailsWhen_ParamsAreValid()
        {
            //Arrange
            var options            = TestUtilities.GetOptions(nameof(CorrectlyRemoveCocktailsWhen_ParamsAreValid));
            var mapperMock         = new Mock <IDtoMapper <Bar, BarDto> >();
            var searchMapperMock   = new Mock <IDtoMapper <Bar, SearchBarDto> >();
            var cocktailMapperMock = new Mock <IDtoMapper <Cocktail, CocktailDto> >();
            var testGuid           = Guid.NewGuid();
            var cocktailGuid       = Guid.NewGuid();

            var bar1 = new Bar
            {
                Id   = testGuid,
                Name = "testBar1",
            };

            var barDto1 = new BarDto
            {
                Id   = testGuid,
                Name = "testBar1",
            };

            var list = new List <string> {
                "Boza"
            };

            var cocktail = new Cocktail {
                Id = cocktailGuid, Name = "Boza"
            };
            var barCocktail = new BarCocktail {
                BarId = bar1.Id, CocktailId = cocktailGuid
            };

            mapperMock.Setup(x => x.MapFrom(It.IsAny <Bar>())).Returns(barDto1);

            using (var arrangeContext = new CWContext(options))
            {
                await arrangeContext.Bars.AddAsync(bar1);

                await arrangeContext.Cocktails.AddAsync(cocktail);

                await arrangeContext.BarCocktails.AddAsync(barCocktail);

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Act & Assert
                var sut    = new BarService(assertContext, mapperMock.Object, searchMapperMock.Object, cocktailMapperMock.Object);
                var result = await sut.RemoveCocktailsAsync(barDto1, list);

                var barCocktailEntity = await assertContext.BarCocktails.FirstAsync();

                Assert.IsInstanceOfType(result, typeof(BarDto));
                Assert.AreEqual("testBar1", result.Name);
                Assert.AreEqual(true, barCocktailEntity.IsDeleted);
            }
        }
Ejemplo n.º 12
0
        public async Task ReturnCorectTypeOfInstance()
        {
            //Arrange
            var options            = TestUtilities.GetOptions(nameof(ReturnCorectTypeOfInstance));
            var mapperMock         = new Mock <IDtoMapper <Bar, BarDto> >();
            var searchMapperMock   = new Mock <IDtoMapper <Bar, SearchBarDto> >();
            var cocktailMapperMock = new Mock <IDtoMapper <Cocktail, CocktailDto> >();
            var guid = Guid.NewGuid();

            var entityDto = new BarDto
            {
                Id        = guid,
                Name      = "testBar",
                Info      = "testInfo",
                Address   = "testAddress",
                ImagePath = "testImagePath",
                Phone     = "111-333-666"
            };

            var bar = new Bar
            {
                Id        = guid,
                Name      = "testBar",
                Info      = "testInfo",
                Address   = "testAddress",
                ImagePath = "testImagePath",
                Phone     = "111-333-666"
            };

            var cocktail = new Cocktail
            {
                Id   = Guid.NewGuid(),
                Name = "testCocktail",
                Info = "testCocktailInfo",
            };

            var list = new List <string>()
            {
                "testCocktail"
            };

            using (var arrangeContext = new CWContext(options))
            {
                await arrangeContext.Bars.AddAsync(bar);

                await arrangeContext.Cocktails.AddAsync(cocktail);

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Act & Assert
                var sut    = new BarService(assertContext, mapperMock.Object, searchMapperMock.Object, cocktailMapperMock.Object);
                var result = await sut.AddCocktailsAsync(entityDto, list);

                Assert.IsInstanceOfType(result, typeof(BarDto));
            }
        }
        public async Task CorrectlyDeleteCocktail()
        {
            //Arrange
            var options = TestUtilities.GetOptions(nameof(CorrectlyDeleteCocktail));

            var mapperMock                = new Mock <IDtoMapper <Cocktail, CocktailDto> >();
            var barMapperMock             = new Mock <IDtoMapper <Bar, BarDto> >();
            var cocktailDetailsMapperMock = new Mock <IDtoMapper <Cocktail, DetailsCocktailDto> >();
            var ingredientMapperMock      = new Mock <IDtoMapper <Ingredient, IngredientDto> >();

            var ingredientServiceMock         = new Mock <IIngredientService>();
            var cocktailIngredientServiceMock = new Mock <ICocktailIngredientService>();

            var testGuid = Guid.NewGuid();

            var cocktail = new Cocktail
            {
                Id        = testGuid,
                Name      = "TestOneName",
                ImagePath = "ImagePathOne"
            };

            using (var actContext = new CWContext(options))
            {
                //Act
                await actContext.Cocktails.AddAsync(cocktail);

                await actContext.SaveChangesAsync();

                var service = new CocktailService(actContext, mapperMock.Object, barMapperMock.Object, cocktailDetailsMapperMock.Object,
                                                  ingredientServiceMock.Object, cocktailIngredientServiceMock.Object);
                var result = await service.DeleteAsync(testGuid);

                await actContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Assert

                var result = await assertContext.Cocktails.FirstAsync();

                Assert.AreEqual(true, result.IsDeleted);
            }
        }
Ejemplo n.º 14
0
        public async Task ThrowWhen_NobarFound()
        {
            //Arrange
            var options            = TestUtilities.GetOptions(nameof(ThrowWhen_NobarFound));
            var mapperMock         = new Mock <IDtoMapper <Bar, BarDto> >();
            var searchMapperMock   = new Mock <IDtoMapper <Bar, SearchBarDto> >();
            var cocktailMapperMock = new Mock <IDtoMapper <Cocktail, CocktailDto> >();
            var guid = Guid.NewGuid();

            var entityDto = new BarDto
            {
                Id        = Guid.NewGuid(),
                Name      = "testBar",
                Info      = "testInfo",
                Address   = "testAddress",
                ImagePath = "testImagePath",
                Phone     = "111-333-666"
            };

            var bar = new Bar
            {
                Id        = guid,
                Name      = "testBar",
                Info      = "testInfo",
                Address   = "testAddress",
                ImagePath = "testImagePath",
                Phone     = "111-333-666"
            };

            var cocktail = new Cocktail
            {
                Id   = Guid.NewGuid(),
                Name = "testCocktail",
                Info = "testCocktailInfo",
            };

            var list = new List <string>()
            {
                "testCocktail"
            };

            using (var arrangeContext = new CWContext(options))
            {
                await arrangeContext.Bars.AddAsync(bar);

                await arrangeContext.Cocktails.AddAsync(cocktail);

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Act & Assert
                var sut = new BarService(assertContext, mapperMock.Object, searchMapperMock.Object, cocktailMapperMock.Object);
                await Assert.ThrowsExceptionAsync <BusinessLogicException>(() => sut.AddCocktailsAsync(entityDto, list));
            }
        }
Ejemplo n.º 15
0
        static async Task FillAreas()
        {
            var hh    = new HeadHunterScraper();
            var areas = await hh.GetAreasAsync();

            using var db = new CWContext();
            await db.Areas.AddRangeAsync(areas);

            await db.SaveChangesAsync();
        }
Ejemplo n.º 16
0
        public async Task ReturnInstanceOfCollectionTypeBarDto()
        {
            var options            = TestUtilities.GetOptions(nameof(ReturnInstanceOfCollectionTypeBarDto));
            var mapperMock         = new Mock <IDtoMapper <Bar, BarDto> >();
            var searchMapperMock   = new Mock <IDtoMapper <Bar, SearchBarDto> >();
            var cocktailMapperMock = new Mock <IDtoMapper <Cocktail, CocktailDto> >();
            var testGuid           = Guid.NewGuid();
            var testGuid2          = Guid.NewGuid();

            var bar1 = new Bar
            {
                Id   = testGuid,
                Name = "testBar1",
            };
            var bar2 = new Bar
            {
                Id   = testGuid2,
                Name = "testBar2",
            };

            var list = new List <BarDto>()
            {
                new BarDto {
                    Id = testGuid, Name = "testBar1"
                }, new BarDto {
                    Id = testGuid2, Name = "testBar2"
                }
            };


            mapperMock.Setup(x => x.MapFrom(It.IsAny <ICollection <Bar> >())).Returns(list);

            using (var arrangeContext = new CWContext(options))
            {
                await arrangeContext.Bars.AddAsync(bar1);

                await arrangeContext.Bars.AddAsync(bar2);

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Act & Assert
                var sut    = new BarService(assertContext, mapperMock.Object, searchMapperMock.Object, cocktailMapperMock.Object);
                var result = await sut.GetAllBarsAsync();

                Assert.IsInstanceOfType(result, typeof(ICollection <BarDto>));
                Assert.AreEqual(2, result.Count());
                Assert.AreEqual(bar1.Name, result.First().Name);
                Assert.AreEqual(bar1.Id, result.First().Id);
                Assert.AreEqual(bar2.Name, result.Last().Name);
                Assert.AreEqual(bar2.Id, result.Last().Id);
            }
        }
Ejemplo n.º 17
0
        public async Task CorrectlyRemoveBan_WhenParamsAreValid()
        {
            //Arrange
            var options    = TestUtilities.GetOptions(nameof(CorrectlyRemoveBan_WhenParamsAreValid));
            var mapperMock = new Mock <IDtoMapper <User, UserDto> >();
            var testGuid   = Guid.NewGuid();
            var userTest   = new User {
                Id = testGuid, UserName = "******", IsBanned = true, LockoutEnabled = true, LockoutEnd = DateTime.UtcNow.AddDays(1)
            };
            var banTest = new Ban {
                HasExpired = false, User = userTest
            };

            using (var actContext = new CWContext(options))
            {
                //Act
                await actContext.Users.AddAsync(userTest);

                await actContext.Bans.AddAsync(banTest);

                await actContext.SaveChangesAsync();

                var sut = new BanService(actContext, mapperMock.Object);
                await sut.RemoveAsync(testGuid);

                await actContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Assert
                var user = await assertContext.Users.FirstAsync();

                var ban = assertContext.Bans
                          .Include(u => u.User)
                          .Where(b => b.User == user)
                          .FirstOrDefault();
                Assert.AreEqual(user.IsBanned, false);
                Assert.AreEqual(ban.HasExpired, true);
                Assert.AreEqual(user.LockoutEnd < DateTime.Now, true);
            }
        }
        public async Task ReturnCorrectTypeOfInstance()
        {
            //Arrange
            var options = TestUtilities.GetOptions(nameof(ReturnCorrectTypeOfInstance));

            var mapperMock                = new Mock <IDtoMapper <Cocktail, CocktailDto> >();
            var barMapperMock             = new Mock <IDtoMapper <Bar, BarDto> >();
            var cocktailDetailsMapperMock = new Mock <IDtoMapper <Cocktail, DetailsCocktailDto> >();
            var ingredientMapperMock      = new Mock <IDtoMapper <Ingredient, IngredientDto> >();

            var ingredientServiceMock         = new Mock <IIngredientService>();
            var cocktailIngredientServiceMock = new Mock <ICocktailIngredientService>();

            var testGuid = Guid.NewGuid();

            var entity = new Cocktail
            {
                Id        = testGuid,
                Name      = "TestOneName",
                ImagePath = "ImagePathOne",
                Info      = "TestInfo"
            };

            var dtoEntity = new CocktailDto
            {
                Id        = testGuid,
                Name      = "TestOneName",
                ImagePath = "ImagePathOne",
                Info      = "TestInfo"
            };

            mapperMock.Setup(x => x.MapFrom(It.IsAny <Cocktail>())).Returns(dtoEntity);

            using (var actContext = new CWContext(options))
            {
                //Act
                await actContext.Cocktails.AddAsync(entity);

                await actContext.SaveChangesAsync();
            }
            using (var assertContext = new CWContext(options))
            {
                //Assert
                var sut = new CocktailService(assertContext, mapperMock.Object, barMapperMock.Object, cocktailDetailsMapperMock.Object,
                                              ingredientServiceMock.Object, cocktailIngredientServiceMock.Object);
                var result = await sut.DeleteAsync(testGuid);

                Assert.IsInstanceOfType(result, typeof(CocktailDto));
                Assert.AreEqual(dtoEntity.Name, result.Name);
                Assert.AreEqual(dtoEntity.Info, result.Info);
                Assert.AreEqual(dtoEntity.ImagePath, result.ImagePath);
                Assert.AreEqual(dtoEntity.Id, result.Id);
            }
        }
Ejemplo n.º 19
0
        public async Task ReturnCorrectElementsWhen_SortIsByNameAsc()
        {
            //Arrange
            var options            = TestUtilities.GetOptions(nameof(ReturnCorrectElementsWhen_SortIsByNameAsc));
            var mapperMock         = new Mock <IDtoMapper <Bar, BarDto> >();
            var searchMapperMock   = new Mock <IDtoMapper <Bar, SearchBarDto> >();
            var cocktailMapperMock = new Mock <IDtoMapper <Cocktail, CocktailDto> >();
            var testGuid           = Guid.NewGuid();
            var testGuid2          = Guid.NewGuid();

            var bar1 = new Bar
            {
                Id   = testGuid,
                Name = "atestBar",
            };
            var bar2 = new Bar
            {
                Id   = testGuid2,
                Name = "ztestBar",
            };

            var list = new List <BarDto>()
            {
                new BarDto {
                    Id = testGuid2, Name = "ztestBar"
                }, new BarDto {
                    Id = testGuid, Name = "atestBar"
                }
            };

            mapperMock.Setup(x => x.MapFrom(It.IsAny <ICollection <Bar> >())).Returns(list);

            using (var arrangeContext = new CWContext(options))
            {
                await arrangeContext.Bars.AddAsync(bar1);

                await arrangeContext.Bars.AddAsync(bar2);

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Act & Assert
                var sut    = new BarService(assertContext, mapperMock.Object, searchMapperMock.Object, cocktailMapperMock.Object);
                var result = await sut.GetFiveBarsAsync(1, "Name");

                Assert.AreEqual("atestBar", result.ToArray()[1].Name);
                Assert.AreEqual(testGuid, result.ToArray()[1].Id);
                Assert.AreEqual("ztestBar", result.ToArray()[0].Name);
                Assert.AreEqual(testGuid2, result.ToArray()[0].Id);
            }
        }
Ejemplo n.º 20
0
        public async Task <IActionResult> Create(Route route)
        {
            var cityfrom = _context.Cities.FirstOrDefault(x => x.CityId == route.CityFrom);
            var cityto   = _context.Cities.FirstOrDefault(x => x.CityId == route.CityTo);

            route.Mileage = (int?)CalcDist(cityfrom, cityto);
            if (ModelState.IsValid)
            {
                route.DateAdded   = DateTime.Now;
                route.DateUpdated = DateTime.Now;
                _context.Add(route);
                await _context.SaveChangesAsync();

                var r = _context.Routes.ToList().Last();
                AddStoping(r);
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CityFrom"] = new SelectList(_context.Cities, "CityId", "City1", route.CityFrom);
            ViewData["CityTo"]   = new SelectList(_context.Cities, "CityId", "City1", route.CityTo);
            return(View(route));
        }
        public async Task ThrowWhen_IngredientNotFound()
        {
            //Arrange
            var options            = TestUtilities.GetOptions(nameof(ThrowWhen_IngredientNotFound));
            var mapperMock         = new Mock <IDtoMapper <Bar, BarDto> >();
            var searchMapperMock   = new Mock <IDtoMapper <Bar, SearchBarDto> >();
            var cocktailMapperMock = new Mock <IDtoMapper <Cocktail, CocktailDto> >();
            var testGuid           = Guid.NewGuid();
            var cocktailGuid       = Guid.NewGuid();

            var bar1 = new Bar
            {
                Id   = testGuid,
                Name = "testBar1",
            };

            var barDto1 = new BarDto
            {
                Id   = testGuid,
                Name = "testBar1",
            };

            var list = new List <string> {
                "Banica"
            };

            var cocktail = new Cocktail {
                Id = cocktailGuid, Name = "Boza"
            };
            var barCocktail = new BarCocktail {
                BarId = bar1.Id, CocktailId = cocktailGuid
            };

            mapperMock.Setup(x => x.MapFrom(It.IsAny <Bar>())).Returns(barDto1);

            using (var arrangeContext = new CWContext(options))
            {
                await arrangeContext.Bars.AddAsync(bar1);

                await arrangeContext.Cocktails.AddAsync(cocktail);

                await arrangeContext.BarCocktails.AddAsync(barCocktail);

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Act & Assert
                var sut = new BarService(assertContext, mapperMock.Object, searchMapperMock.Object, cocktailMapperMock.Object);
                await Assert.ThrowsExceptionAsync <BusinessLogicException>(() => sut.RemoveCocktailsAsync(barDto1, list));
            }
        }
Ejemplo n.º 22
0
        public async Task ReturnCorrectTypeOfInstance()
        {
            //Arrange
            var options            = TestUtilities.GetOptions(nameof(ReturnCorrectTypeOfInstance));
            var mapperMock         = new Mock <IDtoMapper <Bar, BarDto> >();
            var searchMapperMock   = new Mock <IDtoMapper <Bar, SearchBarDto> >();
            var cocktailMapperMock = new Mock <IDtoMapper <Cocktail, CocktailDto> >();
            var guid = Guid.NewGuid();

            var entity = new Bar
            {
                Id        = guid,
                Name      = "testBar",
                Info      = "testInfo",
                Address   = "testAddress",
                ImagePath = "testImagePath",
                Phone     = "111-333-666"
            };

            var dtoEntity = new BarDto
            {
                Id        = guid,
                Name      = "testBar",
                Info      = "testInfo",
                Address   = "testAddress",
                ImagePath = "testImagePath",
                Phone     = "111-333-666"
            };

            mapperMock.Setup(x => x.MapFrom(It.IsAny <Bar>())).Returns(dtoEntity);

            using (var actContext = new CWContext(options))
            {
                //Act
                await actContext.Bars.AddAsync(entity);

                await actContext.SaveChangesAsync();
            }
            using (var assertContext = new CWContext(options))
            {
                //Assert
                var sut    = new BarService(assertContext, mapperMock.Object, searchMapperMock.Object, cocktailMapperMock.Object);
                var result = await sut.DeleteAsync(guid);

                Assert.IsInstanceOfType(result, typeof(BarDto));
                Assert.AreEqual(dtoEntity.Name, result.Name);
                Assert.AreEqual(dtoEntity.Info, result.Info);
                Assert.AreEqual(dtoEntity.Address, result.Address);
                Assert.AreEqual(dtoEntity.ImagePath, result.ImagePath);
                Assert.AreEqual(dtoEntity.Phone, result.Phone);
                Assert.AreEqual(dtoEntity.Id, result.Id);
            }
        }
Ejemplo n.º 23
0
        public async Task ReturnCorrectTypeOfCollectionWhen_ValidValueIsPassed()
        {
            //Arrange
            var options    = TestUtilities.GetOptions(nameof(ReturnCorrectTypeOfCollectionWhen_ValidValueIsPassed));
            var mapperMock = new Mock <IDtoMapper <Ingredient, IngredientDto> >();

            var testGuid  = Guid.NewGuid();
            var testGuid2 = Guid.NewGuid();

            var entity = new Ingredient
            {
                Id        = testGuid,
                Name      = "djodjan1",
                IsDeleted = false
            };
            var entity2 = new Ingredient
            {
                Id        = testGuid2,
                Name      = "djodjan2",
                IsDeleted = false
            };

            var list = new List <IngredientDto>()
            {
                new IngredientDto {
                    Id = testGuid, Name = "djodjan"
                }, new IngredientDto {
                    Id = testGuid2, Name = "testIngredient"
                }
            };

            mapperMock.Setup(x => x.MapFrom(It.IsAny <ICollection <Ingredient> >())).Returns(list);

            using (var arrangeContext = new CWContext(options))
            {
                await arrangeContext.Ingredients.AddAsync(entity);

                await arrangeContext.Ingredients.AddAsync(entity2);

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Act & Assert
                var sut    = new IngredientService(assertContext, mapperMock.Object);
                var result = await sut.GetTenIngredientsOrderedByNameAsync(1);

                Assert.IsInstanceOfType(result, typeof(ICollection <IngredientDto>));
                Assert.AreEqual(result.Count, 2);
            }
        }
Ejemplo n.º 24
0
        public async Task CorrectlyDeleteBar()
        {
            //Arrange
            var options            = TestUtilities.GetOptions(nameof(CorrectlyDeleteBar));
            var mapperMock         = new Mock <IDtoMapper <Bar, BarDto> >();
            var searchMapperMock   = new Mock <IDtoMapper <Bar, SearchBarDto> >();
            var cocktailMapperMock = new Mock <IDtoMapper <Cocktail, CocktailDto> >();
            var guid = Guid.NewGuid();

            var entity = new Bar
            {
                Id        = guid,
                Name      = "testBar",
                Info      = "testInfo",
                Address   = "testAddress",
                ImagePath = "testImagePath",
                Phone     = "111-333-666"
            };

            using (var actContext = new CWContext(options))
            {
                //Act
                await actContext.Bars.AddAsync(entity);

                await actContext.SaveChangesAsync();

                var service = new BarService(actContext, mapperMock.Object, searchMapperMock.Object, cocktailMapperMock.Object);
                var result  = await service.DeleteAsync(guid);

                await actContext.SaveChangesAsync();
            }
            using (var assertContext = new CWContext(options))
            {
                //Assert
                var result = await assertContext.Bars.FirstAsync();

                Assert.AreEqual(true, result.IsDeleted);
            }
        }
        public async Task ReturnCorrectObjects_WhenParamIsActive()
        {
            //Arrange
            var options    = TestUtilities.GetOptions(nameof(ReturnCorrectObjects_WhenParamIsActive));
            var mapperMock = new Mock <IDtoMapper <User, UserDto> >();
            var testGuid   = Guid.NewGuid();
            var testGuid2  = Guid.NewGuid();

            var users = new List <User>
            {
                new User {
                    Id = testGuid, UserName = "******", IsBanned = false
                },
                new User {
                    Id = testGuid2, UserName = "******", IsBanned = false
                },
            };

            var userDtos = new List <UserDto>
            {
                new UserDto {
                    Id = testGuid, UserName = "******", IsBanned = false
                },
                new UserDto {
                    Id = testGuid2, UserName = "******", IsBanned = false
                },
            };


            mapperMock.Setup(u => u.MapFrom(It.IsAny <ICollection <User> >())).Returns(userDtos);

            using (var actContext = new CWContext(options))
            {
                //Act & Assert
                var sut = new BanService(actContext, mapperMock.Object);
                await actContext.Users.AddAsync(users[0]);

                await actContext.Users.AddAsync(users[1]);

                await actContext.SaveChangesAsync();

                var result = await sut.GetAllAsync("active");

                Assert.AreEqual(users[0].UserName, result.AsQueryable().First().UserName);
                Assert.AreEqual(users[0].Id, result.AsQueryable().First().Id);
                Assert.AreEqual(users[0].IsBanned, result.AsQueryable().First().IsBanned);
                Assert.AreEqual(users[1].UserName, result.AsQueryable().Last().UserName);
                Assert.AreEqual(users[1].Id, result.AsQueryable().Last().Id);
                Assert.AreEqual(users[1].IsBanned, result.AsQueryable().Last().IsBanned);
            }
        }
Ejemplo n.º 26
0
        public async Task ReturnCorrectTypeOfEntity()
        {
            //Arrange
            var options = TestUtilities.GetOptions(nameof(ReturnCorrectTypeOfEntity));

            var mapperMock = new Mock <IDtoMapper <CocktailComment, CocktailCommentDto> >();

            var id         = Guid.NewGuid();
            var cocktailId = Guid.NewGuid();
            var userId     = Guid.NewGuid();

            var createdOn = DateTime.UtcNow;

            var entityDto = new CocktailCommentDto
            {
                Id         = id,
                CocktailId = cocktailId,
                UserId     = userId,
                UserName   = "******",
                Body       = "testbody",
                CreatedOn  = createdOn
            };

            var entity = new CocktailComment
            {
                Id         = id,
                CocktailId = cocktailId,
                UserId     = userId,
                Body       = "testbody",
                CreatedOn  = createdOn
            };

            mapperMock.Setup(x => x.MapFrom(It.IsAny <CocktailComment>())).Returns(entityDto);

            using (var arrangeContext = new CWContext(options))
            {
                await arrangeContext.CocktailComments.AddAsync(entity);

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Act & Assert
                var sut = new CocktailCommentService(assertContext, mapperMock.Object);

                var result = await sut.EditAsync(id, "newbody");

                Assert.IsInstanceOfType(result, typeof(CocktailCommentDto));
            }
        }
Ejemplo n.º 27
0
        public async Task ReturnInstanceOfCollectionTypeIngredientDto()
        {
            //Arrange
            var options    = TestUtilities.GetOptions(nameof(ReturnInstanceOfCollectionTypeIngredientDto));
            var mapperMock = new Mock <IDtoMapper <Ingredient, IngredientDto> >();
            var testGuid   = new Guid();
            var testGuid2  = new Guid();

            var entity = new Ingredient
            {
                Id        = testGuid,
                Name      = "djodjan",
                IsDeleted = false
            };
            var entity2 = new Ingredient
            {
                Id        = testGuid2,
                Name      = "testIngredient",
                IsDeleted = false
            };

            var list = new List <IngredientDto>()
            {
                new IngredientDto {
                    Id = testGuid, Name = "djodjan"
                }, new IngredientDto {
                    Id = testGuid2, Name = "testIngredient"
                }
            };

            mapperMock.Setup(x => x.MapFrom(It.IsAny <ICollection <Ingredient> >())).Returns(list);

            using (var arrangeContext = new CWContext(options))
            {
                await arrangeContext.Ingredients.AddAsync(entity);

                await arrangeContext.Ingredients.AddAsync(entity2);

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Act & Assert
                var sut        = new IngredientService(assertContext, mapperMock.Object);
                var ingredient = await sut.GetIngredientsAsync();

                Assert.IsInstanceOfType(ingredient, typeof(ICollection <IngredientDto>));
            }
        }
        public async Task ReturnCorrectDtoWhen_ParamsAreValid()
        {
            //Arrange
            var options = TestUtilities.GetOptions(nameof(ReturnCorrectDtoWhen_ParamsAreValid));

            var mapperMock                = new Mock <IDtoMapper <Cocktail, CocktailDto> >();
            var barMapperMock             = new Mock <IDtoMapper <Bar, BarDto> >();
            var cocktailDetailsMapperMock = new Mock <IDtoMapper <Cocktail, DetailsCocktailDto> >();
            var ingredientMapperMock      = new Mock <IDtoMapper <Ingredient, IngredientDto> >();

            var ingredientServiceMock         = new Mock <IIngredientService>();
            var cocktailIngredientServiceMock = new Mock <ICocktailIngredientService>();

            var testGuid = Guid.NewGuid();

            var cocktail = new Cocktail
            {
                Id        = testGuid,
                Name      = "TestOneName",
                ImagePath = "ImagePathOne"
            };

            var cocktailDto = new CocktailDto
            {
                Id        = testGuid,
                Name      = "TestOneName",
                ImagePath = "ImagePathOne"
            };

            mapperMock.Setup(x => x.MapFrom(It.IsAny <Cocktail>())).Returns(cocktailDto);

            using (var arrangeContext = new CWContext(options))
            {
                await arrangeContext.Cocktails.AddAsync(cocktail);

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Act & Assert
                var sut = new CocktailService(assertContext, mapperMock.Object, barMapperMock.Object, cocktailDetailsMapperMock.Object,
                                              ingredientServiceMock.Object, cocktailIngredientServiceMock.Object);

                var result = await sut.GetCocktailAsync(testGuid);

                Assert.IsInstanceOfType(result, typeof(CocktailDto));
                Assert.AreEqual("TestOneName", result.Name);
            }
        }
Ejemplo n.º 29
0
        public async Task ReturnCorrectBarCommentWhen_ParamsAreValid()
        {
            //Arrange
            var options = TestUtilities.GetOptions(nameof(ReturnCorrectBarCommentWhen_ParamsAreValid));

            var mapperMock = new Mock <IDtoMapper <BarComment, BarCommentDto> >();

            var id        = Guid.NewGuid();
            var barId     = Guid.NewGuid();
            var createdOn = DateTime.UtcNow;

            var entity = new BarComment
            {
                Id        = id,
                BarId     = barId,
                UserId    = Guid.NewGuid(),
                Body      = "testbody",
                CreatedOn = createdOn
            };

            //var entityDto = new BarCommentDto
            //{
            //    Id = id,
            //    BarId = barId,
            //    UserId = Guid.NewGuid(),
            //    Body = "testbody",
            //    CreatedOn = createdOn
            //};

            //mapperMock.Setup(x => x.MapFrom(It.IsAny<BarComment>())).Returns(entityDto);

            using (var arrangeContext = new CWContext(options))
            {
                await arrangeContext.BarComments.AddAsync(entity);

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Act & Assert
                var sut = new BarCommentService(assertContext, mapperMock.Object);
                //var result = await sut.GetBarCommentAsync(barId);

                //Assert.IsInstanceOfType(result, typeof(BarComment));
                //Assert.AreEqual("testbody", result.Body);
            }
        }
        public async Task ThrowWhen_CocktailIsNull()
        {
            //Arrange
            var options                   = TestUtilities.GetOptions(nameof(ThrowWhen_CocktailIsNull));
            var mapperMock                = new Mock <IDtoMapper <Cocktail, CocktailDto> >();
            var barMapperMock             = new Mock <IDtoMapper <Bar, BarDto> >();
            var cocktailDetailsMapperMock = new Mock <IDtoMapper <Cocktail, DetailsCocktailDto> >();
            var ingredientMapperMock      = new Mock <IDtoMapper <Ingredient, IngredientDto> >();

            var ingredientServiceMock         = new Mock <IIngredientService>();
            var cocktailIngredientServiceMock = new Mock <ICocktailIngredientService>();

            var testGuid  = Guid.NewGuid();
            var testGuid2 = Guid.NewGuid();

            var cocktail = new Cocktail
            {
                Id        = testGuid,
                Name      = "TestOneName",
                ImagePath = "ImagePathOne"
            };

            var cocktailDto = new CocktailDto
            {
                Id        = testGuid,
                Name      = "TestOneName",
                ImagePath = "ImagePathOne"
            };

            mapperMock.Setup(x => x.MapFrom(It.IsAny <Cocktail>())).Returns(cocktailDto);

            using (var arrangeContext = new CWContext(options))
            {
                await arrangeContext.Cocktails.AddAsync(cocktail);

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CWContext(options))
            {
                //Act & Assert
                var sut = new CocktailService(assertContext, mapperMock.Object, barMapperMock.Object, cocktailDetailsMapperMock.Object,
                                              ingredientServiceMock.Object, cocktailIngredientServiceMock.Object);

                await Assert.ThrowsExceptionAsync <BusinessLogicException>(() => sut.GetCocktailBarsAsync(testGuid2));
            }
        }