-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add some basic testing for the Material class #220
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import h5py | ||
import pytest | ||
|
||
from hexrd.material import Material, load_materials_hdf5 | ||
|
||
# Tolerance for comparing floats | ||
FLOAT_TOL = 1.e-8 | ||
|
||
# Use consistent units to simplify testing | ||
DEFAULT_LENGTH_UNIT = 'angstrom' | ||
DEFAULT_ANGLE_UNIT = 'degrees' | ||
|
||
|
||
@pytest.fixture | ||
def default_material(): | ||
return Material() | ||
|
||
|
||
@pytest.fixture | ||
def test_materials_file(example_repo_path): | ||
return example_repo_path / 'NIST_ruby/single_GE/include/materials.h5' | ||
|
||
|
||
def normalize_unit(v): | ||
if hasattr(v, 'unit'): | ||
# Assume it's a val unit | ||
if v.unit in ('radians', 'degrees'): | ||
# Assume it's an angle | ||
return v.getVal(DEFAULT_ANGLE_UNIT) | ||
else: | ||
# Assume it's a length | ||
return v.getVal(DEFAULT_LENGTH_UNIT) | ||
|
||
return v | ||
|
||
|
||
def are_close(v1, v2, tol=FLOAT_TOL): | ||
return abs(normalize_unit(v1) - normalize_unit(v2)) < tol | ||
|
||
|
||
def lparms_are_close(lparms, indices, tol=FLOAT_TOL): | ||
first = lparms[indices[0]] | ||
return all(are_close(first, lparms[i], tol) for i in indices[1:]) | ||
|
||
|
||
def test_sgnum_setter(default_material): | ||
# Just in case we change the default... | ||
default_material.sgnum = 225 | ||
|
||
# Should have a == b == c and alpha == beta == gamma == 90 | ||
lparms = default_material.latticeParameters | ||
assert lparms_are_close(lparms, [0, 1, 2]) | ||
assert lparms_are_close(lparms, [3, 4, 5]) | ||
assert are_close(lparms[5], 90) | ||
|
||
default_material.sgnum = 165 | ||
lparms = default_material.latticeParameters | ||
|
||
# Gamma should be 120, the other angles should be 90 | ||
assert not lparms_are_close(lparms, [3, 4, 5]) | ||
assert lparms_are_close(lparms, [3, 4]) | ||
assert are_close(lparms[3], 90) | ||
assert are_close(lparms[5], 120) | ||
Comment on lines
+46
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, this is what is actually being tested here right now. You can see it being tested here. |
||
|
||
|
||
def test_load_materials(test_materials_file): | ||
materials = load_materials_hdf5(test_materials_file) | ||
|
||
with h5py.File(test_materials_file, 'r') as f: | ||
# Check that it loaded all of the materials | ||
mat_names = list(f.keys()) | ||
assert all(x in materials for x in mat_names) | ||
|
||
# Check that the values for ruby match | ||
ruby = materials['ruby'] | ||
params = f['ruby']['LatticeParameters'][()] | ||
|
||
# Convert to angstroms... | ||
for i in range(3): | ||
params[i] *= 10 | ||
|
||
for i in range(6): | ||
assert are_close(params[i], ruby.latticeParameters[i]) | ||
Comment on lines
+66
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here is loading a materials file and checking that the lattice parameters for ruby match. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to explain what is going on here, pytest does a few things automatically based upon how you name things.
If you start a function name with
test_
, pytest assumes it is a test and tries to run it.Arguments that you put in the
test_
function can be pytest fixtures. In this case, the argument is nameddefault_material
, which, via name matching, pytest automatically associates with the fixture function defined above nameddefault_material
. It automatically calls that function and passes the returned value as the argument to this test function.