@@ -358,20 +358,17 @@ Let's see how that looks in code form:
358
358
[role="non-head"]
359
359
----
360
360
class Product:
361
-
362
361
def __init__(self, sku: str, batches: List[Batch]):
363
362
self.sku = sku #<1>
364
363
self.batches = batches #<2>
365
364
366
365
def allocate(self, line: OrderLine) -> str: #<3>
367
366
try:
368
- batch = next(
369
- b for b in sorted(self.batches) if b.can_allocate(line)
370
- )
367
+ batch = next(b for b in sorted(self.batches) if b.can_allocate(line))
371
368
batch.allocate(line)
372
369
return batch.reference
373
370
except StopIteration:
374
- raise OutOfStock(f' Out of stock for sku {line.sku}' )
371
+ raise OutOfStock(f" Out of stock for sku {line.sku}" )
375
372
----
376
373
====
377
374
@@ -499,8 +496,8 @@ layer to see how it looks with `Product` as its main entrypoint:
499
496
[source,python]
500
497
----
501
498
def add_batch(
502
- ref: str, sku: str, qty: int, eta: Optional[date],
503
- uow: unit_of_work.AbstractUnitOfWork
499
+ ref: str, sku: str, qty: int, eta: Optional[date],
500
+ uow: unit_of_work.AbstractUnitOfWork,
504
501
):
505
502
with uow:
506
503
product = uow.products.get(sku=sku)
@@ -512,14 +509,14 @@ def add_batch(
512
509
513
510
514
511
def allocate(
515
- orderid: str, sku: str, qty: int,
516
- uow: unit_of_work.AbstractUnitOfWork
512
+ orderid: str, sku: str, qty: int,
513
+ uow: unit_of_work.AbstractUnitOfWork,
517
514
) -> str:
518
515
line = OrderLine(orderid, sku, qty)
519
516
with uow:
520
517
product = uow.products.get(sku=line.sku)
521
518
if product is None:
522
- raise InvalidSku(f' Invalid sku {line.sku}' )
519
+ raise InvalidSku(f" Invalid sku {line.sku}" )
523
520
batchref = product.allocate(line)
524
521
uow.commit()
525
522
return batchref
@@ -743,22 +740,19 @@ you might decide the cleanest trade-off is to put them in the domain:
743
740
[source,python]
744
741
----
745
742
class Product:
746
-
747
743
def __init__(self, sku: str, batches: List[Batch], version_number: int = 0): #<1>
748
744
self.sku = sku
749
745
self.batches = batches
750
746
self.version_number = version_number #<1>
751
747
752
748
def allocate(self, line: OrderLine) -> str:
753
749
try:
754
- batch = next(
755
- b for b in sorted(self.batches) if b.can_allocate(line)
756
- )
750
+ batch = next(b for b in sorted(self.batches) if b.can_allocate(line))
757
751
batch.allocate(line)
758
752
self.version_number += 1 #<1>
759
753
return batch.reference
760
754
except StopIteration:
761
- raise OutOfStock(f' Out of stock for sku {line.sku}' )
755
+ raise OutOfStock(f" Out of stock for sku {line.sku}" )
762
756
----
763
757
====
764
758
@@ -847,18 +841,18 @@ def test_concurrent_updates_to_version_are_not_allowed(postgres_session_factory)
847
841
)
848
842
assert version == 2 #<2>
849
843
[exception] = exceptions
850
- assert ' could not serialize access due to concurrent update' in str(exception) #<3>
844
+ assert " could not serialize access due to concurrent update" in str(exception) #<3>
851
845
852
- orders = list( session.execute(
846
+ orders = session.execute(
853
847
"SELECT orderid FROM allocations"
854
848
" JOIN batches ON allocations.batch_id = batches.id"
855
849
" JOIN order_lines ON allocations.orderline_id = order_lines.id"
856
850
" WHERE order_lines.sku=:sku",
857
851
dict(sku=sku),
858
- ))
859
- assert len( orders) == 1 #<4>
852
+ )
853
+ assert orders.rowcount == 1 #<4>
860
854
with unit_of_work.SqlAlchemyUnitOfWork() as uow:
861
- uow.session.execute(' select 1' )
855
+ uow.session.execute(" select 1" )
862
856
----
863
857
====
864
858
@@ -885,10 +879,12 @@ on our session:
885
879
====
886
880
[source,python]
887
881
----
888
- DEFAULT_SESSION_FACTORY = sessionmaker(bind=create_engine(
889
- config.get_postgres_uri(),
890
- isolation_level="REPEATABLE READ",
891
- ))
882
+ DEFAULT_SESSION_FACTORY = sessionmaker(
883
+ bind=create_engine(
884
+ config.get_postgres_uri(),
885
+ isolation_level="REPEATABLE READ",
886
+ )
887
+ )
892
888
----
893
889
====
894
890
@@ -927,10 +923,12 @@ query time:
927
923
[role="non-head"]
928
924
----
929
925
def get(self, sku):
930
- return self.session.query(model.Product) \
931
- .filter_by(sku=sku) \
932
- .with_for_update() \
933
- .first()
926
+ return (
927
+ self.session.query(model.Product)
928
+ .filter_by(sku=sku)
929
+ .with_for_update()
930
+ .first()
931
+ )
934
932
----
935
933
====
936
934
0 commit comments