from django.test import TestCase from django.db import connection from rest_framework.exceptions import ValidationError as DRFValidationError from django.core.exceptions import ValidationError as DjangoValidationError ValidationError = (DjangoValidationError, DRFValidationError) from django.contrib.auth import get_user_model from rest_framework.test import APITestCase from rest_framework import status from custom_objects.models import CustomObjectDefinition, CustomFieldDefinition, CustomRecord from custom_objects.schema_engine import build_schema_for_object, validate_record_data User = get_user_model() class TestCustomObjectMetamodel(TestCase): def setUp(self): self.user = User.objects.create_user('co_user', 'co@test.com', 'pass') def test_custom_object_slug_must_end_with_c(self): co = CustomObjectDefinition(name='deal__c', label='Deal', created_by=self.user) co.full_clean() co.save() self.assertEqual(co.name, 'deal__c') def test_prevent_reserved_system_names(self): co = CustomObjectDefinition(name='account__c', label='Account', created_by=self.user) with self.assertRaises(ValidationError): co.full_clean() def test_gin_index_exists_on_custom_record(self): with connection.cursor() as cursor: cursor.execute(""" SELECT indexname FROM pg_indexes WHERE tablename = 'custom_record' AND indexname = 'idx_custom_record_data_gin'; """) row = cursor.fetchone() self.assertIsNotNone(row, "GIN index idx_custom_record_data_gin should exist in PostgreSQL") class TestCustomSchemaEngine(TestCase): def setUp(self): self.user = User.objects.create_user('engine_user', 'eng@test.com', 'pass') self.obj = CustomObjectDefinition.objects.create(name='invoice__c', label='Invoice', created_by=self.user) CustomFieldDefinition.objects.create( custom_object=self.obj, name='amount__c', label='Amount', field_type='number', is_required=True ) CustomFieldDefinition.objects.create( custom_object=self.obj, name='status__c', label='Status', field_type='picklist', is_required=False, options=['Draft', 'Sent', 'Paid'] ) def test_schema_validates_valid_payload(self): data = {'amount__c': 1500.50, 'status__c': 'Paid'} self.assertTrue(validate_record_data(self.obj, data)) def test_schema_rejects_missing_required(self): data = {'status__c': 'Paid'} with self.assertRaises(ValidationError): validate_record_data(self.obj, data) def test_schema_rejects_invalid_picklist_choice(self): data = {'amount__c': 100, 'status__c': 'Unknown'} with self.assertRaises(ValidationError): validate_record_data(self.obj, data) def test_schema_rejects_unknown_properties(self): data = {'amount__c': 100, 'injected_key': 'malicious'} with self.assertRaises(ValidationError): validate_record_data(self.obj, data) class TestDynamicCustomObjectAPI(APITestCase): def setUp(self): self.user = User.objects.create_user('api_custom_user', 'api_c@test.com', 'pass') self.client.force_authenticate(user=self.user) self.obj = CustomObjectDefinition.objects.create(name='project__c', label='Project', created_by=self.user) CustomFieldDefinition.objects.create( custom_object=self.obj, name='title__c', label='Title', field_type='text', is_required=True ) def test_create_and_fetch_custom_record(self): resp = self.client.post( f'/api/custom/{self.obj.name}/', {'data': {'title__c': 'Apollo Mission'}}, format='json' ) self.assertEqual(resp.status_code, status.HTTP_201_CREATED) rec_id = resp.data['id'] resp = self.client.get(f'/api/custom/{self.obj.name}/{rec_id}/') self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(resp.data['data']['title__c'], 'Apollo Mission')