Beispiel #1
0
        public async Task SeedAsync(ElectricTravelDbContext dbContext, IServiceProvider serviceProvider)
        {
            var userManager = serviceProvider
                              .GetRequiredService <UserManager <ElectricTravelUser> >();

            var admin = await userManager
                        .FindByEmailAsync("*****@*****.**");

            if (admin == null)
            {
                admin = new ElectricTravelUser()
                {
                    UserName = "******",
                    Email    = "*****@*****.**",
                };

                var result = await userManager
                             .CreateAsync(admin, "123456Gosho");

                if (result.Succeeded)
                {
                    await userManager
                    .AddToRoleAsync(admin, GlobalConstants.AdministratorRoleName);
                }
            }
        }
        public async Task CkeckIfDeleteAsyncFindsArticleObjectInDB()
        {
            var options = new DbContextOptionsBuilder <ElectricTravelDbContext>()
                          .UseInMemoryDatabase(databaseName: "ArticlesTestDb").Options;

            using var dbContext = new ElectricTravelDbContext(options);

            using var repo = new EfDeletableEntityRepository <Article>(dbContext);
            var service = new ArticlesService(repo);

            var userId = "stefkaasd";

            var articleToAdd = new Article
            {
                ShortDescription = "asdasdasd",
                Content          = "asdasdadasd",
                CreatedById      = userId,
                Title            = "asdasasd",
            };

            await repo.AddAsync(articleToAdd);

            await repo.SaveChangesAsync();

            var id = articleToAdd.Id;

            var article = await dbContext.Articles
                          .FirstOrDefaultAsync(x => x.Id == id && x.CreatedById == userId);

            Assert.NotNull(article);
        }
        public async Task ChecksIfGetDriverInfoFindsDriverById()
        {
            var options = new DbContextOptionsBuilder <ElectricTravelDbContext>()
                          .UseInMemoryDatabase(databaseName: "DriversTestDb").Options;

            using var dbContext = new ElectricTravelDbContext(options);

            UserStore <ElectricTravelUser>   userStore   = new UserStore <ElectricTravelUser>(dbContext, null);
            UserManager <ElectricTravelUser> userManager =
                new UserManager <ElectricTravelUser>(userStore, null, null, null, null, null, null, null, null);

            using var userRepo      = new EfDeletableEntityRepository <ElectricTravelUser>(dbContext);
            using var travelRepo    = new EfDeletableEntityRepository <SharedTravelAdvert>(dbContext);
            using var imageTypeRepo = new EfDeletableEntityRepository <ImageType>(dbContext);
            var service = new UsersService(userRepo, travelRepo, imageTypeRepo, userManager);

            var user = new ElectricTravelUser
            {
                Email     = "*****@*****.**",
                UserName  = "******",
                FirstName = "Elmo",
                LastName  = "Elmos",
            };

            await userRepo.AddAsync(user);

            await userRepo.SaveChangesAsync();

            var result = service.GetDriverInfo <DriverInfoViewModel>(user.Id);

            Assert.NotNull(result);
        }
        public async Task CheckIfGetUserByUserNameFindsUser()
        {
            var options = new DbContextOptionsBuilder <ElectricTravelDbContext>()
                          .UseInMemoryDatabase(databaseName: "DriversTestDb").Options;

            using var dbContext = new ElectricTravelDbContext(options);

            UserStore <ElectricTravelUser>   userStore   = new UserStore <ElectricTravelUser>(dbContext, null);
            UserManager <ElectricTravelUser> userManager =
                new UserManager <ElectricTravelUser>(userStore, null, null, null, null, null, null, null, null);

            using var userRepo      = new EfDeletableEntityRepository <ElectricTravelUser>(dbContext);
            using var travelRepo    = new EfDeletableEntityRepository <SharedTravelAdvert>(dbContext);
            using var imageTypeRepo = new EfDeletableEntityRepository <ImageType>(dbContext);
            var service = new UsersService(userRepo, travelRepo, imageTypeRepo, userManager);

            var userName = "******";
            var email    = "*****@*****.**";

            var user = new ElectricTravelUser
            {
                Email    = email,
                UserName = userName,
            };

            await userRepo.AddAsync(user);

            await userRepo.SaveChangesAsync();

            var result = await service.GetUserByUserName(userName);

            Assert.NotNull(result);
        }
        public async Task ChecksIfGetArticleByIdReturnsCorrectResult()
        {
            var options = new DbContextOptionsBuilder <ElectricTravelDbContext>()
                          .UseInMemoryDatabase(databaseName: "ArticlesTestDb").Options;

            using var dbContext = new ElectricTravelDbContext(options);

            var user = new ElectricTravelUser
            {
                UserName = "******",
            };

            var articleToAdd = new Article
            {
                ShortDescription = "asdasd",
                Content          = "asdasdad",
                CreatedBy        = user,
                Title            = "asdas",
            };

            dbContext.Users.Add(user);
            dbContext.Articles.Add(articleToAdd);
            dbContext.SaveChanges();

            using var repo = new EfDeletableEntityRepository <Article>(dbContext);
            var service = new ArticlesService(repo);

            var article = await service.GetArticleById <ArticleViewModel>(articleToAdd.Id);

            Assert.NotNull(article);
        }
Beispiel #6
0
        public async Task CheckIfGetByIdReturnsViewModel()
        {
            var options = new DbContextOptionsBuilder <ElectricTravelDbContext>()
                          .UseInMemoryDatabase(databaseName: "ChargingStationsTestDb").Options;

            using var dbContext = new ElectricTravelDbContext(options);

            using var repo = new EfDeletableEntityRepository <ChargingStation>(dbContext);
            var service = new ChargingStationsService(repo);

            var stationName = "ElDrive";
            var address     = "Drujba 1";
            var cityId      = 1;
            var description = "Something";
            var isFree      = false;
            var workingTime = "10-22";

            var stationToAdd = new ChargingStation
            {
                Name           = stationName,
                Address        = address,
                CityId         = cityId,
                Description    = description,
                IsFreeOfCharge = isFree,
                WorkTime       = workingTime,
            };

            await repo.AddAsync(stationToAdd);

            await repo.SaveChangesAsync();

            var station = repo.All().FirstOrDefault(x => x.Id == stationToAdd.Id);

            Assert.NotNull(station);
        }
Beispiel #7
0
        private static ElectricTravelDbContext GetElectricTravelDbContext()
        {
            var options = new DbContextOptionsBuilder <ElectricTravelDbContext>()
                          .UseInMemoryDatabase(databaseName: "CitiesTestDb").Options;
            var dbContext = new ElectricTravelDbContext(options);

            return(dbContext);
        }
Beispiel #8
0
 public CitiesServiceTests()
 {
     this.InitializeMapper();
     this.dbContext         = GetElectricTravelDbContext();
     this.citiesRepository  = new EfDeletableEntityRepository <City>(this.dbContext);
     this.regionsRepository = new EfDeletableEntityRepository <Region>(this.dbContext);
     this.citiesService     = new CitiesService(this.citiesRepository, this.regionsRepository);
 }
        public async Task SeedAsync(ElectricTravelDbContext dbContext, IServiceProvider serviceProvider)
        {
            var roleManager = serviceProvider.GetRequiredService <RoleManager <Role> >();

            await SeedRoleAsync(roleManager, GlobalConstants.AdministratorRoleName);
            await SeedRoleAsync(roleManager, GlobalConstants.DriverRoleName);
            await SeedRoleAsync(roleManager, GlobalConstants.PassengerRoleName);
        }
        public ImagesServiceTests()
        {
            this.InitializeMapper();
            this.dbContext = InitializeDbContext();

            InitializeRepositories(this.dbContext, out this.imageRepo, out this.imageTypeRepo);

            this.imagesService = InitializeService(this.imageRepo, this.imageTypeRepo);
        }
        public async Task SeedAsync(ElectricTravelDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (dbContext.Settings.Any())
            {
                return;
            }

            await dbContext.Settings.AddAsync(new Setting { Name = "Setting1", Value = "value1" });
        }
        public async Task SeedAsync(ElectricTravelDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (dbContext.Categories.Any())
            {
                return;
            }

            await dbContext.Categories.AddAsync(new Category { Name = "AC" });

            await dbContext.Categories.AddAsync(new Category { Name = "DC" });
        }
        public async Task SeedAsync(ElectricTravelDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (dbContext.CarAdvertStatus.Any())
            {
                return;
            }

            await dbContext.CarAdvertStatus.AddAsync(new CarAdvertStatus { Name = "ForSale" });

            await dbContext.CarAdvertStatus.AddAsync(new CarAdvertStatus { Name = "Sold" });
        }
        public async Task SeedAsync(ElectricTravelDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (dbContext.SharedTravelStatus.Any())
            {
                return;
            }

            await dbContext.SharedTravelStatus.AddAsync(new SharedTravelStatus { Name = "Active" });

            await dbContext.SharedTravelStatus.AddAsync(new SharedTravelStatus { Name = "Inactive" });
        }
Beispiel #15
0
        public async Task SeedAsync(ElectricTravelDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (dbContext.Cities.Any())
            {
                return;
            }

            await dbContext.Cities.AddAsync(new City { Name = "Sofia", RegionId = 1 });

            await dbContext.Cities.AddAsync(new City { Name = "Plovdiv", RegionId = 2 });
        }
Beispiel #16
0
        public async Task SeedAsync(ElectricTravelDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (dbContext.Regions.Any())
            {
                return;
            }

            await dbContext.Regions.AddAsync(new Region { Name = "Sofia Region" });

            await dbContext.Regions.AddAsync(new Region { Name = "Plovdiv Region" });
        }
        public async Task SeedAsync(ElectricTravelDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (dbContext.ImageTypes.Any())
            {
                return;
            }

            await dbContext.ImageTypes.AddAsync(new ImageType { Name = "User Picture" });
            await dbContext.ImageTypes.AddAsync(new ImageType { Name = "Car Exterior" });
            await dbContext.ImageTypes.AddAsync(new ImageType { Name = "Car Interior" });
            await dbContext.ImageTypes.AddAsync(new ImageType { Name = "Charging Station" });
        }
        public async Task SeedAsync(ElectricTravelDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (dbContext.Makes.Any())
            {
                return;
            }

            await dbContext.Makes.AddAsync(new Make { Name = "Tesla" });

            await dbContext.Makes.AddAsync(new Make { Name = "Volvo" });

            await dbContext.Makes.AddAsync(new Make { Name = "Volkswagen" });
        }
Beispiel #19
0
        public async Task SeedAsync(ElectricTravelDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (dbContext.Currencies.Any())
            {
                return;
            }

            await dbContext.Currencies.AddAsync(new Currency { Name = "Leva" });

            await dbContext.Currencies.AddAsync(new Currency { Name = "EUR" });

            await dbContext.Currencies.AddAsync(new Currency { Name = "USD" });
        }
Beispiel #20
0
        public async Task SeedAsync(ElectricTravelDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (dbContext.TypeTravels.Any())
            {
                return;
            }

            await dbContext.TypeTravels.AddAsync(new TypeTravel { Name = "Single Trip" });

            await dbContext.TypeTravels.AddAsync(new TypeTravel { Name = "Weekly Trip" });

            await dbContext.TypeTravels.AddAsync(new TypeTravel { Name = "Monthly Trip" });
        }
Beispiel #21
0
        public async Task CheckIfDbIsEmptyDeleteAsyncShouldReturnFalse()
        {
            var options = new DbContextOptionsBuilder <ElectricTravelDbContext>()
                          .UseInMemoryDatabase(databaseName: "ChargingStationsTestDb").Options;

            using var dbContext = new ElectricTravelDbContext(options);

            using var repo = new EfDeletableEntityRepository <ChargingStation>(dbContext);
            var service = new ChargingStationsService(repo);

            var isTrue = await service.DeleteAsync(2);

            Assert.True(!isTrue);
        }
 private static void InitializeRepositories(
     ElectricTravelDbContext dbContext,
     out EfDeletableEntityRepository <ElectricCar> carsRepo,
     out EfDeletableEntityRepository <Make> makesRepo,
     out EfDeletableEntityRepository <Model> modelsRepo,
     out EfDeletableEntityRepository <CarType> carTypesRepo,
     out EfDeletableEntityRepository <Image> imagesRepo)
 {
     carsRepo     = new EfDeletableEntityRepository <ElectricCar>(dbContext);
     makesRepo    = new EfDeletableEntityRepository <Make>(dbContext);
     modelsRepo   = new EfDeletableEntityRepository <Model>(dbContext);
     carTypesRepo = new EfDeletableEntityRepository <CarType>(dbContext);
     imagesRepo   = new EfDeletableEntityRepository <Image>(dbContext);
 }
        public async Task SeedAsync(ElectricTravelDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (dbContext.Models.Any())
            {
                return;
            }

            await dbContext.Models.AddAsync(new Model { Name = "Model 3", MakeId = 1, Year = 2020 });

            await dbContext.Models.AddAsync(new Model { Name = "Model S", MakeId = 1, Year = 2020 });

            await dbContext.Models.AddAsync(new Model { Name = "ID 3", MakeId = 3, Year = 2020 });

            await dbContext.Models.AddAsync(new Model { Name = "XC40", MakeId = 2, Year = 2020 });
        }
        public CarsServiceTests()
        {
            this.InitializeMapper();
            this.dbContext = InitializeDbContext();

            InitializeRepositories(
                this.dbContext,
                out this.carsRepo,
                out this.makesRepo,
                out this.modelsRepo,
                out this.carTypesRepo,
                out this.imagesRepo);

            this.carsService = InitializeService(
                this.carsRepo, this.makesRepo, this.modelsRepo, this.carTypesRepo, this.imagesRepo);
        }
        public async Task GetCountShouldReturnCorrectNumberUsingDbContext()
        {
            var options = new DbContextOptionsBuilder <ElectricTravelDbContext>()
                          .UseInMemoryDatabase(databaseName: "SettingsTestDb").Options;

            using var dbContext = new ElectricTravelDbContext(options);
            dbContext.Settings.Add(new Setting());
            dbContext.Settings.Add(new Setting());
            dbContext.Settings.Add(new Setting());
            await dbContext.SaveChangesAsync();

            using var repository = new EfDeletableEntityRepository <Setting>(dbContext);
            var service = new SettingsService(repository);

            Assert.Equal(3, service.GetCount());
        }
Beispiel #26
0
        public async Task CheckThatDeleteAsyncShouldReturnTrueIfStationIsDeleted()
        {
            var options = new DbContextOptionsBuilder <ElectricTravelDbContext>()
                          .UseInMemoryDatabase(databaseName: "ChargingStationsTestDb").Options;

            using var dbContext = new ElectricTravelDbContext(options);

            using var repo = new EfDeletableEntityRepository <ChargingStation>(dbContext);
            var service = new ChargingStationsService(repo);

            var stationName = "ElDrive";
            var address     = "Drujba 1";
            var cityId      = 1;
            var description = "Something";
            var isFree      = false;
            var workingTime = "10-22";

            var stationToAdd = new ChargingStation
            {
                Name           = stationName,
                Address        = address,
                CityId         = cityId,
                Description    = description,
                IsFreeOfCharge = isFree,
                WorkTime       = workingTime,
            };

            await repo.AddAsync(stationToAdd);

            await repo.SaveChangesAsync();

            var stations = repo.All().ToList();

            var id = 0;

            if (stations.Count == 1)
            {
                var station = stations.SingleOrDefault();
                id = station.Id;
            }

            var isTrue = await service.DeleteAsync(id);

            Assert.True(isTrue);
        }
        public async Task CheckIfCreateAsyncReturnsFalseIfCannotCreateArticle()
        {
            var options = new DbContextOptionsBuilder <ElectricTravelDbContext>()
                          .UseInMemoryDatabase(databaseName: "ArticlesTestDb").Options;

            using var dbContext = new ElectricTravelDbContext(options);

            using var repo = new EfDeletableEntityRepository <Article>(dbContext);
            var service = new ArticlesService(repo);

            var article = new ArticleInputViewModel();

            article = null;

            var isCreated = await service.CreateAsync(article);

            Assert.True(!isCreated);
        }
Beispiel #28
0
        private static async Task SeedInDb(ElectricTravelDbContext dbContext)
        {
            dbContext.Regions.Add(new Region
            {
                Name = "Plovdiv Region",
            });

            await dbContext.SaveChangesAsync();

            var region = await dbContext.Regions.FirstOrDefaultAsync(x => x.Name == "Plovdiv Region");

            dbContext.Cities.Add(new City
            {
                Name   = "Plovdiv",
                Region = region,
            });

            await dbContext.SaveChangesAsync();
        }
Beispiel #29
0
        public async Task SeedAsync(ElectricTravelDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (dbContext == null)
            {
                throw new ArgumentNullException(nameof(dbContext));
            }

            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            var logger = serviceProvider.GetService <ILoggerFactory>().CreateLogger(typeof(ApplicationDbContextSeeder));

            var seeders = new List <ISeeder>
            {
                new RolesSeeder(),
                new SettingsSeeder(),
                new CurrencySeeder(),
                new CarAdvertStatusSeeder(),
                new TypeTravelSeeder(),
                new ImageTypeSeeder(),
                new SharedTravelStatusSeeder(),
                new RegionSeeder(),
                new CitiesSeeder(),
                new CarTypesSeeder(),
                new CarMakesSeeder(),
                new CarModelsSeeder(),
                new AdminsSeeder(),
            };

            foreach (var seeder in seeders)
            {
                await seeder.SeedAsync(dbContext, serviceProvider);

                await dbContext.SaveChangesAsync();

                logger.LogInformation($"Seeder {seeder.GetType().Name} done.");
            }
        }
        public async Task CheckIfCreateAsyncCreatesArticle()
        {
            var options = new DbContextOptionsBuilder <ElectricTravelDbContext>()
                          .UseInMemoryDatabase(databaseName: "ArticlesTestDb").Options;

            using var dbContext = new ElectricTravelDbContext(options);

            using var repo = new EfDeletableEntityRepository <Article>(dbContext);
            var service = new ArticlesService(repo);

            var article = new ArticleInputViewModel()
            {
                ShortDescription = "DDDDDSASDSASD",
                Content          = "DDDDDSASDSASD",
                UserId           = "goshoasd",
                Title            = "DDDDDASDASDS",
            };

            var isCreated = await service.CreateAsync(article);

            Assert.True(isCreated);
        }