Skip to content

add a testing package #1

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

Merged
merged 2 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
1 change: 1 addition & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ idna==3.10
pydantic==2.10.6
pydantic_core==2.27.2
python-dotenv==1.0.1
pytest==8.0.0
sniffio==1.3.1
starlette==0.45.3
typing_extensions==4.12.2
Expand Down
Empty file added backend/tests/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
PyTest Configuration and Fixtures

Provides common test fixtures and configuration for all test modules.
Add your fixtures here as needed.
"""
import os
import pytest
from fastapi.testclient import TestClient
from main import app
from utils.db import init_db, get_db_connection

@pytest.fixture
def test_db():
"""Creates a temporary test database"""
test_db_path = "data/test.sqlite3"
# Initialize test database
init_db(test_db_path)
yield test_db_path
# Cleanup after tests
if os.path.exists(test_db_path):
os.remove(test_db_path)

@pytest.fixture
def client():
"""Creates a test client for the FastAPI application"""
return TestClient(app)

@pytest.fixture
def db(test_db):
"""Provides a database connection for tests"""
with get_db_connection(test_db) as conn:
yield conn
89 changes: 89 additions & 0 deletions backend/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""
API Integration Tests

Tests all API endpoints including:
- GET /inventory
- GET /snacks/{sku}
- POST /snacks
- PUT /snacks/{sku}
- DELETE /snacks/{sku}

TODO: Implement test cases for each endpoint
"""
from fastapi.testclient import TestClient
from main import app
from utils.db import get_db_connection
from dotenv import load_dotenv
import os
# TODO: Implement your API tests here
load_dotenv()
client=TestClient(app)


def test_create_snack(client):
"""Test creating a new snack"""
#response = client.post("/api/v1/inventory/snacks", json={"sku": "12345", "name": "Coke Zero", "quantity": 20})
#assert response.status_code == 200
#assert response.json()['sku'] == "12345"
# assert response.json()['name'] == "Coke Zero"
#response = client.post("/api/v1/inventory/snacks", json={"sku": "1629", "name": "Rice Krispy Treat", "quantity": 40})
#assert response.status_code ==200
# assert response.json()['sku'] == "1629"
# assert response.json['name'] =='Rice Krispy Treat'

# response = client.post("/api/v1/inventory/snacks", json={"sku": "1629", "name": "Rice Krispy Treat", "quantity": 40})
# assert response.status_code ==200
# assert response.json()['sku'] == "1629"
# assert response.json['name'] =='Rice Krispy Treat'

#create_snack(SnackCreateSchema(sku='4200',name='Gummy Bears', quantity=20))

#response=client.post("/api/v1/inventory/snakcs",json={"sku":"4200","name":"Gummy Bears","quantity":20})
#assert response.status_code==200
#assert response.json()['sku'] == '4200'
#assert response.json['name']=='Gummy Bears'





def test_get_inventory(client):
"""Test retrieving all snacks"""
response = client.get("/api/v1/inventory")
assert response.status_code == 200
inventory = response.json()
print(f" Line 35 for get inventory test : {response.text}")
assert len(inventory) > 0
# assert any(snack['sku'] == "12345" for snack in inventory)


def test_get_snack(client):
"""Test retrieving a specific snack by SKU"""
response = client.get(f"/api/v1/inventory/snacks/{'12345'}")
assert response.status_code == 200


snack = response.json()
assert snack['sku'] == "12345"
assert snack['name'] == "Coke Zero"

def test_update_snack(client):
"""Test updating an existing snack"""
response = client.put(f"/api/v1/inventory/snacks/{'1629'}", json={"name": "Rice Krispy Treat", "quantity": 60})
assert response.status_code == 200
updated_snack = response.json()


print(f" this is the test for line 55 for update_snack :{response.text}")
#assert updated_snack['sku'] == "1629"

#assert updated_snack['quantity'] == 60

assert updated_snack['sku']

assert updated_snack

def test_delete_snack(client):
#"""Test deleting a snack"""
response = client.delete(f"/api/v1/inventory/snacks/{'1629'}")
assert response.status_code == 200 # Not found
85 changes: 85 additions & 0 deletions backend/tests/test_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""
Database Utility Tests

Tests all database operations including:
- CRUD operations for snacks
- Database connection handling
- Schema initialization

TODO: Implement test cases for:
- Creating snacks
- Reading snacks
- Updating snacks
- Deleting snacks
- Getting inventory
"""
import pytest
from utils.db import (
get_inventory,
get_snack,
create_snack,
update_snack,
delete_snack,
get_db_connection
)

from models.snack import SnackCreateSchema, SnackUpdateSchema
from pytest import fixture
# TODO: Implement your database tests here

@pytest.fixture # Martin was here :)
def test_create_snack(db):
"""Test creating a new snack"""
get_db_connection(db)
#Arrange:Prepares the data and Act: Adds the snacks to the db
#create_snack(SnackCreateSchema(sku="12345",name="Coke Zero",quantity=20))
#create_snack(SnackCreateSchema(sku="1629",name="Rice Krispy Treat",quantity=25))
#create_snack(SnackCreateSchema(sku='4200',name='Gummy Bears', quantity=20))

@pytest.fixture
def test_get_snack(db):
"""Test retrieving a single snack"""
get_db_connection(db)
#Assert checks if conditions to check if the test passes or fails
snack_I_Want=get_snack("12345")

assert snack_I_Want.sku == "12345"
assert snack_I_Want.name =='Coke Zero'

assert snack_I_Want.quantity == 20

@pytest.fixture
def test_update_snack(db):
"""Test updating a snack"""
get_db_connection(db)

update_snack(sku="1629",updates=SnackUpdateSchema(name="Rice Krispy Treat",quantity=60)) #SCE went to a costco trip

#Assert conditions

snack_I_Want=get_snack('1629')

assert snack_I_Want.quantity == 60


@pytest.fixture
def test_delete_snack(db):
"""Test deleting a snack"""
get_db_connection(db)
#Act:Delete the snack

delete_snack('4200')

assert get_snack('4200') ==None


@pytest.fixture
def test_get_inventory(db):
"""Test retrieving all snacks"""
get_db_connection(db)
grab_all_snacks=get_inventory()

assert len(grab_all_snacks) > 0

for i in grab_all_snacks:
print(i)