Spaces:
Running
Running
File size: 23,178 Bytes
e4346d3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 | #!/usr/bin/env python3
"""
Complete end-to-end taxonomy implementation test script.
Tests all taxonomy categories from the provided JSON structure.
This script demonstrates:
1. Creating taxonomy data with all categories
2. Using projection lists for performance optimization
3. Filtering by specific taxonomy types
4. UOM conversions management
5. CRUD operations on taxonomy data
Based on the JSON structure:
{
"merchant_id": "company_cuatro_beauty_ltd",
"brands": ["L'OrΓ©al Professional", "Schwarzkopf", "Kerastase", ...],
"categories": ["Hair", "Skin", "Nails", "Spa", "Makeup", ...],
"lines": ["Hair Care", "Hair Styling", "Facials", ...],
"classes": ["Premium", "Luxury", "Basic", "VIP"],
"subcategories": {"Hair": ["Hair Cut", "Hair Color", ...], ...},
"specializations": ["Hairdresser", "Nail technician", ...],
"job_role": ["Senior Stylist", "Director", ...],
"languages": ["English", "Hindi", "Tamil"],
"customer_group": ["VIP", "Regular", "Premium", ...],
"pos_tender_modes": ["Cash", "Credit Card", ...],
"payment_types": ["Cash and Carry", "Credit"],
"payment_methods": ["Bank Transfer", "Cash", "Cheque"],
"asset_location": ["Store Floor", "Warehouse", ...],
"asset_category": ["Electronics", "Furniture", "Equipment"],
"stock_bin_location": ["A1-01", "B2-15", ...],
"branch_types": ["Flagship", "Outlet", "Pop-up", "Franchise"],
"expense_types": ["Travel", "Office Supplies"],
"uom_conversions": [...]
}
"""
import asyncio
import json
import sys
import os
from datetime import datetime
from typing import Dict, List, Any
# Add the app directory to Python path
sys.path.append(os.path.join(os.path.dirname(__file__), 'app'))
from motor.motor_asyncio import AsyncIOMotorClient
from app.taxonomy.services.service import TaxonomyService
from app.taxonomy.models.model import TaxonomyModel
from app.taxonomy.schemas.schema import TaxonomyInfo, TaxonomyListRequest, UOMConversionGroup, UOMConversionDetail
from app.constants.collections import SCM_TAXONOMY_COLLECTION
from app.core.config import settings as app_settings
class TaxonomyTestRunner:
"""Complete taxonomy implementation test runner."""
def __init__(self):
self.client = None
self.db = None
self.service = None
self.test_merchant_id = "company_cuatro_beauty_ltd"
async def setup(self):
"""Setup test environment."""
print("π§ Setting up test environment...")
# Create database connection
self.client = AsyncIOMotorClient(app_settings.MONGODB_URI)
self.db = self.client["scm_test_complete_taxonomy"]
# Create service instance
self.service = TaxonomyService.__new__(TaxonomyService)
self.service.db = self.db
self.service.model = TaxonomyModel.__new__(TaxonomyModel)
self.service.model.db = self.db
self.service.model.collection = self.db[SCM_TAXONOMY_COLLECTION]
# Clear any existing test data
await self.db[SCM_TAXONOMY_COLLECTION].delete_many({"merchant_id": self.test_merchant_id})
print("β
Test environment setup complete")
async def cleanup(self):
"""Cleanup test environment."""
print("π§Ή Cleaning up test environment...")
try:
await self.db[SCM_TAXONOMY_COLLECTION].delete_many({"merchant_id": self.test_merchant_id})
self.client.close()
print("β
Cleanup complete")
except Exception as e:
print(f"β οΈ Cleanup warning: {e}")
def create_complete_taxonomy_data(self) -> TaxonomyInfo:
"""Create complete taxonomy data based on the provided JSON structure."""
# UOM conversions from the JSON
uom_conversions = [
UOMConversionGroup(
base_uom="g",
conversions=[
UOMConversionDetail(
alt_uom="kg",
factor=1000.0,
description="1 kg = 1000 g"
)
]
),
UOMConversionGroup(
base_uom="ml",
conversions=[
UOMConversionDetail(
alt_uom="L",
factor=1000.0,
description="1 L = 1000 ml"
),
UOMConversionDetail(
alt_uom="bottle",
factor=650.0,
description="650 ml bottle"
)
]
),
UOMConversionGroup(
base_uom="pcs",
conversions=[
UOMConversionDetail(
alt_uom="dozen",
factor=12.0,
description="1 dozen = 12 pcs"
),
UOMConversionDetail(
alt_uom="pack",
factor=6.0,
description="1 pack = 6 pcs (example)"
)
]
),
UOMConversionGroup(
base_uom="hr",
conversions=[]
),
UOMConversionGroup(
base_uom="L",
conversions=[
UOMConversionDetail(
alt_uom="bottle",
factor=1.0,
description="1 bottle = 1 L (example SKU)"
)
]
)
]
# Subcategories from the JSON
subcategories = {
"Hair": ["Hair Cut", "Hair Color", "Hair Spa", "Keratin Treatment", "Hair Smoothening"],
"Skin": ["Clean-up", "Facial", "De-Tan", "Peel Treatments", "Skin Brightening"],
"Nails": ["Manicure", "Pedicure", "Nail Art", "Gel Polish"],
"Spa": ["Head Massage", "Full Body Massage", "Aromatherapy", "Deep Tissue Massage"],
"Makeup": ["Bridal Makeup", "Party Makeup"],
"Foot": ["Pedicure"],
"H": ["Serum"]
}
return TaxonomyInfo(
merchant_id=self.test_merchant_id,
# Product Taxonomies
brands=[
"L'OrΓ©al Professional", "Schwarzkopf", "Kerastase", "Olaplex", "Lotus",
"b1", "g", " Services", "Brand1", "Brand2", "Brand3", "Brand4",
"Glory", "NewBrand", "vb", ""
],
categories=[
"Hair", "Skin", "Nails", "Spa", "Makeup", "Foot", "ea", "saddle",
"d", "Glory", "Dummy123", "Alpha1", "Category1", "Hello",
"test-category", "Child Hair Stylist", "Hair color", "hair sap"
],
lines=[
"Hair Care", "Hair Styling", "Facials", "Body Treatments",
"Manicure", "Pedicure", "Ayurveda", "GLoryt"
],
classes=["Premium", "Luxury", "Basic", "Class1", "Class2", "VIP"],
subcategories=subcategories,
# Employee Taxonomies
job_role=["Senior Stylist", "Director", "Massage therapist", "Esthetician"],
specializations=["Hairdresser", "Nail technician", "Massage therapist", "Esthetician", "hair style"],
languages=["English", "Hindi", "Tamil"],
# Customer Taxonomies
customer_group=["VIP", "Regular", "Premium", "Enterprise", "Gold"],
# Operational Taxonomies - POS & Payment
pos_tender_modes=["Cash", "Credit Card", "Debit Card", "Mobile Payment"],
payment_types=["Cash and Carry", "Credit"],
payment_methods=["Bank Transfer", "Cash", "Cheque"],
# Operational Taxonomies - Asset & Inventory
asset_location=["Store Floor", "Warehouse", "Back Office"],
asset_category=["Electronics", "Furniture", "Equipment"],
stock_bin_location=["A1-01", "B2-15", "C3-07", "Receiving"],
# Operational Taxonomies - Business Structure
branch_types=["Flagship", "Outlet", "Pop-up", "Franchise"],
expense_types=["Travel", "Office Supplies"],
# UOM Conversions
uom_conversions=uom_conversions,
# Metadata
created_by="AST011",
created_at=datetime(2025, 8, 19, 9, 50, 11, 927000),
updated_at=datetime(2025, 12, 12, 6, 31, 8, 509000)
)
async def test_create_complete_taxonomy(self):
"""Test 1: Create complete taxonomy data."""
print("\nπ Test 1: Creating complete taxonomy data...")
taxonomy_data = self.create_complete_taxonomy_data()
result = await self.service.create_or_update_taxonomy(
data=taxonomy_data,
created_by="AST011"
)
assert result["success"] is True
assert result["operation"] == "create"
assert "taxonomy_id" in result
print(f"β
Created taxonomy with ID: {result['taxonomy_id']}")
print(f" Operation: {result['operation']}")
return result["taxonomy_id"]
async def test_list_complete_taxonomy(self, taxonomy_id: str):
"""Test 2: List complete taxonomy data without projection."""
print("\nπ Test 2: Listing complete taxonomy data...")
request = TaxonomyListRequest(merchant_id=self.test_merchant_id)
result = await self.service.list_taxonomy(request)
assert result["success"] is True
assert result["projection_applied"] is False
assert result["merchant_id"] == self.test_merchant_id
data = result["data"]
print(f"β
Retrieved complete taxonomy data")
print(f" Data type: {type(data)}")
print(f" Has taxonomies: {'taxonomies' in data if isinstance(data, dict) else 'N/A'}")
if isinstance(data, dict) and "taxonomies" in data:
taxonomies = data["taxonomies"]
print(f" Available taxonomy types: {list(taxonomies.keys())}")
print(f" Brands count: {len(taxonomies.get('brands', []))}")
print(f" Categories count: {len(taxonomies.get('categories', []))}")
print(f" UOM conversions count: {len(taxonomies.get('uom_conversions', []))}")
return data
async def test_projection_list_performance(self):
"""Test 3: Test projection list for performance optimization."""
print("\nπ Test 3: Testing projection list performance...")
# Test with specific fields projection
projection_fields = ["merchant_id", "brands", "categories", "job_role", "created_at"]
request = TaxonomyListRequest(
merchant_id=self.test_merchant_id,
projection_list=projection_fields
)
result = await self.service.list_taxonomy(request)
assert result["success"] is True
assert result["projection_applied"] is True
data = result["data"]
print(f"β
Projection applied successfully")
print(f" Requested fields: {projection_fields}")
print(f" Data type: {type(data)}")
if isinstance(data, list) and data:
doc = data[0]
print(f" Returned fields: {list(doc.keys())}")
# Verify only requested fields are present (plus any defaults)
for field in projection_fields:
if field in ["brands", "categories", "job_role"] and doc.get(field):
print(f" β {field}: {len(doc[field])} items")
return data
async def test_taxonomy_type_filtering(self):
"""Test 4: Test filtering by specific taxonomy type."""
print("\nπ Test 4: Testing taxonomy type filtering...")
# Test filtering by brands
request = TaxonomyListRequest(
merchant_id=self.test_merchant_id,
taxonomy_type="brands"
)
result = await self.service.list_taxonomy(request)
assert result["success"] is True
assert result["taxonomy_type"] == "brands"
data = result["data"]
print(f"β
Filtered by taxonomy type: brands")
print(f" Data type: {type(data)}")
if isinstance(data, list) and data:
doc = data[0]
print(f" Document keys: {list(doc.keys())}")
if "data" in doc:
print(f" Brands count: {len(doc['data'])}")
print(f" Sample brands: {doc['data'][:3]}")
# Test filtering by UOM conversions
request_uom = TaxonomyListRequest(
merchant_id=self.test_merchant_id,
taxonomy_type="uom_conversions"
)
result_uom = await self.service.list_taxonomy(request_uom)
assert result_uom["success"] is True
print(f"β
Filtered by taxonomy type: uom_conversions")
return data
async def test_projection_with_taxonomy_type(self):
"""Test 5: Test projection combined with taxonomy type filtering."""
print("\nπ― Test 5: Testing projection + taxonomy type filtering...")
request = TaxonomyListRequest(
merchant_id=self.test_merchant_id,
taxonomy_type="categories",
projection_list=["merchant_id", "categories", "created_at"]
)
result = await self.service.list_taxonomy(request)
assert result["success"] is True
assert result["projection_applied"] is True
assert result["taxonomy_type"] == "categories"
data = result["data"]
print(f"β
Combined projection + filtering applied")
print(f" Taxonomy type: categories")
print(f" Projection fields: merchant_id, categories, created_at")
print(f" Data type: {type(data)}")
if isinstance(data, list) and data:
doc = data[0]
print(f" Returned fields: {list(doc.keys())}")
return data
async def test_uom_conversions_management(self):
"""Test 6: Test UOM conversions CRUD operations."""
print("\nβοΈ Test 6: Testing UOM conversions management...")
# Test getting UOM conversions
uom_result = await self.service.get_uom_conversions(
merchant_id=self.test_merchant_id
)
assert uom_result["success"] is True
uom_data = uom_result["data"]
print(f"β
Retrieved UOM conversions")
print(f" Conversion groups: {len(uom_data)}")
print(f" Is default: {uom_result.get('is_default', False)}")
# Print sample conversions
for i, group in enumerate(uom_data[:2]): # Show first 2 groups
if isinstance(group, dict):
base_uom = group.get("base_uom")
conversions = group.get("conversions", [])
print(f" Group {i+1}: {base_uom} -> {len(conversions)} conversions")
if conversions:
sample = conversions[0]
print(f" Sample: {sample.get('alt_uom')} (factor: {sample.get('factor')})")
return uom_data
async def test_update_taxonomy_data(self):
"""Test 7: Test updating taxonomy data."""
print("\nβοΈ Test 7: Testing taxonomy data updates...")
# Add new brands
update_data = TaxonomyInfo(
merchant_id=self.test_merchant_id,
brands=["New Brand 1", "New Brand 2", "Updated Brand"],
categories=["New Category"],
expense_types=["Marketing", "Training"],
created_by="AST014"
)
result = await self.service.create_or_update_taxonomy(
data=update_data,
created_by="AST014"
)
assert result["success"] is True
assert result["operation"] == "update"
print(f"β
Updated taxonomy data")
print(f" Operation: {result['operation']}")
print(f" Modified count: {result.get('modified_count', 0)}")
# Verify the update
request = TaxonomyListRequest(
merchant_id=self.test_merchant_id,
projection_list=["brands", "categories", "expense_types"]
)
verify_result = await self.service.list_taxonomy(request)
data = verify_result["data"]
if isinstance(data, list) and data:
doc = data[0]
brands = doc.get("brands", [])
expense_types = doc.get("expense_types", [])
print(f" Updated brands count: {len(brands)}")
print(f" Updated expense_types count: {len(expense_types)}")
# Check if new items were added
new_brands = ["New Brand 1", "New Brand 2", "Updated Brand"]
new_expenses = ["Marketing", "Training"]
for brand in new_brands:
if brand in brands:
print(f" β Added brand: {brand}")
for expense in new_expenses:
if expense in expense_types:
print(f" β Added expense type: {expense}")
return result
async def test_delete_taxonomy_values(self):
"""Test 8: Test deleting specific taxonomy values."""
print("\nποΈ Test 8: Testing taxonomy value deletion...")
# Delete specific brands using the delete flag
delete_data = TaxonomyInfo(
merchant_id=self.test_merchant_id,
brands=["New Brand 1", "Updated Brand"], # These will be deleted
is_delete=True,
created_by="AST014"
)
result = await self.service.create_or_update_taxonomy(
data=delete_data,
created_by="AST014"
)
assert result["success"] is True
assert result["operation"] == "delete"
print(f"β
Deleted taxonomy values")
print(f" Operation: {result['operation']}")
print(f" Modified count: {result.get('modified_count', 0)}")
# Verify the deletion
request = TaxonomyListRequest(
merchant_id=self.test_merchant_id,
projection_list=["brands"]
)
verify_result = await self.service.list_taxonomy(request)
data = verify_result["data"]
if isinstance(data, list) and data:
doc = data[0]
brands = doc.get("brands", [])
print(f" Remaining brands count: {len(brands)}")
# Check if items were deleted
deleted_brands = ["New Brand 1", "Updated Brand"]
for brand in deleted_brands:
if brand not in brands:
print(f" β Deleted brand: {brand}")
else:
print(f" β οΈ Brand still exists: {brand}")
return result
async def test_performance_comparison(self):
"""Test 9: Compare performance with and without projection."""
print("\nβ‘ Test 9: Performance comparison...")
import time
# Test without projection (full data)
start_time = time.time()
request_full = TaxonomyListRequest(merchant_id=self.test_merchant_id)
result_full = await self.service.list_taxonomy(request_full)
full_time = time.time() - start_time
# Test with projection (limited fields)
start_time = time.time()
request_projected = TaxonomyListRequest(
merchant_id=self.test_merchant_id,
projection_list=["merchant_id", "brands", "categories"]
)
result_projected = await self.service.list_taxonomy(request_projected)
projected_time = time.time() - start_time
print(f"β
Performance comparison completed")
print(f" Full data query time: {full_time:.4f}s")
print(f" Projected query time: {projected_time:.4f}s")
# Calculate data size difference (rough estimate)
full_data = result_full["data"]
projected_data = result_projected["data"]
if isinstance(full_data, dict) and isinstance(projected_data, list):
full_str = json.dumps(full_data, default=str)
projected_str = json.dumps(projected_data, default=str)
full_size = len(full_str)
projected_size = len(projected_str)
size_reduction = ((full_size - projected_size) / full_size) * 100 if full_size > 0 else 0
print(f" Full data size: {full_size} chars")
print(f" Projected data size: {projected_size} chars")
print(f" Size reduction: {size_reduction:.1f}%")
return {
"full_time": full_time,
"projected_time": projected_time,
"full_data": full_data,
"projected_data": projected_data
}
async def run_all_tests(self):
"""Run all taxonomy tests."""
print("π Starting Complete Taxonomy Implementation Tests")
print("=" * 60)
try:
await self.setup()
# Run all tests in sequence
taxonomy_id = await self.test_create_complete_taxonomy()
await self.test_list_complete_taxonomy(taxonomy_id)
await self.test_projection_list_performance()
await self.test_taxonomy_type_filtering()
await self.test_projection_with_taxonomy_type()
await self.test_uom_conversions_management()
await self.test_update_taxonomy_data()
await self.test_delete_taxonomy_values()
await self.test_performance_comparison()
print("\n" + "=" * 60)
print("π All tests completed successfully!")
print("\nπ Test Summary:")
print("β
Complete taxonomy creation")
print("β
Full data retrieval")
print("β
Projection list optimization")
print("β
Taxonomy type filtering")
print("β
Combined projection + filtering")
print("β
UOM conversions management")
print("β
Data updates")
print("β
Value deletion")
print("β
Performance comparison")
except Exception as e:
print(f"\nβ Test failed with error: {e}")
import traceback
traceback.print_exc()
finally:
await self.cleanup()
async def main():
"""Main test runner."""
runner = TaxonomyTestRunner()
await runner.run_all_tests()
if __name__ == "__main__":
asyncio.run(main()) |