Skip to content

Commit 36bb9eb

Browse files
surajitsurajit
authored andcommitted
modify logs and test
1 parent 1296222 commit 36bb9eb

File tree

4 files changed

+59
-21
lines changed

4 files changed

+59
-21
lines changed

routes/v1/company.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from fastapi import APIRouter
22
from fastapi import Depends
33
from fastapi import HTTPException
4+
from fastapi import Request
45
from sqlalchemy.orm import Session
56

67
from crud.company import CompanyExistException
8+
from logger.log import save_log
79
from schemas import schema
810
from hubspot_api import utils
911
from dependencies.dependencies import get_db
@@ -17,7 +19,8 @@
1719

1820

1921
@router.post("/companies/")
20-
def create_company(company: schema.CreateCompany, db: Session = Depends(get_db)):
22+
@save_log
23+
async def create_company(request: Request, company: schema.CreateCompany, db: Session = Depends(get_db)):
2124
db_company = None
2225
try:
2326
db_company = _company.create_company(db, company)
@@ -30,7 +33,8 @@ def create_company(company: schema.CreateCompany, db: Session = Depends(get_db))
3033

3134

3235
@router.get("/company/{company_name}/")
33-
def get_company(company_name):
36+
@save_log
37+
async def get_company(request: Request, company_name):
3438
try:
3539
company = utils.get_company_by_name(company_name)
3640
except utils.CompanyException as exc:

routes/v1/contact.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from fastapi import APIRouter
22
from fastapi import Depends
33
from fastapi import HTTPException
4+
from fastapi import Request
45
from sqlalchemy.orm import Session
56

67
from crud.contact import ContactExistException
78
from crud import contact as _contact
9+
from logger.log import save_log
810
from schemas import schema
911
from hubspot_api import utils
1012
from dependencies.dependencies import get_db
@@ -18,7 +20,9 @@
1820

1921

2022
@router.post("/contacts/")
21-
def create_contact(contact: schema.CreateContact, db: Session = Depends(get_db)):
23+
@save_log
24+
async def create_contact(request: Request, contact: schema.CreateContact,
25+
db: Session = Depends(get_db)):
2226
db_contact = None
2327
try:
2428
org_name = contact.company_name
@@ -33,7 +37,8 @@ def create_contact(contact: schema.CreateContact, db: Session = Depends(get_db))
3337

3438

3539
@router.get("/contact/{email}/")
36-
def get_contact(email):
40+
@save_log
41+
async def get_contact(request: Request, email):
3742
try:
3843
contact = utils.get_contact_by_email(email)
3944
except utils.ContactException as exc:

tests/routes/test_company.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@
55

66

77
@mock.patch("routes.v1.company.utils.get_company_by_name")
8-
def test_company(mock_get_company_by_name, client):
8+
@mock.patch("logger.log.logger.debug")
9+
def test_company(mock_logger_debug,
10+
mock_get_company_by_name, client):
11+
mock_logger_debug.return_value = "Ok"
912
mock_get_company_by_name.return_value = {"id": 12345}
1013
response = client.get("/api/v1/company/test-company/")
1114
assert response.status_code == 200
1215
assert response.json() == {"id": 12345}
1316

1417

1518
@mock.patch("routes.v1.company.utils.get_company_by_name")
16-
def test_company_for_hubspot_companyexception(mock_get_company_by_name, client):
19+
@mock.patch("logger.log.logger.debug")
20+
def test_company_for_hubspot_company_exception(mock_logger_debug,
21+
mock_get_company_by_name, client):
22+
mock_logger_debug.return_value = "Ok"
1723
mock_get_company_by_name.side_effect = CompanyException(
1824
"Something went wrong in fetching company from Hubspot"
1925
)
@@ -25,7 +31,9 @@ def test_company_for_hubspot_companyexception(mock_get_company_by_name, client):
2531

2632

2733
@mock.patch("routes.v1.company.utils.create_company")
28-
def test_create_company(mock_create_company, client):
34+
@mock.patch("logger.log.logger.debug")
35+
def test_create_company(mock_logger_debug, mock_create_company, client):
36+
mock_logger_debug.return_value = "Ok"
2937
mock_create_company.return_value = {"company_id": 1111}
3038
response = client.post(
3139
"/api/v1/companies/", json={"org_id": 123, "name": "Warner Bros co"}
@@ -35,7 +43,10 @@ def test_create_company(mock_create_company, client):
3543

3644

3745
@mock.patch("routes.v1.company.utils.create_company")
38-
def test_create_company_with_hubspot_companyexception(mock_create_company, client, db):
46+
@mock.patch("logger.log.logger.debug")
47+
def test_create_company_with_hubspot_company_exception(mock_logger_debug, mock_create_company,
48+
client, db):
49+
mock_logger_debug.return_value = "Ok"
3950
mock_create_company.side_effect = CompanyException(
4051
"Something went wrong in creating company in Hubspot"
4152
)
@@ -50,25 +61,31 @@ def test_create_company_with_hubspot_companyexception(mock_create_company, clien
5061
assert company == []
5162

5263

53-
def test_create_company_with_duplicate_organization_org_id(client, organization):
64+
@mock.patch("logger.log.logger.debug")
65+
def test_create_company_with_duplicate_organization_org_id(mock_logger_debug,
66+
client, organization):
67+
mock_logger_debug.return_value = "Ok"
5468
response = client.post(
5569
"/api/v1/companies/", json={"org_id": 12345, "name": "UWISO"}
5670
)
5771
assert response.status_code == 200
5872
assert response.json() == {
5973
"detail": "duplicate key value violates unique constraint "
60-
'"organization_org_id_key"\nDETAIL: Key (org_id)=('
61-
"12345) already exists.\n"
74+
'"organization_org_id_key"\nDETAIL: Key (org_id)=('
75+
"12345) already exists.\n"
6276
}
6377

6478

65-
def test_create_company_with_duplicate_organization_name(client, organization):
79+
@mock.patch("logger.log.logger.debug")
80+
def test_create_company_with_duplicate_organization_name(mock_logger_debug,
81+
client, organization):
82+
mock_logger_debug.return_value = "Ok"
6683
response = client.post(
6784
"/api/v1/companies/", json={"org_id": 1232245, "name": "Test org"}
6885
)
6986
assert response.status_code == 200
7087
assert response.json() == {
7188
"detail": "duplicate key value violates unique constraint "
72-
'"organization_name_key"\nDETAIL: Key (name)=('
73-
"Test org) already exists.\n"
89+
'"organization_name_key"\nDETAIL: Key (name)=('
90+
"Test org) already exists.\n"
7491
}

tests/routes/test_contact.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66

77
@mock.patch("routes.v1.contact.utils.get_contact_by_email")
8-
def test_get_contact_by_email(mock_get_contact_by_email, client):
8+
@mock.patch("logger.log.logger.debug")
9+
def test_get_contact_by_email(mock_logger_debug, mock_get_contact_by_email, client):
10+
mock_logger_debug.return_value = "Ok"
911
mock_get_contact_by_email.return_value = {
1012
"id": "1234",
1113
"email": "[email protected]",
@@ -24,9 +26,11 @@ def test_get_contact_by_email(mock_get_contact_by_email, client):
2426

2527
@mock.patch("routes.v1.contact.utils.create_contact")
2628
@mock.patch("routes.v1.contact._contact.create_contact")
29+
@mock.patch("logger.log.logger.debug")
2730
def test_create_contact(
28-
mock_db_create_contact, mock_hubspot_create_contact, organization, client
31+
mock_logger_debug, mock_db_create_contact, mock_hubspot_create_contact, organization, client
2932
):
33+
mock_logger_debug.return_value = "Ok"
3034
mock_db_create_contact.return_value = {
3135
"first_name": "raaj",
3236
"last_name": "das",
@@ -48,7 +52,10 @@ def test_create_contact(
4852
assert response.json() == {"contact_id": 12356}
4953

5054

51-
def test_create_contact_returns_duplicate_email_error(organization, contact, client):
55+
@mock.patch("logger.log.logger.debug")
56+
def test_create_contact_returns_duplicate_email_error(mock_logger_debug,
57+
organization, contact, client):
58+
mock_logger_debug.return_value = "Ok"
5259
response = client.post(
5360
"/api/v1/contacts/",
5461
json={
@@ -62,16 +69,19 @@ def test_create_contact_returns_duplicate_email_error(organization, contact, cli
6269
assert response.status_code == 200
6370
assert response.json() == {
6471
"detail": "duplicate key value violates unique constraint "
65-
'"contact_email_key"\nDETAIL: Key (email)=('
66-
"[email protected]) already exists.\n"
72+
'"contact_email_key"\nDETAIL: Key (email)=('
73+
"[email protected]) already exists.\n"
6774
}
6875

6976

7077
@mock.patch("routes.v1.contact._contact.delete_contact")
7178
@mock.patch("routes.v1.contact.utils.create_contact")
79+
@mock.patch("logger.log.logger.debug")
7280
def test_create_contact_raises_hubspot_contactexception(
73-
mock_create_contact, mock_delete_contact, organization, client
81+
mock_logger_debug,
82+
mock_create_contact, mock_delete_contact, organization, client
7483
):
84+
mock_logger_debug.return_value = "Ok"
7585
mock_create_contact.side_effect = ContactException(
7686
"Email with this contact exists in Hubspot"
7787
)
@@ -91,9 +101,11 @@ def test_create_contact_raises_hubspot_contactexception(
91101

92102

93103
@mock.patch("routes.v1.contact.utils.create_contact")
104+
@mock.patch("logger.log.logger.debug")
94105
def test_create_contact_rollbacks_db_contact_for_hubspot_exception(
95-
mock_create_contact, db, organization, client
106+
mock_logger_debug, mock_create_contact, db, organization, client
96107
):
108+
mock_logger_debug.return_value = "Ok"
97109
mock_create_contact.side_effect = ContactException(
98110
"Email with this contact exists " "in Hubspot"
99111
)

0 commit comments

Comments
 (0)