示例#1
0
        public static LocationWeather LocationWeather(int cityId)
        {
            // var xx = new Faker<City>().RuleForType(typeof(string), x => Guid.NewGuid().ToString()).Generate();
            Randomizer.Seed = new Random(8675309);
            var city = FakerT <City> .FakeIt().Generate();

            city.id = cityId;
            var main = FakerT <Main> .FakeIt();

            var wind = FakerT <Wind> .FakeIt();

            var snow = FakerT <Snow> .FakeIt();

            var clouds = FakerT <Clouds> .FakeIt();

            var weather = FakerT <Core.Models.Weather> .FakeIt();

            var list = FakerT <List> .FakeIt()
                       .RuleFor(f => f.main, x => main.Generate())
                       .RuleFor(f => f.wind, x => wind.Generate())
                       .RuleFor(f => f.snow, x => snow.Generate())
                       .RuleFor(f => f.weather, x => weather.Generate(1).ToList())
                       .RuleFor(f => f.clouds, x => clouds.Generate());

            var locationWeather = new Faker <LocationWeather>()

                                  .RuleFor(f => f.city, c => city)
                                  .RuleFor(f => f.list, l => list.Generate(3).ToList());

            return(locationWeather.Generate());
        }
        public async Task FetchAndSyncWeather_StoresNewDataInCache_WhenNoneExistsInCache()
        {
            // Expire cache data, by making it older than 4 hours
            var weatherDto = FakerT <WeatherDto> .Generate();

            weatherDto.LocaleId = 3;
            var mockWeatherApi = new Mock <IWeatherApi>();

            mockWeatherApi.Setup(api => api.GetWeatherForLocation(3)).Returns(Task.FromResult(TestDataGenerator.LocationWeather(3)));

            var option     = new DbContextOptionsBuilder <WeatherDbContext>().UseInMemoryDatabase("WeatherInMemoryDatabase").Options;
            var dbContext  = new WeatherDbContext(option);
            var repository = new WeatherRepository(dbContext);

            // Act
            var sut = new FetchManager.FetchManager(mockWeatherApi.Object, repository, new OpenWeatherSettings()
            {
                CacheExpiryMinutes = 180
            });
            var result = (await sut.FetchAndSyncWeatherForLocationAsync(3)).FirstOrDefault();

            // Assert
            Assert.Equal(3, result.LocaleId);
            var insertedObject = await repository.GetWeatherById(3);

            Assert.Equal(3, insertedObject.Count);
        }