Skip to content

Commit 28a8e18

Browse files
committed
Created failing tests for Tag and Source
1 parent 7dea84c commit 28a8e18

File tree

1 file changed

+61
-8
lines changed

1 file changed

+61
-8
lines changed
Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
from django.test import TestCase
22
from rest_framework.test import APIClient, APITestCase
33
from accounts.models import User
4-
from challenges.models import Challenge
4+
from challenges.models import Challenge, Tag, Attachment, Source
55
import sure
66
import json
7+
import pdb
78

89
class ChallengeApiTests(APITestCase):
910
def setUp(self):
1011
self.client = APIClient()
1112
self.content_type = 'application/vnd.api+json'
1213

13-
def test_challenge_create(self):
14-
"""
15-
Ensure we get the correct challenge
16-
"""
17-
user = User.objects.filter(username="codecorgi").first()
14+
def get_sample_challenge(self, user=None, tags=None):
15+
if user is None:
16+
user = User.objects.filter(username="codecorgi").first()
1817

19-
challenge = Challenge.objects.create(user=user,
18+
challenge = Challenge.objects.create(
19+
user=user,
2020
title = 'Test Challenge',
2121
short_title = 'Test Challenge',
2222
owner = 'Gandalf',
@@ -31,12 +31,65 @@ def test_challenge_create(self):
3131
code_tips = 'Use a linter',
3232
)
3333

34+
if not tags is None:
35+
challenge.tags.set(tags)
36+
37+
return challenge
38+
39+
40+
def test_challenge_create(self):
41+
"""
42+
Ensure we get the correct challenge
43+
"""
44+
challenge = self.get_sample_challenge()
45+
3446
response = self.client.get(f'/services/api/challenges?pk={ challenge.id }',
3547
content_type=self.content_type)
3648

3749
response.status_code.should.equal(200)
3850

3951
response_data = json.loads(response.content)
4052
attributes = response_data['data'][0]['attributes']
41-
relationships = response_data['data'][0]['relationships']
4253
attributes['title'].should.equal(challenge.title)
54+
55+
def test_challenge_create_with_tags(self):
56+
"""
57+
Ensure we get the correct challenge
58+
"""
59+
tag1 = Tag.objects.create(name='Javascript')
60+
61+
challenge = self.get_sample_challenge(tags=[ tag1 ])
62+
63+
response = self.client.get(f'/services/api/challenges?pk={ challenge.id }',
64+
content_type=self.content_type)
65+
66+
response.status_code.should.equal(200)
67+
68+
response_data = json.loads(response.content)
69+
attributes = response_data['data'][0]['attributes']
70+
included = response_data['included']
71+
72+
attributes['title'].should.equal(challenge.title)
73+
74+
[item for item in included if item.get('type') == 'tags'][0]['attributes']['name'].should.equal(tag1.name)
75+
76+
def test_challenge_create_with_source(self):
77+
"""
78+
Ensure we get the correct challenge
79+
"""
80+
challenge = self.get_sample_challenge()
81+
source = Source.objects.create(
82+
challenge=challenge,
83+
name='github',
84+
url='https://github.com/corgicode'
85+
)
86+
87+
response = self.client.get(f'/services/api/challenges?pk={ challenge.id }',
88+
content_type=self.content_type)
89+
90+
response.status_code.should.equal(200)
91+
92+
response_data = json.loads(response.content)
93+
included = response_data['included']
94+
95+
[item for item in included if item.get('type') == 'sources'][0]['attributes']['name'].should.equal(source.name)

0 commit comments

Comments
 (0)