コード例 #1
0
        public async Task TestEditCarDataAsync_WithTestData_ShouldEditTheCarData()
        {
            // Arrange
            var contextFactory = new ApplicationDbContextFactory();
            var context        = contextFactory.CreateApplicationDbContext();

            var mapperFactory = new AutoMapperFactory();
            var mapper        = mapperFactory.CreateMapper();

            SeedDbWithCars(context);

            var carServices     = new CarServices(context, mapper);
            var car             = context.Cars.FirstOrDefault();
            var carServiceModel = mapper.Map <CarServiceModel>(car);

            carServiceModel.Brand = "Tesla";
            carServiceModel.Model = "Model S";

            // Act
            await carServices.EditCarDataAsync(carServiceModel);

            var carFormDb = context.Cars.FirstOrDefault(c => c.Id == car.Id);

            var expectedBrand = "Tesla";
            var expectedModel = "Model S";

            var actualBrand = carFormDb.Brand;
            var actualModel = carFormDb.Model;

            // Assert
            Assert.True(expectedBrand == actualBrand &&
                        expectedModel == actualModel,
                        "The method EditCarDataAsnc does not work!");
        }
コード例 #2
0
        public void TestGetUserByName_WithTestData_ShouldReturnTheUserWithName()
        {
            // Arrange
            var contextFactory = new ApplicationDbContextFactory();
            var context        = contextFactory.CreateApplicationDbContext();

            var mapperFactory = new AutoMapperFactory();
            var mapper        = mapperFactory.CreateMapper();

            var services = new UserServices(context, mapper);


            SeedDbWithUsers(context);

            var testUsername = "******";

            var user = context.Users.FirstOrDefault(x => x.UserName == testUsername);

            // Act
            var resultUser = services.GetUserByName(testUsername);

            var expectedUser = user;
            var actualUser   = resultUser;

            // Assert
            Assert.True(expectedUser.Id == actualUser.Id &&
                        expectedUser.UserName == actualUser.UserName &&
                        expectedUser.Email == actualUser.Email);
        }
コード例 #3
0
        public void TestGetAllCarsForUserWithId_WithTestData_ShouldReturnAllCarsForTheUserWithId()
        {
            // Arrange
            var contextFactory = new ApplicationDbContextFactory();
            var context        = contextFactory.CreateApplicationDbContext();

            var mapperFactory = new AutoMapperFactory();
            var mapper        = mapperFactory.CreateMapper();

            var carServices = new CarServices(context, mapper);


            SeedDbWithUsers(context);
            SeedDbWithCars(context);
            SeedCarsToTheFirstUser(context);

            var firstUser = context.Users.FirstOrDefault();

            // Act
            var count = carServices.GetAllCarsForUserWithId(firstUser.Id).Count();

            var expectedCount = 2;
            var actualCount   = count;

            // Assert
            Assert.Equal(expected: expectedCount, actual: actualCount);
        }
コード例 #4
0
        public void TestGetAllCarsForUserWithId_WithUserIdNull_ShouldReturnZeroCarsForTheUserWithIdNull()
        {
            // Arrange
            var contextFactory = new ApplicationDbContextFactory();
            var context        = contextFactory.CreateApplicationDbContext();

            var mapperFactory = new AutoMapperFactory();
            var mapper        = mapperFactory.CreateMapper();

            var carServices = new CarServices(context, mapper);


            SeedDbWithUsers(context);
            SeedDbWithCars(context);
            SeedCarsToTheFirstUser(context);

            // Act
            var count = carServices.GetAllCarsForUserWithId(null).Count();

            var expectedCount = 0;
            var actualCount   = count;

            // Assert
            Assert.Equal(expected: expectedCount, actual: actualCount);
        }
コード例 #5
0
        public AdminServicesTests()
        {
            // DbContext.
            var contextFactory = new ApplicationDbContextFactory();

            this.context = contextFactory.CreateApplicationDbContext();

            if (this.context.Users.Any(x => x.Role == "Admin") == false)
            {
                this.SeedDbWithAdmins(this.context);
            }

            if (this.context.Users.Any(x => x.Role == "User") == false)
            {
                this.SeedDbWithUsers(this.context);
            }
            if (this.context.Users.Any(x => x.Role == "Banned") == false)
            {
                this.SeedDbWithBanned(this.context);
            }
            if (this.context.Cars.Any() == false)
            {
                this.SeedDbWithCars(this.context);
            }

            // AutoMapper.
            var mapperFactory = new AutoMapperFactory();

            this.mapper = mapperFactory.CreateMapper();

            // Services.
            this.adminServices = new AdminServices(this.context, this.mapper);
        }
コード例 #6
0
        public void TestGetCarById_WithTestData_ShouldReturnTheCarWithId()
        {
            // Arrange
            var contextFactory = new ApplicationDbContextFactory();
            var context        = contextFactory.CreateApplicationDbContext();

            var mapperFactory = new AutoMapperFactory();
            var mapper        = mapperFactory.CreateMapper();

            var carServices = new CarServices(context, mapper);

            SeedDbWithCars(context);

            var car   = context.Cars.FirstOrDefault();
            var carId = car.Id;

            // Act
            var expetedCar = car;
            var actualCar  = carServices.GetCarById(carId);

            // Assert
            Assert.True(expetedCar.Id == actualCar.Id &&
                        expetedCar.Brand == actualCar.Brand &&
                        expetedCar.Model == actualCar.Model &&
                        expetedCar.Number == actualCar.Number,
                        "The Method: GetCarById() does not work correctly!");
        }
コード例 #7
0
        public async Task TestAddCarAsync_WithTestData_ShouldAddCarToDb()
        {
            // Arrange
            var contextFactory = new ApplicationDbContextFactory();
            var context        = contextFactory.CreateApplicationDbContext();

            var mapperFactory = new AutoMapperFactory();
            var mapper        = mapperFactory.CreateMapper();

            var carServices = new CarServices(context, mapper);

            var car = new Car()
            {
                Brand    = "BMW",
                Model    = "X6",
                Number   = "PB1234K",
                YearFrom = DateTime.Now
            };
            var carServiceModel = mapper.Map <CarServiceModel>(car);

            // Act
            await carServices.AddCarAsync(carServiceModel);

            var expectedCount = 1;
            var actualCount   = context.Cars.Count();

            // Assert
            Assert.Equal(expected: expectedCount, actual: actualCount);
        }
コード例 #8
0
        public async Task TestEditPersonalityDesctriptionAsync_WithWithDescriptionIdNull_ShouldNotClearThePersonalityDescription()
        {
            // Arrange
            var contextFactory = new ApplicationDbContextFactory();
            var context        = contextFactory.CreateApplicationDbContext();

            var mapperFactory = new AutoMapperFactory();
            var mapper        = mapperFactory.CreateMapper();

            var adminServices = new AdminServices(context, mapper);

            SeedDbWithUsers(context);                                 // Count: 2

            var testApplicationUser = context.Users.FirstOrDefault(); // Take the first user.
            var userId = testApplicationUser.Id;


            // Act
            await adminServices.EditPersonalityDesctriptionAsync(userId, null);

            var actualDescription = context.Users
                                    .FirstOrDefault(user => user.Id == userId)
                                    .PersonalityDesctription;

            // Assert
            Assert.Equal(expected: null, actual: actualDescription);
        }
コード例 #9
0
        public void TestGetUserByCarId_WithoutUser_ShouldReturnNull()
        {
            // Arrange
            var contextFactory = new ApplicationDbContextFactory();
            var context        = contextFactory.CreateApplicationDbContext();

            var mapperFactory = new AutoMapperFactory();
            var mapper        = mapperFactory.CreateMapper();

            var services = new UserServices(context, mapper);

            SeedDbWithCars(context);


            var carId = context.Cars.FirstOrDefault().Id;

            // Act
            var result = services.GetUserByCarId(carId);

            var actualUser = result;

            // Assert
            Assert.True(null == actualUser,
                        "The method: GetUserById(string userId) does not work correctly!");
        }
コード例 #10
0
        public void TestGetUserByCarId_WithTestData_ShouldReturnTheUserWithCarId()
        {
            // Arrange
            var contextFactory = new ApplicationDbContextFactory();
            var context        = contextFactory.CreateApplicationDbContext();

            var mapperFactory = new AutoMapperFactory();
            var mapper        = mapperFactory.CreateMapper();


            var services = new UserServices(context, mapper);

            SeedDbWithUsers(context);
            SeedDbWithCars(context);
            SeedCarsToTheFirstUser(context);

            var carId = context.Cars.FirstOrDefault().Id;
            var user  = context.Users.FirstOrDefault(x => x.Cars.Any(car => car.Id == carId));

            // Act
            var result = services.GetUserByCarId(carId);

            var expectedUser = user;
            var actualUser   = result;

            Assert.True(expectedUser.Id == actualUser.Id &&
                        expectedUser.UserName == actualUser.UserName &&
                        expectedUser.Email == actualUser.Email,
                        "The method: GetUserById(string userId) does not work correctly!");
        }
コード例 #11
0
        public void TestGetAllCars_WithoutTestData_ShouldGetAllCars()
        {
            // Arrange
            var contextFactory = new ApplicationDbContextFactory();
            var context        = contextFactory.CreateApplicationDbContext();

            var mapperFactory = new AutoMapperFactory();
            var mapper        = mapperFactory.CreateMapper();

            var carServices = new CarServices(context, mapper);

            // Act
            var actualCount   = carServices.GetAllCars().Count;
            var expectedCount = 0;

            // Assert
            Assert.Equal(expectedCount, actualCount);
        }
コード例 #12
0
        public void TestGetCarById_WithoutTestData_ShouldReturnNull()
        {
            // Arrange
            var contextFactory = new ApplicationDbContextFactory();
            var context        = contextFactory.CreateApplicationDbContext();

            var mapperFactory = new AutoMapperFactory();
            var mapper        = mapperFactory.CreateMapper();

            var carServices = new CarServices(context, mapper);

            var carId = 1;

            // Act
            var actualCar = carServices.GetCarById(carId);

            // Assert
            Assert.True(null == actualCar,
                        "The Method: GetCarById() does not work correctly!");
        }
コード例 #13
0
        public async Task TestAddCarAsync_WithCarServiceModelNull_ShouldNotAddCarToDb()
        {
            // Arrange
            var contextFactory = new ApplicationDbContextFactory();
            var context        = contextFactory.CreateApplicationDbContext();

            var mapperFactory = new AutoMapperFactory();
            var mapper        = mapperFactory.CreateMapper();

            var carServices = new CarServices(context, mapper);


            // Act
            await carServices.AddCarAsync(null);

            var expectedCount = 0;
            var actualCount   = context.Cars.Count();

            // Assert
            Assert.Equal(expected: expectedCount, actual: actualCount);
        }
コード例 #14
0
        public void TestGetAllUsers_WithTestData_ShouldGetAllUsers()
        {
            // Arrange
            var contextFactory = new ApplicationDbContextFactory();
            var context        = contextFactory.CreateApplicationDbContext();

            var mapperFactory = new AutoMapperFactory();
            var mapper        = mapperFactory.CreateMapper();

            var services = new UserServices(context, mapper);

            SeedDbWithBanned(context); // Count: 2
            SeedDbWithAdmins(context); // Count: 2
            SeedDbWithUsers(context);  // Count: 2

            var resultCount = services.GetAllUsers().Count();

            var expectedCount = context.Users.Count();
            var actualCount   = resultCount;

            Assert.Equal(expectedCount, actualCount);
        }
コード例 #15
0
        public void TestGetUserById_WithTestData_ShouldGetTheUserWithId()
        {
            // Arrange
            var contextFactory = new ApplicationDbContextFactory();
            var context        = contextFactory.CreateApplicationDbContext();

            var mapperFactory = new AutoMapperFactory();
            var mapper        = mapperFactory.CreateMapper();

            var userServices = new UserServices(context, mapper);

            SeedDbWithUsers(context);  // Count: 2

            var user = context.Users.FirstOrDefault();

            var resultUser = userServices.GetUserById(user.Id);

            var actualUser   = resultUser;
            var expectedUser = user;

            Assert.True(expectedUser.Id == actualUser.Id && expectedUser.UserName == actualUser.UserName);
        }
コード例 #16
0
        public void TestIsThereSuchCarWithNumber_WithTestData_ShouldReturnIsThereSuchCarWithNumberFalse()
        {
            // Arrange
            var contextFactory = new ApplicationDbContextFactory();
            var context        = contextFactory.CreateApplicationDbContext();

            var mapperFactory = new AutoMapperFactory();
            var mapper        = mapperFactory.CreateMapper();

            var carServices = new CarServices(context, mapper);

            SeedDbWithCars(context);

            var testNumber = "PB1234K1";

            var isThreSuchCarWithNumber = carServices.IsThereSuchCarWithNumber(testNumber);

            var expectedResult = false;
            var actualResut    = isThreSuchCarWithNumber;

            Assert.True(expectedResult == actualResut,
                        "The method: IsThereSuchCarWithNumber(string number)(False) does not work correctly!");
        }
コード例 #17
0
        public async Task TestDeleteCarAsync_WithTestData_ShouldDeleteCarrFromDb()
        {
            // Arrange
            var contextFactory = new ApplicationDbContextFactory();
            var context        = contextFactory.CreateApplicationDbContext();

            var mapperFactory = new AutoMapperFactory();
            var mapper        = mapperFactory.CreateMapper();

            SeedDbWithCars(context);

            var carServices = new CarServices(context, mapper);
            var carId       = context.Cars.FirstOrDefault().Id;

            // Act
            await carServices.DeleteCarAsync(carId);

            var expectedCount = 1;
            var actualCount   = context.Cars.Count();

            // Assert
            Assert.Equal(expected: expectedCount, actual: actualCount);
        }
コード例 #18
0
        public void TestGetAllCarsForUserWithId_WithoutCarsForThisUser_ShouldReturnZeroCarsForTheUserWithIdNull()
        {
            // Arrange
            var contextFactory = new ApplicationDbContextFactory();
            var context        = contextFactory.CreateApplicationDbContext();

            var mapperFactory = new AutoMapperFactory();
            var mapper        = mapperFactory.CreateMapper();

            var carServices = new CarServices(context, mapper);


            SeedDbWithUsers(context);
            string userId = context.Users.FirstOrDefault().Id;

            // Act
            var count = carServices.GetAllCarsForUserWithId(userId).Count();

            var expectedCount = 0;
            var actualCount   = count;

            // Assert
            Assert.Equal(expected: expectedCount, actual: actualCount);
        }