|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | +from hypothesis import given |
| 5 | +from hypothesis import strategies as st |
| 6 | + |
| 7 | +from variantlib.constants import VALIDATION_FEATURE_REGEX |
| 8 | +from variantlib.constants import VALIDATION_NAMESPACE_REGEX |
| 9 | +from variantlib.constants import VALIDATION_VALUE_REGEX |
| 10 | +from variantlib.models.configuration import VariantConfiguration |
| 11 | +from variantlib.models.variant import VariantFeature |
| 12 | +from variantlib.models.variant import VariantProperty |
| 13 | + |
| 14 | + |
| 15 | +def test_default_configuration(): |
| 16 | + config = VariantConfiguration.default() |
| 17 | + assert config.namespaces_priority == [] |
| 18 | + assert config.features_priority == [] |
| 19 | + assert config.property_priority == [] |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.parametrize( |
| 23 | + "config_params", |
| 24 | + [ |
| 25 | + { |
| 26 | + "namespaces": ["OmniCorp"], |
| 27 | + "features": [], |
| 28 | + "properties": [], |
| 29 | + }, |
| 30 | + { |
| 31 | + "namespaces": ["OmniCorp"], |
| 32 | + "features": ["OmniCorp::custom_feat"], |
| 33 | + "properties": ["OmniCorp::custom_feat::secret_value"], |
| 34 | + }, |
| 35 | + { |
| 36 | + "namespaces": ["OmniCorp", "AcmeCorp"], |
| 37 | + "features": ["OmniCorp::custom_feat", "AcmeCorp :: custom_feat"], |
| 38 | + "properties": [ |
| 39 | + "OmniCorp :: custom_featA :: secret_value", |
| 40 | + "OmniCorp :: custom_featB:: secret_value", |
| 41 | + "AcmeCorp::custom_feat::secret_value", |
| 42 | + ], |
| 43 | + }, |
| 44 | + ], |
| 45 | +) |
| 46 | +def test_from_toml_config(config_params: dict[str, list[str]]): |
| 47 | + _ = VariantConfiguration.from_toml_config( |
| 48 | + namespaces_priority=config_params["namespaces"], |
| 49 | + features_priority=config_params["features"], |
| 50 | + property_priority=config_params["properties"], |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +@given(st.lists(st.from_regex(VALIDATION_NAMESPACE_REGEX))) |
| 55 | +def test_namespaces_priority_validation(namespaces: list[str]): |
| 56 | + config = VariantConfiguration(namespaces_priority=namespaces) |
| 57 | + assert config.namespaces_priority == namespaces |
| 58 | + |
| 59 | + |
| 60 | +@given( |
| 61 | + st.lists(st.from_regex(VALIDATION_NAMESPACE_REGEX)), |
| 62 | + st.lists( |
| 63 | + st.builds( |
| 64 | + VariantFeature, |
| 65 | + namespace=st.just("OmniCorp"), |
| 66 | + feature=st.from_regex(VALIDATION_FEATURE_REGEX), |
| 67 | + ) |
| 68 | + ), |
| 69 | +) |
| 70 | +def test_features_priority_validation( |
| 71 | + namespaces: list[str], features: list[VariantFeature] |
| 72 | +): |
| 73 | + config = VariantConfiguration( |
| 74 | + namespaces_priority=namespaces, features_priority=features |
| 75 | + ) |
| 76 | + assert config.features_priority == features |
| 77 | + |
| 78 | + |
| 79 | +@given( |
| 80 | + st.lists(st.from_regex(VALIDATION_NAMESPACE_REGEX)), |
| 81 | + st.lists( |
| 82 | + st.builds( |
| 83 | + VariantFeature, |
| 84 | + namespace=st.from_regex(VALIDATION_NAMESPACE_REGEX), |
| 85 | + feature=st.from_regex(VALIDATION_FEATURE_REGEX), |
| 86 | + ) |
| 87 | + ), |
| 88 | + st.lists( |
| 89 | + st.builds( |
| 90 | + VariantProperty, |
| 91 | + namespace=st.from_regex(VALIDATION_NAMESPACE_REGEX), |
| 92 | + feature=st.from_regex(VALIDATION_FEATURE_REGEX), |
| 93 | + value=st.from_regex(VALIDATION_VALUE_REGEX), |
| 94 | + ) |
| 95 | + ), |
| 96 | +) |
| 97 | +def test_property_priority_validation( |
| 98 | + namespaces: list[str], |
| 99 | + features: list[VariantFeature], |
| 100 | + properties: list[VariantProperty], |
| 101 | +): |
| 102 | + config = VariantConfiguration( |
| 103 | + namespaces_priority=namespaces, |
| 104 | + features_priority=features, |
| 105 | + property_priority=properties, |
| 106 | + ) |
| 107 | + assert config.property_priority == properties |
0 commit comments