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
@@ -135,41 +138,121 @@ class DetailComponentViewTest(TestCase):
135
138
def setUpTestData (cls ):
136
139
# Called initially when test is executed, create objects to be used by test methods
137
140
# 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 ()
141
+ hdr = {'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11' }
142
+ req = urllib2 .Request ("http://registry.npmjs.com/-/v1/search?text=biojs-vis-rohart-msc-test" , headers = hdr )
143
+ response = urllib2 .urlopen (req )
144
+ data = json .load (response )
145
+ for component in data ['objects' ]:
146
+ component_data = component ['package' ]
147
+ _component = Component .objects .create (name = component_data ['name' ])
148
+ try :
149
+ _component .version = component_data ['version' ]
150
+ except :
151
+ pass
152
+ try :
153
+ _component .short_description = component_data ['description' ]
154
+ except :
155
+ pass
156
+ try :
157
+ tags = component_data ['keywords' ]
158
+ except :
159
+ tags = []
160
+ for tag in tags :
161
+ try :
162
+ _tag = Tag .objects .get (name = tag )
163
+ except :
164
+ _tag = Tag .objects .create (name = tag )
165
+ _component .tags .add (_tag )
166
+ if not _tag in _component .tags .all ():
167
+ _component .tags .add (_tag )
168
+ try :
169
+ str_date = component_data ['date' ]
170
+ req_date = datetime .strptime (str_date , "%Y-%m-%dT%H:%M:%S.%fZ" ) #This object is timezone unaware
171
+ aware_date = pytz .utc .localize (req_date ) #This object is now timezone aware
172
+ _component .modified_time = aware_date
173
+ except :
174
+ pass
175
+ try :
176
+ _component .npm_url = component_data ['links' ]['npm' ]
177
+ except :
178
+ pass
179
+ try :
180
+ _component .homepage_url = component_data ['links' ]['homepage' ]
181
+ except :
182
+ pass
183
+ try :
184
+ github_url = component_data ['links' ]['repository' ]
185
+ url_list = github_url .split ('/' )
186
+ _component .github_url = 'https://api.github.com/repos/' + str (url_list [3 ]) + '/' + str (url_list [4 ])
187
+ except :
188
+ pass
189
+ try :
190
+ _component .author = component_data ['author' ]['name' ]
191
+ except :
192
+ pass
193
+ try :
194
+ _component .author_email = component_data ['author' ]['email' ]
195
+ except :
196
+ pass
197
+ _component .save ()
198
+
199
+ if _component .github_url :
200
+ response = urllib .urlopen (_component .github_url )
201
+ github_data = json .load (response )
202
+ _component .stars = github_data ['stargazers_count' ]
203
+ _component .forks = github_data ['forks' ]
204
+ _component .watchers = github_data ['watchers' ]
205
+ _component .icon_url = github_data ['owner' ]['avatar_url' ]
206
+ _component .open_issues = github_data ['open_issues' ]
207
+ try :
208
+ _component .license = github_data ['license' ]['name' ]
209
+ except :
210
+ pass
211
+ try :
212
+ str_date = github_data ['created_at' ]
213
+ req_date = datetime .strptime (str_date , "%Y-%m-%dT%H:%M:%SZ" ) #This object is timezone unaware
214
+ aware_date = pytz .utc .localize (req_date ) #This object is now timezone aware
215
+ _component .created_time = aware_date
216
+ except :
217
+ pass
218
+ _component .save ()
219
+ contributors_data = json .load (urllib .urlopen (str (github_data ['contributors_url' ])))
220
+ commits = 0
221
+ count = 0
222
+ for contributor in contributors_data :
223
+ try :
224
+ _contributor = Contributor .objects .get (username = contributor ["login" ])
225
+ except :
226
+ _contributor = Contributor .objects .create (username = contributor ["login" ], avatar_url = contributor ["avatar_url" ])
227
+ try :
228
+ _contribution = Contribution .objects .get (component = _component , contributor = _contributor )
229
+ _contribution .contributions = contributor ["contributions" ]
230
+ _contribution .save ()
231
+ except :
232
+ _contribution = Contribution .objects .create (component = _component , contributor = _contributor , contributions = contributor ["contributions" ])
233
+ commits += _contribution .contributions
234
+ count += 1
235
+ response = urllib .urlopen (github_data ['downloads_url' ])
236
+ downloads = 0
237
+ data = ast .literal_eval (response .read ())
238
+ for download in data :
239
+ downloads += int (download ['download_count' ])
240
+ _component .downloads = downloads
241
+ _component .commits = commits
242
+ _component .no_of_contributors = count
243
+ _component .save ()
161
244
162
245
def test_view_url_exists_at_desired_location (self ):
163
- response = self .client .get ('/details/testcomponent /' )
246
+ response = self .client .get ('/details/biojs-vis-rohart-msc-test /' )
164
247
self .assertEqual (response .status_code , 200 )
165
248
166
249
def test_view_accessible_by_name (self ):
167
- response = self .client .get (reverse ('main:component_details' , kwargs = {'url_name' :'testcomponent ' }))
250
+ response = self .client .get (reverse ('main:component_details' , kwargs = {'url_name' :'biojs-vis-rohart-msc-test ' }))
168
251
self .assertEqual (response .status_code , 200 )
169
252
170
253
# 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
254
def test_relevance_of_response (self ):
172
- response = self .client .get (reverse ('main:component_details' , kwargs = {'url_name' :'testcomponent ' }))
255
+ response = self .client .get (reverse ('main:component_details' , kwargs = {'url_name' :'biojs-vis-rohart-msc-test ' }))
173
256
self .assertTrue ('details' in response .json ())
174
257
# Test if all the required fields are present in the response
175
258
object = response .json ()['details' ]
@@ -193,8 +276,10 @@ def test_relevance_of_response(self):
193
276
'author' in object and
194
277
'license' in object
195
278
)
279
+ # check if number of commits >= 50, from the time the tests were initiated
280
+ self .assertTrue (int (response .json ()['details' ]['commits' ]) >= 50 )
196
281
# 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 ())
282
+ self .assertEqual (response .json ()['details' ]['no_of_contributors' ], Contribution .objects .filter (component = Component .objects .get (name = 'biojs-vis-rohart-msc-test ' )).count ())
198
283
self .assertEqual (response .json ()['details' ]['no_of_contributors' ], len (response .json ()['contributors' ]))
199
284
for object in response .json ()['contributors' ]:
200
285
self .assertTrue (
0 commit comments