diff --git a/core/tests/unit/views/test_ofm_stadium_detail_view.py b/core/tests/unit/views/test_ofm_stadium_detail_view.py index ce52117..3fdf867 100644 --- a/core/tests/unit/views/test_ofm_stadium_detail_view.py +++ b/core/tests/unit/views/test_ofm_stadium_detail_view.py @@ -9,7 +9,7 @@ class OFMStadiumDetailsViewTestCase(TestCase): def setUp(self): MatchdayFactory.create() MatchdayFactory.create(number=1) - user1 = OFMUser.objects.create_user( + self.user1 = OFMUser.objects.create_user( username='alice', email='alice@ofmhelper.com', password='alice', @@ -23,7 +23,7 @@ def setUp(self): ofm_username='bob', ofm_password='bob' ) - match = MatchFactory.create(user=user1) + match = MatchFactory.create(user=self.user1) self.stadium_stat = MatchStadiumStatisticsFactory.create(match=match) StadiumStandStatisticsFactory.create(stadium_statistics=self.stadium_stat, sector='N') StadiumStandStatisticsFactory.create(stadium_statistics=self.stadium_stat, sector='S') @@ -35,10 +35,10 @@ def test_user_can_see_his_data(self): response = self.client.get('/ofm/stadium/' + str(self.stadium_stat.id)) self.assertEqual(response.status_code, 200) self.assertTrue('stadium_stat' in response.context_data) - self.assertTrue('north_stand' in response.context_data) - self.assertTrue('south_stand' in response.context_data) - self.assertTrue('west_stand' in response.context_data) - self.assertTrue('east_stand' in response.context_data) + self.assertTrue(response.context_data['north_stand'] is not None) + self.assertTrue(response.context_data['south_stand'] is not None) + self.assertTrue(response.context_data['west_stand'] is not None) + self.assertTrue(response.context_data['east_stand'] is not None) def test_user_cannot_see_other_users_data(self): self.client.login(username='bob', password='bob') @@ -49,3 +49,17 @@ def test_user_cannot_see_other_users_data(self): self.assertFalse('south_stand' in response.context_data) self.assertFalse('west_stand' in response.context_data) self.assertFalse('east_stand' in response.context_data) + + def test_stadium_data_if_one_stand_not_used(self): + MatchdayFactory.create(number=2) + match = MatchFactory.create(user=self.user1) + stadium_stat_2 = MatchStadiumStatisticsFactory.create(match=match) + StadiumStandStatisticsFactory.create(stadium_statistics=stadium_stat_2, sector='N') + StadiumStandStatisticsFactory.create(stadium_statistics=stadium_stat_2, sector='S') + StadiumStandStatisticsFactory.create(stadium_statistics=stadium_stat_2, sector='W') + response = self.client.get('/ofm/stadium/' + str(stadium_stat_2.id)) + self.assertEqual(response.status_code, 200) + self.assertTrue(response.context_data['north_stand'] is not None) + self.assertTrue(response.context_data['south_stand'] is not None) + self.assertTrue(response.context_data['west_stand'] is not None) + self.assertTrue(response.context_data['east_stand'] is None) diff --git a/core/views/ofm/stadium_views.py b/core/views/ofm/stadium_views.py index dcbffe0..95d863e 100644 --- a/core/views/ofm/stadium_views.py +++ b/core/views/ofm/stadium_views.py @@ -156,10 +156,10 @@ def get_context_data(self, **kwargs): context = super(StadiumDetailView, self).get_context_data(**kwargs) if self.get_object(): - context['north_stand'] = self._get_stand_sector('N') - context['south_stand'] = self._get_stand_sector('S') - context['west_stand'] = self._get_stand_sector('W') - context['east_stand'] = self._get_stand_sector('O') + context['north_stand'] = self._get_stand_by_sector('N') + context['south_stand'] = self._get_stand_by_sector('S') + context['west_stand'] = self._get_stand_by_sector('W') + context['east_stand'] = self._get_stand_by_sector('O') return context @@ -168,8 +168,9 @@ def get_object(self, **kwargs): matches = Match.objects.filter(user=self.request.user, stadium_statistics=stadium_stat) return stadium_stat if matches.count() > 0 else None - def _get_stand_sector(self, sector): - return StadiumStandStatistics.objects.get(stadium_statistics=self.get_object(), sector=sector) + def _get_stand_by_sector(self, sector): + stand_statistics = StadiumStandStatistics.objects.filter(stadium_statistics=self.get_object(), sector=sector) + return stand_statistics[0] if stand_statistics.count() > 0 else None @method_decorator(login_required, name='dispatch')