|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Licensed under the terms of the BSD 3-Clause |
| 4 | +# (see guidata/LICENSE for details) |
| 5 | + |
| 6 | +""" |
| 7 | +Test None default values with allow_none=False |
| 8 | +
|
| 9 | +This test verifies that DataItem objects can have None as default values |
| 10 | +even when allow_none=False is set, while still preventing users from |
| 11 | +setting None values at runtime. |
| 12 | +""" |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +from guidata.config import ValidationMode, set_validation_mode |
| 17 | +from guidata.dataset import DataSet |
| 18 | +from guidata.dataset.dataitems import StringItem |
| 19 | +from guidata.dataset.datatypes import DataItemValidationError |
| 20 | + |
| 21 | + |
| 22 | +class _TestDataSet(DataSet): |
| 23 | + """Test dataset for None default values""" |
| 24 | + |
| 25 | + name = StringItem("Name", default=None, allow_none=False) |
| 26 | + description = StringItem("Description", default=None, allow_none=True) |
| 27 | + title = StringItem("Title", default="Default Title", allow_none=False) |
| 28 | + |
| 29 | + |
| 30 | +def test_none_defaults_creation(): |
| 31 | + """Test that datasets can be created with None defaults even when |
| 32 | + allow_none=False""" |
| 33 | + # Set validation to strict mode to catch any issues |
| 34 | + set_validation_mode(ValidationMode.STRICT) |
| 35 | + |
| 36 | + # This should work: None default is allowed even when allow_none=False |
| 37 | + dataset = _TestDataSet() |
| 38 | + |
| 39 | + # Verify the default values are set correctly |
| 40 | + assert dataset.name is None |
| 41 | + assert dataset.description is None |
| 42 | + assert dataset.title == "Default Title" |
| 43 | + |
| 44 | + |
| 45 | +def test_allow_none_true_accepts_none(): |
| 46 | + """Test that items with allow_none=True accept None values at runtime""" |
| 47 | + set_validation_mode(ValidationMode.STRICT) |
| 48 | + dataset = _TestDataSet() |
| 49 | + |
| 50 | + # This should work: allow_none=True |
| 51 | + dataset.description = None |
| 52 | + assert dataset.description is None |
| 53 | + |
| 54 | + # Should also accept valid string values |
| 55 | + dataset.description = "Test Description" |
| 56 | + assert dataset.description == "Test Description" |
| 57 | + |
| 58 | + |
| 59 | +def test_allow_none_false_rejects_none(): |
| 60 | + """Test that items with allow_none=False reject None values at runtime""" |
| 61 | + set_validation_mode(ValidationMode.STRICT) |
| 62 | + dataset = _TestDataSet() |
| 63 | + |
| 64 | + # This should fail: allow_none=False |
| 65 | + with pytest.raises(DataItemValidationError): |
| 66 | + dataset.name = None |
| 67 | + |
| 68 | + |
| 69 | +def test_allow_none_false_accepts_valid_values(): |
| 70 | + """Test that items with allow_none=False accept valid non-None values""" |
| 71 | + set_validation_mode(ValidationMode.STRICT) |
| 72 | + dataset = _TestDataSet() |
| 73 | + |
| 74 | + # This should work: valid string value |
| 75 | + dataset.name = "Test Name" |
| 76 | + assert dataset.name == "Test Name" |
| 77 | + |
| 78 | + # Test changing values |
| 79 | + dataset.name = "Another Name" |
| 80 | + assert dataset.name == "Another Name" |
| 81 | + |
| 82 | + |
| 83 | +def test_validation_mode_disabled(): |
| 84 | + """Test that validation is skipped when validation mode is disabled""" |
| 85 | + set_validation_mode(ValidationMode.DISABLED) |
| 86 | + dataset = _TestDataSet() |
| 87 | + |
| 88 | + # Even with allow_none=False, None should be accepted in disabled mode |
| 89 | + dataset.name = None |
| 90 | + assert dataset.name is None |
| 91 | + |
| 92 | + |
| 93 | +def test_validation_mode_enabled_warnings(): |
| 94 | + """Test that warnings are shown instead of exceptions in enabled mode""" |
| 95 | + set_validation_mode(ValidationMode.ENABLED) |
| 96 | + dataset = _TestDataSet() |
| 97 | + |
| 98 | + # Should show warnings but not raise exceptions |
| 99 | + with pytest.warns(UserWarning): |
| 100 | + dataset.name = None |
| 101 | + assert dataset.name is None |
| 102 | + |
| 103 | + |
| 104 | +@pytest.fixture(autouse=True) |
| 105 | +def reset_validation_mode(): |
| 106 | + """Reset validation mode after each test""" |
| 107 | + yield |
| 108 | + set_validation_mode(ValidationMode.DISABLED) # Reset to default |
0 commit comments