Skip to content

Commit 44750dc

Browse files
committed
Test for detail components view
1 parent 46efe90 commit 44750dc

File tree

2 files changed

+90
-4
lines changed

2 files changed

+90
-4
lines changed

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

tests/test_views.py

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class AllComponentViewTest(TestCase):
101101
@classmethod
102102
def setUpTestData(cls):
103103
# Called initially when test is executed, create objects to be used by test methods
104-
# create 10 random objects
104+
# create 20 random objects
105105
number_of_components = 20
106106
for component_no in range(number_of_components):
107107
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):
128128
'id' in object and
129129
'url_name' in object
130130
)
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

Comments
 (0)