forked from learn-co-curriculum/python-p3-freebie-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_models.py
More file actions
32 lines (27 loc) · 989 Bytes
/
test_models.py
File metadata and controls
32 lines (27 loc) · 989 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import Base, Company, Dev
# Setup an in-memory SQLite database for testing
@pytest.fixture(scope="module")
def session():
engine = create_engine('sqlite:///:memory:')
Base.metadata.create_all(engine) # Create tables
Session = sessionmaker(bind=engine)
session = Session()
yield session
session.close()
def test_create_company(session):
new_company = Company(name="TestCo", founding_year=2020)
session.add(new_company)
session.commit()
company_in_db = session.query(Company).filter_by(name="TestCo").first()
assert company_in_db is not None
assert company_in_db.founding_year == 2020
def test_create_dev(session):
new_dev = Dev(name="Alice")
session.add(new_dev)
session.commit()
dev_in_db = session.query(Dev).filter_by(name="Alice").first()
assert dev_in_db is not None
assert dev_in_db.name == "Alice"