Skip to content

Commit 5ecf9b1

Browse files
surajitsurajit
authored andcommitted
add tests for charge
1 parent 978962b commit 5ecf9b1

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

crud/charge.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from sqlalchemy.orm import Session
33

44
from models import payment
5+
from models.payment import Organization
56

67

78
class ChargeExistException(Exception):

tests/conftest.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
from sqlalchemy.orm import Session
88

99
import schemas.schema
10+
from crud.charge import add_charge
1011
from crud.company import create_company
1112
from crud.contact import create_contact
1213
from crud.message import save_message
1314
from models.message import MessageType
1415
from models.message import Carrier
15-
from models.message import Message
1616
from models.payment import Organization
1717
from tests.test_database import SQLALCHEMY_DATABASE_URL
1818

@@ -52,20 +52,36 @@ def client(db):
5252

5353
@pytest.fixture
5454
def organization(db):
55-
create_company(db, schemas.schema.CreateCompany(name="Test org", org_id=12345))
55+
yield create_company(
56+
db, schemas.schema.CreateCompany(name="Test org", org_id=12345)
57+
)
58+
59+
60+
@pytest.fixture
61+
@pytest.mark.asyncio
62+
async def charge_onboarding(db, organization):
63+
await add_charge(
64+
db,
65+
schemas.schema.CreateCharge(
66+
company_name="Test org",
67+
currency="USD",
68+
charge_type="ONBOARDING",
69+
amount=200,
70+
),
71+
organization.id,
72+
)
5673

5774

5875
@pytest.fixture
59-
def message(db):
76+
@pytest.mark.asyncio
77+
async def message(db):
6078
message_data = dict(
6179
message_id="123657ab",
6280
status_code=202,
6381
message_type=MessageType.EMAIL.value,
6482
carrier=Carrier.SENDGRID.value,
6583
)
66-
db_item = Message(**message_data)
67-
db.add(db_item)
68-
db.commit()
84+
await save_message(db, message_data)
6985

7086

7187
@pytest.fixture

tests/routes/test_charge.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from unittest import mock
2+
3+
import pytest
4+
5+
6+
@mock.patch("logger.log.logger.debug")
7+
@pytest.mark.asyncio
8+
async def test_create_charge(mock_logger_debug, organization, client):
9+
mock_logger_debug.return_value = "Ok"
10+
response = client.post(
11+
"/api/v1/charges/",
12+
json={
13+
"currency": "USD",
14+
"charge_type": "MAINTENANCE",
15+
"amount": 200,
16+
"company_name": "Test org",
17+
},
18+
)
19+
assert response.status_code == 201
20+
assert response.json()["charge_type"] == "maintenance"
21+
22+
23+
@mock.patch("logger.log.logger.debug")
24+
@pytest.mark.asyncio
25+
async def test_create_charge_for_duplicate_charge(
26+
mock_logger_debug, charge_onboarding, client
27+
):
28+
mock_logger_debug.return_value = "Ok"
29+
response = client.post(
30+
"/api/v1/charges/",
31+
json={
32+
"currency": "USD",
33+
"charge_type": "ONBOARDING",
34+
"amount": 200,
35+
"company_name": "Test org",
36+
},
37+
)
38+
assert response.status_code == 200

0 commit comments

Comments
 (0)