Skip to content

Commit 6dcc1bc

Browse files
authored
Merge pull request #25 from biojs/tests
Tests
2 parents 2e317e0 + d176492 commit 6dcc1bc

File tree

4 files changed

+94
-7
lines changed

4 files changed

+94
-7
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ install:
99
script:
1010
- python manage.py makemigrations
1111
- python manage.py migrate
12-
# - python manage.py test
12+
- python manage.py test

main/management/commands/updatecomponents.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def get_commit_hash(commits_url):
2424
return data['sha']
2525

2626
### store the dependency urls and snippet urls
27-
def update_visualizations(component, commit_hash):
27+
def update_visualizations(component, commit_hash, test=False):
2828
github_url_list = component.github_url.split('?')[0].split('/')
2929
owner = github_url_list[4]
3030
repo_name = github_url_list[5]
@@ -94,7 +94,8 @@ def update_visualizations(component, commit_hash):
9494

9595
### For Snippets URLs
9696
try:
97-
print ("https://api.github.com/repos/" + str(owner) + "/" + str(repo_name) + "/contents/" + sniperData.snippets_dir_name + "?ref=master&client_id="
97+
if not test:
98+
print ("https://api.github.com/repos/" + str(owner) + "/" + str(repo_name) + "/contents/" + sniperData.snippets_dir_name + "?ref=master&client_id="
9899
+ GITHUB_CLIENT_ID + "&client_secret=" + GITHUB_CLIENT_SECRET)
99100
snippets_data = urllib.urlopen("https://api.github.com/repos/" + str(owner) + "/" + str(repo_name) + "/contents/" + sniperData.snippets_dir_name + "?ref=master&client_id="
100101
+ GITHUB_CLIENT_ID + "&client_secret=" + GITHUB_CLIENT_SECRET)

main/serializers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class Meta:
8080
'version',
8181
'author',
8282
'license',
83+
'github_update_time',
84+
'latest_commit_hash',
8385
)
8486

8587
class BaseComponentSerializer(serializers.ModelSerializer): #serializer fields used while querying

tests/test_views.py

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import urllib, urllib2
1010
import json
1111
import ast
12+
from main.management.commands import updatecomponents
1213

1314
class IndexViewTest(TestCase):
1415

@@ -147,6 +148,11 @@ def setUpTestData(cls):
147148
req = urllib2.Request("http://registry.npmjs.com/-/v1/search?text=cytoscape&size=1", headers=hdr)
148149
response = urllib2.urlopen(req)
149150
data['objects'].append(json.load(response)['objects'][0])
151+
### BENCHMARK COMPONENT
152+
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'}
153+
req = urllib2.Request("http://registry.npmjs.com/-/v1/search?text=gmkhsuorlom4n85u&size=1", headers=hdr)
154+
response = urllib2.urlopen(req)
155+
data['objects'].append(json.load(response)['objects'][0])
150156
for component in data['objects']:
151157
component_data = component['package']
152158
_component = Component.objects.create(name=component_data['name'])
@@ -220,7 +226,16 @@ def setUpTestData(cls):
220226
_component.created_time = aware_date
221227
except:
222228
pass
229+
str_date = github_data['updated_at']
230+
req_date = datetime.strptime(str_date, "%Y-%m-%dT%H:%M:%SZ")
231+
aware_date = pytz.utc.localize(req_date)
232+
_component.github_update_time = aware_date
233+
commits_url = github_data['commits_url'].split('{')[0]
234+
response = urllib.urlopen(commits_url)
235+
data = json.loads(response.read())[0]
236+
_component.latest_commit_hash = data['sha']
223237
_component.save()
238+
updatecomponents.update_visualizations(_component, _component.latest_commit_hash, True)
224239
contributors_data = json.load(urllib.urlopen(str(github_data['contributors_url'])))
225240
commits = 0
226241
count = 0
@@ -252,12 +267,16 @@ def test_view_url_exists_at_desired_location(self):
252267
self.assertEqual(response.status_code, 200)
253268
response = self.client.get('/details/cytoscape/')
254269
self.assertEqual(response.status_code, 200)
270+
response = self.client.get('/details/mplexviz-ngraph/')
271+
self.assertEqual(response.status_code, 200)
255272

256273
def test_view_accessible_by_name(self):
257274
response = self.client.get(reverse('main:component_details', kwargs={'url_name':'biojs-vis-rohart-msc-test'}))
258275
self.assertEqual(response.status_code, 200)
259276
response = self.client.get(reverse('main:component_details', kwargs={'url_name':'cytoscape'}))
260277
self.assertEqual(response.status_code, 200)
278+
response = self.client.get(reverse('main:component_details', kwargs={'url_name':'mplexviz-ngraph'}))
279+
self.assertEqual(response.status_code, 200)
261280

262281
# Tests whether the relevant keys are present in the json response and length of response list is same as number of components in database
263282
def test_relevance_of_response(self):
@@ -270,6 +289,10 @@ def test_relevance_of_response(self):
270289
response_2 = self.client.get(reverse('main:component_details', kwargs={'url_name':'cytoscape'}))
271290
self.assertTrue('details' in response_2.json())
272291
objects.append(response_2.json()['details'])
292+
# call for mplexviz-ngraph
293+
response_3 = self.client.get(reverse('main:component_details', kwargs={'url_name':'mplexviz-ngraph'}))
294+
self.assertTrue('details' in response_3.json())
295+
objects.append(response_3.json()['details'])
273296
# Test if all the required fields are present in the response
274297
for object in objects:
275298
self.assertTrue(
@@ -290,23 +313,32 @@ def test_relevance_of_response(self):
290313
'open_issues' in object and
291314
'version' in object and
292315
'author' in object and
293-
'license' in object
316+
'license' in object and
317+
'github_update_time' in object and
318+
'latest_commit_hash' in object
294319
)
295320
# check if number of commits >= 50 for biojs-vis-rohart-msc-test
296321
# and >= 3757 for cytoscape, from the time the tests were initiated
297322
### As number of stars, watchers might go down in the future so they haven't been tested
298323
self.assertTrue(int(response_1.json()['details']['commits']) >= 50)
299324
self.assertTrue(int(response_2.json()['details']['commits']) >= 3757)
300325

326+
# Tests for benchmark component
327+
self.assertTrue(int(response_3.json()['details']['stars']) >= 3)
328+
self.assertTrue(int(response_2.json()['details']['commits']) >= 70)
329+
301330
# 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
331+
self.assertTrue(response_1.json()['details']['created_time'] <= response_1.json()['details']['github_update_time']) # for biojs-vis-rohart-msc-test
332+
self.assertTrue(response_2.json()['details']['created_time'] <= response_2.json()['details']['github_update_time']) # for cytoscape
333+
self.assertTrue(response_3.json()['details']['created_time'] <= response_3.json()['details']['github_update_time']) # for mplexviz-ngraph
304334
# check if number of contributors is same as contributors added
305335
self.assertEqual(response_1.json()['details']['no_of_contributors'], Contribution.objects.filter(component=Component.objects.get(name='biojs-vis-rohart-msc-test')).count())
306336
self.assertEqual(response_1.json()['details']['no_of_contributors'], len(response_1.json()['contributors']))
307337
self.assertEqual(response_2.json()['details']['no_of_contributors'], Contribution.objects.filter(component=Component.objects.get(name='cytoscape')).count())
308338
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']:
339+
self.assertEqual(response_3.json()['details']['no_of_contributors'], Contribution.objects.filter(component=Component.objects.get(name='mplexviz-ngraph')).count())
340+
self.assertEqual(response_3.json()['details']['no_of_contributors'], len(response_3.json()['contributors']))
341+
for object in response_1.json()['contributors'] + response_2.json()['contributors'] + response_3.json()['contributors']:
310342
self.assertTrue(
311343
'contributor' in object and
312344
'contributions' in object and
@@ -317,3 +349,55 @@ def test_relevance_of_response(self):
317349
'username' in contributor_details and
318350
'avatar_url' in contributor_details
319351
)
352+
353+
# tests for visualizations
354+
# Test for JS and CSS dependencies as well as snippets names.
355+
# Cytoscape
356+
if 'js_dependencies' in response_2.json():
357+
for js_dependency in response_2.json()['js_dependencies']:
358+
url = js_dependency['js_url']
359+
latest_commit_hash = response_2.json()['details']['latest_commit_hash']
360+
# Verify that cdn URL is configured correctly
361+
if('cdn.rawgit.com' in url):
362+
self.assertTrue(latest_commit_hash in url)
363+
if 'css_dependencies' in response_2.json():
364+
for css_dependency in response_2.json()['css_dependencies']:
365+
url = css_dependency['css_url']
366+
latest_commit_hash = response_2.json()['details']['latest_commit_hash']
367+
# Verify that cdn URL is configured correctly
368+
if('cdn.rawgit.com' in url):
369+
self.assertTrue(latest_commit_hash in url)
370+
if 'snippets' in response_2.json():
371+
snippets_list = ['animated-bfs.js', 'images.js', 'performance-tuning.js', 'visual.js']
372+
for snippet in response_2.json()['snippets']:
373+
latest_commit_hash = response_2.json()['details']['latest_commit_hash']
374+
url = snippet['url']
375+
name = snippet['name']
376+
if('cdn.rawgit.com' in url):
377+
self.assertTrue(latest_commit_hash in url)
378+
self.assertTrue(name in snippets_list)
379+
380+
# mplexviz-ngraph
381+
if 'js_dependencies' in response_3.json():
382+
for js_dependency in response_3.json()['js_dependencies']:
383+
url = js_dependency['js_url']
384+
latest_commit_hash = response_3.json()['details']['latest_commit_hash']
385+
# Verify that cdn URL is configured correctly
386+
if('cdn.rawgit.com' in url):
387+
self.assertTrue(latest_commit_hash in url)
388+
if 'css_dependencies' in response_3.json():
389+
for css_dependency in response_3.json()['css_dependencies']:
390+
url = css_dependency['css_url']
391+
latest_commit_hash = response_3.json()['details']['latest_commit_hash']
392+
# Verify that cdn URL is configured correctly
393+
if('cdn.rawgit.com' in url):
394+
self.assertTrue(latest_commit_hash in url)
395+
if 'snippets' in response_3.json():
396+
snippets_list = ['one.js', 'two.js', 'three.js']
397+
for snippet in response_3.json()['snippets']:
398+
latest_commit_hash = response_3.json()['details']['latest_commit_hash']
399+
url = snippet['url']
400+
name = snippet['name']
401+
if('cdn.rawgit.com' in url):
402+
self.assertTrue(latest_commit_hash in url)
403+
self.assertTrue(name in snippets_list)

0 commit comments

Comments
 (0)