|
8 | 8 | from decimal import Decimal
|
9 | 9 | from enum import Enum
|
10 | 10 | from typing import Any, Dict, FrozenSet, List, Optional, Set, Union
|
| 11 | +from uuid import UUID |
11 | 12 |
|
12 | 13 | import pytest
|
13 | 14 | from dirty_equals import IsStr
|
@@ -693,6 +694,57 @@ class PydanticModel(BaseModel):
|
693 | 694 | benchmark(PydanticModel.parse_obj, some_bytes)
|
694 | 695 |
|
695 | 696 |
|
| 697 | +class TestBenchmarkUuid: |
| 698 | + @pytest.fixture(scope='class') |
| 699 | + def pydantic_model(self): |
| 700 | + class PydanticModel(BaseModel): |
| 701 | + uuid: UUID |
| 702 | + |
| 703 | + return PydanticModel |
| 704 | + |
| 705 | + @pytest.fixture(scope='class') |
| 706 | + def core_validator(self): |
| 707 | + class CoreModel: |
| 708 | + __slots__ = '__dict__', '__pydantic_extra__', '__pydantic_fields_set__' |
| 709 | + |
| 710 | + return SchemaValidator( |
| 711 | + { |
| 712 | + 'type': 'model', |
| 713 | + 'cls': CoreModel, |
| 714 | + 'schema': { |
| 715 | + 'type': 'model-fields', |
| 716 | + 'fields': {'uuid': {'type': 'model-field', 'schema': {'type': 'uuid'}}}, |
| 717 | + }, |
| 718 | + } |
| 719 | + ) |
| 720 | + |
| 721 | + @pytest.fixture(scope='class') |
| 722 | + def uuid_str(self): |
| 723 | + return '12345678-1234-5678-1234-567812345678' |
| 724 | + |
| 725 | + @pytest.fixture(scope='class') |
| 726 | + def json_dict_data(self, uuid_str): |
| 727 | + return json.dumps({'uuid': uuid_str}) |
| 728 | + |
| 729 | + @skip_pydantic |
| 730 | + @pytest.mark.benchmark(group='uuid model - JSON') |
| 731 | + def test_model_pyd_json(self, pydantic_model, benchmark, json_dict_data): |
| 732 | + @benchmark |
| 733 | + def pydantic_json(): |
| 734 | + obj = json.loads(json_dict_data) |
| 735 | + return pydantic_model.parse_obj(obj) |
| 736 | + |
| 737 | + @pytest.mark.benchmark(group='uuid model - JSON') |
| 738 | + def test_model_core_json(self, core_validator, benchmark, json_dict_data): |
| 739 | + benchmark(core_validator.validate_json, json_dict_data) |
| 740 | + |
| 741 | + @pytest.mark.benchmark(group='uuid str') |
| 742 | + def test_core_str(self, benchmark, uuid_str): |
| 743 | + v = SchemaValidator({'type': 'uuid'}) |
| 744 | + |
| 745 | + benchmark(v.validate_python, uuid_str) |
| 746 | + |
| 747 | + |
696 | 748 | class TestBenchmarkDateTime:
|
697 | 749 | @pytest.fixture(scope='class')
|
698 | 750 | def pydantic_model(self):
|
|
0 commit comments