From 0d485905246f3c166faa0196180931e21191d586 Mon Sep 17 00:00:00 2001 From: iscai-msft <43154838+iscai-msft@users.noreply.github.com> Date: Tue, 16 Apr 2024 20:59:52 -0400 Subject: [PATCH] Update model tests (#2528) --- .../test_model_base_serialization.py | 30 +++++++++++++++++++ .../unittests/test_model_init.py | 26 ---------------- 2 files changed, 30 insertions(+), 26 deletions(-) delete mode 100644 packages/typespec-python/test/generic_mock_api_tests/unittests/test_model_init.py diff --git a/packages/typespec-python/test/generic_mock_api_tests/unittests/test_model_base_serialization.py b/packages/typespec-python/test/generic_mock_api_tests/unittests/test_model_base_serialization.py index b30bfe3af2c..20f1a8dccf1 100644 --- a/packages/typespec-python/test/generic_mock_api_tests/unittests/test_model_base_serialization.py +++ b/packages/typespec-python/test/generic_mock_api_tests/unittests/test_model_base_serialization.py @@ -6,6 +6,7 @@ import decimal import json import datetime +from pathlib import Path from typing import Any, Iterable, List, Literal, Dict, Mapping, Sequence, Set, Tuple, Optional, overload, Union import pytest import isodate @@ -3968,3 +3969,32 @@ class ModelWithEnumProperty(Model): model = ModelWithEnumProperty({"enumProperty": MyEnum.A}) assert model.enum_property == MyEnum.A assert model["enumProperty"] == "a" + +def test_not_mutating_original_dict(): + class MyInnerModel(Model): + property: str = rest_field() + + class MyModel(Model): + property: MyInnerModel = rest_field() + + origin = {"property": {"property": "hello"}} + + dpg_model = MyModel(origin) + assert dpg_model["property"]["property"] == "hello" + + origin["property"]["property"] = "world" + assert dpg_model["property"]["property"] == "hello" + +def test_model_init_io(): + class BytesModel(Model): + property: bytes = rest_field() + + JPG = Path(__file__).parent.parent / "data/image.jpg" + with open(JPG, "rb") as f: + b = BytesModel({"property": f}) + assert b.property == f + assert b["property"] == f + with open(JPG, "rb") as f: + b = BytesModel(property=f) + assert b.property == f + assert b["property"] == f diff --git a/packages/typespec-python/test/generic_mock_api_tests/unittests/test_model_init.py b/packages/typespec-python/test/generic_mock_api_tests/unittests/test_model_init.py deleted file mode 100644 index bc2b17cfbf5..00000000000 --- a/packages/typespec-python/test/generic_mock_api_tests/unittests/test_model_init.py +++ /dev/null @@ -1,26 +0,0 @@ -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ -from typetest.property.valuetypes.models import ModelProperty, InnerModel, BytesProperty -import io -from pathlib import Path - -JPG = Path(__file__).parent.parent / "data/image.jpg" - -def test_model_init_remove_deepcopy(): - origin = {"property": InnerModel(property="hello")} - assert origin["property"].property == "hello" - - dpg_model = ModelProperty(origin) - assert origin["property"]["property"] == "hello" - assert dpg_model["property"]["property"] == "hello" - - origin["property"]["property"] = "world" - # After remove deepcopy for model init, value change of original input will also influence the model - assert dpg_model["property"]["property"] == "world" - -def test_model_init_io(): - # it is OK to use io when model init after remove deepcopy - with open(JPG, "rb") as file: - BytesProperty(property=file)