|
23 | 23 | get_column_display_order, |
24 | 24 | ) |
25 | 25 | from data_designer.config.errors import InvalidConfigError |
26 | | -from data_designer.config.sampler_params import SamplerType, UUIDSamplerParams |
| 26 | +from data_designer.config.sampler_params import ( |
| 27 | + CategorySamplerParams, |
| 28 | + GaussianSamplerParams, |
| 29 | + PersonFromFakerSamplerParams, |
| 30 | + PersonSamplerParams, |
| 31 | + SamplerType, |
| 32 | + UniformSamplerParams, |
| 33 | + UUIDSamplerParams, |
| 34 | +) |
27 | 35 | from data_designer.config.utils.code_lang import CodeLang |
28 | 36 | from data_designer.config.utils.errors import UserJinjaTemplateSyntaxError |
29 | 37 | from data_designer.config.validator_params import CodeValidatorParams |
@@ -324,3 +332,114 @@ def test_get_column_config_from_kwargs(): |
324 | 332 | ), |
325 | 333 | SeedDatasetColumnConfig, |
326 | 334 | ) |
| 335 | + |
| 336 | + |
| 337 | +def test_sampler_column_config_discriminated_union_with_dict_params(): |
| 338 | + """Test that sampler_type field is automatically injected into params dict.""" |
| 339 | + config = SamplerColumnConfig( |
| 340 | + name="test_uniform", |
| 341 | + sampler_type=SamplerType.UNIFORM, |
| 342 | + params={"low": 0.0, "high": 1.0, "decimal_places": 2}, |
| 343 | + ) |
| 344 | + assert config.name == "test_uniform" |
| 345 | + assert config.sampler_type == SamplerType.UNIFORM |
| 346 | + assert isinstance(config.params, UniformSamplerParams) |
| 347 | + assert config.params.sampler_type == SamplerType.UNIFORM |
| 348 | + assert config.params.low == 0.0 |
| 349 | + assert config.params.high == 1.0 |
| 350 | + assert config.params.decimal_places == 2 |
| 351 | + |
| 352 | + |
| 353 | +def test_sampler_column_config_discriminated_union_with_explicit_sampler_type(): |
| 354 | + """Test that explicit sampler_type in params dict is preserved.""" |
| 355 | + config = SamplerColumnConfig( |
| 356 | + name="test_category", |
| 357 | + sampler_type=SamplerType.CATEGORY, |
| 358 | + params={"sampler_type": "category", "values": ["A", "B", "C"], "weights": [0.5, 0.3, 0.2]}, |
| 359 | + ) |
| 360 | + assert config.name == "test_category" |
| 361 | + assert config.sampler_type == SamplerType.CATEGORY |
| 362 | + assert isinstance(config.params, CategorySamplerParams) |
| 363 | + assert config.params.sampler_type == SamplerType.CATEGORY |
| 364 | + assert config.params.values == ["A", "B", "C"] |
| 365 | + |
| 366 | + |
| 367 | +def test_sampler_column_config_discriminated_union_serialization(): |
| 368 | + """Test that discriminated union works correctly with serialization/deserialization.""" |
| 369 | + config = SamplerColumnConfig( |
| 370 | + name="test_person", |
| 371 | + sampler_type=SamplerType.PERSON, |
| 372 | + params={"locale": "en_US", "sex": "Female", "age_range": [25, 45]}, |
| 373 | + ) |
| 374 | + |
| 375 | + # Serialize |
| 376 | + serialized = config.model_dump() |
| 377 | + assert "sampler_type" in serialized["params"] |
| 378 | + assert serialized["params"]["sampler_type"] == "person" |
| 379 | + |
| 380 | + # Deserialize |
| 381 | + deserialized = SamplerColumnConfig(**serialized) |
| 382 | + assert isinstance(deserialized.params, PersonSamplerParams) |
| 383 | + assert deserialized.params.locale == "en_US" |
| 384 | + assert deserialized.params.sex == "Female" |
| 385 | + assert deserialized.params.age_range == [25, 45] |
| 386 | + |
| 387 | + |
| 388 | +def test_sampler_column_config_discriminated_union_person_vs_person_from_faker(): |
| 389 | + """Test that discriminated union correctly distinguishes between person and person_from_faker.""" |
| 390 | + # Test person sampler (managed datasets) |
| 391 | + person_config = SamplerColumnConfig( |
| 392 | + name="test_person", |
| 393 | + sampler_type=SamplerType.PERSON, |
| 394 | + params={"locale": "en_US", "sex": "Male", "age_range": [30, 50]}, |
| 395 | + ) |
| 396 | + assert isinstance(person_config.params, PersonSamplerParams) |
| 397 | + assert person_config.params.sampler_type == SamplerType.PERSON |
| 398 | + assert person_config.params.locale == "en_US" |
| 399 | + |
| 400 | + # Test person_from_faker sampler (Faker-based) |
| 401 | + person_faker_config = SamplerColumnConfig( |
| 402 | + name="test_person_faker", |
| 403 | + sampler_type=SamplerType.PERSON_FROM_FAKER, |
| 404 | + params={"locale": "en_GB", "sex": "Female", "age_range": [20, 40]}, |
| 405 | + ) |
| 406 | + assert isinstance(person_faker_config.params, PersonFromFakerSamplerParams) |
| 407 | + assert person_faker_config.params.sampler_type == SamplerType.PERSON_FROM_FAKER |
| 408 | + assert person_faker_config.params.locale == "en_GB" |
| 409 | + |
| 410 | + # Verify they are different types |
| 411 | + assert type(person_config.params) != type(person_faker_config.params) |
| 412 | + assert isinstance(person_config.params, PersonSamplerParams) |
| 413 | + assert isinstance(person_faker_config.params, PersonFromFakerSamplerParams) |
| 414 | + |
| 415 | + |
| 416 | +def test_sampler_column_config_discriminated_union_with_conditional_params(): |
| 417 | + """Test that sampler_type is injected into conditional_params as well.""" |
| 418 | + config = SamplerColumnConfig( |
| 419 | + name="test_gaussian", |
| 420 | + sampler_type=SamplerType.GAUSSIAN, |
| 421 | + params={"mean": 0.0, "stddev": 1.0}, |
| 422 | + conditional_params={"age > 21": {"mean": 5.0, "stddev": 2.0}}, |
| 423 | + ) |
| 424 | + |
| 425 | + assert isinstance(config.params, GaussianSamplerParams) |
| 426 | + assert config.params.mean == 0.0 |
| 427 | + assert config.params.stddev == 1.0 |
| 428 | + |
| 429 | + # Check conditional params |
| 430 | + assert "age > 21" in config.conditional_params |
| 431 | + cond_param = config.conditional_params["age > 21"] |
| 432 | + assert isinstance(cond_param, GaussianSamplerParams) |
| 433 | + assert cond_param.sampler_type == SamplerType.GAUSSIAN |
| 434 | + assert cond_param.mean == 5.0 |
| 435 | + assert cond_param.stddev == 2.0 |
| 436 | + |
| 437 | + |
| 438 | +def test_sampler_column_config_discriminated_union_wrong_params_type(): |
| 439 | + """Test that discriminated union rejects params that don't match the sampler_type.""" |
| 440 | + with pytest.raises(ValidationError): |
| 441 | + SamplerColumnConfig( |
| 442 | + name="test_wrong_params", |
| 443 | + sampler_type=SamplerType.UNIFORM, |
| 444 | + params={"values": ["A", "B"]}, # Category params for uniform sampler |
| 445 | + ) |
0 commit comments