diff --git a/py-kivy/pos_system/database.py b/py-kivy/pos_system/database.py index 4f04378..68a936c 100644 --- a/py-kivy/pos_system/database.py +++ b/py-kivy/pos_system/database.py @@ -1,13 +1,7 @@ from pymongo import MongoClient -client = None -db = None - - -def init_db(): - global client, db - client = MongoClient("mongodb://localhost:27017") - db = client["pos_system"] +client = MongoClient("mongodb://localhost:27017") +db = client["pos_system"] def get_db(): diff --git a/py-kivy/pos_system/routers/items.py b/py-kivy/pos_system/routers/items.py index a27a065..ad17a03 100644 --- a/py-kivy/pos_system/routers/items.py +++ b/py-kivy/pos_system/routers/items.py @@ -1,7 +1,8 @@ from fastapi import APIRouter, HTTPException, Depends +from bson import ObjectId from ..models import Item from ..database import get_db -from bson import ObjectId +from typing import List router = APIRouter( prefix="/items", @@ -9,17 +10,47 @@ router = APIRouter( ) -@router.post("/") +@router.post("/", response_model=Item) async def create_item(item: Item): db = get_db() - result = db.items.insert_one(item.dict(by_alias=True)) - return {"id": str(result.inserted_id)} + item_dict = item.model_dump(exclude={"id"}) + result = db.items.insert_one(item_dict) + created_item = db.items.find_one({"_id": result.inserted_id}) + return Item(**created_item) -@router.get("/") +@router.get("/", response_model=List[Item]) async def read_items(): db = get_db() items = list(db.items.find()) - return [Item(**item).dict(by_alias=True) for item in items] + return [Item(**item) for item in items] -# Add other item-related routes here + +@router.get("/{item_id}", response_model=Item) +async def read_item(item_id: str): + db = get_db() + item = db.items.find_one({"_id": ObjectId(item_id)}) + if item is None: + raise HTTPException(status_code=404, detail="Item not found") + return Item(**item) + + +@router.put("/{item_id}", response_model=Item) +async def update_item(item_id: str, item: Item): + db = get_db() + update_data = item.model_dump(exclude={"id"}) + result = db.items.update_one( + {"_id": ObjectId(item_id)}, {"$set": update_data}) + if result.modified_count == 0: + raise HTTPException(status_code=404, detail="Item not found") + updated_item = db.items.find_one({"_id": ObjectId(item_id)}) + return Item(**updated_item) + + +@router.delete("/{item_id}", response_model=dict) +async def delete_item(item_id: str): + db = get_db() + result = db.items.delete_one({"_id": ObjectId(item_id)}) + if result.deleted_count == 0: + raise HTTPException(status_code=404, detail="Item not found") + return {"message": "Item deleted successfully"} diff --git a/py-kivy/pos_system/server.py b/py-kivy/pos_system/server.py index 10df2ab..eaab707 100644 --- a/py-kivy/pos_system/server.py +++ b/py-kivy/pos_system/server.py @@ -1,10 +1,9 @@ from fastapi import FastAPI from .routers import items, orders, users -from .database import init_db app = FastAPI() # Initialize database -init_db() +# init_db() # Include routers app.include_router(items.router) diff --git a/py-kivy/tests/test_fastapi.py b/py-kivy/tests/test_fastapi.py index ee6c25d..b374a42 100644 --- a/py-kivy/tests/test_fastapi.py +++ b/py-kivy/tests/test_fastapi.py @@ -3,21 +3,18 @@ from fastapi.testclient import TestClient from hypothesis import given, strategies as st from bson import ObjectId from pos_system.server import app -from pos_system.database import db +from pos_system.database import get_db client = TestClient(app) @pytest.fixture(autouse=True) def clear_db(): + db = get_db() db.items.delete_many({}) yield db.items.delete_many({}) -# --======-----------==== -# Unit tests -# --======-----------==== - def test_create_item(): response = client.post( @@ -29,63 +26,45 @@ def test_create_item(): def test_read_items(): - # Create a test item client.post("/items/", json={"name": "Test Item", "price": 10.99, "quantity": 5, "unit": "piece"}) - response = client.get("/items/") assert response.status_code == 200 items = response.json() - assert len(items) > 0 + assert len(items) == 1 assert items[0]["name"] == "Test Item" def test_read_item(): - # Create a test item create_response = client.post( "/items/", json={"name": "Test Item", "price": 10.99, "quantity": 5, "unit": "piece"}) item_id = create_response.json()["id"] - response = client.get(f"/items/{item_id}") assert response.status_code == 200 assert response.json()["name"] == "Test Item" def test_update_item(): - # Create a test item create_response = client.post( "/items/", json={"name": "Test Item", "price": 10.99, "quantity": 5, "unit": "piece"}) item_id = create_response.json()["id"] - update_data = {"name": "Updated Item", "price": 15.99, "quantity": 10, "unit": "piece"} response = client.put(f"/items/{item_id}", json=update_data) assert response.status_code == 200 - assert response.json()["message"] == "Item updated successfully" - - # Verify the update - get_response = client.get(f"/items/{item_id}") - assert get_response.json()["name"] == "Updated Item" + assert response.json()["name"] == "Updated Item" def test_delete_item(): - # Create a test item create_response = client.post( "/items/", json={"name": "Test Item", "price": 10.99, "quantity": 5, "unit": "piece"}) item_id = create_response.json()["id"] - response = client.delete(f"/items/{item_id}") assert response.status_code == 200 assert response.json()["message"] == "Item deleted successfully" - - # Verify the deletion get_response = client.get(f"/items/{item_id}") assert get_response.status_code == 404 -# --======-----------==== -# Property-based tests -# --======-----------==== - @given( name=st.text(min_size=1, max_size=50), @@ -101,8 +80,6 @@ def test_create_item_property(name, price, quantity, unit): ) assert response.status_code == 200 assert "id" in response.json() - - # Verify the created item item_id = response.json()["id"] get_response = client.get(f"/items/{item_id}") assert get_response.status_code == 200 @@ -126,15 +103,12 @@ def test_create_item_property(name, price, quantity, unit): ) ) def test_read_items_property(items): - # Create multiple items for item in items: client.post("/items/", json=item) - response = client.get("/items/") assert response.status_code == 200 retrieved_items = response.json() assert len(retrieved_items) == len(items) - for retrieved_item in retrieved_items: assert "name" in retrieved_item assert "price" in retrieved_item