Skip to content

Commit

Permalink
Update testing code to be compatible with PyCharm Django test runner …
Browse files Browse the repository at this point in the history
…+ coverage
  • Loading branch information
dr-Jess committed Mar 24, 2024
1 parent 3f3ca47 commit 2e3439d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
3 changes: 3 additions & 0 deletions backend/sublet/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ class Meta:
read_only_fields = ["id", "created_at", "subletter", "sublettees"]
fields = [
"id",
"subletter",
"sublettees",
"is_published",
"amenities",
"title",
Expand All @@ -173,6 +175,7 @@ class Meta:
"external_link",
"price",
"negotiable",
"created_at",
"start_date",
"end_date",
"expires_at",
Expand Down
34 changes: 17 additions & 17 deletions backend/tests/portal/test_polls.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def setUp(self):
poll.save()

poll_1 = Poll.objects.get(question="How is your day")
self.id = poll_1.id
self.poll_id = poll_1.id

@mock.patch("portal.serializers.get_user_clubs", mock_get_user_clubs)
def test_create_poll(self):
Expand All @@ -122,11 +122,11 @@ def test_update_poll(self):
payload = {
"question": "New question",
}
response = self.client.patch(f"/portal/polls/{self.id}/", payload)
response = self.client.patch(f"/portal/polls/{self.poll_id}/", payload)
res_json = json.loads(response.content)
# asserts that the update worked
self.assertEqual(self.id, res_json["id"])
self.assertEqual("New question", Poll.objects.get(id=self.id).question)
self.assertEqual(self.poll_id, res_json["id"])
self.assertEqual("New question", Poll.objects.get(id=self.poll_id).question)

@mock.patch("portal.serializers.get_user_clubs", mock_get_user_clubs)
@mock.patch("portal.logic.get_user_info", mock_get_user_info)
Expand Down Expand Up @@ -158,26 +158,26 @@ def test_null_user_info_browse(self):
@mock.patch("portal.permissions.get_user_clubs", mock_get_user_clubs)
@mock.patch("portal.logic.get_user_info", mock_get_user_info)
def test_create_option(self):
payload_1 = {"poll": self.id, "choice": "yes!"}
payload_2 = {"poll": self.id, "choice": "no!"}
payload_1 = {"poll": self.poll_id, "choice": "yes!"}
payload_2 = {"poll": self.poll_id, "choice": "no!"}
self.client.post("/portal/options/", payload_1)
self.client.post("/portal/options/", payload_2)
self.assertEqual(2, PollOption.objects.all().count())
# asserts options were created and were placed to right poll
for poll_option in PollOption.objects.all():
self.assertEqual(Poll.objects.get(id=self.id), poll_option.poll)
self.assertEqual(Poll.objects.get(id=self.poll_id), poll_option.poll)
response = self.client.post("/portal/polls/browse/", {"id_hash": 1})
res_json = json.loads(response.content)
self.assertEqual(2, len(res_json[0]["options"]))

@mock.patch("portal.permissions.get_user_clubs", mock_get_user_clubs)
@mock.patch("portal.views.get_user_clubs", mock_get_user_clubs)
def test_update_option(self):
payload_1 = {"poll": self.id, "choice": "yes!"}
payload_1 = {"poll": self.poll_id, "choice": "yes!"}
response = self.client.post("/portal/options/", payload_1)
res_json = json.loads(response.content)
self.assertEqual("yes!", PollOption.objects.get(id=res_json["id"]).choice)
payload_2 = {"poll": self.id, "choice": "no!"}
payload_2 = {"poll": self.poll_id, "choice": "no!"}
# checks that poll's option was changed
self.client.patch(f'/portal/options/{res_json["id"]}/', payload_2)
self.assertEqual("no!", PollOption.objects.get(id=res_json["id"]).choice)
Expand All @@ -200,11 +200,11 @@ def test_review_poll(self):
@mock.patch("portal.permissions.get_user_clubs", mock_get_user_clubs)
@mock.patch("portal.logic.get_user_info", mock_get_user_info)
def test_more_than_five_options(self):
payload_1 = {"poll": self.id, "choice": "1"}
payload_2 = {"poll": self.id, "choice": "2"}
payload_3 = {"poll": self.id, "choice": "3"}
payload_4 = {"poll": self.id, "choice": "4"}
payload_5 = {"poll": self.id, "choice": "5"}
payload_1 = {"poll": self.poll_id, "choice": "1"}
payload_2 = {"poll": self.poll_id, "choice": "2"}
payload_3 = {"poll": self.poll_id, "choice": "3"}
payload_4 = {"poll": self.poll_id, "choice": "4"}
payload_5 = {"poll": self.poll_id, "choice": "5"}
self.client.post("/portal/options/", payload_1)
self.client.post("/portal/options/", payload_2)
self.client.post("/portal/options/", payload_3)
Expand All @@ -213,17 +213,17 @@ def test_more_than_five_options(self):
self.assertEqual(5, PollOption.objects.all().count())
# asserts options were created and were placed to right poll
for poll_option in PollOption.objects.all():
self.assertEqual(Poll.objects.get(id=self.id), poll_option.poll)
self.assertEqual(Poll.objects.get(id=self.poll_id), poll_option.poll)
response = self.client.post("/portal/polls/browse/", {"id_hash": 1})
res_json = json.loads(response.content)
self.assertEqual(5, len(res_json[0]["options"]))
# adding more than 5 options to same poll should not be allowed
payload_6 = {"poll": self.id, "choice": "6"}
payload_6 = {"poll": self.poll_id, "choice": "6"}
response = self.client.post("/portal/options/", payload_6)
self.assertEqual(5, PollOption.objects.all().count())

def test_option_vote_view(self):
response = self.client.get(f"/portal/polls/{self.id}/option_view/")
response = self.client.get(f"/portal/polls/{self.poll_id}/option_view/")
res_json = json.loads(response.content)
self.assertEqual("pennlabs", res_json["club_code"])
# test that options key is in response
Expand Down
12 changes: 6 additions & 6 deletions backend/tests/portal/test_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def setUp(self):
post_1 = Post.objects.all().first()
post_1.status = Post.STATUS_APPROVED
post_1.save()
self.id = post_1.id
self.post_id = post_1.id

@mock.patch("portal.serializers.get_user_clubs", mock_get_user_clubs)
def test_create_post(self):
Expand Down Expand Up @@ -100,10 +100,10 @@ def test_fail_post(self):
@mock.patch("portal.permissions.get_user_clubs", mock_get_user_clubs)
def test_update_post(self):
payload = {"title": "New Test Title 3"}
response = self.client.patch(f"/portal/posts/{self.id}/", payload)
response = self.client.patch(f"/portal/posts/{self.post_id}/", payload)
res_json = json.loads(response.content)
self.assertEqual(self.id, res_json["id"])
self.assertEqual("New Test Title 3", Post.objects.get(id=self.id).title)
self.assertEqual(self.post_id, res_json["id"])
self.assertEqual("New Test Title 3", Post.objects.get(id=self.post_id).title)
# since the user is not an admin, approved should be set to false after update
self.assertEqual(Post.STATUS_DRAFT, res_json["status"])

Expand All @@ -113,9 +113,9 @@ def test_update_post_admin(self):
admin = User.objects.create_superuser("[email protected]", "admin", "admin")
self.client.force_authenticate(user=admin)
payload = {"title": "New Test Title 3"}
response = self.client.patch(f"/portal/posts/{self.id}/", payload)
response = self.client.patch(f"/portal/posts/{self.post_id}/", payload)
res_json = json.loads(response.content)
self.assertEqual(self.id, res_json["id"])
self.assertEqual(self.post_id, res_json["id"])
self.assertEqual(Post.STATUS_APPROVED, res_json["status"])

@mock.patch("portal.serializers.get_user_clubs", mock_get_user_clubs)
Expand Down
1 change: 0 additions & 1 deletion backend/tests/sublet/test_sublets.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ def test_create_sublet(self):
}
response = self.client.post("/sublet/properties/", payload)
res_json = json.loads(response.content)
print(res_json)
match_keys = [
"title",
"address",
Expand Down
Empty file added backend/utils/__init__.py
Empty file.

0 comments on commit 2e3439d

Please sign in to comment.