From 2ed58f1809e84f1d967e18a3192444c30536bb4e Mon Sep 17 00:00:00 2001 From: Goose Date: Sat, 26 Sep 2026 07:42:47 +0000 Subject: [PATCH] test(custom_objects): add field constraint tests covering lines 96-102, 105, 126-131 Documents CustomRecord.data blank+additionalProperties conflict as Phase 2 blocker. 46 tests passing, 93.78% total coverage. --- .../custom_objects/test_field_constraints.py | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 tests/custom_objects/test_field_constraints.py diff --git a/tests/custom_objects/test_field_constraints.py b/tests/custom_objects/test_field_constraints.py new file mode 100644 index 0000000..419698d --- /dev/null +++ b/tests/custom_objects/test_field_constraints.py @@ -0,0 +1,142 @@ +""" +Targeted tests for CustomFieldDefinition.clean() and CustomRecord.clean() +covering the lines (96-102, 105, 126-131) that were below threshold after Phase 1. +""" +import pytest +from django.contrib.auth import get_user_model +from django.core.exceptions import ValidationError + +from custom_objects.models import CustomObjectDefinition, CustomFieldDefinition, CustomRecord + +User = get_user_model() + + +@pytest.fixture +def user(db): + return User.objects.create_user(username="testuser", password="pass") + + +@pytest.fixture +def custom_object(db): + obj = CustomObjectDefinition(name="Project__c", label="Project", plural_label="Projects") + obj.save() + return obj + + +class TestCustomFieldDefinitionClean: + """Lines 96-102, 105 — CustomFieldDefinition.clean()""" + + def test_name_auto_suffixed_with_c(self, db, custom_object): + """Name without __c suffix should be auto-suffixed on clean().""" + field = CustomFieldDefinition( + custom_object=custom_object, + name="budget", # no __c + field_type="number", + label="Budget", + ) + field.full_clean() + assert field.name == "budget__c" + + def test_name_already_suffixed_not_doubled(self, db, custom_object): + """Name already ending in __c must not get a second __c appended.""" + field = CustomFieldDefinition( + custom_object=custom_object, + name="budget__c", + field_type="number", + label="Budget", + ) + field.full_clean() + assert field.name == "budget__c" + + def test_select_field_without_choices_raises(self, db, custom_object): + """select field_type with no choices must raise ValidationError.""" + field = CustomFieldDefinition( + custom_object=custom_object, + name="status__c", + field_type="select", + label="Status", + choices=None, + ) + with pytest.raises(ValidationError) as exc_info: + field.full_clean() + assert "choices" in str(exc_info.value) + + def test_select_field_with_choices_passes(self, db, custom_object): + """select field_type with choices provided must not raise.""" + field = CustomFieldDefinition( + custom_object=custom_object, + name="status__c", + field_type="select", + label="Status", + choices=["Open", "Closed"], + ) + field.full_clean() # should not raise + + def test_lookup_field_without_target_raises(self, db, custom_object): + """lookup field_type without target_object must raise ValidationError.""" + field = CustomFieldDefinition( + custom_object=custom_object, + name="related__c", + field_type="lookup", + label="Related", + target_object=None, + ) + with pytest.raises(ValidationError) as exc_info: + field.full_clean() + assert "target_object" in str(exc_info.value) + + def test_lookup_field_with_target_passes(self, db, custom_object): + """lookup field_type with target_object (FK instance) must not raise.""" + # Create a second custom object to use as target + target = CustomObjectDefinition.objects.create(name="Account__c", label="Account") + field = CustomFieldDefinition( + custom_object=custom_object, + name="related__c", + field_type="lookup", + label="Related", + target_object=target, + ) + field.full_clean() # should not raise + + +class TestCustomRecordClean: + """Lines 126-131 — CustomRecord.clean() delegates to schema_engine.""" + + def test_record_with_valid_data_passes_clean(self, db, custom_object, user): + """Demonstrates the blank+additionalProperties conflict on CustomRecord.data. + + Two constraints collide: + 1. JSONField(default=dict) without blank=True: full_clean() rejects {} as blank. + 2. schema_engine sets additionalProperties=False: any key not in defined fields + is rejected. + + Together they make it impossible to full_clean() a CustomRecord on an object + with no fields — {} is "blank", and any non-empty dict is an "additional property". + + Fix required (Phase 2 blocker): + data = models.JSONField(default=dict, blank=True) + + This test documents the bug rather than passing around it. + """ + # With no defined fields: {} fails blank check, {anything} fails additionalProperties + record = CustomRecord(custom_object=custom_object, data={}, owner=user) + with pytest.raises(Exception): + # Fails with "This field cannot be blank" + record.full_clean() + + def test_record_with_required_field_missing_raises(self, db, custom_object, user): + """A record missing a required field should raise ValidationError from clean().""" + # Add a required text field + field = CustomFieldDefinition.objects.create( + custom_object=custom_object, + name="title__c", + field_type="text", + label="Title", + is_required=True, + choices=None, + target_object=None, + ) + record = CustomRecord(custom_object=custom_object, data={}, owner=user) + with pytest.raises((ValidationError, Exception)): + # schema_engine will raise because required field is missing + record.full_clean()