Skip to content

Commit

Permalink
add link test
Browse files Browse the repository at this point in the history
  • Loading branch information
zcemycl committed Oct 24, 2023
1 parent 6128273 commit 47d3f01
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
44 changes: 44 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from sqlalchemy.orm import Session, sessionmaker
from sqlalchemy.sql import insert

import example_package.dataclasses.orm as d
from app.database import get_session
from app.main import app
from example_package.dataclasses import metadata, person
Expand All @@ -32,6 +33,39 @@ def get_engine() -> Engine:
engine.dispose()


@pytest.fixture(autouse=True)
def reset(get_engine: Engine):
engine = get_engine
metadata.drop_all(bind=engine)
metadata.create_all(bind=engine)


@pytest.fixture(scope="session")
def get_engine2() -> Engine:
db_url = "postgresql://postgres:postgres@localhost/postgres"
engine = create_engine(db_url)

d.metadata.drop_all(bind=engine)
d.metadata.create_all(bind=engine)

with open(Path("tests/test_data/base-persons.json"), "r") as f:
jsons = json.load(f)

stmt = insert(person).values(jsons)
with engine.begin() as conn:
_ = conn.execute(stmt)

yield engine
engine.dispose()


@pytest.fixture(autouse=True)
def reset2(get_engine2: Engine):
engine = get_engine2
d.metadata.drop_all(bind=engine)
d.metadata.create_all(bind=engine)


@pytest.fixture(scope="function", autouse=True)
def test_session(get_engine: Engine) -> Session:
session = sessionmaker(
Expand All @@ -42,6 +76,16 @@ def test_session(get_engine: Engine) -> Session:
session.close()


@pytest.fixture(scope="function", autouse=True)
def test_session2(get_engine2: Engine) -> Session:
session = sessionmaker(
bind=get_engine2, expire_on_commit=False, class_=Session
)
with session() as session:
yield session
session.close()


@pytest.fixture(scope="function", autouse=True)
def test_client(test_session: Session) -> TestClient:
def _local_session():
Expand Down
28 changes: 27 additions & 1 deletion tests/test_db.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import json
from pathlib import Path

from sqlalchemy.sql import insert
from sqlalchemy.sql import insert, select

import example_package.dataclasses.orm as d
from example_package.dataclasses import person


Expand All @@ -15,3 +16,28 @@ def test_db(get_engine):
stmt = insert(person).values(jsons)
with engine.begin() as conn:
_ = conn.execute(stmt)


def test_orm_link(test_session2):
p1 = d.person(firstname="1", lastname="1", others={}, role="developer")
s1 = d.skill(name="python", persons=[p1])
p2 = d.person(
firstname="2",
lastname="2",
others={},
role="developer",
parent_friendships=[p1],
)
test_session2.add_all([p1, s1, p2])
test_session2.commit()

p1_ = test_session2.execute(
select(d.person).where(d.person.id == p1.id)
).scalar()

assert s1.id == p1_.skills[0].id
assert p1_.parent_friendships == []
assert p1_.child_friendships[0].id == p2.id
assert p1.child_friendships[0].id == p2.id
assert p2.child_friendships == []
assert p2.parent_friendships == [p1]

0 comments on commit 47d3f01

Please sign in to comment.