Пример #1
0
    def test_no_version_on_same_weather(self):
        with patch('weather.yahoo_weather.fetch') as mock_fetch:
            mock_fetch.return_value = Version("sunny", self.created_time)
            source = Source("tokyo, japan", True)

            versions = run_check(source, Version("sunny", ""))
            self.assertEqual(len(versions), 0)
Пример #2
0
    def test_new_version_on_same_weather(self):
        with patch('weather.yahoo_weather.fetch') as mock_fetch:
            mock_fetch.return_value = Version("sunny", self.created_time)

            versions = run_check(self.source, Version(
                "sunny", "1970-01-01T00:00:00Z"))
            self.assertEqual(len(versions), 1)
Пример #3
0
def fetch(source: Source) -> Version:
    url = 'https://query.yahooapis.com/v1/public/yql'
    query = 'select item.condition from weather.forecast where woeid in (select woeid from geo.places(1) where text="%s")' % source.city
    response = requests.get(url, {'q': query, 'format': 'json'})
    data = response.json()['query']
    weather = data['results']['channel']['item']['condition']['text']
    forecast_date = data['results']['channel']['item']['condition']['date']

    return Version(weather, forecast_date)
Пример #4
0
 def test_outputs_versions(self):
     output_versions([Version("a", "one"), Version("b", "two")])
     captured_output = self.stdout.getvalue().strip()
     self.assertEqual(
         captured_output, '[{"weather": "a", "date": "one"}, {"weather": "b", "date": "two"}]')
Пример #5
0
    def test_new_version_when_previous_empty(self):
        with patch('weather.yahoo_weather.fetch') as mock_fetch:
            mock_fetch.return_value = Version("sunny", self.created_time)

            versions = run_check(self.source, EmptyVersion())
            self.assertEqual(len(versions), 1)
Пример #6
0
    def test_fetches_from_yahoo_weather(self):
        with patch('weather.yahoo_weather.fetch') as mock_fetch:
            mock_fetch.return_value = Version("sunny", self.created_time)
            run_check(self.source, EmptyVersion())

            mock_fetch.assert_called_once_with(self.source)
Пример #7
0
    def test_new_version_on_weather_change(self):
        with patch('weather.yahoo_weather.fetch') as mock_fetch:
            mock_fetch.return_value = Version("sunny", self.created_time)

            versions = run_check(self.source, Version("cloudy", ""))
            self.assertGreaterEqual(len(versions), 1)
Пример #8
0
 def test_outputs_version(self):
     output_version(Version("a", "one"))
     captured_output = self.stdout.getvalue().strip()
     self.assertEqual(captured_output,
                      '{"version": {"weather": "a", "date": "one"}}')
Пример #9
0
 def setUp(self):
     self.version = Version("lovely weather", "2018-05-21T16:26:37Z")
     self.input_dir = tempfile.mkdtemp()
Пример #10
0
 def test_loads_from_json(self):
     version = Version.from_json(
         {"weather": "sunny", "date": "2018-05-21T16:26:37Z"}
     )
     self.assertEqual(version.weather, "sunny")
     self.assertEqual(version.date, "2018-05-21T16:26:37Z")
Пример #11
0
 def test_serializes_to_json(self):
     version = Version("storm", "2018-05-21T16:26:37Z")
     self.assertEqual(
         version.to_dict(),
         {"weather": "storm", "date": "2018-05-21T16:26:37Z"}
     )
Пример #12
0
 def test_loads_from_json_empty_version(self):
     version = Version.from_json({})
     self.assertIsInstance(version, EmptyVersion)