Skip to content

Commit 81ce752

Browse files
committed
move exceptions out into their own module
1 parent 9e0cbdd commit 81ce752

File tree

6 files changed

+17
-22
lines changed

6 files changed

+17
-22
lines changed

src/allocation/exceptions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class OutOfStock(Exception):
2+
pass
3+
4+
class InvalidSku(Exception):
5+
pass
6+

src/allocation/flask_app.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
from datetime import datetime
22
from flask import Flask, jsonify, request
3-
4-
from allocation import unit_of_work
5-
from allocation import model
6-
from allocation import services
7-
from allocation import orm
3+
from allocation import exceptions, orm, services, unit_of_work
84

95
app = Flask(__name__)
106
orm.start_mappers()
@@ -31,7 +27,7 @@ def allocate_endpoint():
3127
request.json['qty'],
3228
unit_of_work.SqlAlchemyUnitOfWork(),
3329
)
34-
except (model.OutOfStock, services.InvalidSku) as e:
30+
except (exceptions.OutOfStock, exceptions.InvalidSku) as e:
3531
return jsonify({'message': str(e)}), 400
3632

3733
return jsonify({'batchref': batchref}), 201

src/allocation/model.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
from dataclasses import dataclass
33
from datetime import date
44
from typing import Optional, List, Set
5-
6-
7-
class OutOfStock(Exception):
8-
pass
5+
from allocation import exceptions
96

107

118
class Product:
@@ -24,7 +21,7 @@ def allocate(self, line: OrderLine) -> str:
2421
self.version_number += 1
2522
return batch.reference
2623
except StopIteration:
27-
raise OutOfStock(f'Out of stock for sku {line.sku}')
24+
raise exceptions.OutOfStock(f'Out of stock for sku {line.sku}')
2825

2926

3027
@dataclass(unsafe_hash=True)

src/allocation/services.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@
22
from typing import Optional
33
from datetime import date
44

5+
from allocation import exceptions, model, unit_of_work
56
from allocation.model import OrderLine
6-
from allocation import model, unit_of_work
7-
8-
9-
10-
class InvalidSku(Exception):
11-
pass
127

138

149
def add_batch(
@@ -32,7 +27,7 @@ def allocate(
3227
with uow:
3328
product = uow.products.get(sku=line.sku)
3429
if product is None:
35-
raise InvalidSku(f'Invalid sku {line.sku}')
30+
raise exceptions.InvalidSku(f'Invalid sku {line.sku}')
3631
batchref = product.allocate(line)
3732
uow.commit()
3833
return batchref

tests/unit/test_product.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from datetime import date, timedelta
22
import pytest
3-
from allocation.model import Product, OrderLine, Batch, OutOfStock
3+
from allocation import exceptions
4+
from allocation.model import Product, OrderLine, Batch
45

56
today = date.today()
67
tomorrow = today + timedelta(days=1)
@@ -46,7 +47,7 @@ def test_raises_out_of_stock_exception_if_cannot_allocate():
4647
different_sku_line = OrderLine('oref', 'SMALL-FORK', 10)
4748
product = Product(sku="HIGHBROW-POSTER", batches=[batch])
4849

49-
with pytest.raises(OutOfStock, match='SMALL-FORK'):
50+
with pytest.raises(exceptions.OutOfStock, match='SMALL-FORK'):
5051
product.allocate(different_sku_line)
5152

5253

tests/unit/test_services.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pytest
2-
from allocation import repository, services, unit_of_work
2+
from allocation import services, exceptions, repository, unit_of_work
33

44

55
class FakeRepository(repository.AbstractRepository):
@@ -53,7 +53,7 @@ def test_allocate_errors_for_invalid_sku():
5353
uow = FakeUnitOfWork()
5454
services.add_batch("b1", "AREALSKU", 100, None, uow)
5555

56-
with pytest.raises(services.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
56+
with pytest.raises(exceptions.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
5757
services.allocate("o1", "NONEXISTENTSKU", 10, uow)
5858

5959

0 commit comments

Comments
 (0)