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 1 commit
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
165 changes: 164 additions & 1 deletion src/prompt/tests.py
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"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalize first word

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()
Copy link
Member

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The 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"""
Copy link
Member

Choose a reason for hiding this comment

The 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"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

misspelled prompts

populate = self.populate()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inconsistent naming with what you had before (line 27 is response)

Copy link
Member

Choose a reason for hiding this comment

The 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"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto misspell

poulate = self.populate()
Copy link
Member

Choose a reason for hiding this comment

The 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"))
Copy link
Member

Choose a reason for hiding this comment

The 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"))
Copy link
Member

Choose a reason for hiding this comment

The 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 ...",
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 ..."}
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)
15 changes: 6 additions & 9 deletions src/prompt/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Member

Choose a reason for hiding this comment

The 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):
Expand All @@ -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)
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