def test_get(self, m): """ 【正常系】『週間天気予報』画面 選択したエリアの、週間天気予報を表示する。 """ m.return_value = date(2017, 8, 11) area = testing.factory_area() channel_weekly = testing.factory_channel(area=area) channel_daily = testing.factory_channel( area=area, weather_type=Channel.TYPE_DAILY) weather1 = testing.factory_weather(channel=channel_weekly, date=date(2017, 8, 10)) weather2 = testing.factory_weather(channel=channel_weekly, date=date(2017, 8, 11)) weather3 = testing.factory_weather(channel=channel_weekly, date=date(2017, 8, 12)) hourlyweather = testing.factory_hourlyweather(channel=channel_daily, date=weather2) res = self.client.get(reverse('weather:weekly', args=(area.id, ))) self.assertTemplateUsed(res, 'weather/weekly.html') self.assertEqual(res.context['area'], area) self.assertContains(res, '草津町の週間天気予報') all_weekly_weather = res.context['all_weekly_weather'] self.assertEqual(len(all_weekly_weather), 1) self.assertTrue( channel_weekly.get_name_display() in all_weekly_weather) self.assertContains(res, 'Yahoo!天気') # 今日より前の天気予報は表示しない。 weathers = all_weekly_weather[channel_weekly.get_name_display()] self.assertEqual(len(weathers), 2) self.assertEqual(weathers[0], weather2) self.assertEqual(weathers[0].daily_weather_count, 1) self.assertEqual(weathers[1], weather3) self.assertEqual(weathers[1].daily_weather_count, 0) # hourlyweatherが存在する場合は、日付をリンク表示 self.assertContains(res, reverse('weather:daily', args=(weather2.id, ))) self.assertNotContains(res, reverse('weather:daily', args=(weather3.id, )))
def test_post(self, m): """ 【正常系】『週間天気予報』画面 選択したエリアの、週間天気予報を表示する。 """ m.return_value = date(2017, 8, 11) # 同一エリアに、'Yahoo!天気'と'日本気象協会 tenki.jp'のWeatherが存在する場合。 area = testing.factory_area() channel_yahoo = testing.factory_channel(area=area) channel_tenkijp = testing.factory_channel(area=area, name=Channel.CHANNEL_TENKIJP) weather_yahoo = testing.factory_weather(channel=channel_yahoo, date=date(2017, 8, 11)) weather_tenkijp = testing.factory_weather(channel=channel_tenkijp, date=date(2017, 8, 12)) res = self.client.get(reverse('weather:weekly', args=(area.id, ))) self.assertTemplateUsed(res, 'weather/weekly.html') self.assertEqual(res.context['area'], area) self.assertContains(res, '草津町の週間天気予報') all_weekly_weather = res.context['all_weekly_weather'] self.assertEqual(len(all_weekly_weather), 2) self.assertTrue(channel_yahoo.get_name_display() in all_weekly_weather) self.assertTrue( channel_tenkijp.get_name_display() in all_weekly_weather) self.assertContains(res, 'Yahoo!天気') self.assertContains(res, '日本気象協会 tenki.jp') yahoo_weathers = all_weekly_weather[channel_yahoo.get_name_display()] self.assertEqual(len(yahoo_weathers), 1) self.assertEqual(yahoo_weathers[0], weather_yahoo) tenkijp_weathers = all_weekly_weather[ channel_tenkijp.get_name_display()] self.assertEqual(len(tenkijp_weathers), 1) self.assertEqual(tenkijp_weathers[0], weather_tenkijp)
def test_post(self): """ 【正常系】『今日の天気予報』画面 選択した日付の、今日の天気予報(時間ごと)を表示する。 """ area = testing.factory_area() channel_weekly = testing.factory_channel(area=area) channel_daily = testing.factory_channel( area=area, weather_type=Channel.TYPE_DAILY) weather11 = testing.factory_weather(channel=channel_weekly, date=date(2017, 8, 11)) hourlyweather00 = testing.factory_hourlyweather(channel=channel_daily, date=weather11, time='00:00:00') hourlyweather12 = testing.factory_hourlyweather(channel=channel_daily, date=weather11, time='12:00:00') hourlyweather21 = testing.factory_hourlyweather(channel=channel_daily, date=weather11, time='21:00:00') weather12 = testing.factory_weather(channel=channel_weekly, date=date(2017, 8, 12)) hourlyweather09 = testing.factory_hourlyweather(channel=channel_daily, date=weather12, time='09:00:00') res = self.client.post(reverse('weather:daily', args=(weather11.id, ))) self.assertTemplateUsed(res, 'weather/daily.html') self.assertEqual(res.context['weather'], weather11) # H1タイトル self.assertContains(res, '草津町 08/11(金)の天気予報') # H2タイトル self.assertContains(res, 'Yahoo!天気') daily_weather = res.context['daily_weather'] self.assertEqual(len(daily_weather), 3) self.assertEqual(daily_weather[0], hourlyweather00) self.assertEqual(daily_weather[1], hourlyweather12) self.assertEqual(daily_weather[2], hourlyweather21)
def test_date_display(self): target = testing.factory_weather(date=date(2017, 8, 11)) self.assertEqual(target.date_display(), '08/11')
def test_chance_of_rain_display_999(self): target = testing.factory_weather(chance_of_rain=999) self.assertEqual(target.chance_of_rain_display(), '---')
def test_chance_of_rain_display_normal(self): target = testing.factory_weather(chance_of_rain=50) self.assertEqual(target.chance_of_rain_display(), 50)
def test_weekday_display(self): target = testing.factory_weather(date=date(2017, 8, 11)) self.assertEqual(target.weekday_display(), '金')