-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Created Tests for Prompt Model #117
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,164 @@ | ||
# Create your tests here. | ||
from django.test import Client | ||
from django.urls import reverse | ||
from rest_framework.test import APITestCase | ||
import json | ||
from prompt.models import Prompt | ||
from api.tests import PearTestCase | ||
from django.urls import reverse | ||
from rest_framework.test import APIClient | ||
|
||
|
||
class PromptTestCase(APITestCase): | ||
|
||
def setUp(self): | ||
pear_test_case = PearTestCase() | ||
pear_test_case.setUp() | ||
self.user_token = pear_test_case._create_user_and_login() | ||
self.client.credentials(HTTP_AUTHORIZATION="Token " + self.user_token) | ||
|
||
def populate(self): | ||
"""populates the database""" | ||
url = reverse("populate") | ||
return self.client.post(url, {"filenames": ["pear_prompts.txt"]}, format='json') | ||
|
||
def test_populate_prompts(self): | ||
"""tests that the code populates the 27 prompts in pear_prompts.txt""" | ||
|
||
response = self.populate() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see you use self.populate() a lot; why don't you put this in the setUp function which runs before each test? |
||
content = json.loads(response.content) | ||
self.assertTrue(content.get("success")) | ||
self.assertEqual(response.status_code, 201) | ||
self.assertEqual(Prompt.objects.count(), 27) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't want to make a test checking for exactly 27 prompts since we'd have to change this case every time we add a new prompt. What we really care about is that we get more than 0 prompts since we're assuming that there is at least one prompt on Pear. |
||
|
||
def test_repopulate(self): | ||
"""test repopulate""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO implement |
||
|
||
def test_get_all_prompts(self): | ||
"""tests getting all the promts""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. misspelled prompts |
||
populate = self.populate() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inconsistent naming with what you had before (line 27 is response) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also where are you using this variable? |
||
|
||
url = reverse("prompts") | ||
response = self.client.get(url, format='json') | ||
content = json.loads(response.content) | ||
self.assertTrue(content.get("success")) | ||
self.assertEqual(response.status_code, 200) | ||
|
||
def test_get_prompt_by_id(self): | ||
"""tests getting a promt by a valid id""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto misspell |
||
poulate = self.populate() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. misspelled variable |
||
|
||
url = reverse("prompt", args=[1]) | ||
response = self.client.get(url, format='json') | ||
content = json.loads(response.content) | ||
self.assertTrue(content.get("success")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see this logic quite a lot; have you considered making a helper function that asserts the contents of content? |
||
self.assertEqual(response.status_code, 200) | ||
data = content.get("data") | ||
self.assertIn("id", data) | ||
self.assertIn("question_name", data) | ||
self.assertIn("question_placeholder", data) | ||
|
||
def test_get_invalid_prompt(self): | ||
"""tests getting an invalid prompt""" | ||
poulate = self.populate() | ||
|
||
url = reverse("prompt", args=[1000]) | ||
response = self.client.get(url, format='json') | ||
content = json.loads(response.content) | ||
self.assertFalse(content.get("sucess")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. success is mispelled - how did this pass? |
||
self.assertEqual(response.status_code, 404) | ||
|
||
def test_create_prompt(self): | ||
"""tests creating a valid prompt""" | ||
poulate = self.populate() | ||
url = reverse("prompts") | ||
|
||
data = {"question_name": "My favorite animal is ...", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This constant is redefined in a couple functions, make a constant at the beginning of the class and refer to it in your tests |
||
"question_placeholder": "I really like the animal ..."} | ||
response = self.client.post(url, data, format='json') | ||
content = json.loads(response.content) | ||
self.assertTrue(content.get("success")) | ||
self.assertEqual(response.status_code, 201) | ||
data = content.get("data") | ||
self.assertIn("id", data) | ||
self.assertIn("question_name", data) | ||
self.assertIn("question_placeholder", data) | ||
|
||
def test_create_invalid_prompt(self): | ||
"""tests creating an invalid prompt""" | ||
poulate = self.populate() | ||
|
||
url = reverse("prompts") | ||
data = {"question": "some question"} | ||
response = self.client.post(url, data, format='json') | ||
content = json.loads(response.content) | ||
self.assertFalse(content.get("success")) | ||
self.assertEqual(response.status_code, 400) | ||
self.assertIn("error", content) | ||
|
||
def test_create_existing_prompt(self): | ||
"""tests creating already existing prompt""" | ||
poulate = self.populate() | ||
url = reverse("prompts") | ||
|
||
data = {"question_name": "Why are you on Pear?", | ||
"question_placeholder": "I'm on Pear because..."} | ||
response = self.client.post(url, data, format='json') | ||
content = json.loads(response.content) | ||
self.assertTrue(content.get("success")) | ||
data = content.get("data") | ||
self.assertEqual(response.status_code, 200) | ||
self.assertIn("id", data) | ||
self.assertIn("question_name", data) | ||
self.assertIn("question_placeholder", data) | ||
|
||
def test_update_prompt(self): | ||
"""tests updating a valid prompt """ | ||
poulate = self.populate() | ||
url = reverse("prompt", args=[1]) | ||
|
||
data = {"question_name": "Why are you on Pear?", | ||
"question_placeholder": "I am on Pear ..... "} | ||
response = self.client.post(url, data, format='json') | ||
content = json.loads(response.content) | ||
self.assertTrue(content.get("success")) | ||
self.assertEqual(response.status_code, 200) | ||
data = content.get("data") | ||
self.assertIn("id", data) | ||
self.assertIn("question_name", data) | ||
self.assertIn("question_placeholder", data) | ||
|
||
def test_update_invalid_prompt(self): | ||
"""tests updating invalid prompt""" | ||
poulate = self.populate() | ||
url = reverse("prompt", args=[100]) | ||
|
||
data = {"question_name": "anything", | ||
"question_placeholder": "anything"} | ||
response = self.client.post(url, data, format='json') | ||
content = json.loads(response.content) | ||
self.assertFalse(content.get("success")) | ||
self.assertEqual(response.status_code, 404) | ||
|
||
def test_delete_prompt(self): | ||
"""tests deleting a valid prompt""" | ||
poulate = self.populate() | ||
url = reverse("prompt", args=[1]) | ||
|
||
response = self.client.delete(url, format='json') | ||
content = json.loads(response.content) | ||
self.assertTrue(content.get("success")) | ||
self.assertEqual(response.status_code, 200) | ||
data = content.get("data") | ||
self.assertIn("id", data) | ||
self.assertIn("question_name", data) | ||
self.assertIn("question_placeholder", data) | ||
|
||
def test_delete_invalid_prompt(self): | ||
"""tests deleting an invalid prompt""" | ||
poulate = self.populate() | ||
url = reverse("prompt", args=[100]) | ||
|
||
response = self.client.delete(url, format='json') | ||
content = json.loads(response.content) | ||
self.assertFalse(content.get("success")) | ||
self.assertEqual(response.status_code, 404) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,11 +25,11 @@ def get(self, request): | |
|
||
def post(self, request): | ||
"""Create a prompt.""" | ||
try: | ||
data = json.loads(request.body) | ||
except json.JSONDecodeError: | ||
data = request.data | ||
return CreatePromptController(data, self.serializer_class).process() | ||
# try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete comments |
||
# data = json.loads(request.body) | ||
# except json.JSONDecodeError: | ||
# data = request.data | ||
return CreatePromptController(json.loads(request.body), self.serializer_class).process() | ||
|
||
|
||
class PromptView(generics.GenericAPIView): | ||
|
@@ -47,10 +47,7 @@ def get(self, request, id): | |
|
||
def post(self, request, id): | ||
"""Update prompt by id.""" | ||
try: | ||
data = json.loads(request.body) | ||
except json.JSONDecodeError: | ||
data = request.data | ||
data = json.loads(request.body) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is inconsistent with what you passed into CreatePromptController; make sure the code is symmetric so put the |
||
return UpdatePromptController(id, data, self.serializer_class).process() | ||
|
||
def delete(self, request, id): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Capitalize first word