Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #79 from Mehdi-MosTafavi/fix/overall
Browse files Browse the repository at this point in the history
add some tests
  • Loading branch information
ShahrzadAzari authored Jul 10, 2022
2 parents 826c110 + edf7122 commit 739afa5
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 42 deletions.
30 changes: 29 additions & 1 deletion saku/account/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,22 @@ def test_login(self):
self.assertTrue("refresh" in response.data)
self.assertTrue("access" in response.data)

def test_change_password_failure_(self):
def test_change_password_failure_length(self):
self.client.force_authenticate(self.user)
url = reverse("account:change_password")
data = {
"old_password": "654321",
"new_password": "654321",
"new_password2": "654321",
}
response = self.client.put(url, data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn(
ErrorDetail(string="Password was entered incorrectly", code="invalid"),
response.data["non_field_errors"],
)

def test_change_password_failure_length(self):
self.client.force_authenticate(self.user)
url = reverse("account:change_password")
data = {
Expand Down Expand Up @@ -123,3 +138,16 @@ def test_forgot_password_success(self):
Profile.objects.create(user=self.user, email="[email protected]")
response = self.client.post(url, data, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_logout(self):
url = reverse("account:login")
response = self.client.post(
url, {"username": "test_user", "password": "Ab654321"}, format="json"
)

url = reverse("account:logout")
response = self.client.post(
url, {"refresh": response.data["refresh"]}, format="json"
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertTrue("access" in response.data)
30 changes: 15 additions & 15 deletions saku/auction/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ class Mode(models.IntegerChoices):
def __str__(self):
return self.name

def save(self, *args, **kwargs):
try:
pre_finished_at = Auction.objects.get(pk=self.pk).finished_at
except Auction.DoesNotExist:
pre_finished_at = None
super().save(*args, **kwargs)
post_finished_at = self.finished_at
if not self.celery_task_id: # initial task creation
task_object = save_best_bid.apply_async((self.pk,), eta=post_finished_at)
Auction.objects.filter(pk=self.pk).update(celery_task_id=task_object.id)
elif pre_finished_at != post_finished_at:
# revoke the old task
app.control.revoke(self.celery_task_id, terminate=True)
task_object = save_best_bid.apply_async((self.pk,), eta=post_finished_at)
Auction.objects.filter(pk=self.pk).update(celery_task_id=task_object.id)
# def save(self, *args, **kwargs):
# try:
# pre_finished_at = Auction.objects.get(pk=self.pk).finished_at
# except Auction.DoesNotExist:
# pre_finished_at = None
# super().save(*args, **kwargs)
# post_finished_at = self.finished_at
# if not self.celery_task_id: # initial task creation
# task_object = save_best_bid.apply_async((self.pk,), eta=post_finished_at)
# Auction.objects.filter(pk=self.pk).update(celery_task_id=task_object.id)
# elif pre_finished_at != post_finished_at:
# # revoke the old task
# app.control.revoke(self.celery_task_id, terminate=True)
# task_object = save_best_bid.apply_async((self.pk,), eta=post_finished_at)
# Auction.objects.filter(pk=self.pk).update(celery_task_id=task_object.id)
20 changes: 20 additions & 0 deletions saku/auction/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from rest_framework.exceptions import ErrorDetail
from rest_framework.test import APITestCase
from django.contrib.auth.models import User
from rest_framework import status
from auction.models import Auction, Tags, Category


Expand All @@ -22,6 +23,7 @@ def setUp(self) -> None:
"user": 0,
"category": "C1",
"tags": "T1,T2",
"token": "qwertyui",
}

def test_not_found_user(self):
Expand Down Expand Up @@ -161,3 +163,21 @@ def test_edit_auction_failure(self):
),
response.data["non_field_errors"],
)

def test_update_image_failure(self):
data1 = {"auction_image": "1.jpg"}
response = self.client.put(path="/auction/qwertyui", data=data1)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn(
ErrorDetail(
string="The submitted data was not a file. Check the encoding type on the form.",
code="invalid",
),
response.data["auction_image"],
)

def test_delete_image_success(self):
response = self.client.post(path="/auction/picture/qwertyui")
self.assertEqual(response.status_code, status.HTTP_200_OK)
response = self.client.get(path="/auction/qwertyui")
self.assertEqual(response.data["auction_image"], None)
2 changes: 1 addition & 1 deletion saku/bid/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.db.models import Max
from django.db.models import Max, Min
from rest_framework import serializers
from user_profile.serializers import GeneralProfileSerializer
from auction.models import Auction
Expand Down
31 changes: 31 additions & 0 deletions saku/bid/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ def setUp(self):
self.auction2.tags.set(tags)
self.auction2.save()

self.auction3 = Auction.objects.create(
created_at="2019-08-24T14:15:22Z",
token="33sdInBc",
name="string",
finished_at="2023-08-24T14:15:22Z",
mode=2,
limit=3000,
is_private=False,
user=self.user,
category=category,
)
self.auction2.tags.set(tags)
self.auction2.save()

self.client.force_authenticate(self.user2)

def test_create_bid_success(self):
Expand Down Expand Up @@ -133,6 +147,23 @@ def test_create_bid_failure_limit_2(self):
response.data["non_field_errors"],
)

def test_create_bid_failure_lower_exists(self):
url = reverse("bid:list_create_bid", args=(self.auction3.token,))

data = {"price": "1000"}
response = self.client.post(url, data, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

data = {"price": "2000"}
response = self.client.post(url, data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn(
ErrorDetail(
string="Lower bid for this auction already exists.", code="invalid"
),
response.data["non_field_errors"],
)

def test_create_bid_success_private_auction(self):
url = reverse("bid:list_create_bid", args=(self.auction2.token,))

Expand Down
73 changes: 48 additions & 25 deletions saku/homepage/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,51 +127,67 @@ def setUp(self):
self.auction4_user2.participants_num = 1
self.auction4_user2.save()

def test_homepage(self):
def test_income(self):
url = reverse("homepage:homepage", args=(2020,))
response = self.client.get(url, format="json")
data = response.data["data"]

income = data["income"]
seccussfull_auction_count = data["seccussfull_auction_count"]
auctions_participants_num = data["auctions_participants_num"]
auctions_count = data["auctions_count"]
last_auctions_participated = data["last_auctions_participated"]
last_auctions_created = data["last_auctions_created"]
income_list = data["income_list"]
your_colaberation_list = data["your_colaberation_list"]
your_colaberation_count = data["your_colaberation_count"]
others_colaberation_list = data["others_colaberation_list"]
others_colaberation_count = data["others_colaberation_count"]
expense_list = data["expense_list"]
expense = data["expense"]
auction1_participate_count = data["auction1_participate_count"]
auction1_create_count = data["auction1_create_count"]
auction2_participate_count = data["auction2_participate_count"]
auction2_create_count = data["auction2_create_count"]
yearly_income_list = data["yearly_income_list"]
yearly_expense_list = data["yearly_expense_list"]

# income
self.assertEqual(income, 500)
self.assertEqual(len(income_list), 1)
self.assertEqual(income_list[0], 500)
self.assertEqual(sum(income_list), income)
# colaberation

def test_colaberation(self):
url = reverse("homepage:homepage", args=(2020,))
response = self.client.get(url, format="json")
data = response.data["data"]
your_colaberation_list = data["your_colaberation_list"]
your_colaberation_count = data["your_colaberation_count"]
others_colaberation_list = data["others_colaberation_list"]
others_colaberation_count = data["others_colaberation_count"]

self.assertEqual(sum(your_colaberation_list), your_colaberation_count)
self.assertEqual(sum(others_colaberation_list), others_colaberation_count)
# expense

def test_expense(self):
url = reverse("homepage:homepage", args=(2020,))
response = self.client.get(url, format="json")
data = response.data["data"]
expense_list = data["expense_list"]
expense = data["expense"]

self.assertEqual(expense, 300)
self.assertEqual(len(expense_list), 1)
self.assertEqual(expense_list[0], 300)
self.assertEqual(sum(expense_list), expense)
# auction created

def test_auction_created(self):
url = reverse("homepage:homepage", args=(2020,))
response = self.client.get(url, format="json")
data = response.data["data"]
auctions_count = data["auctions_count"]
last_auctions_created = data["last_auctions_created"]
auction1_create_count = data["auction1_create_count"]
auction2_create_count = data["auction2_create_count"]

self.assertEqual(auctions_count, 2)
self.assertEqual(len(last_auctions_created), 2)
self.assertEqual(auction1_create_count, 1)
self.assertEqual(auction2_create_count, 1)
self.assertEqual(auction1_create_count + auction2_create_count, auctions_count)
# auction participated

def test_auction_participated(self):
url = reverse("homepage:homepage", args=(2020,))
response = self.client.get(url, format="json")
data = response.data["data"]
seccussfull_auction_count = data["seccussfull_auction_count"]
auctions_participants_num = data["auctions_participants_num"]
last_auctions_participated = data["last_auctions_participated"]
auction1_participate_count = data["auction1_participate_count"]
auction2_participate_count = data["auction2_participate_count"]

self.assertEqual(seccussfull_auction_count, 1)
self.assertEqual(auctions_participants_num, 2)
self.assertEqual(len(last_auctions_participated), 2)
Expand All @@ -181,7 +197,14 @@ def test_homepage(self):
auction1_participate_count + auction2_participate_count,
auctions_participants_num,
)
# yearly report

def test_yearly_report(self):
url = reverse("homepage:homepage", args=(2020,))
response = self.client.get(url, format="json")
data = response.data["data"]
yearly_income_list = data["yearly_income_list"]
yearly_expense_list = data["yearly_expense_list"]

self.assertEqual(len(yearly_income_list), 1)
self.assertEqual(yearly_income_list[0], 500)
self.assertEqual(len(yearly_expense_list), 0)
3 changes: 3 additions & 0 deletions saku/user_profile/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ def setUp(self):
self.user = User.objects.create_user(
username="test_user", password="Ab654321", email="[email protected]"
)
user2 = User.objects.create_user(
username="test_user2", password="Ab654321", email="[email protected]"
)
self.user.is_active = True
self.user.save()
self.profile = Profile.objects.create(user=self.user, email=self.user.email)
Expand Down

0 comments on commit 739afa5

Please sign in to comment.