public async Task CreateNewCarAdvertShouldBeSuccess()
        {
            var expected = GetCarAdvertDto();

            var id = await _advertService.Add(expected);

            var obj = await _advertService.GetById(id);

            Assert.NotNull(obj);
            AssertEquals(expected, obj);
        }
예제 #2
0
        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);
            }
        }
예제 #3
0
        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);
            }
        }
예제 #4
0
        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);
            }
        }
예제 #5
0
        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);
            }
        }
예제 #6
0
        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);
            }
        }
예제 #7
0
        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);
            }
        }
        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);
        }