4
4
from django .urls import reverse
5
5
import random
6
6
from time import sleep
7
+ from datetime import datetime
8
+ import pytz
7
9
8
10
class IndexViewTest (TestCase ):
9
11
@@ -13,7 +15,7 @@ def setUpTestData(cls):
13
15
# create 10 random objects
14
16
number_of_components = 10
15
17
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 ()) )
17
19
sleep (0.5 )
18
20
19
21
def test_view_url_exists_at_desired_location (self ):
@@ -22,4 +24,14 @@ def test_view_url_exists_at_desired_location(self):
22
24
23
25
def test_view_accessible_by_name (self ):
24
26
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