def test_weather_output_values_are_correct(): wc = WeatherCondition(name='MyCity', latitude=-32.4566, longitude=158.246912, elevation=345, temperature=20) wc.calculate() output = str(wc) assert_in(wc.name, output) assert_in(str(wc.temperature), output) assert_in(str(wc.humidity), output) pretty_pressure = str(math.ceil(wc.pressure / 100)) assert_in(pretty_pressure, output) assert_in(wc.condition, output) pretty_datetime = wc.datetime.isoformat().replace('+', 'Z') assert_in(pretty_datetime, output) assert_in(str(wc.longitude), output) assert_in(str(wc.latitude), output) assert_in(str(wc.elevation), output)
def test_weather_output_format_is_correct(): wc = WeatherCondition(name='MyCity', latitude=-32.4566, longitude=158.246912, elevation=345, temperature=20) wc.calculate() output = str(wc) # Darwin|-12.46113,130.84185,31|1970-12-31T09:30:00Z09:30|Sunny|25.5|980|83 expected = "\s*|\d+,\d+,\d+|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z\d{2}:\d{2}|\s+|\d+|\d+|\d+" assert_true(re.match(expected, output, re.IGNORECASE))
def test_WeatherCondition_init_elevation_must_be_float_or_integer(): with assert_raises(TypeError, ) as te: wc = WeatherCondition(name='MyCity', latitude=64, longitude=158.246912, elevation='345', temperature=15) with assert_raises(TypeError, ) as te: wc = WeatherCondition(name='MyCity', latitude=23, longitude=2, elevation=None, temperature=15)
def test_WeatherCondition_init_latitude_must_be_float(): with assert_raises(TypeError, ) as te: wc = WeatherCondition(name='MyCity', latitude='39', longitude=158.246912, elevation=345, temperature=15) with assert_raises(TypeError, ) as te: wc = WeatherCondition(name='MyCity', latitude=None, longitude=158.246912, elevation=345, temperature=15)
def test_WeatherCondition_init_longitude_is_range_bound(): with assert_raises(TypeError, ) as te: wc = WeatherCondition(name='MyCity', latitude=23.534536, longitude=1208.23, elevation=345, temperature=15) with assert_raises(TypeError, ) as te: wc = WeatherCondition(name='MyCity', latitude=90.000215, longitude=-190.0001, elevation=345, temperature=15)
def test_WeatherCondition_init_name_must_be_string(): with assert_raises(TypeError, ) as te: wc = WeatherCondition(name=234, latitude=-32.4566, longitude=158.246912, elevation=345, temperature=15)
def test_WeatherCondition_init(): wc1 = WeatherCondition(name='MyCity', latitude=-32.4566, longitude=158.246912, elevation=345, temperature=15) assert_equal(wc1.name, 'MyCity') assert_equal(wc1.latitude, -32.4566) assert_equal(wc1.longitude, 158.246912) assert_equal(wc1.elevation, 345) assert_equal(wc1.temperature, 15) wc2 = WeatherCondition(latitude=-32.4566, longitude=158.246912, elevation=345, temperature=15) assert_equal(wc2.name, None)
def test_weather_condition_condition_is_rainy(): with mock.patch.multiple('weathersimulator.weather.WeatherCondition', humidity=70, deviation=0.9) as mock_humidity: wc1 = WeatherCondition(name='MyCity', latitude=-32.4566, longitude=158.246912, elevation=345, temperature=20) assert_equal(wc1.condition, 'Rainy')
def generate(start_date, end_date, data_file): """ Generates the weather data and outputs to stdout. :param data_file: Absolute path to the source data file. :param start_date: The starting date to begin generating weather data for. :param end_date: The end date to stop generating weather date for. """ location_records = [] with open(data_file) as location_file: location_records = json.load(location_file) current_date = start_date while current_date <= end_date: current_month = current_date.datetime.month - 1 for location in location_records: tf = TimezoneFinder() timezone_str = tf.timezone_at(lng=location['longitude'], lat=location['latitude']) tz = pytz.timezone(timezone_str) weather_condition = WeatherCondition( name=location['name'], latitude=location['latitude'], longitude=location['longitude'], elevation=location['elevation'], temperature=uniform(location['temps']['min'][current_month], location['temps']['max'][current_month]), datetime=current_date.astimezone(tz)) weather_condition.calculate() print(weather_condition) current_date = current_date.replace(days=1)
def test_WeatherCondition_init_missing_parameter_throws_TypeError(): with assert_raises(TypeError, ) as te: wc = WeatherCondition(name='MyCity', longitude=158.246912, elevation=345, temperature=15) with assert_raises(TypeError, ) as te: wc = WeatherCondition(name='MyCity', latitude=-32.4566, elevation=345, temperature=15) with assert_raises(TypeError, ) as te: wc = WeatherCondition(name='MyCity', latitude=-32.4566, longitude=158.246912, temperature=15) with assert_raises(TypeError, ) as te: wc = WeatherCondition(name='MyCity', latitude=-32.4566, longitude=158.246912, elevation=1234)
def test_weather_temperature_affects_humidity(): with mock.patch.multiple('weathersimulator.weather.WeatherCondition', deviation=1.0, min_temperature=20): wc1 = WeatherCondition(name='MyCity', latitude=-32.4566, longitude=158.246912, elevation=345, temperature=20) wc1.calculate() orig_humidity = wc1.humidity assert_equal(wc1.humidity, 100) wc1.min_temperature = 15 wc1.calculate() new_humidity = wc1.humidity assert_less(new_humidity, orig_humidity)
def test_weather_temperature_generates_accurate_humidity(): # See https://www.engineeringtoolbox.com/humidity-measurement-d_561.html for the dry vs wet bulb lookup table. with mock.patch.multiple('weathersimulator.weather.WeatherCondition', deviation=1.0, min_temperature=15, wetbulb_temp=15): wc = WeatherCondition(name='MyCity', latitude=-32.4566, longitude=158.246912, elevation=0, temperature=15) wc.calculate() assert_equal(wc.humidity, 100) wc.min_temperature = wc.wetbulb_temp = 14 wc.calculate() assert_equal(wc.humidity, 90) wc.min_temperature = wc.wetbulb_temp = 13 wc.calculate() assert_equal(wc.humidity, 80) wc.temperature = 33 wc.min_temperature = wc.wetbulb_temp = 23 wc.calculate() assert_equal(wc.humidity, 43)