All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 52s
177 lines
8.1 KiB
Python
177 lines
8.1 KiB
Python
"""
|
|
Test suite to thoroughly verify the GI accreditation logic, default state,
|
|
and CRM approval/rejection sync workflow.
|
|
"""
|
|
import os
|
|
import sys
|
|
|
|
# Configure minimal in-memory Django environment
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
|
|
import django
|
|
from django.conf import settings
|
|
|
|
# Override databases to in-memory SQLite for instantaneous local test execution
|
|
settings.DATABASES = {
|
|
'default': {
|
|
'ENGINE': 'django.db.backends.sqlite3',
|
|
'NAME': ':memory:',
|
|
},
|
|
'product_db': {
|
|
'ENGINE': 'django.db.backends.sqlite3',
|
|
'NAME': ':memory:',
|
|
},
|
|
'seller_db': {
|
|
'ENGINE': 'django.db.backends.sqlite3',
|
|
'NAME': ':memory:',
|
|
}
|
|
}
|
|
|
|
django.setup()
|
|
|
|
from django.core.management import call_command
|
|
from django.contrib.auth.models import User
|
|
from api.models import SupplierProfile, SupplierOnboardingDraft
|
|
from api.serializers import SupplierProfileSerializer
|
|
|
|
print("Setting up in-memory database...")
|
|
call_command('migrate', verbosity=0)
|
|
|
|
# ==============================================================================
|
|
# TEST 1: Default seller values are Non-GI Craft ('non_gi')
|
|
# ==============================================================================
|
|
print("\n--- TEST 1: Checking default GI accreditation fields ---")
|
|
user1 = User.objects.create(username="artisan_ramesh", email="ramesh@example.com")
|
|
profile1 = SupplierProfile.objects.create(
|
|
user=user1,
|
|
store_name="Ramesh Weaves",
|
|
phone="9876543210"
|
|
)
|
|
|
|
assert profile1.gi_seller_type == 'non_gi', f"Expected default 'non_gi', got {profile1.gi_seller_type}"
|
|
assert profile1.is_exclusive_gi is False, f"Expected is_exclusive_gi=False, got {profile1.is_exclusive_gi}"
|
|
assert profile1.gi_registry_verified is False, f"Expected gi_registry_verified=False, got {profile1.gi_registry_verified}"
|
|
assert profile1.gi_certificate_number is None
|
|
assert profile1.guild_status is None
|
|
print("✓ TEST 1 PASSED: New seller profile defaults to 'non_gi' and unverified.")
|
|
|
|
# ==============================================================================
|
|
# TEST 2: SupplierOnboardingDraft defaults
|
|
# ==============================================================================
|
|
print("\n--- TEST 2: Checking SupplierOnboardingDraft defaults ---")
|
|
draft = SupplierOnboardingDraft.objects.create(
|
|
user=user1,
|
|
phone="9876543210"
|
|
)
|
|
assert draft.gi_seller_type == 'non_gi', f"Expected default 'non_gi', got {draft.gi_seller_type}"
|
|
assert draft.is_exclusive_gi is False, f"Expected is_exclusive_gi=False, got {draft.is_exclusive_gi}"
|
|
print("✓ TEST 2 PASSED: SupplierOnboardingDraft defaults to 'non_gi' and False.")
|
|
|
|
# ==============================================================================
|
|
# TEST 3: Serializer serialization of all 5 GI fields
|
|
# ==============================================================================
|
|
print("\n--- TEST 3: Checking SupplierProfileSerializer output ---")
|
|
profile1.gi_seller_type = 'exclusive'
|
|
profile1.is_exclusive_gi = True
|
|
profile1.gi_certificate_number = 'GI-TN-2005-0012'
|
|
profile1.guild_status = 'Kanchipuram Silk Weavers Guild'
|
|
profile1.gi_registry_verified = True
|
|
profile1.save()
|
|
|
|
serialized = SupplierProfileSerializer(profile1).data
|
|
assert serialized['gi_seller_type'] == 'exclusive', f"Serialized error: {serialized.get('gi_seller_type')}"
|
|
assert serialized['is_exclusive_gi'] is True, f"Serialized error: {serialized.get('is_exclusive_gi')}"
|
|
assert serialized['gi_certificate_number'] == 'GI-TN-2005-0012', f"Serialized error: {serialized.get('gi_certificate_number')}"
|
|
assert serialized['guild_status'] == 'Kanchipuram Silk Weavers Guild', f"Serialized error: {serialized.get('guild_status')}"
|
|
assert serialized['gi_registry_verified'] is True, f"Serialized error: {serialized.get('gi_registry_verified')}"
|
|
print("✓ TEST 3 PASSED: Serializer correctly exposes all 5 GI accreditation fields.")
|
|
|
|
# ==============================================================================
|
|
# TEST 4: Simulating CRM Product Approval & Seller GI Auto-Upgrade
|
|
# ==============================================================================
|
|
print("\n--- TEST 4: Verifying CRM Sync Logic ---")
|
|
class MockProduct:
|
|
def __init__(self, id, supplier_id, title, is_gi_tagged, gi_tag_name, geographical_origin, status="approved"):
|
|
self.id = id
|
|
self.supplier_id = supplier_id
|
|
self.title = title
|
|
self.is_gi_tagged = is_gi_tagged
|
|
self.gi_tag_name = gi_tag_name
|
|
self.geographical_origin = geographical_origin
|
|
self.status = status
|
|
self.is_active = (status == "approved")
|
|
|
|
def simulate_crm_sync(seller, products):
|
|
approved_prods = [p for p in products if p.status == "approved" and p.is_active]
|
|
total_approved = len(approved_prods)
|
|
gi_approved = [p for p in approved_prods if p.is_gi_tagged]
|
|
gi_count = len(gi_approved)
|
|
|
|
if gi_count > 0:
|
|
first_gi = gi_approved[0]
|
|
gi_name = first_gi.gi_tag_name or "Govt. of India GI Registry Verified"
|
|
if gi_count == total_approved and total_approved > 0:
|
|
seller.gi_seller_type = "exclusive"
|
|
seller.is_exclusive_gi = True
|
|
else:
|
|
seller.gi_seller_type = "mixed"
|
|
seller.is_exclusive_gi = False
|
|
seller.gi_registry_verified = True
|
|
seller.gi_certificate_number = gi_name
|
|
seller.guild_status = first_gi.geographical_origin or "Certified GI Artisan Maker"
|
|
else:
|
|
seller.gi_seller_type = "non_gi"
|
|
seller.is_exclusive_gi = False
|
|
seller.gi_registry_verified = False
|
|
seller.save()
|
|
|
|
# Scenario 4A: Seller has only standard craft product approved
|
|
prods_non_gi = [
|
|
MockProduct(1, profile1.id, "Terracotta Pot", is_gi_tagged=False, gi_tag_name=None, geographical_origin=None, status="approved")
|
|
]
|
|
simulate_crm_sync(profile1, prods_non_gi)
|
|
profile1.refresh_from_db()
|
|
assert profile1.gi_seller_type == "non_gi"
|
|
assert profile1.is_exclusive_gi is False
|
|
assert profile1.gi_registry_verified is False
|
|
print("✓ TEST 4A PASSED: Standard non-GI product keeps seller as 'non_gi'.")
|
|
|
|
# Scenario 4B: Seller has a GI product approved -> Upgrades to 'exclusive'
|
|
prods_gi_only = [
|
|
MockProduct(2, profile1.id, "Kanchipuram Silk Saree", is_gi_tagged=True, gi_tag_name="GI Tag #26 (1999)", geographical_origin="Kanchipuram, Tamil Nadu", status="approved")
|
|
]
|
|
simulate_crm_sync(profile1, prods_gi_only)
|
|
profile1.refresh_from_db()
|
|
assert profile1.gi_seller_type == "exclusive"
|
|
assert profile1.is_exclusive_gi is True
|
|
assert profile1.gi_registry_verified is True
|
|
assert profile1.gi_certificate_number == "GI Tag #26 (1999)"
|
|
print("✓ TEST 4B PASSED: 100% GI approved products upgrades seller to 'exclusive' with verified GI badge.")
|
|
|
|
# Scenario 4C: Mixed catalog (1 GI product + 1 non-GI product)
|
|
prods_mixed = [
|
|
MockProduct(2, profile1.id, "Kanchipuram Silk Saree", is_gi_tagged=True, gi_tag_name="GI Tag #26 (1999)", geographical_origin="Kanchipuram, Tamil Nadu", status="approved"),
|
|
MockProduct(1, profile1.id, "Terracotta Pot", is_gi_tagged=False, gi_tag_name=None, geographical_origin=None, status="approved")
|
|
]
|
|
simulate_crm_sync(profile1, prods_mixed)
|
|
profile1.refresh_from_db()
|
|
assert profile1.gi_seller_type == "mixed"
|
|
assert profile1.is_exclusive_gi is False
|
|
assert profile1.gi_registry_verified is True
|
|
print("✓ TEST 4C PASSED: Combination of GI and non-GI products categorizes seller as 'mixed'.")
|
|
|
|
# Scenario 4D: Admin rejects the GI product -> Reverts to 'non_gi'
|
|
prods_rejected = [
|
|
MockProduct(2, profile1.id, "Kanchipuram Silk Saree", is_gi_tagged=True, gi_tag_name="GI Tag #26 (1999)", geographical_origin="Kanchipuram, Tamil Nadu", status="rejected"),
|
|
MockProduct(1, profile1.id, "Terracotta Pot", is_gi_tagged=False, gi_tag_name=None, geographical_origin=None, status="approved")
|
|
]
|
|
simulate_crm_sync(profile1, prods_rejected)
|
|
profile1.refresh_from_db()
|
|
assert profile1.gi_seller_type == "non_gi"
|
|
assert profile1.is_exclusive_gi is False
|
|
assert profile1.gi_registry_verified is False
|
|
print("✓ TEST 4D PASSED: Rejection of GI products reverts seller back to 'non_gi'.")
|
|
|
|
print("\n=======================================================")
|
|
print(" ALL 4 TEST SCENARIOS PASSED WITH 100% SUCCESS! ")
|
|
print("=======================================================\n")
|
|
|