Skip to content

Commit 49e8757

Browse files
committed
start adding tests for consistency on version_number
1 parent d0d72ae commit 49e8757

9 files changed

Lines changed: 81 additions & 19 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,4 +22,5 @@ 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()

‎tests/__init__.py‎

Whitespace-only changes.

‎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/e2e/__init__.py‎

Whitespace-only changes.

‎tests/e2e/test_api.py‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,7 @@
33
import requests
44

55
from allocation import config
6-
7-
def random_suffix():
8-
return uuid.uuid4().hex[:6]
9-
10-
def random_sku(name=''):
11-
return f'sku-{name}-{random_suffix()}'
12-
13-
def random_batchref(name=''):
14-
return f'batch-{name}-{random_suffix()}'
15-
16-
def random_orderid(name=''):
17-
return f'order-{name}-{random_suffix()}'
6+
from ..random_refs import random_sku, random_batchref, random_orderid
187

198

209
def post_to_add_batch(ref, sku, qty, eta):

‎tests/integration/__init__.py‎

Whitespace-only changes.

‎tests/integration/test_uow.py‎

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
# pylint: disable=broad-except
2+
import threading
3+
import time
4+
import traceback
5+
from typing import List
16
import pytest
27
from allocation import model
38
from allocation import unit_of_work
9+
from ..random_refs import random_sku, random_batchref, random_orderid
410

5-
def insert_batch(session, ref, sku, qty, eta):
11+
def insert_batch(session, ref, sku, qty, eta, product_version=1):
612
session.execute(
7-
'INSERT INTO products (sku) VALUES (:sku)',
8-
dict(sku=sku),
13+
'INSERT INTO products (sku, version_number) VALUES (:sku, :version)',
14+
dict(sku=sku, version=product_version),
915
)
1016
session.execute(
1117
'INSERT INTO batches (reference, sku, _purchased_quantity, eta)'
@@ -65,3 +71,53 @@ class MyException(Exception):
6571
new_session = session_factory()
6672
rows = list(new_session.execute('SELECT * FROM "batches"'))
6773
assert rows == []
74+
75+
76+
def try_to_allocate(orderid, sku, exceptions):
77+
line = model.OrderLine(orderid, sku, 10)
78+
try:
79+
with unit_of_work.SqlAlchemyUnitOfWork() as uow:
80+
product = uow.products.get(sku=sku)
81+
product.allocate(line)
82+
time.sleep(0.2)
83+
uow.commit()
84+
except Exception as e:
85+
print(traceback.format_exc())
86+
exceptions.append(e)
87+
88+
89+
def test_concurrent_updates_to_version_are_not_allowed(postgres_session_factory):
90+
sku, batch = random_sku(), random_batchref()
91+
session = postgres_session_factory()
92+
insert_batch(session, batch, sku, 100, eta=None, product_version=1)
93+
session.commit()
94+
95+
order1, order2 = random_orderid(1), random_orderid(2)
96+
exceptions = [] # type: List[Exception]
97+
try_to_allocate_order1 = lambda: try_to_allocate(order1, sku, exceptions)
98+
try_to_allocate_order2 = lambda: try_to_allocate(order2, sku, exceptions)
99+
thread1 = threading.Thread(target=try_to_allocate_order1)
100+
thread2 = threading.Thread(target=try_to_allocate_order2)
101+
thread1.start()
102+
thread2.start()
103+
thread1.join()
104+
thread2.join()
105+
106+
[[version]] = session.execute(
107+
"SELECT version_number FROM products WHERE sku=:sku",
108+
dict(sku=sku),
109+
)
110+
assert version == 2
111+
exception = [exceptions]
112+
assert 'could not serialize access due to concurrent update' in str(exception)
113+
114+
orders = list(session.execute(
115+
"SELECT orderid FROM allocations"
116+
" JOIN batches ON allocations.batch_id = batches.id"
117+
" JOIN order_lines ON allocations.orderline_id = order_lines.id"
118+
" WHERE order_lines.sku=:sku",
119+
dict(sku=sku),
120+
))
121+
assert len(orders) == 1
122+
with unit_of_work.SqlAlchemyUnitOfWork() as uow:
123+
uow.session.execute('select 1')

‎tests/random_refs.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import uuid
2+
3+
def random_suffix():
4+
return uuid.uuid4().hex[:6]
5+
6+
def random_sku(name=''):
7+
return f'sku-{name}-{random_suffix()}'
8+
9+
def random_batchref(name=''):
10+
return f'batch-{name}-{random_suffix()}'
11+
12+
def random_orderid(name=''):
13+
return f'order-{name}-{random_suffix()}'

0 commit comments

Comments
 (0)