Repository: Extraweich/homopy (language: python)

Issue:
Extend tests converter
Hi Nicolas,

just could not resist checking your implementation against `mechkit`.
Maybe the pytest-fixtures and parametrize decorators are interesting for you.

Cheers Julian
Extend tests converter
Hi Nicolas,

just could not resist checking your implementation against `mechkit`.
Maybe the pytest-fixtures and parametrize decorators are interesting for you.

Cheers Julian


Discussion / hints:

Fix patch:
diff --git a/.gitignore b/.gitignore
index 02338b4..7868a1d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,165 @@
 .venv/
 .vscode/
 mk_doc.sh
-refresh.sh
\ No newline at end of file
+refresh.sh
+
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+*$py.class
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+wheels/
+share/python-wheels/
+*.egg-info/
+.installed.cfg
+*.egg
+MANIFEST
+
+# PyInstaller
+#  Usually these files are written by a python script from a template
+#  before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.nox/
+.coverage
+.coverage.*
+.cache
+nosetests.xml
+coverage.xml
+*.cover
+*.py,cover
+.hypothesis/
+.pytest_cache/
+cover/
+
+# Translations
+*.mo
+*.pot
+
+# Django stuff:
+*.log
+local_settings.py
+db.sqlite3
+db.sqlite3-journal
+
+# Flask stuff:
+instance/
+.webassets-cache
+
+# Scrapy stuff:
+.scrapy
+
+# Sphinx documentation
+docs/_build/
+
+# PyBuilder
+.pybuilder/
+target/
+
+# Jupyter Notebook
+.ipynb_checkpoints
+
+# IPython
+profile_default/
+ipython_config.py
+
+# pyenv
+#   For a library or package, you might want to ignore these files since the code is
+#   intended to run in multiple environments; otherwise, check them in:
+# .python-version
+
+# pipenv
+#   According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
+#   However, in case of collaboration, if having platform-specific dependencies or dependencies
+#   having no cross-platform support, pipenv may install dependencies that don't work, or not
+#   install all needed dependencies.
+#Pipfile.lock
+
+# poetry
+#   Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
+#   This is especially recommended for binary packages to ensure reproducibility, and is more
+#   commonly ignored for libraries.
+#   https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
+#poetry.lock
+
+# pdm
+#   Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
+#pdm.lock
+#   pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
+#   in version control.
+#   https://pdm.fming.dev/#use-with-ide
+.pdm.toml
+
+# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
+__pypackages__/
+
+# Celery stuff
+celerybeat-schedule
+celerybeat.pid
+
+# SageMath parsed files
+*.sage.py
+
+# Environments
+.env
+.venv
+env/
+venv/
+ENV/
+env.bak/
+venv.bak/
+
+# Spyder project settings
+.spyderproject
+.spyproject
+
+# Rope project settings
+.ropeproject
+
+# mkdocs documentation
+/site
+
+# mypy
+.mypy_cache/
+.dmypy.json
+dmypy.json
+
+# Pyre type checker
+.pyre/
+
+# pytype static type analyzer
+.pytype/
+
+# Cython debug symbols
+cython_debug/
+
+# PyCharm
+#  JetBrains specific template is maintained in a separate JetBrains.gitignore that can
+#  be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
+#  and can be added to the global gitignore or merged into this file.  For a more nuclear
+#  option (not recommended) you can uncomment the following to ignore the entire idea folder.
+#.idea/
diff --git a/homopy/__pycache__/Methods.cpython-39.pyc b/homopy/__pycache__/Methods.cpython-39.pyc
deleted file mode 100644
index c8d6c0f..0000000
Binary files a/homopy/__pycache__/Methods.cpython-39.pyc and /dev/null differ
diff --git a/homopy/__pycache__/Mori_Tanaka.cpython-39.pyc b/homopy/__pycache__/Mori_Tanaka.cpython-39.pyc
deleted file mode 100644
index 5fabf89..0000000
Binary files a/homopy/__pycache__/Mori_Tanaka.cpython-39.pyc and /dev/null differ
diff --git a/homopy/__pycache__/Stiffness.cpython-39.pyc b/homopy/__pycache__/Stiffness.cpython-39.pyc
deleted file mode 100644
index a405f40..0000000
Binary files a/homopy/__pycache__/Stiffness.cpython-39.pyc and /dev/null differ
diff --git a/homopy/__pycache__/Stiffness_Plot.cpython-38.pyc b/homopy/__pycache__/Stiffness_Plot.cpython-38.pyc
deleted file mode 100644
index 14c1cbd..0000000
Binary files a/homopy/__pycache__/Stiffness_Plot.cpython-38.pyc and /dev/null differ
diff --git a/homopy/__pycache__/Stiffness_Plot.cpython-39.pyc b/homopy/__pycache__/Stiffness_Plot.cpython-39.pyc
deleted file mode 100644
index 298ce38..0000000
Binary files a/homopy/__pycache__/Stiffness_Plot.cpython-39.pyc and /dev/null differ
diff --git a/homopy/__pycache__/Tensor.cpython-39.pyc b/homopy/__pycache__/Tensor.cpython-39.pyc
deleted file mode 100644
index 4aef0c6..0000000
Binary files a/homopy/__pycache__/Tensor.cpython-39.pyc and /dev/null differ
diff --git a/homopy/__pycache__/Tsai_Hill.cpython-38.pyc b/homopy/__pycache__/Tsai_Hill.cpython-38.pyc
deleted file mode 100644
index e5c2669..0000000
Binary files a/homopy/__pycache__/Tsai_Hill.cpython-38.pyc and /dev/null differ
diff --git a/homopy/__pycache__/Tsai_Hill.cpython-39.pyc b/homopy/__pycache__/Tsai_Hill.cpython-39.pyc
deleted file mode 100644
index fe34b6e..0000000
Binary files a/homopy/__pycache__/Tsai_Hill.cpython-39.pyc and /dev/null differ
diff --git a/homopy/__pycache__/__init__.cpython-39.pyc b/homopy/__pycache__/__init__.cpython-39.pyc
deleted file mode 100644
index 69745c1..0000000
Binary files a/homopy/__pycache__/__init__.cpython-39.pyc and /dev/null differ
diff --git a/homopy/__pycache__/elasticity.cpython-39.pyc b/homopy/__pycache__/elasticity.cpython-39.pyc
deleted file mode 100644
index a7cc0da..0000000
Binary files a/homopy/__pycache__/elasticity.cpython-39.pyc and /dev/null differ
diff --git a/homopy/__pycache__/functions.cpython-39.pyc b/homopy/__pycache__/functions.cpython-39.pyc
deleted file mode 100644
index 2549495..0000000
Binary files a/homopy/__pycache__/functions.cpython-39.pyc and /dev/null differ


Test patch:
diff --git a/test/README.md b/test/README.md
new file mode 100644
index 0000000..e4b1e92
--- /dev/null
+++ b/test/README.md
@@ -0,0 +1,14 @@
+# Run tests against installed package homopy
+Install test requirements
+```
+pip install pytest
+pip install mechkit
+```
+Install homopy from source code in current directory to current environment in development mode (`-e` flag)
+```
+python -m pip install -e .
+```
+Run tests
+```
+pytest
+```
diff --git a/test/test_tensor.py b/test/test_tensor.py
index f00ff69..13f3dbb 100644
--- a/test/test_tensor.py
+++ b/test/test_tensor.py
@@ -1,10 +1,40 @@
 import numpy as np
+import pytest
+import mechkit
+from mechkit.operators import Sym_Fourth_Order_Special
+from homopy import tensor
 
-from homopy.tensor import *
 
+@pytest.fixture()
+def random_hooke_sym():
+    return mechkit.operators.Sym_Fourth_Order_Special(label="inner")(
+        np.random.rand(3, 3, 3, 3)
+    )
 
-def test_orthogonal_base():
-    t = Tensor()
+
+@pytest.fixture()
+def random_complete_sym():
+    return mechkit.operators.Sym_Fourth_Order_Special(label="complete")(
+        np.random.rand(3, 3, 3, 3)
+    )
+
+
+@pytest.fixture()
+def random_mandel6():
+    return np.random.rand(6, 6)
+
+
+@pytest.fixture()
+def t():
+    return tensor.Tensor()
+
+
+@pytest.fixture(name="mechkit_converter")
+def set_up_implicit_mechkit_converter():
+    return mechkit.notation.Converter()
+
+
+def test_orthogonal_base(t):
 
     orthogonal_base = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
     tensor_base = np.vstack((t.e1, t.e2, t.e3))
@@ -12,13 +42,35 @@ def test_orthogonal_base():
     assert np.allclose(orthogonal_base, tensor_base, rtol=1e-6)
 
 
-def test_converter():
-    t = Tensor()
+class Test_Converter:
+    def test_random_mandel_to_tensor_and_back(self, t, random_mandel6):
+
+        random_tensor_sym = t.mandel2tensor(random_mandel6)
+        recovered_mat = t.tensor2mandel(random_tensor_sym)
+
+        assert np.allclose(random_mandel6, recovered_mat, rtol=1e-6)
+
+    @pytest.mark.parametrize(
+        "fixture_name", ["random_hooke_sym", "random_complete_sym"]
+    )
+    def test_compare_to_mandel_with_mechkit(
+        self, fixture_name, mechkit_converter, t, request
+    ):
+
+        # Get the random tensors which are stored as fixture following
+        # https://engineeringfordatascience.com/posts/pytest_fixtures_with_parameterize/
+        random_tensor = request.getfixturevalue(fixture_name)
+
+        mandel_homopy = t.tensor2mandel(random_tensor)
+        mandel_mechkit = mechkit_converter.to_mandel6(random_tensor)
+
+        assert np.allclose(mandel_homopy, mandel_mechkit)
 
-    random_mat = np.random.rand(6, 6)
-    random_mat_sym = 1 / 2 * (random_mat + random_mat.T)
+    def test_compare_to_tensor_with_mechkit(
+        self, random_mandel6, mechkit_converter, t
+    ):
 
-    random_tensor_sym = t.mandel2tensor(random_mat_sym)
-    recovered_mat = t.tensor2mandel(random_tensor_sym)
+        tensor_homopy = t.mandel2tensor(random_mandel6)
+        tensor_mechkit = mechkit_converter.to_tensor(random_mandel6)
 
-    assert np.allclose(random_mat_sym, recovered_mat, rtol=1e-6)
+        assert np.allclose(tensor_homopy, tensor_mechkit)
\ No newline at end of file