public void WhenStoreIsPopulated_ReturnsSingleItemById()
        {
            //arrange
            var options = new DbContextOptionsBuilder <ApplicationContext>()
                          .UseInMemoryDatabase(databaseName: "ReturnSingleItem")
                          .Options;

            //act
            using (var context = new ApplicationContext(options))
            {
                var service = new CarAdvertService(context);
                var items   = GetTestCarAdverts().ToList();

                items.ForEach(carAdvert => service.Add(carAdvert));

                context.SaveChanges();
            }

            //assert
            using (var context = new ApplicationContext(options))
            {
                var service   = new CarAdvertService(context);
                var testGuid  = new Guid("981a7ab2-7887-4afd-b123-7d440c181627");
                var carAdvert = service.GetById(testGuid);
                Assert.IsType <CarAdvert>(carAdvert);
                Assert.Equal(testGuid, carAdvert.Id);
            }
        }
        public void Remove_WhenStoreIsPopulated_RemovessSingleItemById()
        {
            //arrange
            var options = new DbContextOptionsBuilder <ApplicationContext>()
                          .UseInMemoryDatabase(databaseName: "RemoveSingleItem")
                          .Options;

            //act
            using (var context = new ApplicationContext(options))
            {
                var service = new CarAdvertService(context);
                var items   = GetTestCarAdverts().ToList();

                items.ForEach(carAdvert => service.Add(carAdvert));

                context.SaveChanges();
            }

            //assert
            using (var context = new ApplicationContext(options))
            {
                var service  = new CarAdvertService(context);
                var testGuid = new Guid("981a7ab2-7887-4afd-b123-7d440c181627");
                service.Remove(testGuid);

                var deletedCarAdvert = service.GetById(testGuid);
                var allCarAdverts    = service.GetAllItems().ToList();

                Assert.Null(deletedCarAdvert);
                Assert.Equal(4, allCarAdverts.Count);
            }
        }
        public void GetAllItems_WhenStoreIsPopulated_ReturnsAllItems()
        {
            //arrange
            var options = new DbContextOptionsBuilder <ApplicationContext>()
                          .UseInMemoryDatabase(databaseName: "PopulatedStore")
                          .Options;

            //act
            using (var context = new ApplicationContext(options))
            {
                var service = new CarAdvertService(context);
                var items   = GetTestCarAdverts().ToList();

                items.ForEach(carAdvert => service.Add(carAdvert));

                context.SaveChanges();
            }

            //assert
            using (var context = new ApplicationContext(options))
            {
                var service       = new CarAdvertService(context);
                var allCarAdverts = service.GetAllItems().ToList();
                Assert.Equal(5, allCarAdverts.Count);
            }
        }
        public void GetByQuery_WhenTitleIsSupplied_FiltersByTitle()
        {
            //arrange
            var options = new DbContextOptionsBuilder <ApplicationContext>()
                          .UseInMemoryDatabase(databaseName: "GetByQueryTitleSupplied")
                          .Options;

            using (var context = new ApplicationContext(options))
            {
                var service = new CarAdvertService(context);
                var items   = GetTestCarAdverts().ToList();

                items.ForEach(carAdvert => service.Add(carAdvert));

                context.SaveChanges();
            }
            var query = new CarAdvertQueryModel
            {
                Title = "Used Toyota Matrix"
            };

            //act & Assert
            using (var context = new ApplicationContext(options))
            {
                var service       = new CarAdvertService(context);
                var allCarAdverts = service.GetByQuery(query).ToList();
                Assert.Single(allCarAdverts);
            }
        }
        public void GetByQuery_WhenQueryIsEmpty_ReturnsAllItems()
        {
            //arrange
            var options = new DbContextOptionsBuilder <ApplicationContext>()
                          .UseInMemoryDatabase(databaseName: "GetByQuery")
                          .Options;

            using (var context = new ApplicationContext(options))
            {
                var service = new CarAdvertService(context);
                var items   = GetTestCarAdverts().ToList();

                items.ForEach(carAdvert => service.Add(carAdvert));

                context.SaveChanges();
            }

            //act & Assert
            using (var context = new ApplicationContext(options))
            {
                var service       = new CarAdvertService(context);
                var allCarAdverts = service.GetByQuery(new CarAdvertQueryModel()).ToList();
                Assert.Equal(5, allCarAdverts.Count);
            }
        }
        public void Update_WhenFieldsAreModified_ReturnsUpdatedFields()
        {
            //arrange
            var options = new DbContextOptionsBuilder <ApplicationContext>()
                          .UseInMemoryDatabase(databaseName: "UpdateSingleItem")
                          .Options;
            var testGuid   = new Guid("981a7ab2-7887-4afd-b123-7d440c181627");
            var newTitle   = "Used Merceded Benz G50 for Sale";
            var newFuel    = FuelType.Gasoline;
            var newPrice   = 200000;
            var newStatus  = false;
            var newMileage = 105123;
            var newFirstRegistrationDate = new DateTime(2018, 3, 2);

            using (var context = new ApplicationContext(options))
            {
                var service = new CarAdvertService(context);
                var items   = GetTestCarAdverts().ToList();

                items.ForEach(carAdvert => service.Add(carAdvert));

                context.SaveChanges();
            }

            //act
            using (var context = new ApplicationContext(options))
            {
                var service           = new CarAdvertService(context);
                var carAdvertToUpdate = service.GetById(testGuid);

                carAdvertToUpdate.Title             = newTitle;
                carAdvertToUpdate.Fuel              = newFuel;
                carAdvertToUpdate.Price             = newPrice;
                carAdvertToUpdate.New               = newStatus;
                carAdvertToUpdate.Mileage           = newMileage;
                carAdvertToUpdate.FirstRegistration = newFirstRegistrationDate;

                service.Update(carAdvertToUpdate);
            }

            //assert
            using (var context = new ApplicationContext(options))
            {
                var service = new CarAdvertService(context);

                var updatedCarAdvert = service.GetById(testGuid);

                Assert.Equal(updatedCarAdvert.Title, newTitle);
                Assert.Equal(updatedCarAdvert.Fuel, newFuel);
                Assert.Equal(updatedCarAdvert.Price, newPrice);
                Assert.Equal(updatedCarAdvert.New, newStatus);
                Assert.Equal(updatedCarAdvert.Mileage, newMileage);
                Assert.Equal(updatedCarAdvert.FirstRegistration, newFirstRegistrationDate);
            }
        }
        public void GetAllItems_WhenStoreIsEmpty_ReturnsEmptyList()
        {
            //arrange
            var options = new DbContextOptionsBuilder <ApplicationContext>()
                          .UseInMemoryDatabase(databaseName: "EmptyStore")
                          .Options;

            //act

            //assert
            using (var context = new ApplicationContext(options))
            {
                var service       = new CarAdvertService(context);
                var allCarAdverts = service.GetAllItems();
                Assert.Equal(new List <CarAdvert>(), allCarAdverts);
            }
        }
        private CarAdvertsController GetSUT(string databaseName)
        {
            var options = new DbContextOptionsBuilder <ApplicationContext>()
                          .UseInMemoryDatabase(databaseName: databaseName)
                          .Options;
            var context = new ApplicationContext(options);
            var service = new CarAdvertService(context);
            var items   = GetTestCarAdverts().ToList();

            items.ForEach(carAdvert => service.Add(carAdvert));

            context.SaveChanges();

            var configuration = new MapperConfiguration(cfg => cfg.AddProfile(new CarAdvertMappingProfile()));
            var mapper        = new Mapper(configuration);

            var controller = new CarAdvertsController(service, mapper);

            return(controller);
        }
        public CarAdvertServiceTest()
        {
            var repository = new Repository <CarAdvert>(Helpers.TestDbHelper.GetInMemoryDbContext(DbName));

            _advertService = new CarAdvertService(repository);
        }