Skip to content

Commit d251899

Browse files
committed
start adding tests for consistency on version_number
1 parent cf07ccf commit d251899

4 files changed

Lines changed: 60 additions & 7 deletions

File tree

‎src/allocation/orm.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
products = Table(
2121
'products', metadata,
2222
Column('sku', String(255), primary_key=True),
23-
# Column('version_number', Integer, nullable=False, default=0),
23+
Column('version_number', Integer, nullable=False, server_default='0'),
2424
)
2525

2626
batches = Table(

‎src/allocation/repository.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ def add(self, product):
2222
self.session.add(product)
2323

2424
def get(self, sku):
25+
print(sku, type(sku))
2526
return self.session.query(model.Product).filter_by(sku=sku).first()
2627

‎tests/conftest.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,16 @@ def postgres_db():
5959
metadata.create_all(engine)
6060
return engine
6161

62-
6362
@pytest.fixture
64-
def postgres_session(postgres_db):
63+
def postgres_session_factory(postgres_db):
6564
start_mappers()
66-
yield sessionmaker(bind=postgres_db)()
65+
yield sessionmaker(bind=postgres_db)
6766
clear_mappers()
6867

68+
@pytest.fixture
69+
def postgres_session(postgres_session_factory):
70+
return postgres_session_factory()
71+
6972

7073
@pytest.fixture
7174
def restart_api():

‎tests/integration/test_uow.py‎

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
import uuid
12
import pytest
23
from allocation import model
34
from allocation import unit_of_work
45

5-
def insert_batch(session, ref, sku, qty, eta):
6+
def random_ref(prefix):
7+
return prefix + '-' + uuid.uuid4().hex[:10]
8+
9+
def insert_batch(session, ref, sku, qty, eta, product_version=1):
610
session.execute(
7-
'INSERT INTO products (sku) VALUES (:sku)',
8-
dict(sku=sku),
11+
'INSERT INTO products (sku, version_number) VALUES (:sku, :version)',
12+
dict(sku=sku, version=product_version),
913
)
1014
session.execute(
1115
'INSERT INTO batches (reference, sku, _purchased_quantity, eta)'
@@ -65,3 +69,48 @@ class MyException(Exception):
6569
new_session = session_factory()
6670
rows = list(new_session.execute('SELECT * FROM "batches"'))
6771
assert rows == []
72+
73+
74+
import threading
75+
import traceback
76+
import time
77+
78+
def try_to_allocate(sku, session_factory, exceptions):
79+
line = model.OrderLine(random_ref('o'), sku, 10)
80+
try:
81+
with unit_of_work.start(session_factory) as uow:
82+
product = uow.products.get(sku=sku)
83+
product.allocate(line)
84+
time.sleep(0.2)
85+
uow.commit()
86+
except Exception as e:
87+
print(traceback.format_exc())
88+
exceptions.append(e)
89+
90+
91+
def test_concurrent_sessions_cannot_both_increment_product_version(postgres_session_factory):
92+
sku, batch = random_ref('s'), random_ref('b')
93+
session = postgres_session_factory()
94+
insert_batch(session, batch, sku, 100, eta=None, product_version=3)
95+
session.commit()
96+
97+
exceptions = []
98+
target = lambda: try_to_allocate(sku, postgres_session_factory, exceptions)
99+
t1 = threading.Thread(target=target)
100+
t2 = threading.Thread(target=target)
101+
t1.start()
102+
t2.start()
103+
t1.join()
104+
t2.join()
105+
106+
[[version]] = session.execute(
107+
"SELECT version_number FROM products WHERE sku=:sku",
108+
dict(sku=sku),
109+
)
110+
assert exceptions == []
111+
assert version == 4
112+
assert list(session.execute(
113+
"SELECT * FROM allocations JOIN batches ON allocations.batch_id = batches.id WHERE sku=:sku",
114+
dict(sku=sku),
115+
)) == []
116+

0 commit comments

Comments
 (0)