public void GetCountriesAsyncTest()
        {
            var sut = new CountryService();

            List<Country> countries = sut.GetCountriesAsync().Result;

            Assert.IsTrue(countries.Any());
        }
Example #2
0
        public void ImportCountriesAndNamesIntoDB()
        {
            var countryService = new CountryService();
            var nameService = new NameService();

            Trace.WriteLine("Create DB if not exists");
            System.Data.Entity.Database.SetInitializer(new DBInitializer());

            Trace.WriteLine("Start DB sync");

            List<Country> countries = countryService.GetCountriesAsync().Result;

            using (var dbContext = new DBContext())
            {
                dbContext.BulkInsert(countries);
                dbContext.SaveChanges();

                int totalCountries = countries.Count();
                int countriesCount = 0;
                Trace.WriteLine($"DB sync: {totalCountries} countries downloaded");

                foreach (Country country in dbContext.Countries)
                {
                    Trace.WriteLine($"DB sync: start country {country.Name} ({countriesCount++} of {totalCountries})");
                    BabyName[] names = nameService.GetNamesAsync(country.HRef).Result.ToArray();

                    foreach (BabyName name in names)
                    {
                        name.CountryId = country.Id;
                    }

                    using (var nameContext = new DBContext())
                    {
                        nameContext.BulkInsert(names);
                        nameContext.SaveChanges();
                    }

                    Trace.WriteLine($"DB sync: end country {country.Name}: {names.Count()} names downloaded");
                }
            }

            Trace.WriteLine("End DB sync");
        }