Skip to content

Commit 301c3e3

Browse files
authored
Merge pull request #22 from biojs/tests
Tests
2 parents 6ca75e3 + daf1d9c commit 301c3e3

File tree

4 files changed

+216
-9
lines changed

4 files changed

+216
-9
lines changed

main/management/commands/updatecomponents.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,14 @@ def handle(self, *args, **options):
105105

106106
if _component.github_url:
107107
print _component.github_url
108-
github_data = get_github_data(_component.github_url)
108+
try:
109+
github_data = get_github_data(_component.github_url)
110+
except:
111+
continue
109112
_component.stars = github_data['stargazers_count']
110113
_component.forks = github_data['forks']
111-
_component.watchers = github_data['watchers']
114+
# subscriber_count
115+
_component.watchers = github_data['subscribers_count']
112116
_component.icon_url = github_data['owner']['avatar_url']
113117
_component.open_issues = github_data['open_issues']
114118
try:
@@ -124,7 +128,10 @@ def handle(self, *args, **options):
124128
pass
125129
_component.save()
126130
print str(github_data['contributors_url']) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET
127-
contributors_data = get_contributors_data(str(github_data['contributors_url']) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET)
131+
try:
132+
contributors_data = get_contributors_data(str(github_data['contributors_url']) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET)
133+
except:
134+
continue
128135
commits = 0
129136
count = 0
130137
for contributor in contributors_data:
@@ -140,7 +147,10 @@ def handle(self, *args, **options):
140147
_contribution = Contribution.objects.create(component=_component, contributor=_contributor, contributions=contributor["contributions"])
141148
commits += _contribution.contributions
142149
count +=1
143-
_component.downloads = get_downloads(str(github_data['downloads_url']) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET)
150+
try:
151+
_component.downloads = get_downloads(str(github_data['downloads_url']) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET)
152+
except:
153+
pass
144154
_component.commits = commits
145155
_component.no_of_contributors = count
146156
_component.save()

main/serializers.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,21 @@ class Meta:
4444

4545
class DetailComponentSerializer(serializers.ModelSerializer):
4646
tags = serializers.SerializerMethodField()
47+
github_url = serializers.SerializerMethodField()
48+
4749
def get_tags(self, obj):
4850
tags = []
4951
for t in obj.tags.all():
5052
tags.append(t.name)
5153
return tags
54+
55+
def get_github_url(self, obj):
56+
if obj.github_url:
57+
list_url = obj.github_url.split('/')
58+
return list_url[0] + '//github.com/' + list_url[4] + '/' + list_url[5].split('?')[0] # remove get request params(client_id and secret)
59+
else:
60+
return ''
61+
5262
class Meta:
5363
model = Component
5464
fields = (
@@ -69,9 +79,6 @@ class Meta:
6979
'open_issues',
7080
'version',
7181
'author',
72-
'author_email',
73-
'npm_url',
74-
'homepage_url',
7582
'license',
7683
)
7784

main/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def all_components(request): # requested on_load() for querying
2626
})
2727

2828
def top_components(request):
29-
top_components = TopComponentSerializer(Component.objects.all().order_by('-downloads')[:10], many=True)
29+
# Download data is from Github and hence stars are used
30+
top_components = TopComponentSerializer(Component.objects.all().order_by('-stars')[:10], many=True)
3031
return JsonResponse({
3132
'top_components':top_components.data,
3233
})

tests/test_views.py

Lines changed: 190 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
from time import sleep
77
from datetime import datetime
88
import pytz
9+
import urllib, urllib2
10+
import json
11+
import ast
912

1013
class IndexViewTest(TestCase):
1114

@@ -101,7 +104,7 @@ class AllComponentViewTest(TestCase):
101104
@classmethod
102105
def setUpTestData(cls):
103106
# Called initially when test is executed, create objects to be used by test methods
104-
# create 10 random objects
107+
# create 20 random objects
105108
number_of_components = 20
106109
for component_no in range(number_of_components):
107110
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):
128131
'id' in object and
129132
'url_name' in object
130133
)
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

Comments
 (0)