public void Can_Paginate() { Mock <ICarRepository> mock = new Mock <ICarRepository>(); mock.Setup(m => m.Cars).Returns(new Car[] { new Car { CarID = 1, Model = "P1", Brand = "B1", Category = "" }, new Car { CarID = 2, Model = "P2", Brand = "B2", Category = "" }, new Car { CarID = 3, Model = "P3", Brand = "B3", Category = "" }, new Car { CarID = 4, Model = "P4", Brand = "B4", Category = "" }, new Car { CarID = 5, Model = "P5", Brand = "B5", Category = "" } }); CarController controller = new CarController(mock.Object); controller.PageSize = 3; //działanie CarsListMainModel result = (CarsListMainModel)controller.List(new CarSearch(), 2).Model; //asercje Car[] carArray = result.CarListViewModel.Cars.ToArray(); Assert.IsTrue(carArray.Length == 2); Assert.AreEqual(carArray[0].Model, "P4"); Assert.AreEqual(carArray[1].Model, "P5"); }
public void Can_Search_Numeric() { //przygotowanie Mock <ICarRepository> mock = new Mock <ICarRepository>(); mock.Setup(m => m.Cars).Returns(new Car[] { new Car { CarID = 1, Model = "P1", Brand = "B1", Category = "", PricePerDay = 100, Year = 2001, Capacity = 2 }, new Car { CarID = 2, Model = "P2", Brand = "B2", Category = "", PricePerDay = 200, Year = 2002, Capacity = 2 }, new Car { CarID = 3, Model = "P3", Brand = "B3", Category = "", PricePerDay = 300, Year = 2003, Capacity = 2 }, new Car { CarID = 4, Model = "P4", Brand = "B4", Category = "", PricePerDay = 400, Year = 1990, Capacity = 1 }, new Car { CarID = 5, Model = "P5", Brand = "B5", Category = "", PricePerDay = 500, Year = 1991, Capacity = 1 }, new Car { CarID = 6, Model = "P6", Brand = "B6", Category = "", PricePerDay = 600, Year = 1993, Capacity = 2 }, new Car { CarID = 7, Model = "P7", Brand = "B7", Category = "", PricePerDay = 700, Year = 1999, Capacity = 2 }, new Car { CarID = 8, Model = "P8", Brand = "B8", Category = "", PricePerDay = 800, Year = 2001, Capacity = 2 }, }); CarController controller = new CarController(mock.Object); CarSearch carSearch = new CarSearch { MinPrice = 200, MaxPrice = 600, MinYear = 1990, MaxYear = 2000, MinCapacity = 1, MaxCapacity = (decimal)1.6 }; //działanie CarsListMainModel result = (CarsListMainModel)controller.List(carSearch, 1).Model; Car[] carArray = result.CarListViewModel.Cars.ToArray(); //asercje Assert.IsTrue(carArray.Length == 2); Assert.AreEqual(carArray[0].CarID, 4); Assert.AreEqual(carArray[1].CarID, 5); }
public void Can_Sort_Numeric() { //przygotowanie Mock <ICarRepository> mock = new Mock <ICarRepository>(); mock.Setup(m => m.Cars).Returns(new Car[] { new Car { CarID = 1, Model = "F", Brand = "B1", Category = "", PricePerDay = 200 }, new Car { CarID = 2, Model = "B", Brand = "B2", Category = "", PricePerDay = 100 }, new Car { CarID = 3, Model = "D", Brand = "B3", Category = "", PricePerDay = 50 }, new Car { CarID = 4, Model = "A", Brand = "B1", Category = "", PricePerDay = 500 }, new Car { CarID = 5, Model = "G", Brand = "B5", Category = "", PricePerDay = 150 } }); CarController controller = new CarController(mock.Object); CarSearch carSearch = new CarSearch { Sort = "PriceUp" }; //działanie CarsListMainModel result = (CarsListMainModel)controller.List(carSearch, 1).Model; Car[] carArray = result.CarListViewModel.Cars.ToArray(); //asercje Assert.IsTrue(carArray.Length == 5); Assert.AreEqual(carArray[0].CarID, 3); Assert.AreEqual(carArray[1].CarID, 2); Assert.AreEqual(carArray[2].CarID, 5); Assert.AreEqual(carArray[3].CarID, 1); Assert.AreEqual(carArray[4].CarID, 4); }
public void Can_Search_Strings() { //przygotowanie Mock <ICarRepository> mock = new Mock <ICarRepository>(); mock.Setup(m => m.Cars).Returns(new Car[] { new Car { CarID = 1, Model = "P1", Brand = "B1", Category = "" }, new Car { CarID = 2, Model = "P1", Brand = "B2", Category = "" }, new Car { CarID = 3, Model = "P2", Brand = "B3", Category = "" }, new Car { CarID = 4, Model = "P1", Brand = "B1", Category = "" }, new Car { CarID = 5, Model = "P2", Brand = "B5", Category = "" } }); CarController controller = new CarController(mock.Object); CarSearch carSearch = new CarSearch { NameSearch = "P1", BrandSearch = "B1" }; //działanie CarsListMainModel result = (CarsListMainModel)controller.List(carSearch, 1).Model; Car[] carArray = result.CarListViewModel.Cars.ToArray(); //asercje Assert.IsTrue(carArray.Length == 2); Assert.AreEqual(carArray[0].CarID, 1); Assert.AreEqual(carArray[1].CarID, 4); }
public ViewResult List(CarSearch searchModel, int page = 1) { var cars = repository.Cars; if (ModelState.IsValid) { if (searchModel.NameSearch == null) { searchModel.NameSearch = ""; } if (searchModel.BrandSearch == null) { searchModel.BrandSearch = ""; } if (searchModel.MaxPrice == 0) { searchModel.MaxPrice = decimal.MaxValue; } if (searchModel.MaxYear == 0) { searchModel.MaxYear = int.MaxValue; } if (searchModel.MaxMileage == 0) { searchModel.MaxMileage = decimal.MaxValue; } if (searchModel.MaxCapacity == 0) { searchModel.MaxCapacity = decimal.MaxValue; } if (searchModel.Category == null) { searchModel.Category = ""; } cars = from i in repository.Cars where i.Model.Contains(searchModel.NameSearch) && i.Brand.Contains(searchModel.BrandSearch) && i.PricePerDay >= searchModel.MinPrice && i.PricePerDay <= searchModel.MaxPrice && i.Mileage >= searchModel.MinMileage && i.Mileage <= searchModel.MaxMileage && i.Year >= searchModel.MinYear && i.Year <= searchModel.MaxYear && i.Capacity >= searchModel.MinCapacity && i.Capacity <= searchModel.MaxCapacity select i; if (searchModel.Hired == "false") { cars = cars.Where(p => p.Hired == true); } else if (searchModel.Hired == "true") { cars = cars.Where(p => p.Hired == false); } if (searchModel.Category != "") { cars = cars.Where(p => p.Category.Equals(searchModel.Category)); } if (searchModel.Sort == "ModelUp") { cars = cars.OrderBy(p => p.Model); } else if (searchModel.Sort == "ModelDown") { cars = cars.OrderByDescending(p => p.Model); } else if (searchModel.Sort == "BrandUp") { cars = cars.OrderBy(p => p.Brand); } else if (searchModel.Sort == "BrandDown") { cars = cars.OrderByDescending(p => p.Brand); } else if (searchModel.Sort == "PriceUp") { cars = cars.OrderBy(p => p.PricePerDay); } else if (searchModel.Sort == "PriceDown") { cars = cars.OrderByDescending(p => p.PricePerDay); } else if (searchModel.Sort == "YearUp") { cars = cars.OrderBy(p => p.Year); } else if (searchModel.Sort == "YearDown") { cars = cars.OrderByDescending(p => p.Year); } else if (searchModel.Sort == "MileageUp") { cars = cars.OrderBy(p => p.Mileage); } else if (searchModel.Sort == "MileageDown") { cars = cars.OrderByDescending(p => p.Mileage); } else if (searchModel.Sort == "CapacityUp") { cars = cars.OrderBy(p => p.Capacity); } else if (searchModel.Sort == "CapacityDown") { cars = cars.OrderByDescending(p => p.Capacity); } else { cars = cars.OrderBy(p => p.CarID); } } var totalCars = cars.Count(); cars = cars.Skip((page - 1) * PageSize); cars = cars.Take(PageSize); var model = new CarsListMainModel { CarListViewModel = new CarsListViewModel { Cars = cars, PagingInfo = new PagingInfo { CurrentPage = page, CarsPerPage = PageSize, TotalCars = totalCars } }, CarSearch = searchModel }; return(View(model)); }