Skip to content

Commit 074a177

Browse files
committed
wip on db stuff
1 parent 58eb819 commit 074a177

4 files changed

Lines changed: 69 additions & 78 deletions

File tree

adapters/orm.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,3 @@
3232
Column("orderline_id", ForeignKey("order_lines.id")),
3333
Column("batch_id", ForeignKey("batches.id")),
3434
)
35-
36-
37-
def start_mappers():
38-
lines_mapper = mapper(model.OrderLine, order_lines)
39-
mapper(
40-
model.Batch,
41-
batches,
42-
properties={
43-
"_allocations": relationship(
44-
lines_mapper, secondary=allocations, collection_class=set,
45-
)
46-
},
47-
)

mypy.ini

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
[mypy]
22
ignore_missing_imports = False
3-
4-
[mypy-pytest.*]
5-
ignore_missing_imports = True
6-
7-
[mypy-sqlalchemy.*]
8-
ignore_missing_imports = True
9-
3+
plugins = sqlalchemy.ext.mypy.plugin

tests/conftest.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,31 @@
55
import pytest
66
import requests
77
from requests.exceptions import ConnectionError
8+
from typing import Callable
89
from sqlalchemy.exc import OperationalError
9-
from sqlalchemy import create_engine
10+
from sqlalchemy.ext.asyncio import (
11+
create_async_engine,
12+
AsyncEngine,
13+
AsyncSession,
14+
)
15+
1016
from sqlalchemy.orm import sessionmaker, clear_mappers
1117

12-
from adapters.orm import metadata, start_mappers
18+
from adapters.orm import metadata
1319
import config
1420

1521

1622
@pytest.fixture
17-
def in_memory_db():
18-
engine = create_engine("sqlite:///:memory:")
19-
metadata.create_all(engine)
23+
async def in_memory_db() -> AsyncEngine:
24+
engine = create_async_engine("sqlite:///:memory:")
25+
async with engine.begin() as conn:
26+
await conn.run_sync(metadata.create_all)
2027
return engine
2128

2229

2330
@pytest.fixture
24-
def session(in_memory_db):
25-
start_mappers()
26-
yield sessionmaker(bind=in_memory_db)()
27-
clear_mappers()
31+
def session_maker(in_memory_db: AsyncEngine):
32+
yield sessionmaker(bind=in_memory_db, class_=AsyncSession)
2833

2934

3035
def wait_for_postgres_to_come_up(engine):
Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,72 @@
11
# pylint: disable=protected-access
2+
import pytest
23
from domain import model
34
from adapters import repository
45

56

6-
def test_repository_can_save_a_batch(session):
7+
@pytest.mark.asyncio
8+
async def test_repository_can_save_a_batch(session_maker):
79
batch = model.Batch("batch1", "RUSTY-SOAPDISH", 100, eta=None)
810

9-
repo = repository.SqlAlchemyRepository(session)
10-
repo.add(batch)
11-
session.commit()
11+
async with session_maker() as session:
12+
repo = repository.SqlAlchemyRepository(session)
13+
await repo.add(batch)
14+
await repo.commit()
1215

13-
rows = session.execute(
14-
'SELECT reference, sku, _purchased_quantity, eta FROM "batches"'
15-
)
16+
async with session_maker() as session:
17+
async with session.begin():
18+
rows = session.execute(
19+
'SELECT reference, sku, _purchased_quantity, eta FROM "batches"'
20+
)
1621
assert list(rows) == [("batch1", "RUSTY-SOAPDISH", 100, None)]
1722

23+
if False:
24+
def insert_order_line(session):
25+
session.execute(
26+
"INSERT INTO order_lines (orderid, sku, qty)"
27+
' VALUES ("order1", "GENERIC-SOFA", 12)'
28+
)
29+
[[orderline_id]] = session.execute(
30+
"SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku",
31+
dict(orderid="order1", sku="GENERIC-SOFA"),
32+
)
33+
return orderline_id
1834

19-
def insert_order_line(session):
20-
session.execute(
21-
"INSERT INTO order_lines (orderid, sku, qty)"
22-
' VALUES ("order1", "GENERIC-SOFA", 12)'
23-
)
24-
[[orderline_id]] = session.execute(
25-
"SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku",
26-
dict(orderid="order1", sku="GENERIC-SOFA"),
27-
)
28-
return orderline_id
2935

36+
def insert_batch(session, batch_id):
37+
session.execute(
38+
"INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
39+
' VALUES (:batch_id, "GENERIC-SOFA", 100, null)',
40+
dict(batch_id=batch_id),
41+
)
42+
[[batch_id]] = session.execute(
43+
'SELECT id FROM batches WHERE reference=:batch_id AND sku="GENERIC-SOFA"',
44+
dict(batch_id=batch_id),
45+
)
46+
return batch_id
3047

31-
def insert_batch(session, batch_id):
32-
session.execute(
33-
"INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
34-
' VALUES (:batch_id, "GENERIC-SOFA", 100, null)',
35-
dict(batch_id=batch_id),
36-
)
37-
[[batch_id]] = session.execute(
38-
'SELECT id FROM batches WHERE reference=:batch_id AND sku="GENERIC-SOFA"',
39-
dict(batch_id=batch_id),
40-
)
41-
return batch_id
4248

49+
def insert_allocation(session, orderline_id, batch_id):
50+
session.execute(
51+
"INSERT INTO allocations (orderline_id, batch_id)"
52+
" VALUES (:orderline_id, :batch_id)",
53+
dict(orderline_id=orderline_id, batch_id=batch_id),
54+
)
4355

44-
def insert_allocation(session, orderline_id, batch_id):
45-
session.execute(
46-
"INSERT INTO allocations (orderline_id, batch_id)"
47-
" VALUES (:orderline_id, :batch_id)",
48-
dict(orderline_id=orderline_id, batch_id=batch_id),
49-
)
5056

57+
def test_repository_can_retrieve_a_batch_with_allocations(session):
58+
orderline_id = insert_order_line(session)
59+
batch1_id = insert_batch(session, "batch1")
60+
insert_batch(session, "batch2")
61+
insert_allocation(session, orderline_id, batch1_id)
5162

52-
def test_repository_can_retrieve_a_batch_with_allocations(session):
53-
orderline_id = insert_order_line(session)
54-
batch1_id = insert_batch(session, "batch1")
55-
insert_batch(session, "batch2")
56-
insert_allocation(session, orderline_id, batch1_id)
63+
repo = repository.SqlAlchemyRepository(session)
64+
retrieved = repo.get("batch1")
5765

58-
repo = repository.SqlAlchemyRepository(session)
59-
retrieved = repo.get("batch1")
60-
61-
expected = model.Batch("batch1", "GENERIC-SOFA", 100, eta=None)
62-
assert retrieved == expected # Batch.__eq__ only compares reference
63-
assert retrieved.sku == expected.sku
64-
assert retrieved._purchased_quantity == expected._purchased_quantity
65-
assert retrieved._allocations == {
66-
model.OrderLine("order1", "GENERIC-SOFA", 12),
67-
}
66+
expected = model.Batch("batch1", "GENERIC-SOFA", 100, eta=None)
67+
assert retrieved == expected # Batch.__eq__ only compares reference
68+
assert retrieved.sku == expected.sku
69+
assert retrieved._purchased_quantity == expected._purchased_quantity
70+
assert retrieved._allocations == {
71+
model.OrderLine("order1", "GENERIC-SOFA", 12),
72+
}

0 commit comments

Comments
 (0)