From c3b3037cb03f9d264246a3fd57b0a38b66b1e1b3 Mon Sep 17 00:00:00 2001 From: Stig Ofstad Date: Fri, 10 Nov 2023 12:16:48 +0100 Subject: [PATCH] fix: CreateEntity will now correctly use default, even on optional --- poetry.lock | 53 ++++++++++++++++++- pyproject.toml | 1 + .../create_entity.py | 2 +- src/home/system/SIMOS/Blueprint.json | 6 ++- .../bdd/entity/instantiate_entity.feature | 7 ++- .../test_create_entity.py | 34 ++++++++++++ 6 files changed, 97 insertions(+), 6 deletions(-) diff --git a/poetry.lock b/poetry.lock index 2adf185cb..b18c4f6f0 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.5.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand. [[package]] name = "aiofiles" @@ -31,6 +31,24 @@ doc = ["Sphinx", "packaging", "sphinx-autodoc-typehints (>=1.2.0)", "sphinx-rtd- test = ["anyio[trio]", "coverage[toml] (>=4.5)", "hypothesis (>=4.0)", "mock (>=4)", "psutil (>=5.9)", "pytest (>=7.0)", "pytest-mock (>=3.6.1)", "trustme", "uvloop (>=0.17)"] trio = ["trio (<0.22)"] +[[package]] +name = "asttokens" +version = "2.4.1" +description = "Annotate AST trees with source code positions" +optional = false +python-versions = "*" +files = [ + {file = "asttokens-2.4.1-py2.py3-none-any.whl", hash = "sha256:051ed49c3dcae8913ea7cd08e46a606dba30b79993209636c4875bc1d637bc24"}, + {file = "asttokens-2.4.1.tar.gz", hash = "sha256:b03869718ba9a6eb027e134bfdf69f38a236d681c83c160d510768af11254ba0"}, +] + +[package.dependencies] +six = ">=1.12.0" + +[package.extras] +astroid = ["astroid (>=1,<2)", "astroid (>=2,<4)"] +test = ["astroid (>=1,<2)", "astroid (>=2,<4)", "pytest"] + [[package]] name = "azure-core" version = "1.29.5" @@ -397,6 +415,20 @@ six = ">=1.9.0" gmpy = ["gmpy"] gmpy2 = ["gmpy2"] +[[package]] +name = "executing" +version = "2.0.1" +description = "Get the currently executing AST node of a frame, and other information" +optional = false +python-versions = ">=3.5" +files = [ + {file = "executing-2.0.1-py2.py3-none-any.whl", hash = "sha256:eac49ca94516ccc753f9fb5ce82603156e590b27525a8bc32cce8ae302eb61bc"}, + {file = "executing-2.0.1.tar.gz", hash = "sha256:35afe2ce3affba8ee97f2d69927fa823b08b472b7b994e36a52a964b93d16147"}, +] + +[package.extras] +tests = ["asttokens (>=2.1.0)", "coverage", "coverage-enable-subprocess", "ipython", "littleutils", "pytest", "rich"] + [[package]] name = "fastapi" version = "0.104.0" @@ -492,6 +524,23 @@ cli = ["click (==8.*)", "pygments (==2.*)", "rich (>=10,<14)"] http2 = ["h2 (>=3,<5)"] socks = ["socksio (==1.*)"] +[[package]] +name = "icecream" +version = "2.1.3" +description = "Never use print() to debug again; inspect variables, expressions, and program execution with a single, simple function call." +optional = false +python-versions = "*" +files = [ + {file = "icecream-2.1.3-py2.py3-none-any.whl", hash = "sha256:757aec31ad4488b949bc4f499d18e6e5973c40cc4d4fc607229e78cfaec94c34"}, + {file = "icecream-2.1.3.tar.gz", hash = "sha256:0aa4a7c3374ec36153a1d08f81e3080e83d8ac1eefd97d2f4fe9544e8f9b49de"}, +] + +[package.dependencies] +asttokens = ">=2.0.1" +colorama = ">=0.3.9" +executing = ">=0.3.1" +pygments = ">=2.2.0" + [[package]] name = "idna" version = "3.4" @@ -1067,4 +1116,4 @@ standard = ["colorama (>=0.4)", "httptools (>=0.5.0)", "python-dotenv (>=0.13)", [metadata] lock-version = "2.0" python-versions = "^3.12" -content-hash = "691162b136ebe00dee8096703a43a468d11fe2f09b5a7953397dc04c8362287e" +content-hash = "ab246f4218ba0ee3c4e7b72caa01446b67259d18496280ccc3eb25b61d878a9b" diff --git a/pyproject.toml b/pyproject.toml index 54c2a056c..077cd44d7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,6 +30,7 @@ pygments = "^2.16.1" pytest = "^7.4.3" dictdiffer = "^0.9.0" setuptools = "^68.2.2" +icecream = "^2.1.3" [tool.pytest] addopts = ["--ignore gen"] diff --git a/src/features/entity/use_cases/instantiate_entity_use_case/create_entity.py b/src/features/entity/use_cases/instantiate_entity_use_case/create_entity.py index 46ddc0602..20e3b721a 100644 --- a/src/features/entity/use_cases/instantiate_entity_use_case/create_entity.py +++ b/src/features/entity/use_cases/instantiate_entity_use_case/create_entity.py @@ -72,7 +72,7 @@ def _get_entity(self, blueprint: Blueprint, entity: dict): for attr in blueprint.attributes: if attr.attribute_type == BuiltinDataTypes.BINARY.value: continue - if attr.is_optional: + if attr.is_optional and not attr.default: # skip attribute if it is optional continue if attr.attribute_type in PRIMITIVES: diff --git a/src/home/system/SIMOS/Blueprint.json b/src/home/system/SIMOS/Blueprint.json index 7d94e2faf..6dcb5146b 100644 --- a/src/home/system/SIMOS/Blueprint.json +++ b/src/home/system/SIMOS/Blueprint.json @@ -48,7 +48,8 @@ { "name": "name", "type": "dmss://system/SIMOS/BlueprintAttribute", - "attributeType": "string" + "attributeType": "string", + "optional": true }, { "name": "type", @@ -58,7 +59,8 @@ { "name": "description", "type": "dmss://system/SIMOS/BlueprintAttribute", - "attributeType": "string" + "attributeType": "string", + "optional": true } ], "contained": true, diff --git a/src/tests/bdd/entity/instantiate_entity.feature b/src/tests/bdd/entity/instantiate_entity.feature index 2b8a3e680..0c63e04ef 100644 --- a/src/tests/bdd/entity/instantiate_entity.feature +++ b/src/tests/bdd/entity/instantiate_entity.feature @@ -119,6 +119,11 @@ Feature: Instantiate entity "type": "dmss://system/SIMOS/Reference", "referenceType": "link" } - ] + ], + "bestFriendAtWork": { + "type": "dmss://data-source-name/root_package/Employee", + "name": "Kari", + "isManager": false + } } """ \ No newline at end of file diff --git a/src/tests/unit/use_cases/instantiate_entity_use_case/test_create_entity.py b/src/tests/unit/use_cases/instantiate_entity_use_case/test_create_entity.py index e8a68abc7..ac4889a7d 100644 --- a/src/tests/unit/use_cases/instantiate_entity_use_case/test_create_entity.py +++ b/src/tests/unit/use_cases/instantiate_entity_use_case/test_create_entity.py @@ -15,6 +15,7 @@ def setUp(self): "dmss://system/SIMOS/BlueprintAttribute", "dmss://system/SIMOS/Entity", "dmss://system/SIMOS/NamedEntity", + "dmss://system/SIMOS/Blueprint", ] mock_blueprint_folder = "src/tests/unit/use_cases/instantiate_entity_use_case/mock_data/" mock_blueprints_and_file_names = { @@ -55,10 +56,43 @@ def test_blueprint_entity(self): "intValues": [1, 5, 4, 2], "boolValues": [True, False, True], "stringValues": ["one", "two", "three"], + "engine3": { + "name": "default engine", + "type": "EngineTest", + "fuelPump": {"name": "fuelPump", "type": "FuelPumpTest", "description": "A standard fuel pump"}, + "power": 9, + }, } entity = CreateEntity(blueprint_provider=self.mock_document_service.get_blueprint, type="CarTest").entity + self.assertDictEqual(expected_entity, entity) + + def test_create_default_blueprint(self): + expected_entity = { + "type": "dmss://system/SIMOS/Blueprint", + "name": "", + "attributes": [ + { + "name": "name", + "type": "dmss://system/SIMOS/BlueprintAttribute", + "attributeType": "string", + "optional": True, + }, + {"name": "type", "type": "dmss://system/SIMOS/BlueprintAttribute", "attributeType": "string"}, + { + "name": "description", + "type": "dmss://system/SIMOS/BlueprintAttribute", + "attributeType": "string", + "optional": True, + }, + ], + } + + entity = CreateEntity( + blueprint_provider=self.mock_document_service.get_blueprint, type="dmss://system/SIMOS/Blueprint" + ).entity + self.assertEqual(expected_entity, entity) def test_is_not_json(self):