Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Noyan Aziz authored and Muhammad Noyan Aziz committed Dec 12, 2024
1 parent f60b48c commit c8a1720
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 50 deletions.
76 changes: 33 additions & 43 deletions commerce_coordinator/apps/commercetools/tests/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,94 +806,84 @@ def test_update_customer_with_anonymized_fields_exception(self):
def test_is_first_time_discount_eligible_success(self):
base_url = self.client_set.get_base_url_from_client()
email = '[email protected]'
code = 'discount-code'

mock_discount_codes = {
mock_orders = {
"total": 1,
"results": [
{"id": "discount-id-1"},
{"id": "discount-id-2"}
{
"discountCodes": [
{
"discountCode": {
"obj": {
"code": 'another-code'
}
}
}
]
}
]
}

mock_orders = {
"total": 0
}

with requests_mock.Mocker(real_http=True, case_sensitive=False) as mocker:
mocker.get(
f"{base_url}discount-codes",
json=mock_discount_codes,
status_code=200
)

mocker.get(
f"{base_url}orders",
json=mock_orders,
status_code=200
)

result = self.client_set.client.is_first_time_discount_eligible(email)
result = self.client_set.client.is_first_time_discount_eligible(email, code)
self.assertTrue(result)

def test_is_first_time_discount_eligible_not_eligible(self):
def test_is_first_time_discount_not_eligible(self):
base_url = self.client_set.get_base_url_from_client()
email = '[email protected]'
code = 'discount-code'

mock_discount_codes = {
mock_orders = {
"total": 1,
"results": [
{"id": "discount-id-1"},
{"id": "discount-id-2"}
{
"discountCodes": [
{
"discountCode": {
"obj": {
"code": code
}
}
}
]
}
]
}

mock_orders = {
"total": 1
}

with requests_mock.Mocker(real_http=True, case_sensitive=False) as mocker:
mocker.get(
f"{base_url}discount-codes",
json=mock_discount_codes,
status_code=200
)

mocker.get(
f"{base_url}orders",
json=mock_orders,
status_code=200
)

result = self.client_set.client.is_first_time_discount_eligible(email)
result = self.client_set.client.is_first_time_discount_eligible(email, code)
self.assertFalse(result)

def test_is_first_time_discount_eligible_invalid_email(self):
invalid_email = "[email protected]"
code = 'discount-code'
base_url = self.client_set.get_base_url_from_client()

mock_discount_codes = {
"results": [
{"id": "discount-id-1"},
{"id": "discount-id-2"}
]
}

mock_orders = {
"total": 0
}

with requests_mock.Mocker(real_http=True, case_sensitive=False) as mocker:
mocker.get(
f"{base_url}discount-codes",
json=mock_discount_codes,
status_code=200
)

mocker.get(
f"{base_url}orders",
json=mock_orders,
status_code=200
)

result = self.client_set.client.is_first_time_discount_eligible(invalid_email)
result = self.client_set.client.is_first_time_discount_eligible(invalid_email, code)
self.assertTrue(result)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ class CommercetoolsDiscountEligibilityPipelineTests(TestCase):
"""Commercetools pipeline testcase for checking discount eligibility in CT"""
def setUp(self) -> None:
self.mock_email = "[email protected]"
self.mock_code = "mock_code"
self.mock_eligible_result = True
self.mock_ineligible_result = False

Expand All @@ -281,7 +282,7 @@ def setUp(self) -> None:
def test_pipeline_eligible(self, mock_is_eligible):
pipe = CheckCommercetoolsDiscountEligibility("test_pipe", None)
mock_is_eligible.return_value = self.mock_eligible_result
ret = pipe.run_filter(self.mock_email)
ret = pipe.run_filter(self.mock_email, self.mock_code)
result_data = ret['is_eligible']
self.assertEqual(result_data, self.mock_eligible_result)

Expand All @@ -292,6 +293,6 @@ def test_pipeline_eligible(self, mock_is_eligible):
def test_pipeline_ineligible(self, mock_is_eligible):
pipe = CheckCommercetoolsDiscountEligibility("test_pipe", None)
mock_is_eligible.return_value = self.mock_ineligible_result
ret = pipe.run_filter(self.mock_email)
ret = pipe.run_filter(self.mock_email, self.mock_code)
result_data = ret['is_eligible']
self.assertEqual(result_data, self.mock_ineligible_result)
11 changes: 6 additions & 5 deletions commerce_coordinator/apps/lms/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ class FirstTimeDiscountEligibleViewTests(APITestCase):
test_user_username = 'test'
test_user_email = '[email protected]'
test_user_password = 'secret'
test_discount = 'first_time_discount'

url = reverse('lms:first_time_discount_eligible')

Expand Down Expand Up @@ -421,11 +422,11 @@ def test_get_with_valid_email_eligibility_true(self, mock_filter):
self.authenticate_user()
mock_filter.return_value = {'is_eligible': True}

response = self.client.get(self.url, {'email': self.test_user_email})
response = self.client.get(self.url, {'email': self.test_user_email, 'code': self.test_discount})

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, {"is_eligible": True})
mock_filter.assert_called_once_with(email=self.test_user_email)
mock_filter.assert_called_once_with(email=self.test_user_email, code='first_time_discount')

@patch('commerce_coordinator.apps.lms.views.CheckFirstTimeDiscountEligibility.run_filter')
def test_get_with_valid_email_eligibility_false(self, mock_filter):
Expand All @@ -435,11 +436,11 @@ def test_get_with_valid_email_eligibility_false(self, mock_filter):
self.authenticate_user()
mock_filter.return_value = {'is_eligible': False}

response = self.client.get(self.url, {'email': self.test_user_email})
response = self.client.get(self.url, {'email': self.test_user_email, 'code': self.test_discount})

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, {"is_eligible": False})
mock_filter.assert_called_once_with(email=self.test_user_email)
mock_filter.assert_called_once_with(email=self.test_user_email, code=self.test_discount)

def test_get_with_missing_email_fails(self):
"""
Expand All @@ -450,4 +451,4 @@ def test_get_with_missing_email_fails(self):
response = self.client.get(self.url)

self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertEqual(response.data, {'detail': 'Could not detect user email.'})
self.assertEqual(response.data, {'detail': 'Could not detect user email or discount code.'})

0 comments on commit c8a1720

Please sign in to comment.