6
6
from time import sleep
7
7
from datetime import datetime
8
8
import pytz
9
+ import urllib , urllib2
10
+ import json
11
+ import ast
9
12
10
13
class IndexViewTest (TestCase ):
11
14
@@ -101,7 +104,7 @@ class AllComponentViewTest(TestCase):
101
104
@classmethod
102
105
def setUpTestData (cls ):
103
106
# Called initially when test is executed, create objects to be used by test methods
104
- # create 10 random objects
107
+ # create 20 random objects
105
108
number_of_components = 20
106
109
for component_no in range (number_of_components ):
107
110
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 +131,189 @@ def test_relevance_of_response(self):
128
131
'id' in object and
129
132
'url_name' in object
130
133
)
134
+
135
+ class DetailComponentViewTest (TestCase ):
136
+
137
+ @classmethod
138
+ def setUpTestData (cls ):
139
+ # Called initially when test is executed, create objects to be used by test methods
140
+ # create a random object
141
+ # Add a benchmark component
142
+ hdr = {'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11' }
143
+ req = urllib2 .Request ("http://registry.npmjs.com/-/v1/search?text=biojs-vis-rohart-msc-test" , headers = hdr )
144
+ response = urllib2 .urlopen (req )
145
+ data = json .load (response )
146
+ hdr = {'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11' }
147
+ req = urllib2 .Request ("http://registry.npmjs.com/-/v1/search?text=cytoscape&size=1" , headers = hdr )
148
+ response = urllib2 .urlopen (req )
149
+ data ['objects' ].append (json .load (response )['objects' ][0 ])
150
+ for component in data ['objects' ]:
151
+ component_data = component ['package' ]
152
+ _component = Component .objects .create (name = component_data ['name' ])
153
+ try :
154
+ _component .version = component_data ['version' ]
155
+ except :
156
+ pass
157
+ try :
158
+ _component .short_description = component_data ['description' ]
159
+ except :
160
+ pass
161
+ try :
162
+ tags = component_data ['keywords' ]
163
+ except :
164
+ tags = []
165
+ for tag in tags :
166
+ try :
167
+ _tag = Tag .objects .get (name = tag )
168
+ except :
169
+ _tag = Tag .objects .create (name = tag )
170
+ _component .tags .add (_tag )
171
+ if not _tag in _component .tags .all ():
172
+ _component .tags .add (_tag )
173
+ try :
174
+ str_date = component_data ['date' ]
175
+ req_date = datetime .strptime (str_date , "%Y-%m-%dT%H:%M:%S.%fZ" ) #This object is timezone unaware
176
+ aware_date = pytz .utc .localize (req_date ) #This object is now timezone aware
177
+ _component .modified_time = aware_date
178
+ except :
179
+ pass
180
+ try :
181
+ _component .npm_url = component_data ['links' ]['npm' ]
182
+ except :
183
+ pass
184
+ try :
185
+ _component .homepage_url = component_data ['links' ]['homepage' ]
186
+ except :
187
+ pass
188
+ try :
189
+ github_url = component_data ['links' ]['repository' ]
190
+ url_list = github_url .split ('/' )
191
+ _component .github_url = 'https://api.github.com/repos/' + str (url_list [3 ]) + '/' + str (url_list [4 ])
192
+ except :
193
+ pass
194
+ try :
195
+ _component .author = component_data ['author' ]['name' ]
196
+ except :
197
+ pass
198
+ try :
199
+ _component .author_email = component_data ['author' ]['email' ]
200
+ except :
201
+ pass
202
+ _component .save ()
203
+
204
+ if _component .github_url :
205
+ response = urllib .urlopen (_component .github_url )
206
+ github_data = json .load (response )
207
+ _component .stars = github_data ['stargazers_count' ]
208
+ _component .forks = github_data ['forks' ]
209
+ _component .watchers = github_data ['watchers' ]
210
+ _component .icon_url = github_data ['owner' ]['avatar_url' ]
211
+ _component .open_issues = github_data ['open_issues' ]
212
+ try :
213
+ _component .license = github_data ['license' ]['name' ]
214
+ except :
215
+ pass
216
+ try :
217
+ str_date = github_data ['created_at' ]
218
+ req_date = datetime .strptime (str_date , "%Y-%m-%dT%H:%M:%SZ" ) #This object is timezone unaware
219
+ aware_date = pytz .utc .localize (req_date ) #This object is now timezone aware
220
+ _component .created_time = aware_date
221
+ except :
222
+ pass
223
+ _component .save ()
224
+ contributors_data = json .load (urllib .urlopen (str (github_data ['contributors_url' ])))
225
+ commits = 0
226
+ count = 0
227
+ for contributor in contributors_data :
228
+ try :
229
+ _contributor = Contributor .objects .get (username = contributor ["login" ])
230
+ except :
231
+ _contributor = Contributor .objects .create (username = contributor ["login" ], avatar_url = contributor ["avatar_url" ])
232
+ try :
233
+ _contribution = Contribution .objects .get (component = _component , contributor = _contributor )
234
+ _contribution .contributions = contributor ["contributions" ]
235
+ _contribution .save ()
236
+ except :
237
+ _contribution = Contribution .objects .create (component = _component , contributor = _contributor , contributions = contributor ["contributions" ])
238
+ commits += _contribution .contributions
239
+ count += 1
240
+ response = urllib .urlopen (github_data ['downloads_url' ])
241
+ downloads = 0
242
+ data = ast .literal_eval (response .read ())
243
+ for download in data :
244
+ downloads += int (download ['download_count' ])
245
+ _component .downloads = downloads
246
+ _component .commits = commits
247
+ _component .no_of_contributors = count
248
+ _component .save ()
249
+
250
+ def test_view_url_exists_at_desired_location (self ):
251
+ response = self .client .get ('/details/biojs-vis-rohart-msc-test/' )
252
+ self .assertEqual (response .status_code , 200 )
253
+ response = self .client .get ('/details/cytoscape/' )
254
+ self .assertEqual (response .status_code , 200 )
255
+
256
+ def test_view_accessible_by_name (self ):
257
+ response = self .client .get (reverse ('main:component_details' , kwargs = {'url_name' :'biojs-vis-rohart-msc-test' }))
258
+ self .assertEqual (response .status_code , 200 )
259
+ response = self .client .get (reverse ('main:component_details' , kwargs = {'url_name' :'cytoscape' }))
260
+ self .assertEqual (response .status_code , 200 )
261
+
262
+ # Tests whether the relevant keys are present in the json response and length of response list is same as number of components in database
263
+ def test_relevance_of_response (self ):
264
+ # call for biojs-vis-rohart-msc-test
265
+ response_1 = self .client .get (reverse ('main:component_details' , kwargs = {'url_name' :'biojs-vis-rohart-msc-test' }))
266
+ self .assertTrue ('details' in response_1 .json ())
267
+ objects = []
268
+ objects .append (response_1 .json ()['details' ])
269
+ # call for cytoscape
270
+ response_2 = self .client .get (reverse ('main:component_details' , kwargs = {'url_name' :'cytoscape' }))
271
+ self .assertTrue ('details' in response_2 .json ())
272
+ objects .append (response_2 .json ()['details' ])
273
+ # Test if all the required fields are present in the response
274
+ for object in objects :
275
+ self .assertTrue (
276
+ 'name' in object and
277
+ 'tags' in object and
278
+ 'stars' in object and
279
+ 'downloads' in object and
280
+ 'created_time' in object and
281
+ 'modified_time' in object and
282
+ 'icon_url' in object and
283
+ 'github_url' in object and
284
+ 'short_description' in object and
285
+ 'url_name' in object and
286
+ 'commits' in object and
287
+ 'forks' in object and
288
+ 'watchers' in object and
289
+ 'no_of_contributors' in object and
290
+ 'open_issues' in object and
291
+ 'version' in object and
292
+ 'author' in object and
293
+ 'license' in object
294
+ )
295
+ # check if number of commits >= 50 for biojs-vis-rohart-msc-test
296
+ # and >= 3757 for cytoscape, from the time the tests were initiated
297
+ ### As number of stars, watchers might go down in the future so they haven't been tested
298
+ self .assertTrue (int (response_1 .json ()['details' ]['commits' ]) >= 50 )
299
+ self .assertTrue (int (response_2 .json ()['details' ]['commits' ]) >= 3757 )
300
+
301
+ # modified date should be after created date
302
+ self .assertTrue (response_1 .json ()['details' ]['created_time' ] <= response_1 .json ()['details' ]['modified_time' ]) # for biojs-vis-rohart-msc-test
303
+ self .assertTrue (response_2 .json ()['details' ]['created_time' ] <= response_2 .json ()['details' ]['modified_time' ]) # for cytoscape
304
+ # check if number of contributors is same as contributors added
305
+ self .assertEqual (response_1 .json ()['details' ]['no_of_contributors' ], Contribution .objects .filter (component = Component .objects .get (name = 'biojs-vis-rohart-msc-test' )).count ())
306
+ self .assertEqual (response_1 .json ()['details' ]['no_of_contributors' ], len (response_1 .json ()['contributors' ]))
307
+ self .assertEqual (response_2 .json ()['details' ]['no_of_contributors' ], Contribution .objects .filter (component = Component .objects .get (name = 'cytoscape' )).count ())
308
+ self .assertEqual (response_2 .json ()['details' ]['no_of_contributors' ], len (response_2 .json ()['contributors' ]))
309
+ for object in response_1 .json ()['contributors' ] + response_2 .json ()['contributors' ]:
310
+ self .assertTrue (
311
+ 'contributor' in object and
312
+ 'contributions' in object and
313
+ 'id' in object
314
+ )
315
+ contributor_details = object ['contributor' ]
316
+ self .assertTrue (
317
+ 'username' in contributor_details and
318
+ 'avatar_url' in contributor_details
319
+ )
0 commit comments