Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 135 additions & 1 deletion src/prompt/tests.py
Original file line number Diff line number Diff line change
@@ -1 +1,135 @@
# 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)
self.populate()

def populate(self):
"""Populates the database"""

url = reverse("populate")

return self.client.post(url, {"filenames": ["pear_prompts.txt"]}, format='json')

def assert_helper(self, response, status_code, success, fields):
"""Asserts contents of response are accurate"""

content = json.loads(response.content)

if success:
self.assertTrue(content.get("success"))
else:
self.assertFalse(content.get("success"))
self.assertEqual(response.status_code, status_code)
if fields:
data = content.get("data")
self.assertIn("id", data)
self.assertIn("question_name", data)
self.assertIn("question_placeholder", data)

def prompt_response(self, arg, type, data=None):
"""Generates the response for any tests on a specific prompt"""

url = reverse("prompt", args=[arg])

if type == "get":
return self.client.get(url, format='json')
elif type == "post":
return self.client.post(url, data, format='json')
else:
return self.client.delete(url, format='json')

def prompts_response(self, type, data=None):
"""Generates the response for any tests on many prompts"""

url = reverse("prompts")

if type == "get":
return self.client.get(url, format='json')
else:
return self.client.post(url, data, format='json')

def test_repopulate_prompts(self):
"""Tests that the code repopulates prompts in pear_prompts.txt"""

response = self.populate()

self.assert_helper(response, 200, True, False)
self.assertTrue(Prompt.objects.count() > 0)

def test_get_all_prompts(self):
"""Tests getting all the prompts"""

self.assert_helper(self.prompts_response("get"), 200, True, False)

def test_get_prompt_by_id(self):
"""Tests getting a prompt by a valid id"""

self.assert_helper(self.prompt_response(1, "get"), 200, True, True)

def test_get_invalid_prompt(self):
"""Tests getting an invalid prompt"""

self.assert_helper(self.prompt_response(1000, "get"), 404, False, False)

def test_create_prompt(self):
"""Tests creating a valid prompt"""

data = {"question_name": "My favorite animal is ...",
Copy link
Member

Choose a reason for hiding this comment

The 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 ..."}

self.assert_helper(self.prompts_response("post", data), 201, True, True)

def test_create_invalid_prompt(self):
"""Tests creating an invalid prompt"""

data = {"question": "some question"}

self.assert_helper(self.prompts_response("post", data), 400, False, False)

def test_create_existing_prompt(self):
"""Tests creating already existing prompt"""

data = {"question_name": "Why are you on Pear?",
"question_placeholder": "I'm on Pear because..."}

self.assert_helper(self.prompts_response("post", data), 200, True, True)

def test_update_prompt(self):
"""Tests updating a valid prompt"""

data = {"question_name": "Why are you on Pear?",
"question_placeholder": "I am on Pear ..... "}

self.assert_helper(self.prompt_response(1, "post", data), 200, True, True)

def test_update_invalid_prompt(self):
"""Tests updating invalid prompt"""

data = {"question_name": "anything",
"question_placeholder": "anything"}

self.assert_helper(self.prompt_response(100, "post", data), 404, False, False)

def test_delete_prompt(self):
"""Tests deleting a valid prompt"""

self.assert_helper(self.prompt_response(1, "delete"), 200, True, True)

def test_delete_invalid_prompt(self):
"""Tests deleting an invalid prompt"""

self.assert_helper(self.prompt_response(100, "delete"), 404, False, False)
11 changes: 2 additions & 9 deletions src/prompt/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ 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()
return CreatePromptController(json.loads(request.body), self.serializer_class).process()


class PromptView(generics.GenericAPIView):
Expand All @@ -47,10 +43,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)
Copy link
Member

Choose a reason for hiding this comment

The 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 json.loads(request.body) you passed in directly into the controller as it's own variable data

return UpdatePromptController(id, data, self.serializer_class).process()

def delete(self, request, id):
Expand Down