Spaces:
Sleeping
Sleeping
Delete tests with huggingface_hub
Browse files- tests/__init__.py +0 -1
- tests/test_filters.py +0 -115
- tests/test_metadata_service.py +0 -192
- tests/test_products_api.py +0 -73
- tests/test_products_api_v2.py +0 -105
tests/__init__.py
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
# empty
|
|
|
|
|
|
tests/test_filters.py
DELETED
|
@@ -1,115 +0,0 @@
|
|
| 1 |
-
import sys
|
| 2 |
-
import os
|
| 3 |
-
import json
|
| 4 |
-
from sqlalchemy import create_engine
|
| 5 |
-
from sqlalchemy.orm import sessionmaker
|
| 6 |
-
from sqlalchemy import func
|
| 7 |
-
|
| 8 |
-
# Allow imports from backend root
|
| 9 |
-
sys.path.insert(0, os.path.join(os.getcwd(), "backend"))
|
| 10 |
-
|
| 11 |
-
from app.models.product import Category, Product
|
| 12 |
-
from app.models.user import User
|
| 13 |
-
from app.db.base import Base
|
| 14 |
-
|
| 15 |
-
# Setup in-memory SQLite for testing
|
| 16 |
-
engine = create_engine("sqlite:///:memory:")
|
| 17 |
-
SessionLocal = sessionmaker(bind=engine)
|
| 18 |
-
Base.metadata.create_all(bind=engine)
|
| 19 |
-
|
| 20 |
-
def setup_test_data(db):
|
| 21 |
-
# Create Hierarchy
|
| 22 |
-
electronics = Category(id=1, name_ar="الأجهزة الإلكترونية", name_en="Electronics", icon="📱")
|
| 23 |
-
laptops = Category(id=2, name_ar="لابتوب", name_en="Laptops", icon="💻", parent_id=1)
|
| 24 |
-
smartphones = Category(id=3, name_ar="جوالات", name_en="Smartphones", icon="📲", parent_id=1)
|
| 25 |
-
db.add_all([electronics, laptops, smartphones])
|
| 26 |
-
db.commit()
|
| 27 |
-
|
| 28 |
-
# Create Products
|
| 29 |
-
p1 = Product(
|
| 30 |
-
id=1, name_en="MacBook Pro", name_ar="ماك بوك برو",
|
| 31 |
-
price=8000, category_id=2, is_active=True,
|
| 32 |
-
specs={"brand_name": "Apple", "RAM": "16GB", "Storage": "512GB SSD", "details_en": {"Processor": "M3"}}
|
| 33 |
-
)
|
| 34 |
-
p2 = Product(
|
| 35 |
-
id=2, name_en="Dell XPS", name_ar="ديل اكس بي اس",
|
| 36 |
-
price=6000, category_id=2, is_active=True,
|
| 37 |
-
specs={"brand_name": "Dell", "RAM": "8GB", "Storage": "256GB SSD", "details_en": {"Processor": "i7"}}
|
| 38 |
-
)
|
| 39 |
-
p3 = Product(
|
| 40 |
-
id=3, name_en="iPhone 15", name_ar="ايفون 15",
|
| 41 |
-
price=4000, category_id=3, is_active=True,
|
| 42 |
-
specs={"brand_name": "Apple", "RAM": "6GB", "Color": "Blue"}
|
| 43 |
-
)
|
| 44 |
-
db.add_all([p1, p2, p3])
|
| 45 |
-
db.commit()
|
| 46 |
-
|
| 47 |
-
def test_recursive_category_search():
|
| 48 |
-
db = SessionLocal()
|
| 49 |
-
setup_test_data(db)
|
| 50 |
-
|
| 51 |
-
# Query for "Electronics" (Parent ID 1)
|
| 52 |
-
category_id = 1
|
| 53 |
-
subcat_ids = [c[0] for c in db.query(Category.id).filter(Category.parent_id == category_id).all()]
|
| 54 |
-
all_cat_ids = [category_id] + subcat_ids
|
| 55 |
-
|
| 56 |
-
results = db.query(Product).filter(Product.category_id.in_(all_cat_ids)).all()
|
| 57 |
-
|
| 58 |
-
assert len(results) == 3
|
| 59 |
-
assert any(p.name_en == "MacBook Pro" for p in results)
|
| 60 |
-
assert any(p.name_en == "iPhone 15" for p in results)
|
| 61 |
-
db.close()
|
| 62 |
-
|
| 63 |
-
def test_dynamic_filter_discovery():
|
| 64 |
-
db = SessionLocal()
|
| 65 |
-
# Test for Laptops (ID 2)
|
| 66 |
-
category_id = 2
|
| 67 |
-
|
| 68 |
-
# 1. Price Range
|
| 69 |
-
prices = db.query(func.min(Product.price), func.max(Product.price)).filter(Product.category_id == category_id).first()
|
| 70 |
-
assert prices == (6000.0, 8000.0)
|
| 71 |
-
|
| 72 |
-
# 2. Brands
|
| 73 |
-
brands = db.query(func.json_extract(Product.specs, '$.brand_name')).filter(Product.category_id == category_id).distinct().all()
|
| 74 |
-
brands = [b[0] for b in brands if b[0]]
|
| 75 |
-
assert "Apple" in brands
|
| 76 |
-
assert "Dell" in brands
|
| 77 |
-
|
| 78 |
-
# 3. Dynamic Specs
|
| 79 |
-
# In a real implementation, we'd iterate over known keys or scan a sample
|
| 80 |
-
ram_options = db.query(func.json_extract(Product.specs, '$.RAM')).filter(Product.category_id == category_id).distinct().all()
|
| 81 |
-
ram_options = [r[0] for r in ram_options if r[0]]
|
| 82 |
-
assert "16GB" in ram_options
|
| 83 |
-
assert "8GB" in ram_options
|
| 84 |
-
db.close()
|
| 85 |
-
|
| 86 |
-
def test_specs_filter_logic():
|
| 87 |
-
db = SessionLocal()
|
| 88 |
-
# Mock behavior of specs_filter = {"RAM": ["16GB"]}
|
| 89 |
-
filters = {"RAM": ["16GB"]}
|
| 90 |
-
query = db.query(Product)
|
| 91 |
-
for key, values in filters.items():
|
| 92 |
-
query = query.filter(func.json_extract(Product.specs, f'$.{key}').in_(values))
|
| 93 |
-
|
| 94 |
-
results = query.all()
|
| 95 |
-
assert len(results) == 1
|
| 96 |
-
assert results[0].name_en == "MacBook Pro"
|
| 97 |
-
db.close()
|
| 98 |
-
|
| 99 |
-
if __name__ == "__main__":
|
| 100 |
-
print("Running Filter Logic Tests...")
|
| 101 |
-
try:
|
| 102 |
-
test_recursive_category_search()
|
| 103 |
-
print("[PASS] Recursive Category Search")
|
| 104 |
-
test_dynamic_filter_discovery()
|
| 105 |
-
print("[PASS] Dynamic Filter Discovery")
|
| 106 |
-
test_specs_filter_logic()
|
| 107 |
-
print("[PASS] Specs Filter Logic")
|
| 108 |
-
except AssertionError as e:
|
| 109 |
-
print(f"[FAIL] {e}")
|
| 110 |
-
sys.exit(1)
|
| 111 |
-
except Exception as e:
|
| 112 |
-
print(f"[ERROR] {type(e).__name__}: {e}")
|
| 113 |
-
import traceback
|
| 114 |
-
traceback.print_exc()
|
| 115 |
-
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tests/test_metadata_service.py
DELETED
|
@@ -1,192 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Unit Tests for MetadataService — standalone, no DB or app context needed.
|
| 3 |
-
Tests: parse_user_agent, get_geoip_info, extract_headers, build_metadata
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import json
|
| 7 |
-
import sys
|
| 8 |
-
import os
|
| 9 |
-
import asyncio
|
| 10 |
-
|
| 11 |
-
# Allow imports from backend root
|
| 12 |
-
sys.path.insert(0, os.getcwd())
|
| 13 |
-
|
| 14 |
-
from app.services.metadata_service import parse_user_agent, get_geoip_info, build_metadata
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
# ═══════════════════════════════════════════════════════════════════════════════
|
| 18 |
-
# 1. parse_user_agent tests
|
| 19 |
-
# ═══════════════════════════════════════════════════════════════════════════════
|
| 20 |
-
|
| 21 |
-
def test_parse_chrome_windows():
|
| 22 |
-
ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
| 23 |
-
result = parse_user_agent(ua)
|
| 24 |
-
assert result["browser"] == "Chrome"
|
| 25 |
-
assert result["os"] == "Windows"
|
| 26 |
-
assert result["device_type"] == "Desktop"
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def test_parse_safari_iphone():
|
| 30 |
-
ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_1_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1.2 Mobile/15E148 Safari/604.1"
|
| 31 |
-
result = parse_user_agent(ua)
|
| 32 |
-
assert result["browser"] == "Safari"
|
| 33 |
-
assert result["os"] == "iOS"
|
| 34 |
-
assert result["device_type"] == "Mobile"
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
def test_parse_chrome_android():
|
| 38 |
-
ua = "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36"
|
| 39 |
-
result = parse_user_agent(ua)
|
| 40 |
-
assert result["browser"] == "Chrome"
|
| 41 |
-
assert result["os"] == "Android"
|
| 42 |
-
assert result["device_type"] == "Mobile"
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def test_parse_edge_windows():
|
| 46 |
-
ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0"
|
| 47 |
-
result = parse_user_agent(ua)
|
| 48 |
-
assert result["browser"] == "Edge"
|
| 49 |
-
assert result["os"] == "Windows"
|
| 50 |
-
assert result["device_type"] == "Desktop"
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
def test_parse_firefox_linux():
|
| 54 |
-
ua = "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/119.0"
|
| 55 |
-
result = parse_user_agent(ua)
|
| 56 |
-
assert result["browser"] == "Firefox"
|
| 57 |
-
assert result["os"] == "Linux"
|
| 58 |
-
assert result["device_type"] == "Desktop"
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
def test_parse_safari_macos():
|
| 62 |
-
ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15"
|
| 63 |
-
result = parse_user_agent(ua)
|
| 64 |
-
assert result["browser"] == "Safari"
|
| 65 |
-
assert result["os"] == "MacOS"
|
| 66 |
-
assert result["device_type"] == "Desktop"
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
def test_parse_ipad():
|
| 70 |
-
ua = "Mozilla/5.0 (iPad; CPU OS 17_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Mobile/15E148 Safari/604.1"
|
| 71 |
-
result = parse_user_agent(ua)
|
| 72 |
-
assert result["browser"] == "Safari"
|
| 73 |
-
assert result["os"] == "iPadOS"
|
| 74 |
-
assert result["device_type"] == "Tablet"
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
def test_parse_none():
|
| 78 |
-
result = parse_user_agent(None)
|
| 79 |
-
assert result["browser"] == "Unknown"
|
| 80 |
-
assert result["os"] == "Unknown"
|
| 81 |
-
assert result["device_type"] == "Desktop"
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
def test_parse_empty_string():
|
| 85 |
-
result = parse_user_agent("")
|
| 86 |
-
assert result["browser"] == "Unknown"
|
| 87 |
-
assert result["os"] == "Unknown"
|
| 88 |
-
assert result["device_type"] == "Desktop"
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
# ═══════════════════════════════════════════════════════════════════════════════
|
| 92 |
-
# 2. get_geoip_info tests
|
| 93 |
-
# ═══════════════════════════════════════════════════════════════════════════════
|
| 94 |
-
|
| 95 |
-
def test_geoip_localhost_returns_none():
|
| 96 |
-
result = asyncio.run(get_geoip_info("127.0.0.1"))
|
| 97 |
-
assert result is None
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
def test_geoip_none_returns_none():
|
| 101 |
-
result = asyncio.run(get_geoip_info(None))
|
| 102 |
-
assert result is None
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
def test_geoip_private_ip_returns_none():
|
| 106 |
-
result = asyncio.run(get_geoip_info("192.168.1.1"))
|
| 107 |
-
assert result is None
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
def test_geoip_ipv6_localhost_returns_none():
|
| 111 |
-
result = asyncio.run(get_geoip_info("::1"))
|
| 112 |
-
assert result is None
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
# ═══════════════════════════════════════════════════════════════════════════════
|
| 116 |
-
# 3. build_metadata tests
|
| 117 |
-
# ═══════════════════════════════════════════════════════════════════════════════
|
| 118 |
-
|
| 119 |
-
class FakeHeaders:
|
| 120 |
-
"""Minimal mock for Request.headers"""
|
| 121 |
-
def __init__(self, headers_dict=None):
|
| 122 |
-
self._headers = headers_dict or {}
|
| 123 |
-
|
| 124 |
-
def get(self, key, default=None):
|
| 125 |
-
return self._headers.get(key, default)
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
class FakeRequest:
|
| 129 |
-
"""Minimal mock for FastAPI Request"""
|
| 130 |
-
def __init__(self, headers_dict=None):
|
| 131 |
-
self.headers = FakeHeaders(headers_dict)
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
def test_build_metadata_with_frontend_json():
|
| 135 |
-
frontend = json.dumps({"screenResolution": "1920x1080", "language": "ar"})
|
| 136 |
-
req = FakeRequest({"Referer": "https://vortex.com/checkout", "Accept-Language": "ar-SA"})
|
| 137 |
-
|
| 138 |
-
result = build_metadata(frontend, req)
|
| 139 |
-
parsed = json.loads(result)
|
| 140 |
-
|
| 141 |
-
assert parsed["browser_meta"]["screenResolution"] == "1920x1080"
|
| 142 |
-
assert parsed["browser_meta"]["language"] == "ar"
|
| 143 |
-
assert parsed["headers"]["referer"] == "https://vortex.com/checkout"
|
| 144 |
-
assert parsed["headers"]["accept_language"] == "ar-SA"
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
def test_build_metadata_with_none_frontend():
|
| 148 |
-
req = FakeRequest({"Accept-Language": "en-US"})
|
| 149 |
-
|
| 150 |
-
result = build_metadata(None, req)
|
| 151 |
-
parsed = json.loads(result)
|
| 152 |
-
|
| 153 |
-
assert parsed["browser_meta"] == {}
|
| 154 |
-
assert parsed["headers"]["accept_language"] == "en-US"
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
def test_build_metadata_with_invalid_json():
|
| 158 |
-
req = FakeRequest({})
|
| 159 |
-
|
| 160 |
-
result = build_metadata("not-valid-json", req)
|
| 161 |
-
parsed = json.loads(result)
|
| 162 |
-
|
| 163 |
-
assert "raw" in parsed["browser_meta"]
|
| 164 |
-
assert parsed["browser_meta"]["raw"] == "not-valid-json"
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
# ═══════════════════════════════════════════════════════════════════════════════
|
| 168 |
-
# Runner
|
| 169 |
-
# ═══════════════════════════════════════════════════════════════════════════════
|
| 170 |
-
|
| 171 |
-
if __name__ == "__main__":
|
| 172 |
-
tests = [v for k, v in globals().items() if k.startswith("test_")]
|
| 173 |
-
passed = 0
|
| 174 |
-
failed = 0
|
| 175 |
-
for test_fn in tests:
|
| 176 |
-
try:
|
| 177 |
-
test_fn()
|
| 178 |
-
print(f" [PASS] {test_fn.__name__}")
|
| 179 |
-
passed += 1
|
| 180 |
-
except AssertionError as e:
|
| 181 |
-
print(f" [FAIL] {test_fn.__name__}: {e}")
|
| 182 |
-
failed += 1
|
| 183 |
-
except Exception as e:
|
| 184 |
-
print(f" [FAIL] {test_fn.__name__}: {type(e).__name__}: {e}")
|
| 185 |
-
failed += 1
|
| 186 |
-
|
| 187 |
-
print("")
|
| 188 |
-
print("=" * 50)
|
| 189 |
-
print(f" Results: {passed} passed, {failed} failed, {passed + failed} total")
|
| 190 |
-
print("=" * 50)
|
| 191 |
-
|
| 192 |
-
sys.exit(1 if failed > 0 else 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tests/test_products_api.py
DELETED
|
@@ -1,73 +0,0 @@
|
|
| 1 |
-
import pytest
|
| 2 |
-
from fastapi.testclient import TestClient
|
| 3 |
-
from sqlalchemy import create_engine
|
| 4 |
-
from sqlalchemy.orm import sessionmaker
|
| 5 |
-
|
| 6 |
-
from app.main import app
|
| 7 |
-
from app.db.base import Base, get_db
|
| 8 |
-
from app.models.product import Product, Category
|
| 9 |
-
|
| 10 |
-
# Setup in-memory SQLite for testing
|
| 11 |
-
SQLALCHEMY_DATABASE_URL = "sqlite:///:memory:"
|
| 12 |
-
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
|
| 13 |
-
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 14 |
-
|
| 15 |
-
def override_get_db():
|
| 16 |
-
try:
|
| 17 |
-
db = TestingSessionLocal()
|
| 18 |
-
yield db
|
| 19 |
-
finally:
|
| 20 |
-
db.close()
|
| 21 |
-
|
| 22 |
-
app.dependency_overrides[get_db] = override_get_db
|
| 23 |
-
client = TestClient(app)
|
| 24 |
-
|
| 25 |
-
@pytest.fixture(scope="module", autouse=True)
|
| 26 |
-
def setup_database():
|
| 27 |
-
Base.metadata.create_all(bind=engine)
|
| 28 |
-
db = TestingSessionLocal()
|
| 29 |
-
|
| 30 |
-
# Categories
|
| 31 |
-
elect = Category(id=1, name_ar="الأجهزة الإلكترونية", name_en="Electronics")
|
| 32 |
-
laptop = Category(id=2, name_ar="لابتوب", name_en="Laptops", parent_id=1)
|
| 33 |
-
db.add_all([elect, laptop])
|
| 34 |
-
db.commit()
|
| 35 |
-
|
| 36 |
-
# Products
|
| 37 |
-
p1 = Product(id=1, name_ar="ماك", name_en="Mac", price=5000, category_id=2, is_active=True, specs={"brand_name": "Apple", "RAM": "16GB"})
|
| 38 |
-
p2 = Product(id=2, name_ar="ديل", name_en="Dell", price=3000, category_id=2, is_active=True, specs={"brand_name": "Dell", "RAM": "8GB"})
|
| 39 |
-
db.add_all([p1, p2])
|
| 40 |
-
db.commit()
|
| 41 |
-
yield
|
| 42 |
-
Base.metadata.drop_all(bind=engine)
|
| 43 |
-
|
| 44 |
-
def test_list_products_recursive_category():
|
| 45 |
-
# Filtering by Parent category should return 2 products
|
| 46 |
-
response = client.get("/api/v1/products?category_id=1")
|
| 47 |
-
assert response.status_code == 200
|
| 48 |
-
data = response.json()
|
| 49 |
-
assert data["isSuccess"] == True
|
| 50 |
-
assert len(data["value"]["items"]) == 2
|
| 51 |
-
|
| 52 |
-
def test_list_products_multi_brand():
|
| 53 |
-
response = client.get("/api/v1/products?brand=Apple,Dell")
|
| 54 |
-
assert response.status_code == 200
|
| 55 |
-
data = response.json()
|
| 56 |
-
assert len(data["value"]["items"]) == 2
|
| 57 |
-
|
| 58 |
-
def test_get_global_filters():
|
| 59 |
-
response = client.get("/api/v1/products/filters")
|
| 60 |
-
assert response.status_code == 200
|
| 61 |
-
data = response.json()
|
| 62 |
-
assert "Apple" in data["value"]["brands"]
|
| 63 |
-
assert "Dell" in data["value"]["brands"]
|
| 64 |
-
|
| 65 |
-
def test_get_category_filters():
|
| 66 |
-
response = client.get("/api/v1/products/filters?category_id=2")
|
| 67 |
-
assert response.status_code == 200
|
| 68 |
-
data = response.json()
|
| 69 |
-
# "Laptops" should discover "RAM"
|
| 70 |
-
attrs = data["value"]["attributes"]
|
| 71 |
-
ram_attr = next((a for a in attrs if a["key"] == "RAM"), None)
|
| 72 |
-
assert ram_attr is not None
|
| 73 |
-
assert "16GB" in ram_attr["options"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tests/test_products_api_v2.py
DELETED
|
@@ -1,105 +0,0 @@
|
|
| 1 |
-
import pytest
|
| 2 |
-
from sqlalchemy import create_engine
|
| 3 |
-
from sqlalchemy.orm import sessionmaker
|
| 4 |
-
from app.db.base import Base, get_db
|
| 5 |
-
from app.models.product import Product, Category
|
| 6 |
-
from app.main import app
|
| 7 |
-
from fastapi.testclient import TestClient
|
| 8 |
-
|
| 9 |
-
# Setup Test Database (SQLite in-memory)
|
| 10 |
-
SQLALCHEMY_DATABASE_URL = "sqlite:///./test_v2.db"
|
| 11 |
-
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
|
| 12 |
-
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 13 |
-
|
| 14 |
-
@pytest.fixture(scope="module")
|
| 15 |
-
def db():
|
| 16 |
-
Base.metadata.create_all(bind=engine)
|
| 17 |
-
db = TestingSessionLocal()
|
| 18 |
-
|
| 19 |
-
# Create Hierarchy: Electronics -> Laptops
|
| 20 |
-
electronics = Category(name_ar="الأجهزة الإلكترونية", name_en="Electronics", icon="📱")
|
| 21 |
-
db.add(electronics)
|
| 22 |
-
db.flush()
|
| 23 |
-
|
| 24 |
-
laptops = Category(name_ar="لابتوب", name_en="Laptops", icon="💻", parent_id=electronics.id)
|
| 25 |
-
db.add(laptops)
|
| 26 |
-
db.flush()
|
| 27 |
-
|
| 28 |
-
# Add Products
|
| 29 |
-
p1 = Product(
|
| 30 |
-
name_ar="لابتوب ابل", name_en="Apple Laptop", price=5000,
|
| 31 |
-
category_id=laptops.id, specs={"brand_name": "Apple", "RAM": "16GB", "Processor": "M3"}
|
| 32 |
-
)
|
| 33 |
-
p2 = Product(
|
| 34 |
-
name_ar="لابتوب ديل", name_en="Dell Laptop", price=3000,
|
| 35 |
-
category_id=laptops.id, specs={"brand_name": "Dell", "RAM": "8GB", "Processor": "i7"}
|
| 36 |
-
)
|
| 37 |
-
db.add_all([p1, p2])
|
| 38 |
-
db.commit()
|
| 39 |
-
|
| 40 |
-
yield db
|
| 41 |
-
db.close()
|
| 42 |
-
Base.metadata.drop_all(bind=engine)
|
| 43 |
-
|
| 44 |
-
@pytest.fixture(scope="module")
|
| 45 |
-
def client(db):
|
| 46 |
-
def override_get_db():
|
| 47 |
-
try:
|
| 48 |
-
yield db
|
| 49 |
-
finally:
|
| 50 |
-
pass
|
| 51 |
-
app.dependency_overrides[get_db] = override_get_db
|
| 52 |
-
with TestClient(app) as c:
|
| 53 |
-
yield c
|
| 54 |
-
app.dependency_overrides.clear()
|
| 55 |
-
|
| 56 |
-
def test_recursive_category_filter(client, db):
|
| 57 |
-
# Get Electronics ID
|
| 58 |
-
electronics = db.query(Category).filter(Category.name_en == "Electronics").first()
|
| 59 |
-
|
| 60 |
-
# Query Electronics -> Should return BOTH laptops
|
| 61 |
-
response = client.get(f"/api/products?category_id={electronics.id}")
|
| 62 |
-
assert response.status_code == 200
|
| 63 |
-
data = response.json()
|
| 64 |
-
assert data["value"]["total"] == 2
|
| 65 |
-
|
| 66 |
-
def test_specific_category_filter(client, db):
|
| 67 |
-
# Get Laptops ID
|
| 68 |
-
laptops = db.query(Category).filter(Category.name_en == "Laptops").first()
|
| 69 |
-
|
| 70 |
-
response = client.get(f"/api/products?category_id={laptops.id}")
|
| 71 |
-
assert response.status_code == 200
|
| 72 |
-
data = response.json()
|
| 73 |
-
assert data["value"]["total"] == 2
|
| 74 |
-
|
| 75 |
-
def test_dynamic_filter_discovery(client, db):
|
| 76 |
-
laptops = db.query(Category).filter(Category.name_en == "Laptops").first()
|
| 77 |
-
|
| 78 |
-
response = client.get(f"/api/products/filters?category_id={laptops.id}")
|
| 79 |
-
assert response.status_code == 200
|
| 80 |
-
data = response.json()
|
| 81 |
-
|
| 82 |
-
# Verify Brands
|
| 83 |
-
assert "Apple" in data["value"]["brands"]
|
| 84 |
-
assert "Dell" in data["value"]["brands"]
|
| 85 |
-
|
| 86 |
-
# Verify Dynamic Specs (RAM, Processor)
|
| 87 |
-
attrs = data["value"]["attributes"]
|
| 88 |
-
keys = [a["key"] for a in attrs]
|
| 89 |
-
assert "RAM" in keys
|
| 90 |
-
assert "Processor" in keys
|
| 91 |
-
|
| 92 |
-
# RAM options should contain 16GB and 8GB
|
| 93 |
-
ram_filter = next(a for a in attrs if a["key"] == "RAM")
|
| 94 |
-
assert "16GB" in ram_filter["options"]
|
| 95 |
-
assert "8GB" in ram_filter["options"]
|
| 96 |
-
|
| 97 |
-
def test_specs_filter_application(client, db):
|
| 98 |
-
laptops = db.query(Category).filter(Category.name_en == "Laptops").first()
|
| 99 |
-
|
| 100 |
-
# Filter by RAM = 16GB
|
| 101 |
-
response = client.get(f"/api/products?category_id={laptops.id}&specs_filter=" + '{"RAM": ["16GB"]}')
|
| 102 |
-
assert response.status_code == 200
|
| 103 |
-
data = response.json()
|
| 104 |
-
assert data["value"]["total"] == 1
|
| 105 |
-
assert data["value"]["items"][0]["name_en"] == "Apple Laptop"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|