Skip to content

Commit 2032cb9

Browse files
committed
Updated views test for index view
1 parent 0a8d866 commit 2032cb9

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

tests/test_views.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from django.urls import reverse
55
import random
66
from time import sleep
7+
from datetime import datetime
8+
import pytz
79

810
class IndexViewTest(TestCase):
911

@@ -13,7 +15,7 @@ def setUpTestData(cls):
1315
# create 10 random objects
1416
number_of_components = 10
1517
for component_no in range(number_of_components):
16-
Component.objects.create(name='component'+str(component_no), downloads=random.randint(0,50), stars=random.randint(0,50))
18+
Component.objects.create(name='component'+str(component_no), downloads=random.randint(0,50), stars=random.randint(0,50), modified_time=pytz.utc.localize(datetime.now()))
1719
sleep(0.5)
1820

1921
def test_view_url_exists_at_desired_location(self):
@@ -22,4 +24,14 @@ def test_view_url_exists_at_desired_location(self):
2224

2325
def test_view_accessible_by_name(self):
2426
response = self.client.get(reverse('main:index'))
25-
self.assertEqual(response.status_code, 200)
27+
self.assertEqual(response.status_code, 200)
28+
29+
# Tests whether the relevant keys are present in the json response and length of each list is maximum 3
30+
def test_relevance_of_response(self):
31+
response = self.client.get(reverse('main:index'))
32+
self.assertTrue('most_recent_components' in response.json())
33+
self.assertTrue('top_dl_components' in response.json())
34+
self.assertTrue('top_starred_components' in response.json())
35+
self.assertTrue(len(response.json()['most_recent_components'])<4)
36+
self.assertTrue(len(response.json()['top_dl_components'])<4)
37+
self.assertTrue(len(response.json()['top_starred_components'])<4)

0 commit comments

Comments
 (0)