@@ -101,7 +101,7 @@ class AllComponentViewTest(TestCase):
101
101
@classmethod
102
102
def setUpTestData (cls ):
103
103
# Called initially when test is executed, create objects to be used by test methods
104
- # create 10 random objects
104
+ # create 20 random objects
105
105
number_of_components = 20
106
106
for component_no in range (number_of_components ):
107
107
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 ()))
@@ -128,3 +128,82 @@ def test_relevance_of_response(self):
128
128
'id' in object and
129
129
'url_name' in object
130
130
)
131
+
132
+ class DetailComponentViewTest (TestCase ):
133
+
134
+ @classmethod
135
+ def setUpTestData (cls ):
136
+ # Called initially when test is executed, create objects to be used by test methods
137
+ # create a random object
138
+ component = Component .objects .create (
139
+ name = "testComponent" ,
140
+ stars = random .randint (0 , 20 ),
141
+ downloads = random .randint (0 , 20 ),
142
+ created_time = pytz .utc .localize (datetime .now ()),
143
+ modified_time = pytz .utc .localize (datetime .now ()),
144
+ no_of_contributors = 2 ,
145
+ license = 'MIT'
146
+ )
147
+ contributor1 = Contributor .objects .create (
148
+ username = "contributor1" ,
149
+ avatar_url = "https://avatars1.githubusercontent.com/u/11511612?v=4"
150
+ )
151
+ contributions1 = random .randint (0 ,100 )
152
+ Contribution .objects .create (contributor = contributor1 , component = component , contributions = contributions1 )
153
+ contributor2 = Contributor .objects .create (
154
+ username = "contributor2" ,
155
+ avatar_url = "https://avatars1.githubusercontent.com/u/11511612?v=4"
156
+ )
157
+ contributions2 = random .randint (0 ,100 )
158
+ Contribution .objects .create (contributor = contributor2 , component = component , contributions = contributions2 )
159
+ component .commits = contributions1 + contributions2
160
+ component .save ()
161
+
162
+ def test_view_url_exists_at_desired_location (self ):
163
+ response = self .client .get ('/details/testcomponent/' )
164
+ self .assertEqual (response .status_code , 200 )
165
+
166
+ def test_view_accessible_by_name (self ):
167
+ response = self .client .get (reverse ('main:component_details' , kwargs = {'url_name' :'testcomponent' }))
168
+ self .assertEqual (response .status_code , 200 )
169
+
170
+ # Tests whether the relevant keys are present in the json response and length of response list is same as number of components in database
171
+ def test_relevance_of_response (self ):
172
+ response = self .client .get (reverse ('main:component_details' , kwargs = {'url_name' :'testcomponent' }))
173
+ self .assertTrue ('details' in response .json ())
174
+ # Test if all the required fields are present in the response
175
+ object = response .json ()['details' ]
176
+ self .assertTrue (
177
+ 'name' in object and
178
+ 'tags' in object and
179
+ 'stars' in object and
180
+ 'downloads' in object and
181
+ 'created_time' in object and
182
+ 'modified_time' in object and
183
+ 'icon_url' in object and
184
+ 'github_url' in object and
185
+ 'short_description' in object and
186
+ 'url_name' in object and
187
+ 'commits' in object and
188
+ 'forks' in object and
189
+ 'watchers' in object and
190
+ 'no_of_contributors' in object and
191
+ 'open_issues' in object and
192
+ 'version' in object and
193
+ 'author' in object and
194
+ 'license' in object
195
+ )
196
+ # check if number of contributors is same as contributors added
197
+ self .assertEqual (response .json ()['details' ]['no_of_contributors' ], Contribution .objects .filter (component = Component .objects .get (name = 'testComponent' )).count ())
198
+ self .assertEqual (response .json ()['details' ]['no_of_contributors' ], len (response .json ()['contributors' ]))
199
+ for object in response .json ()['contributors' ]:
200
+ self .assertTrue (
201
+ 'contributor' in object and
202
+ 'contributions' in object and
203
+ 'id' in object
204
+ )
205
+ contributor_details = object ['contributor' ]
206
+ self .assertTrue (
207
+ 'username' in contributor_details and
208
+ 'avatar_url' in contributor_details
209
+ )
0 commit comments