Refactoring code for modular approach
This commit is contained in:
122
tests/test_products.py
Normal file
122
tests/test_products.py
Normal file
@@ -0,0 +1,122 @@
|
||||
# tests/test_products.py
|
||||
import pytest
|
||||
|
||||
|
||||
class TestProductsAPI:
|
||||
def test_get_products_empty(self, client, auth_headers):
|
||||
"""Test getting products when none exist"""
|
||||
response = client.get("/api/v1/products", headers=auth_headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["products"] == []
|
||||
assert data["total"] == 0
|
||||
|
||||
def test_get_products_with_data(self, client, auth_headers, test_product):
|
||||
"""Test getting products with data"""
|
||||
response = client.get("/api/v1/products", headers=auth_headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert len(data["products"]) == 1
|
||||
assert data["total"] == 1
|
||||
assert data["products"][0]["product_id"] == "TEST001"
|
||||
|
||||
def test_get_products_with_filters(self, client, auth_headers, test_product):
|
||||
"""Test filtering products"""
|
||||
# Test brand filter
|
||||
response = client.get("/api/v1/products?brand=TestBrand", headers=auth_headers)
|
||||
assert response.status_code == 200
|
||||
assert response.json()["total"] == 1
|
||||
|
||||
# Test marketplace filter
|
||||
response = client.get("/api/v1/products?marketplace=Letzshop", headers=auth_headers)
|
||||
assert response.status_code == 200
|
||||
assert response.json()["total"] == 1
|
||||
|
||||
# Test search
|
||||
response = client.get("/api/v1/products?search=Test", headers=auth_headers)
|
||||
assert response.status_code == 200
|
||||
assert response.json()["total"] == 1
|
||||
|
||||
def test_create_product(self, client, auth_headers):
|
||||
"""Test creating a new product"""
|
||||
product_data = {
|
||||
"product_id": "NEW001",
|
||||
"title": "New Product",
|
||||
"description": "A new product",
|
||||
"price": "15.99",
|
||||
"brand": "NewBrand",
|
||||
"gtin": "9876543210987",
|
||||
"availability": "in stock",
|
||||
"marketplace": "Amazon"
|
||||
}
|
||||
|
||||
response = client.post("/api/v1/products", headers=auth_headers, json=product_data)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["product_id"] == "NEW001"
|
||||
assert data["title"] == "New Product"
|
||||
assert data["marketplace"] == "Amazon"
|
||||
|
||||
def test_create_product_duplicate_id(self, client, auth_headers, test_product):
|
||||
"""Test creating product with duplicate ID"""
|
||||
product_data = {
|
||||
"product_id": "TEST001", # Same as test_product
|
||||
"title": "Another Product",
|
||||
"price": "20.00"
|
||||
}
|
||||
|
||||
response = client.post("/api/v1/products", headers=auth_headers, json=product_data)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert "already exists" in response.json()["detail"]
|
||||
|
||||
def test_get_product_by_id(self, client, auth_headers, test_product):
|
||||
"""Test getting specific product"""
|
||||
response = client.get(f"/api/v1/products/{test_product.product_id}", headers=auth_headers)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["product"]["product_id"] == test_product.product_id
|
||||
assert data["product"]["title"] == test_product.title
|
||||
|
||||
def test_get_nonexistent_product(self, client, auth_headers):
|
||||
"""Test getting nonexistent product"""
|
||||
response = client.get("/api/v1/products/NONEXISTENT", headers=auth_headers)
|
||||
|
||||
assert response.status_code == 404
|
||||
|
||||
def test_update_product(self, client, auth_headers, test_product):
|
||||
"""Test updating product"""
|
||||
update_data = {
|
||||
"title": "Updated Product Title",
|
||||
"price": "25.99"
|
||||
}
|
||||
|
||||
response = client.put(
|
||||
f"/api/v1/products/{test_product.product_id}",
|
||||
headers=auth_headers,
|
||||
json=update_data
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["title"] == "Updated Product Title"
|
||||
assert data["price"] == "25.99"
|
||||
|
||||
def test_delete_product(self, client, auth_headers, test_product):
|
||||
"""Test deleting product"""
|
||||
response = client.delete(
|
||||
f"/api/v1/products/{test_product.product_id}",
|
||||
headers=auth_headers
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert "deleted successfully" in response.json()["message"]
|
||||
|
||||
def test_products_require_auth(self, client):
|
||||
"""Test that product endpoints require authentication"""
|
||||
response = client.get("/api/v1/products")
|
||||
assert response.status_code == 403
|
||||
Reference in New Issue
Block a user