Skip to content

Commit 4e0b977

Browse files
authored
Merge pull request #20 from biojs/tests
Tests
2 parents 9832572 + 9c5474f commit 4e0b977

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ install:
88

99
script:
1010
- python manage.py migrate
11+
- python manage.py test

tests/__init__.py

Whitespace-only changes.

tests/test_models.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from django.test import TestCase
2+
3+
# Tests for models
4+
5+
from main.models import *
6+
import re
7+
8+
class ComponentModelTest(TestCase):
9+
10+
@classmethod
11+
def setUpTestData(cls):
12+
# Called initially when test is executed, create object to be used by test methods
13+
Component.objects.create(name='@test/component', author='test_author')
14+
15+
# Test max length of name
16+
def test_name_max_length(self):
17+
component = Component.objects.get(id=1)
18+
max_length = component._meta.get_field('name').max_length
19+
self.assertEquals(max_length, 100)
20+
21+
# Test if url_name is a Slug Field
22+
def test_url_name(self):
23+
component = Component.objects.get(id=1)
24+
url_name = component.url_name
25+
self.assertTrue(re.match('[-\w]+', url_name))
26+
27+
# Test max length of author name
28+
def test_author_max_length(self):
29+
component = Component.objects.get(id=1)
30+
max_length = component._meta.get_field('author').max_length
31+
self.assertEquals(max_length, 200)
32+
33+
class TagModelTest(TestCase):
34+
35+
@classmethod
36+
def setUpTestData(cls):
37+
# Called initially when test is executed, create object to be used by test methods
38+
Tag.objects.create(name='test_tag')
39+
40+
# Test max length of name
41+
def test_name_max_length(self):
42+
tag = Tag.objects.get(id=1)
43+
max_length = tag._meta.get_field('name').max_length
44+
self.assertEquals(max_length, 50)
45+
46+
class ContributorModelTest(TestCase):
47+
48+
@classmethod
49+
def setUpTestData(cls):
50+
# Called initially when test is executed, create object to be used by test methods
51+
Contributor.objects.create(username='test_contributor')
52+
53+
# Test max length of name
54+
def test_name_max_length(self):
55+
contributor = Contributor.objects.get(id=1)
56+
max_length = contributor._meta.get_field('username').max_length
57+
self.assertEquals(max_length, 100)

tests/test_views.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from django.test import TestCase
2+
3+
from main.models import *
4+
from django.urls import reverse
5+
import random
6+
from time import sleep
7+
from datetime import datetime
8+
import pytz
9+
10+
class IndexViewTest(TestCase):
11+
12+
@classmethod
13+
def setUpTestData(cls):
14+
# Called initially when test is executed, create objects to be used by test methods
15+
# create 10 random objects
16+
number_of_components = 10
17+
for component_no in range(number_of_components):
18+
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()))
19+
sleep(0.5)
20+
21+
def test_view_url_exists_at_desired_location(self):
22+
response = self.client.get('/')
23+
self.assertEqual(response.status_code, 200)
24+
25+
def test_view_accessible_by_name(self):
26+
response = self.client.get(reverse('main:index'))
27+
self.assertEqual(response.status_code, 200)
28+
29+
# Tests whether the relevant keys are present in the json response and length of each list is maximum 3
30+
def test_relevance_of_response(self):
31+
response = self.client.get(reverse('main:index'))
32+
self.assertTrue('most_recent_components' in response.json())
33+
self.assertTrue('top_dl_components' in response.json())
34+
self.assertTrue('top_starred_components' in response.json())
35+
self.assertTrue(len(response.json()['most_recent_components'])<4)
36+
self.assertTrue(len(response.json()['top_dl_components'])<4)
37+
self.assertTrue(len(response.json()['top_starred_components'])<4)

0 commit comments

Comments
 (0)