Skip to content
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

Merged
merged 2 commits into from
Apr 7, 2021
Merged

Conversation

psavery
Copy link
Collaborator

@psavery psavery commented Mar 16, 2021

This is just adding a little framework for testing the Material class,
through which we can add more tests.

For: #214

This is just adding a little framework for testing the Material class,
through which we can add more tests.

Signed-off-by: Patrick Avery <[email protected]>
return all(are_close(first, lparms[i], tol) for i in indices[1:])


def test_sgnum_setter(default_material):
Copy link
Collaborator Author

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 named default_material, which, via name matching, pytest automatically associates with the fixture function defined above named default_material. It automatically calls that function and passes the returned value as the argument to this test function.

Comment on lines +40 to +57
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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Comment on lines +66 to +83
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])
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

@psavery psavery marked this pull request as ready for review March 16, 2021 16:00
@psavery
Copy link
Collaborator Author

psavery commented Mar 16, 2021

If you want to run the tests locally, you just need to make sure you have also cloned the examples repo, and set this environment variable to the path on your file system:

export HEXRD_EXAMPLE_REPO_PATH=/path/to/examples

Then, in the hexrd repo, you can pip install the requirements and run the tests like

pip install -r tests/requirements-dev.txt
pytest -s tests

Or, specifically only run the materials test:

pytest -s tests/test_material.py

That will skip the fit grains and find orientations tests, and only run the materials tests.

@joelvbernier
Copy link
Member

I'll add a bit to this then merge.

@joelvbernier joelvbernier self-assigned this Apr 6, 2021
@joelvbernier joelvbernier added documentation Improvements or additions to documentation testing labels Apr 6, 2021
@joelvbernier
Copy link
Member

I need to pass along some commits for this... or should we merge this and add additional testing in a later commit?

@psavery
Copy link
Collaborator Author

psavery commented Apr 6, 2021

@joelvbernier I'm good with merging this and adding extra testing later if that's good with you.

Copy link
Member

@saransh13 saransh13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. This is a good start. We can add more tests later.
LGTM.

@psavery psavery merged commit 15a9e57 into master Apr 7, 2021
@psavery psavery deleted the test-material branch April 7, 2021 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants