|
1 | 1 | # flake8: noqa E501
|
2 | 2 | from dataclasses import dataclass
|
3 | 3 | from enum import Enum
|
4 |
| -from typing import Any, Dict, List, Optional, Union |
| 4 | +from typing import Any, Dict, List, Literal, Optional, Union |
5 | 5 |
|
6 | 6 | import pytest
|
7 | 7 |
|
@@ -422,6 +422,78 @@ def test_decode_union_with_unknown_keys_should_raise_typeerror() -> None:
|
422 | 422 | ) == ComplexItemWithUnionTypeWithSimpleAndComplexTypes(SimpleItem(1, 2))
|
423 | 423 |
|
424 | 424 |
|
| 425 | +@pytest.mark.parametrize( |
| 426 | + ("expr", "type", "expected"), |
| 427 | + [ |
| 428 | + ('{"a":1, "b":2, "c":3}', SimpleItem, SimpleItem(1, 2)), |
| 429 | + ('{"a":1, "b":2, "c":3}', SimpleItemWithOnlyOptionalFields, SimpleItemWithOnlyOptionalFields(1, 2)), |
| 430 | + ('{"a":1}', SimpleItemWithOnlyOptionalFields, SimpleItemWithOnlyOptionalFields(1)), |
| 431 | + ("{}", SimpleItemWithOnlyOptionalFields, SimpleItemWithOnlyOptionalFields()), |
| 432 | + ("{}", SimpleItemWithNoFields, SimpleItemWithNoFields()), |
| 433 | + ('{"a": 1}', SimpleItemWithNoFields, SimpleItemWithNoFields()), |
| 434 | + ('{"a":1, "b":2, "c": 3}', (SimpleItemWithNoFields, SimpleItem), SimpleItem(1, 2)), |
| 435 | + ], |
| 436 | +) |
| 437 | +def test_decode_non_strict_should_work(expr: Any, type: Any, expected: str) -> None: |
| 438 | + assert from_json(expr, type) == expected |
| 439 | + |
| 440 | + |
| 441 | +@pytest.mark.parametrize( |
| 442 | + ("expr", "type", "expected"), |
| 443 | + [ |
| 444 | + ('{"a":1, "b":2}', SimpleItem, SimpleItem(1, 2)), |
| 445 | + ('{"a":1, "b":2}', SimpleItemWithOnlyOptionalFields, SimpleItemWithOnlyOptionalFields(1, 2)), |
| 446 | + ('{"a":1}', SimpleItemWithOnlyOptionalFields, SimpleItemWithOnlyOptionalFields(1)), |
| 447 | + ("{}", SimpleItemWithOnlyOptionalFields, SimpleItemWithOnlyOptionalFields()), |
| 448 | + ("{}", SimpleItemWithNoFields, SimpleItemWithNoFields()), |
| 449 | + ("{}", (SimpleItemWithNoFields, SimpleItem), SimpleItemWithNoFields()), |
| 450 | + ('{"a":1, "b":2}', (SimpleItemWithNoFields, SimpleItem), SimpleItem(1, 2)), |
| 451 | + ], |
| 452 | +) |
| 453 | +def test_decode_strict_should_work(expr: Any, type: Any, expected: str) -> None: |
| 454 | + assert from_json(expr, type, strict=True) == expected |
| 455 | + |
| 456 | + |
| 457 | +@pytest.mark.parametrize( |
| 458 | + ("expr", "type"), |
| 459 | + [ |
| 460 | + ('{"a":1, "b": 2, "c": 3}', SimpleItem), |
| 461 | + ('{"a":1, "b": 2, "c": 3}', SimpleItemWithOnlyOptionalFields), |
| 462 | + ('{"a":1, "c": 3}', SimpleItemWithOnlyOptionalFields), |
| 463 | + ('{"c": 3}', SimpleItemWithOnlyOptionalFields), |
| 464 | + ('{"c": 3}', SimpleItemWithNoFields), |
| 465 | + ], |
| 466 | +) |
| 467 | +def test_decode_strict_with_invalid_data_should_raise_typeerror(expr: Any, type: Any) -> None: |
| 468 | + with pytest.raises(TypeError): |
| 469 | + from_json(expr, type, strict=True) |
| 470 | + |
| 471 | + |
| 472 | +@pytest.mark.parametrize( |
| 473 | + ("expr", "type", "expected"), |
| 474 | + [ |
| 475 | + ('"test"', Literal["test", "blah", "bluff"], "test"), |
| 476 | + ('"bluff"', Literal["test", "blah", "bluff"], "bluff"), |
| 477 | + ('"dada"', (Literal["test", "blah", "bluff"], str), "dada"), |
| 478 | + ("1", (Literal["test", "blah", "bluff"], int), 1), |
| 479 | + ], |
| 480 | +) |
| 481 | +def test_literal_should_work(expr: Any, type: Any, expected: str) -> None: |
| 482 | + assert from_json(expr, type) == expected |
| 483 | + |
| 484 | + |
| 485 | +@pytest.mark.parametrize( |
| 486 | + ("expr", "type"), |
| 487 | + [ |
| 488 | + ('"dada"', Literal["test", "blah", "bluff"]), |
| 489 | + ('"dada"', (Literal["test", "blah", "bluff"], int)), |
| 490 | + ], |
| 491 | +) |
| 492 | +def test_literal_with_invalid_args_should_raise_typerror(expr: Any, type: Any) -> None: |
| 493 | + with pytest.raises(TypeError): |
| 494 | + from_json(expr, type) |
| 495 | + |
| 496 | + |
425 | 497 | def test_really_complex_data() -> None:
|
426 | 498 | data = """\
|
427 | 499 | {
|
|
0 commit comments