public void GetAllShouldWorkCorrectlyWithNoAddedCountriesUsingMoq()
        {
            var repository = new Mock <IRepository <Country> >();

            var countriesList = new List <Country>();

            repository.Setup(r => r.AllAsNoTracking()).Returns(countriesList.AsQueryable());

            var service = new CountriesService(repository.Object);

            Assert.Empty(service.GetAll());
            Assert.Equal(countriesList, service.GetAll());

            repository.Verify(x => x.AllAsNoTracking(), Times.Exactly(2));
        }
        public async Task GetAllTest()
        {
            var countries = new List <Country>
            {
                new Country()
                {
                    Name = "Country-1"
                },
                new Country()
                {
                    Name = "Country-2"
                },
            };

            var fakeRepositoryMock = new Mock <ICountriesRepository>();

            fakeRepositoryMock.Setup(x => x.GetAll()).ReturnsAsync(countries);


            var countriesService = new CountriesService(fakeRepositoryMock.Object);

            var result = await countriesService.GetAll();

            Assert.Collection(result, country =>
            {
                Assert.Equal("Country-1", country.Name);
            },
                              country =>
            {
                Assert.Equal("Country-2", country.Name);
            });
        }
        public async Task GetAllWorksCorrectly()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext = new ApplicationDbContext(options);

            var addressesServiceMock = new Mock <IAddressesService>();
            var citiesRepository     = new EfDeletableEntityRepository <City>(dbContext);

            var repository        = new EfDeletableEntityRepository <Country>(dbContext);
            var citiesServiceMock = new Mock <CitiesService>(citiesRepository, addressesServiceMock.Object);

            var service  = new CountriesService(repository, citiesServiceMock.Object);
            var country1 = new Country()
            {
                Id = 1,
            };
            var country2 = new Country()
            {
                Id = 2,
            };
            var country3 = new Country()
            {
                Id = 3,
            };

            dbContext.Add(country1);
            dbContext.Add(country2);
            dbContext.Add(country3);
            await dbContext.SaveChangesAsync();

            var result = await service.GetAll();

            Assert.Equal(3, result.Count());
        }
        public void GetAllShouldWorkCorrectlyUsingMoq()
        {
            var repository = new Mock <IRepository <Country> >();

            var countriesList = new List <Country>
            {
                new Country {
                    Id = 1, CreatedOn = DateTime.UtcNow, Name = "TestCountry1"
                },
                new Country {
                    Id = 1, CreatedOn = DateTime.UtcNow, Name = "TestCountry2"
                },
                new Country {
                    Id = 1, CreatedOn = DateTime.UtcNow, Name = "TestCountry3"
                },
            };

            repository.Setup(r => r.AllAsNoTracking()).Returns(countriesList.AsQueryable());

            var service = new CountriesService(repository.Object);

            Assert.Equal(countriesList, service.GetAll());

            repository.Verify(x => x.AllAsNoTracking(), Times.Once);
        }
        public async Task GetAllShouldWorkCorrectlyUsingDbContext()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "GetAllShouldWorkCorrectlyUsingDbContextCountriesServiceTests").Options;

            using var dbContext = new ApplicationDbContext(options);
            await dbContext.Countries.AddAsync(new Country { Name = "TestCountry1" });

            await dbContext.Countries.AddAsync(new Country { Name = "TestCountry2" });

            await dbContext.Countries.AddAsync(new Country { Name = "TestCountry3" });

            await dbContext.SaveChangesAsync();

            using var repository = new EfRepository <Country>(dbContext);
            var service = new CountriesService(repository);

            Assert.Equal(3, service.GetAll().Count());
            Assert.Equal(1, service.GetAll().FirstOrDefault().Id);
            Assert.Equal("TestCountry1", service.GetAll().FirstOrDefault().Name);
            Assert.Equal(2, service.GetAll().ElementAt(1).Id);
            Assert.Equal("TestCountry2", service.GetAll().ElementAt(1).Name);
            Assert.Equal(3, service.GetAll().ElementAt(2).Id);
            Assert.Equal("TestCountry3", service.GetAll().ElementAt(2).Name);
        }
        public void GetAllShouldWorkCorrectlyWithNoAddedCategoriesUsingDbContext()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "GetAllShouldWorkCorrectlyWithNoAddedCategoriesUsingDbContextCountriesServiceTests").Options;

            using var dbContext = new ApplicationDbContext(options);

            using var repository = new EfRepository <Country>(dbContext);
            var service = new CountriesService(repository);

            Assert.Empty(service.GetAll());
        }
        internal static void Configure()
        {
            Dictionary <string, string[]> locations = new Dictionary <string, string[]>();

            using (WebClient webClient = new WebClient())
            {
                string jsonString = webClient.DownloadString("https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json");
                locations = JsonConvert.DeserializeObject <Dictionary <string, string[]> >(jsonString);
            }

            CountriesService countriesService = new CountriesService();
            List <Country>   countries        = new List <Country>();

            foreach (var item in locations)
            {
                Country country = new Country();
                country.Name = item.Key;
                countries.Add(country);
            }

            countriesService.InsertCollection(countries);

            List <Country> allCountries = countriesService.GetAll();
            List <City>    cities       = new List <City>();

            foreach (var item in locations)
            {
                foreach (Country c in allCountries)
                {
                    if (c.Name == item.Key)
                    {
                        cities.AddRange(item.Value.Select(city => new City()
                        {
                            Name = city, CountryID = c.ID
                        }));
                    }
                }
            }

            CitiesService citieService = new CitiesService();

            citieService.InsertCollection(cities);
        }
        public async Task TestGetAllCountries()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase("Countries");

            var countryRepository = new EfDeletableEntityRepository <Country>(new ApplicationDbContext(options.Options));

            await countryRepository.AddAsync(new Country { Name = "Bulgaria" });

            await countryRepository.AddAsync(new Country { Name = "Turkey" });

            await countryRepository.AddAsync(new Country { Name = "Russia" });

            await countryRepository.SaveChangesAsync();

            var countryService = new CountriesService(countryRepository);

            AutoMapperConfig.RegisterMappings(typeof(MyTestCountry).Assembly);
            var countries = countryService.GetAll <MyTestCountry>();

            Assert.Equal(3, countries.Count());
        }
Exemple #9
0
 // GET: Countries
 public async Task <IActionResult> Index()
 {
     return(View(await _service.GetAll()));
 }
        //public void LoadAddressControl()
        //{
        //    txtAddressId.Value = AddressId;
        //    txtLine1.Text = Line1;
        //    txtLine2.Text = Line2;
        //    txtTownCity.Text = TownCity;
        //    txtStateOrProvince.Text = StateOrProvince;
        //    txtPostCod.Text = PostCod;
        //    //ddlCountries.SelectedItem.Value = CountryCode;
        //    ddlCountries.Items.FindByValue(CountryCode).Selected = true;
        //}
        private TList <Countries> LoadCountries()
        {
            CountriesService countriesService = new CountriesService();

            return(countriesService.GetAll());
        }
        public ActionResult List()
        {
            CountriesService countriesService = new CountriesService();

            CountriesListVM model = new CountriesListVM();
            TryUpdateModel(model);

            model.Countries = countriesService.GetAll();

            if (!String.IsNullOrEmpty(model.Search))
            {
                model.Countries = model.Countries.Where(c => c.Name.ToLower().Contains(model.Search.ToLower())).ToList();
            }

            switch (model.SortOrder)
            {
                case "population_asc":
                    model.Countries = model.Countries.OrderBy(c => c.Population).ToList();
                    break;
                case "population_desc":
                    model.Countries = model.Countries.OrderByDescending(c => c.Population).ToList();
                    break;
                case "date_asc":
                    model.Countries = model.Countries.OrderBy(c => c.FoundationDate).ToList();
                    break;
                case "date_desc":
                    model.Countries = model.Countries.OrderByDescending(c => c.FoundationDate).ToList();
                    break;
                case "name_desc":
                    model.Countries = model.Countries.OrderByDescending(c => c.Name).ToList();
                    break;
                case "name_asc":
                default:
                    model.Countries = model.Countries.OrderBy(c => c.Name).ToList();
                    break;
            }

            return View(model);
        }