Skip to content

Commit 708b2e1

Browse files
surajitsurajit
authored andcommitted
add tests for company
1 parent 70721e5 commit 708b2e1

File tree

7 files changed

+136
-16
lines changed

7 files changed

+136
-16
lines changed

database.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
from settings import DATABASE
66
from settings import DATABASE_USER
77
from settings import DATABASE_PASSWORD
8+
from settings import DATABASE_DRIVER
9+
from settings import DATABASE_HOST
810

9-
SQLALCHEMY_DATABASE_URL = f"postgresql://{DATABASE_USER}:{DATABASE_PASSWORD}@localhost/{DATABASE}"
11+
SQLALCHEMY_DATABASE_URL = f"{DATABASE_DRIVER}://{DATABASE_USER}:{DATABASE_PASSWORD}@{DATABASE_HOST}/{DATABASE}"
1012

11-
engine = create_engine(
12-
SQLALCHEMY_DATABASE_URL
13-
)
13+
engine = create_engine(SQLALCHEMY_DATABASE_URL)
1414
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
1515

1616
Base = declarative_base()

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Jinja2==3.1.2
1313
Mako==1.2.0
1414
MarkupSafe==2.1.1
1515
mypy-extensions==0.4.3
16+
mixer==7.2.1
1617
pytest==5.3.5
1718
pathspec==0.9.0
1819
platformdirs==2.5.2
@@ -31,4 +32,5 @@ tomli==2.0.1
3132
typing_extensions==4.2.0
3233
urllib3==1.26.9
3334
zipp==3.8.0
35+
sqlalchemy-utils==0.38.2
3436

routes/v1/contact.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@
2020
@router.post("/contacts/")
2121
def create_contact(contact: schema.CreateContact, db: Session = Depends(get_db)):
2222
db_contact = None
23-
hubspot_contact = None
2423
try:
2524
org_name = contact.company_name
26-
db_org = _company.filter_company_by_name(db, org_name)
27-
if db_org:
28-
org = db_org.all()[0]
29-
db_contact = _contact.create_contact(db, contact, org.id)
30-
hubspot_contact = utils.create_contact(data=contact.dict())
25+
db_org = _company.filter_company_by_name(db, org_name).all()[0]
26+
db_contact = _contact.create_contact(db, contact, db_org.id)
27+
hubspot_contact = utils.create_contact(data=contact.dict())
3128
except (utils.ContactException, ContactExistException) as exc:
3229
if isinstance(exc, utils.ContactException) and db_contact:
3330
_contact.delete_contact(db, db_contact.id)

settings.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
from decouple import config
44

55
DATABASE_HOST = config("DATABASE_HOST")
6-
DATABASE = config('DATABASE_NAME')
7-
DATABASE_USER = config('DATABASE_USER')
8-
DATABASE_PASSWORD = config('DATABASE_PASSWORD')
6+
DATABASE = config("DATABASE_NAME")
7+
DATABASE_USER = config("DATABASE_USER")
8+
DATABASE_PASSWORD = config("DATABASE_PASSWORD")
99
DATABASE_DRIVER = config("DATABASE_DRIVER")
10+
TEST_DATABASE_NAME = config("TEST_DATABASE_NAME")
1011

1112
STRIPE_PUBLISHABLE_KEY = config("STRIPE_PUBLISHABLE_KEY")
1213
STRIPE_SECRET_KEY = config("STRIPE_SECRET_KEY")
1314

1415
HUBSPOT_API_KEY = config("HUBSPOT_API_KEY")
1516

16-
SENDGRID_API_KEY = config('SENDGRID_API_KEY')
17-
WELCOME_MESSAGE_TEMPLATE_ID = config('WELCOME_MESSAGE_TEMPLATE_ID')
18-
PAYMENT_CONFIRMATION_TEMPLATE_ID = config('WELCOME_MESSAGE_TEMPLATE_ID')
17+
SENDGRID_API_KEY = config("SENDGRID_API_KEY")
18+
WELCOME_MESSAGE_TEMPLATE_ID = config("WELCOME_MESSAGE_TEMPLATE_ID")
19+
PAYMENT_CONFIRMATION_TEMPLATE_ID = config("WELCOME_MESSAGE_TEMPLATE_ID")

tests/conftest.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import pytest
2+
3+
from sqlalchemy import create_engine
4+
from sqlalchemy_utils import database_exists
5+
from sqlalchemy_utils import create_database
6+
from fastapi.testclient import TestClient
7+
from sqlalchemy.orm import Session
8+
9+
import schemas.schema
10+
from crud.company import create_company
11+
from tests.test_database import SQLALCHEMY_DATABASE_URL
12+
13+
from dependencies.dependencies import get_db
14+
from app import app
15+
from database import Base
16+
17+
18+
@pytest.fixture(scope="session")
19+
def db_engine():
20+
engine = create_engine(SQLALCHEMY_DATABASE_URL)
21+
if not database_exists:
22+
create_database(engine.url)
23+
24+
Base.metadata.create_all(bind=engine)
25+
yield engine
26+
27+
28+
@pytest.fixture(scope="function")
29+
def db(db_engine):
30+
connection = db_engine.connect()
31+
# begin a non-ORM transaction
32+
connection.begin()
33+
# bind an individual Session to the connection
34+
db = Session(bind=connection)
35+
yield db
36+
db.rollback()
37+
connection.close()
38+
39+
40+
@pytest.fixture(scope="function")
41+
def client(db):
42+
app.dependency_overrides[get_db] = lambda: db
43+
with TestClient(app) as c:
44+
yield c
45+
46+
47+
@pytest.fixture
48+
def organization(db):
49+
create_company(db, schemas.schema.CreateCompany(name="Test org", org_id=12345))

tests/routes/test_contact.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from unittest import mock
2+
3+
4+
@mock.patch("routes.v1.contact.utils.get_contact_by_email")
5+
def test_get_contact_by_email(mock_get_contact_by_email, client):
6+
mock_get_contact_by_email.return_value = {
7+
"id": "1234",
8+
"email": "[email protected]",
9+
"firstname": "test",
10+
"lastname": "user",
11+
}
12+
response = client.get("/api/v1/contact/[email protected]")
13+
assert response.status_code == 200
14+
assert response.json() == {
15+
"id": "1234",
16+
"email": "[email protected]",
17+
"firstname": "test",
18+
"lastname": "user",
19+
}
20+
21+
22+
@mock.patch("routes.v1.contact.utils.create_contact")
23+
@mock.patch("routes.v1.contact._contact.create_contact")
24+
def test_create_contact(
25+
mock_db_create_contact, mock_hubspot_create_contact, organization, client
26+
):
27+
mock_db_create_contact.return_value = {
28+
"first_name": "raaj",
29+
"last_name": "das",
30+
"email": "[email protected]",
31+
"phone": "+254720323309",
32+
}
33+
mock_hubspot_create_contact.return_value = {"contact_id": 12356}
34+
response = client.post(
35+
"/api/v1/contacts/",
36+
json={
37+
"first_name": "raaj",
38+
"last_name": "das",
39+
"email": "[email protected]",
40+
"phone": "+255720323309",
41+
"company_name": "Test org",
42+
},
43+
)
44+
assert response.status_code == 200
45+
assert response.json() == {"contact_id": 12356}

tests/test_database.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from sqlalchemy import create_engine
2+
from sqlalchemy.orm import sessionmaker
3+
4+
from dependencies.dependencies import get_db
5+
from app import app
6+
from settings import DATABASE_DRIVER
7+
from settings import DATABASE_USER
8+
from settings import DATABASE_PASSWORD
9+
from settings import DATABASE_HOST
10+
from settings import TEST_DATABASE_NAME
11+
12+
SQLALCHEMY_DATABASE_URL = f"{DATABASE_DRIVER}://{DATABASE_USER}:{DATABASE_PASSWORD}@{DATABASE_HOST}/{TEST_DATABASE_NAME}"
13+
14+
engine = create_engine(SQLALCHEMY_DATABASE_URL)
15+
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
16+
17+
18+
def override_get_db():
19+
try:
20+
db = TestingSessionLocal()
21+
yield db
22+
finally:
23+
db.close()
24+
25+
26+
app.dependency_overrides[get_db] = override_get_db

0 commit comments

Comments
 (0)