Skip to content

Commit 465e579

Browse files
surajitsurajit
authored andcommitted
add test for contact exceptions
1 parent 074331b commit 465e579

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

tests/conftest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import schemas.schema
1010
from crud.company import create_company
11+
from crud.contact import create_contact
12+
from models.payment import Organization
1113
from tests.test_database import SQLALCHEMY_DATABASE_URL
1214

1315
from dependencies.dependencies import get_db
@@ -47,3 +49,19 @@ def client(db):
4749
@pytest.fixture
4850
def organization(db):
4951
create_company(db, schemas.schema.CreateCompany(name="Test org", org_id=12345))
52+
53+
54+
@pytest.fixture
55+
def contact(db):
56+
organization = db.query(Organization).filter_by(name="Test org").all()[0]
57+
create_contact(
58+
db,
59+
schemas.schema.CreateContact(
60+
first_name="Test",
61+
last_name="User",
62+
63+
phone="2547120202002",
64+
company_name=organization.name,
65+
),
66+
org_id=organization.id,
67+
)

tests/routes/test_contact.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from unittest import mock
22

3+
from hubspot_api.utils import ContactException
4+
from models.contact import Contact
5+
36

47
@mock.patch("routes.v1.contact.utils.get_contact_by_email")
58
def test_get_contact_by_email(mock_get_contact_by_email, client):
@@ -43,3 +46,68 @@ def test_create_contact(
4346
)
4447
assert response.status_code == 200
4548
assert response.json() == {"contact_id": 12356}
49+
50+
51+
def test_create_contact_returns_duplicate_email_error(organization, contact, client):
52+
response = client.post(
53+
"/api/v1/contacts/",
54+
json={
55+
"first_name": "raaj",
56+
"last_name": "das",
57+
"email": "[email protected]",
58+
"phone": "2547120202002",
59+
"company_name": "Test org",
60+
},
61+
)
62+
assert response.status_code == 200
63+
assert response.json() == {
64+
"detail": "duplicate key value violates unique constraint "
65+
'"contact_email_key"\nDETAIL: Key (email)=('
66+
"[email protected]) already exists.\n"
67+
}
68+
69+
70+
@mock.patch("routes.v1.contact._contact.delete_contact")
71+
@mock.patch("routes.v1.contact.utils.create_contact")
72+
def test_create_contact_raises_hubspot_contactexception(
73+
mock_create_contact, mock_delete_contact, organization, client
74+
):
75+
mock_create_contact.side_effect = ContactException(
76+
"Email with this contact exists " "in Hubspot"
77+
)
78+
mock_delete_contact.return_value = True
79+
response = client.post(
80+
"/api/v1/contacts/",
81+
json={
82+
"first_name": "test",
83+
"last_name": "user",
84+
"email": "[email protected]",
85+
"phone": "2547120202002",
86+
"company_name": "Test org",
87+
},
88+
)
89+
assert response.status_code == 200
90+
assert response.json() == {"detail": "Email with this contact exists in Hubspot"}
91+
92+
93+
@mock.patch("routes.v1.contact.utils.create_contact")
94+
def test_create_contact_rollbacks_db_contact_for_hubspot_exception(
95+
mock_create_contact, db, organization, client
96+
):
97+
mock_create_contact.side_effect = ContactException(
98+
"Email with this contact exists " "in Hubspot"
99+
)
100+
response = client.post(
101+
"/api/v1/contacts/",
102+
json={
103+
"first_name": "test",
104+
"last_name": "user",
105+
"email": "[email protected]",
106+
"phone": "2547120202002",
107+
"company_name": "Test org",
108+
},
109+
)
110+
contact = db.query(Contact).filter_by(email="[email protected]").all()
111+
assert response.status_code == 200
112+
assert response.json() == {"detail": "Email with this contact exists in Hubspot"}
113+
assert contact == []

0 commit comments

Comments
 (0)