Skip to content

Commit 6456c1a

Browse files
test: Enhance test coverage for the business application
This commit expands the test suite for the `business` application, improving validation and model testing. Key additions include: - **Authentication:** Added tests to `InvalidCompanyRegistrationTestCase` and `InvalidCompanyAuthenticationTestCase` to handle cases like missing email on creation and sign-in with an invalid email. - **Models:** Created `promo_code/business/tests/promocodes/test_models.py` to verify the `__str__` representations for the `Company`, `Promo`, and `PromoCode` models. - **Promo Creation:** Added validation tests in `TestPromoCreate` to ensure that `promo_common` is provided for 'COMMON' promos and `promo_unique` is provided for 'UNIQUE' promos.
1 parent 0df0649 commit 6456c1a

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

promo_code/business/tests/auth/test_validation.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ def test_short_company_name(self):
9090
rest_framework.status.HTTP_400_BAD_REQUEST,
9191
)
9292

93+
def test_create_company_missing_email_fiels(self):
94+
with self.assertRaisesMessage(
95+
ValueError,
96+
'The Email must be set',
97+
):
98+
business.models.Company.objects.create_company(
99+
name=self.valid_data['name'],
100+
password=self.valid_data['password'],
101+
email=None,
102+
)
103+
93104

94105
class InvalidCompanyAuthenticationTestCase(
95106
business.tests.auth.base.BaseBusinessAuthTestCase,
@@ -132,3 +143,24 @@ def test_signin_invalid_password(self):
132143
response.status_code,
133144
rest_framework.status.HTTP_401_UNAUTHORIZED,
134145
)
146+
147+
def test_signin_invalid_email(self):
148+
business.models.Company.objects.create_company(
149+
email=self.valid_data['email'],
150+
name=self.valid_data['name'],
151+
password=self.valid_data['password'],
152+
)
153+
154+
data = {
155+
'email': '[email protected]',
156+
'password': self.valid_data['password'],
157+
}
158+
response = self.client.post(
159+
self.company_signin_url,
160+
data,
161+
format='json',
162+
)
163+
self.assertEqual(
164+
response.status_code,
165+
rest_framework.status.HTTP_400_BAD_REQUEST,
166+
)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import django.test
2+
3+
import business.constants
4+
import business.models
5+
6+
7+
class CompanyModelTests(django.test.TestCase):
8+
def test_company_str_representation(self):
9+
company = business.models.Company.objects.create(
10+
11+
name='My Awesome Company',
12+
)
13+
self.assertEqual(str(company), 'My Awesome Company')
14+
15+
16+
class PromoModelTests(django.test.TestCase):
17+
def setUp(self):
18+
self.company = business.models.Company.objects.create(
19+
20+
name='TestCorp',
21+
)
22+
self.common_promo = business.models.Promo.objects.create(
23+
company=self.company,
24+
description='A common promo',
25+
max_count=100,
26+
used_count=50,
27+
mode=business.constants.PROMO_MODE_COMMON,
28+
)
29+
30+
def test_promo_str_representation(self):
31+
expected_str = (
32+
f'Promo {self.common_promo.id} ({self.common_promo.mode})'
33+
)
34+
self.assertEqual(str(self.common_promo), expected_str)
35+
36+
37+
class PromoCodeModelTests(django.test.TestCase):
38+
def test_promo_code_str_representation(self):
39+
company = business.models.Company.objects.create(
40+
41+
name='TestCorp',
42+
)
43+
promo = business.models.Promo.objects.create(
44+
company=company,
45+
description='Unique codes promo',
46+
max_count=10,
47+
mode=business.constants.PROMO_MODE_UNIQUE,
48+
)
49+
promo_code = business.models.PromoCode.objects.create(
50+
promo=promo,
51+
code='UNIQUE123',
52+
)
53+
self.assertEqual(str(promo_code), 'UNIQUE123')

promo_code/business/tests/promocodes/validations/test_create_validation.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,42 @@ def test_too_short_promo_common(self):
306306
rest_framework.status.HTTP_400_BAD_REQUEST,
307307
)
308308

309+
def test_missing_promo_common_field_for_common_promo(self):
310+
payload = {
311+
'description': 'Increased cashback 40% for new bank clients!',
312+
'max_count': 100,
313+
'target': {},
314+
'active_from': '2028-12-20',
315+
'mode': 'COMMON',
316+
}
317+
response = self.client.post(
318+
self.promo_list_create_url,
319+
payload,
320+
format='json',
321+
)
322+
self.assertEqual(
323+
response.status_code,
324+
rest_framework.status.HTTP_400_BAD_REQUEST,
325+
)
326+
327+
def test_missing_promo_unique_field_for_unique_promo(self):
328+
payload = {
329+
'description': 'Increased cashback 40% for new bank clients!',
330+
'max_count': 100,
331+
'target': {},
332+
'active_from': '2028-12-20',
333+
'mode': 'UNIQUE',
334+
}
335+
response = self.client.post(
336+
self.promo_list_create_url,
337+
payload,
338+
format='json',
339+
)
340+
self.assertEqual(
341+
response.status_code,
342+
rest_framework.status.HTTP_400_BAD_REQUEST,
343+
)
344+
309345
@parameterized.parameterized.expand(
310346
[
311347
(

0 commit comments

Comments
 (0)