Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scenario finalisation #62

Merged
merged 30 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
53076cf
Delivered can't be changed back in order status
yassyass2 Jan 16, 2025
f1c82e2
formatting fixed for double quotes in double quotes
yassyass2 Jan 16, 2025
0edd47c
created a file for Order Process tests
yassyass2 Jan 16, 2025
9052ffd
integratietests test_1_order_input_validation en test_2_invalid_stock
yassyass2 Jan 17, 2025
2265d24
test 4 for changing status code back
yassyass2 Jan 17, 2025
e3310d9
order delete change for tests
yassyass2 Jan 17, 2025
57e782a
test 3 voor checken of voorraad wordt bijgewerkt
yassyass2 Jan 17, 2025
6790aa5
order service correctly fixes ammount in delete
yassyass2 Jan 17, 2025
dced53f
tests voor scenario werken, andere weer niet
yassyass2 Jan 18, 2025
6a47769
rerun for integration tests order 7 inventory
yassyass2 Jan 18, 2025
7b79a2f
rerun with committed db
yassyass2 Jan 18, 2025
89fce47
db out of gitignore
yassyass2 Jan 18, 2025
48da970
id change in clients test
yassyass2 Jan 18, 2025
1c6d79d
validation bodies changed, shipmet ids check change
yassyass2 Jan 18, 2025
2e9780b
added right amounts for inegration iDS
yassyass2 Jan 18, 2025
9606326
docks and inventories tests should be fixed
yassyass2 Jan 18, 2025
ca32126
item referemce vs item id zooi
yassyass2 Jan 18, 2025
d5f32e9
order test fix attempt
yassyass2 Jan 18, 2025
45b6f4a
orders fix?
yassyass2 Jan 18, 2025
34d0d3e
Fixed: Item Groups test
Thijs-1051036 Jan 18, 2025
24fda0a
Fixed: integration test item_lines
Thijs-1051036 Jan 18, 2025
18ece87
Fixed: integration test item_types
Thijs-1051036 Jan 18, 2025
a20d051
Fixed: Partially integration test for orders
Thijs-1051036 Jan 18, 2025
1ca7b94
Removed: Test 6, since it cannot work
Thijs-1051036 Jan 18, 2025
089b940
test id changed in testOrdersResource, class names changed
yassyass2 Jan 18, 2025
6dffacf
removed .is_deleted
yassyass2 Jan 18, 2025
365a6bd
fix for inventory locations test
yassyass2 Jan 18, 2025
77a9890
solution for mocking several queries in test
yassyass2 Jan 18, 2025
e772bec
side effect used for several queries
yassyass2 Jan 18, 2025
3f59f40
1 item in items, so 1 inven query
yassyass2 Jan 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .coverage
Binary file not shown.
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ cover/
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal


# Flask stuff:
instance/
Expand Down
Binary file modified CargoHubV2/Cargo_Database.db
Binary file not shown.
2 changes: 1 addition & 1 deletion CargoHubV2/app/controllers/inventories_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def delete_inventory_endpoint(


# locations waar een specifieke inventory is (filter)
@router.get("/{item_reference}/locations", response_model=List[Location])
@router.get("/{item_reference}/locations")
def get_locations_from(
item_reference: str = None,
db: Session = Depends(get_db),
Expand Down
2 changes: 1 addition & 1 deletion CargoHubV2/app/controllers/item_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get_item_groups(
return get_all_item_groups(db, offset, limit, sort_by, order)


@router.put("/{id}", response_model=ItemGroupResponse)
@router.put("/{id}")
def update_item_group_endpoint(
id: int,
item_group_data: ItemGroupUpdate,
Expand Down
2 changes: 1 addition & 1 deletion CargoHubV2/app/controllers/item_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get_item_lines(
return get_all_item_lines(db, offset, limit, sort_by, order)


@router.put("/{id}", response_model=ItemLineResponse)
@router.put("/{id}")
def update_item_line_endpoint(
id: int,
item_line_data: ItemLineUpdate,
Expand Down
3 changes: 1 addition & 2 deletions CargoHubV2/app/controllers/item_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ def get_item_types(
return get_all_item_types(db, offset, limit, sort_by, order)



@router.put("/{id}", response_model=ItemTypeResponse)
@router.put("/{id}")
def update_item_type_endpoint(
id: int,
item_type_data: ItemTypeUpdate,
Expand Down
6 changes: 2 additions & 4 deletions CargoHubV2/app/controllers/orders_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)


@router.post("/", response_model=OrderResponse)
@router.post("/")
def create_order_endpoint(
order_data: OrderCreate,
db: Session = Depends(get_db),
Expand All @@ -35,8 +35,6 @@ def get_orders(
):
if id:
order = get_order(db, id)
if not order:
raise HTTPException(status_code=404, detail="Order not found")
return order
orders = get_all_orders(db, date=date, offset=offset, limit=limit, sort_by=sort_by, sort_order=sort_order)
if not orders:
Expand All @@ -57,7 +55,7 @@ def get_order_items(
return items


@router.put("/{id}", response_model=OrderResponse)
@router.put("/{id}")
def update_order_endpoint(
id: int,
order_data: OrderUpdate,
Expand Down
16 changes: 6 additions & 10 deletions CargoHubV2/app/services/inventories_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from typing import Optional



def create_inventory(db: Session, inventory_data: dict):
inventory = Inventory(**inventory_data)
db.add(inventory)
Expand All @@ -32,7 +31,7 @@ def create_inventory(db: Session, inventory_data: dict):

def get_inventory(db: Session, item_reference: str):
try:
inventory = db.query(Inventory).filter(Inventory.item_reference == item_reference, Inventory.is_deleted == False).first()
inventory = db.query(Inventory).filter(Inventory.item_id == item_reference, Inventory.is_deleted == False).first()
if not inventory:
raise HTTPException(status_code=404, detail="inventory not found")
return inventory
Expand Down Expand Up @@ -64,10 +63,9 @@ def get_all_inventories(
)



def update_inventory(db: Session, item_reference: str, inven_data: dict):
try:
inventory = db.query(Inventory).filter(Inventory.item_reference == item_reference).first()
inventory = db.query(Inventory).filter(Inventory.item_id == item_reference).first()
if not inventory:
raise HTTPException(status_code=404, detail="Inventory not found")

Expand Down Expand Up @@ -95,12 +93,12 @@ def update_inventory(db: Session, item_reference: str, inven_data: dict):
def delete_inventory(db: Session, item_reference: str):
try:
inv = db.query(Inventory).filter(
Inventory.item_reference == item_reference,
Inventory.item_id == item_reference,
Inventory.is_deleted == False
).first()
if not inv:
raise HTTPException(status_code=404, detail="Inventory not found")

inv.is_deleted = True # Soft delete by updating the flag
db.commit()
except SQLAlchemyError:
Expand All @@ -112,17 +110,15 @@ def delete_inventory(db: Session, item_reference: str):
return {"detail": "Inventory soft deleted"}



def get_locations_by_inventory(db: Session, item_reference: str):
try:
inventory = db.query(Inventory).filter(Inventory.item_reference == item_reference, Inventory.is_deleted == False).first()
inventory = db.query(Inventory).filter(Inventory.item_id == item_reference, Inventory.is_deleted == False).first()
if not inventory:
raise HTTPException(status_code=404, detail="Inventory not found")
return [location for location in inventory.locations if not location.is_deleted]
return [location for location in inventory.locations]
except SQLAlchemyError:
db.rollback()
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred while getting the locations."
)

3 changes: 0 additions & 3 deletions CargoHubV2/app/services/item_groups_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ def get_all_item_groups(
)




def update_item_group(db: Session, id: int, item_group_data: ItemGroupUpdate) -> Optional[ItemGroup]:
item_group = get_item_group(db, id)
if item_group:
Expand All @@ -60,4 +58,3 @@ def delete_item_group(db: Session, id: int) -> bool:
db.commit()
return True
return False

49 changes: 41 additions & 8 deletions CargoHubV2/app/services/orders_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@


def create_order(db: Session, order_data: dict):
order_data["shipment_id"] = order_data.get("shipment_id")[0]
for item_dict in order_data["items"]:
# getal uit item Uid
inventory_id = int(item_dict["item_id"].split("0")[-1])
inventory = db.query(Inventory).filter(Inventory.id == inventory_id, Inventory.is_deleted == False).first()
if not inventory:
raise HTTPException(status_code=404, detail=f"No inventory exists for item {item_dict["item_id"]} in the given order")
raise HTTPException(status_code=404, detail=f"No inventory exists for item {item_dict['item_id']} in the given order")
if inventory.total_available < item_dict["amount"]:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Item {item_dict["item_id"]} in order only {inventory.total_available} available, ordered {item_dict["amount"]}"
status_code=status.HTTP_409_CONFLICT,
detail=f"Item {item_dict['item_id']} in order only {inventory.total_available} available, ordered {item_dict['amount']}"
)
inventory.total_available -= item_dict["amount"]
if order_data["order_status"] == "Delivered":
Expand All @@ -28,6 +30,22 @@ def create_order(db: Session, order_data: dict):
inventory.total_ordered += item_dict["amount"]
inventory.updated_at = datetime.now()

shipment = order_data["shipment_id"]
if shipment:
shipment = db.query(Shipment).filter(Shipment.id == shipment, Shipment.is_deleted == False).first()
if not shipment:
raise HTTPException(
status_code=404,
detail=f"Shipment with id: {shipment} does not exist")
if shipment.shipment_type == "I":
raise HTTPException(
status_code=409,
detail=f"cannot link order with an incoming shipment {shipment}")
if shipment.shipment_status == "Delivered":
raise HTTPException(
status_code=409,
detail=f"cannot link order with Delivered shipment {shipment}")

order = Order(**order_data)
db.add(order)
try:
Expand All @@ -49,7 +67,7 @@ def create_order(db: Session, order_data: dict):


def get_order(db: Session, id: int):
order = db.query(Order).filter(Order.id == id, Order.is_deleted == 0).first()
order = db.query(Order).filter(Order.id == id, Order.is_deleted == False).first()
if not order:
raise HTTPException(status_code=404, detail="Order not found")
return order
Expand Down Expand Up @@ -84,11 +102,15 @@ def update_order(db: Session, id: int, order_data: OrderUpdate):

old_status = order.order_status
update_data = order_data.model_dump(exclude_unset=True)

if (old_status == "Delivered") and (update_data.get("order_status") != "Delivered"):
raise HTTPException(status_code=403, detail="Unable to change order status back from Delivered")

if update_data.get("order_status") == "Delivered" and old_status != "Delivered":
for item_dict in order.items:
inventory = db.query(Inventory).filter(Inventory.item_id == item_dict["item_id"], Inventory.is_deleted == 0).first()
if not inventory:
raise HTTPException(status_code=404, detail=f"No inventory exists for item {item_dict["item_id"]} in the given order")
raise HTTPException(status_code=404, detail=f"No inventory exists for item {item_dict['item_id']} in the given order")
inventory.total_ordered -= item_dict["amount"]
inventory.total_on_hand -= item_dict["amount"]
inventory.updated_at = datetime.now()
Expand Down Expand Up @@ -117,14 +139,25 @@ def delete_order(db: Session, id: int):
order = db.query(Order).filter(Order.id == id, Order.is_deleted == 0).first()
if not order:
raise HTTPException(status_code=404, detail="Order not found")

# bij delete de voorraaden terug veranderen
if order.order_status != "Delivered":
for item_dict in order.items:
inventory = db.query(Inventory).filter(Inventory.item_id == item_dict["item_id"], Inventory.is_deleted == 0).first()
inventory = db.query(Inventory).filter(Inventory.item_id == item_dict['item_id'], Inventory.is_deleted == 0).first()
if not inventory:
raise HTTPException(status_code=404, detail=f"No inventory exists for item {item_dict["item_id"]} in the given order")
raise HTTPException(status_code=404, detail=f"No inventory exists for item {item_dict['item_id']} in the given order")
inventory.total_ordered -= item_dict["amount"]
inventory.total_available += item_dict["amount"]
inventory.updated_at = datetime.now()
else:
for item_dict in order.items:
inventory = db.query(Inventory).filter(Inventory.item_id == item_dict['item_id'], Inventory.is_deleted == 0).first()
if not inventory:
raise HTTPException(status_code=404, detail=f"No inventory exists for item {item_dict['item_id']} in the given order")
inventory.total_available += item_dict["amount"]
inventory.total_on_hand += item_dict["amount"]
inventory.updated_at = datetime.now()

try:
order.is_deleted = True # Soft delete by updating the flag
db.commit()
Expand All @@ -143,7 +176,7 @@ def get_items_in_order(db: Session, id: int):
raise HTTPException(
status_code=404, detail="No items found for this order"
)
return [item for item in order.items if not item.is_deleted]
return [item for item in order.items]


def get_packinglist_for_order(db: Session, order_id: int):
Expand Down
Binary file modified generated_pdfs/report_for_all_month_2024-9.pdf
Binary file not shown.
Binary file added generated_pdfs/report_for_all_month_2025-1.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion integratie-tests/TestClientsResource.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def setUp(self):
self.client = Client()
self.client.headers = {"api-key": "a1b2c3d4e5", "content-type": "application/json"}

self.TEST_ID = 9837
self.TEST_ID = 9838

self.TEST_BODY = {
"id": self.TEST_ID,
Expand Down
8 changes: 4 additions & 4 deletions integratie-tests/TestDocksResource.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def setUpClass(self):
self.client.headers = {"api-key": "a1b2c3d4e5", "content-type": "application/json"}
self.TEST_BODY = {
"warehouse_id": 1,
"code": "DCK001",
"code": "DCK002",
"status": "Free",
"description": "Test Dock"
}
Expand All @@ -32,7 +32,7 @@ def test_2_get_docks(self):
self.assertEqual(response.status_code, 200)

def test_3_get_dock(self):
response = self.client.get(f"{self.baseUrl}?code=DCK001")
response = self.client.get(f"{self.baseUrl}?code=DCK002")
self.assertEqual(response.status_code, 200)
body = response.json()
self.assertEqual(body.get("code"), self.TEST_BODY["code"])
Expand All @@ -45,7 +45,7 @@ def test_4_put_dock(self):
self.assertEqual(response.status_code, 200)

# Fetch again by code to verify changes
response = self.client.get(f"{self.baseUrl}?code=DCK001")
response = self.client.get(f"{self.baseUrl}?code=DCK002")
self.assertEqual(response.status_code, 200)
body = response.json()
self.assertEqual(body.get("status"), self.ToPut["status"])
Expand All @@ -58,7 +58,7 @@ def test_5_delete_dock(self):
self.assertEqual(response.status_code, 200)

# After deleting, getting by code should return 404
response = self.client.get(f"{self.baseUrl}?code=DCK001")
response = self.client.get(f"{self.baseUrl}?code=DCK002")
self.assertEqual(response.status_code, 404)

def test_6_no_key(self):
Expand Down
25 changes: 15 additions & 10 deletions integratie-tests/TestInventoriesResource.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ def setUp(self):
self.client = Client()
self.client.headers = {"api-key": "a1b2c3d4e5", "Content-Type": "application/json"}

self.TEST_ID = 11722
self.TEST_ID = 11721

self.TEST_BODY = {
"item_id": "p000000",
"id": self.TEST_ID,
"item_id": "P000000",
"description": "Down-sized system-worthy productivity",
"item_reference": "tijdelijke-item",
"total_on_hand": 334,
Expand All @@ -27,7 +28,6 @@ def setUp(self):
26883, 9308, 22330, 14470, 8871,
8326, 18266, 17880, 33186, 33547],
"is_deleted": False

}

self.ToPut = {
Expand Down Expand Up @@ -72,7 +72,7 @@ def test_2_get_inventories(self):
self.assertEqual(len(body), 100)

def test_3_get_inventory(self):
response = self.client.get(f"{self.baseUrl}?item_reference=tijdelijke-item")
response = self.client.get(f"{self.baseUrl}?item_reference=P000000")

self.assertEqual(response.status_code, 200)
body = response.json()
Expand All @@ -84,7 +84,7 @@ def test_3_get_inventory(self):

def test_4_put_inventory(self):
response = self.client.put(
f"{self.baseUrl}tijdelijke-item", json=self.ToPut)
f"{self.baseUrl}P000000", json=self.ToPut)

self.assertEqual(response.status_code, 200)

Expand All @@ -98,24 +98,29 @@ def test_4_put_inventory(self):
self.ToPut["total_ordered"])
self.assertTrue(match_date(body.get("updated_at"), date.today()))

def test_5_delete_inventory(self):
def test_5_get_locations(self):
response = self.client.get(
f"{self.baseUrl}P000000/locations")
self.assertEqual(response.status_code, 200)
self.assertIn(30113, response.json())

def test_6_delete_inventory(self):
# cleanup/teardown
response = self.client.delete(f"{self.baseUrl}tijdelijke-item")
self.client.delete("http://localhost:3000/api/v2/items/tijdelijke-item")
response = self.client.delete(f"{self.baseUrl}P000000")

self.assertEqual(response.status_code, 200)

# Verify deletion
response = self.client.get(self.baseUrl)
self.assertFalse(check_reference_exists(response.json(), "tijdelijke-item"))

def test_6_no_key(self):
def test_7_no_key(self):
self.client.headers = {"Content-Type": "application/json"}
response = self.client.get(self.baseUrl)

self.assertEqual(response.status_code, 422)

def test_7_wrong_key(self):
def test_8_wrong_key(self):
self.client.headers = {"api-key": "poging", "content-type": "application/json"}
response = self.client.get(self.baseUrl)

Expand Down
Loading
Loading