Skip to content

Commit

Permalink
Correct import of material from json
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-adir committed Feb 13, 2024
1 parent 388b2e2 commit 9fe60c2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/compmec/section/dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def read_material_json(filepath: str) -> Dict:
folder = resources.files("compmec.section")
schema_path = str(folder.joinpath(schema_name))
data = read_json(filepath, schema_path)
for key in data.keys():
for key in tuple(data.keys()):
if key != "materials":
data.pop(key)
return data
Expand All @@ -100,7 +100,7 @@ def read_curve_json(filepath: str) -> Dict:
folder = resources.files("compmec.section")
schema_path = str(folder.joinpath(schema_name))
data = read_json(filepath, schema_path)
for key in data.keys():
for key in tuple(data.keys()):
if key not in ["nodes", "curves"]:
data.pop(key)
return data
Expand Down
3 changes: 2 additions & 1 deletion src/compmec/section/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ class Material(object):
@classmethod
def from_json(cls, filepath: str) -> Dict[str, Material]:
data = dataio.read_material_json(filepath)
data = data["materials"]
materials = {}
for name, info in data.items():
materials[name] = cls.from_dict(info)
return materials

@classmethod
def from_dict(cls, info: Dict) -> Material:
material = Isotropic(info)
material = Isotropic(**info)
return material

def to_dict(self) -> Dict:
Expand Down
27 changes: 25 additions & 2 deletions tests/test_material.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from compmec.section import material

import os

@pytest.mark.order(1)
@pytest.mark.dependency()
Expand Down Expand Up @@ -62,7 +62,30 @@ def test_end(self):
pass


class TestFromJson:
@pytest.mark.order(1)
@pytest.mark.dependency(depends=["TestIsotropic::test_end"])
def test_begin(self):
pass

@pytest.mark.order(1)
@pytest.mark.timeout(1)
@pytest.mark.dependency(depends=["TestFromJson::test_begin"])
def test_main(self):
folder = os.getcwd()
json_filepath = "tests/json/steel_square.json"
mats = material.Isotropic.from_json(json_filepath)
steel = mats["steel"]
assert steel.young_modulus == 210e+3
assert steel.poissons_ratio == 0.3

@pytest.mark.order(1)
@pytest.mark.dependency(depends=["TestFromJson::test_main"])
def test_end(self):
pass


@pytest.mark.order(1)
@pytest.mark.dependency(depends=["TestIsotropic::test_end"])
@pytest.mark.dependency(depends=["TestIsotropic::test_end", "TestFromJson::test_end"])
def test_end():
pass

0 comments on commit 9fe60c2

Please sign in to comment.