Add test for the instantiation of deplete.Operator

Construct a minimal cross sections xml file that only
contains the depletion_chain node required by
the Operator. The path to this file is set to the
OPENMC_CROSS_SECTIONS file, and reverted after the test.

The depletion_chain points towards the chain_simple.xml
files, and a reference Chain is produced in the test.
The test involves creating a bare Operator instance, that
constructs a chain based on the temporary
OPENMC_CROSS_SECTIONS file. This chain is compared to
that produced by reading the chain_simple.xml file stored
in the test directory.
This commit is contained in:
Andrew Johnson 2019-05-30 18:01:33 -04:00
parent cc98d13793
commit f700861646
No known key found for this signature in database
GPG key ID: 253418E91B7F6FEB

View file

@ -0,0 +1,72 @@
"""Basic unit tests for openmc.deplete.Operator instantiation
Modifies and resets environment variable OPENMC_CROSS_SECTIONS
to a custom file with new depletion_chain node
"""
from os import remove
from os import environ
from unittest import mock
from pathlib import Path
import pytest
from openmc.deplete.abc import TransportOperator
from openmc.deplete.chain import Chain
BARE_XS_FILE = "bare_cross_sections.xml"
CHAIN_PATH = Path().cwd() / "tests" / "chain_simple.xml"
@pytest.fixture(scope="module")
def bare_xs():
"""Create a very basic cross_sections file, return simple Chain.
"""
bare_xs_contents = """<?xml version="1.0"?>
<cross_sections>
<depletion_chain path="{}" />
</cross_sections>
""".format(CHAIN_PATH)
with open(BARE_XS_FILE, "w") as out:
out.write(bare_xs_contents)
yield
remove(BARE_XS_FILE)
class BareDepleteOperator(TransportOperator):
"""Very basic class for testing the initialization."""
# declare abstract methods so object can be created
def __call__(self, *args, **kwargs):
pass
def initial_condition(self):
pass
def get_results_info(self):
pass
@mock.patch.dict(environ, {"OPENMC_CROSS_SECTIONS": BARE_XS_FILE})
def test_operator_init(bare_xs):
"""The test will set and unset environment variable OPENMC_CROSS_SECTIONS
to point towards a temporary dummy file. This file will be removed
at the end of the test, and only contains a
depletion_chain node."""
# force operator to read from OPENMC_CROSS_SECTIONS
bare_op = BareDepleteOperator(chain_file=None)
act_chain = bare_op.chain
ref_chain = Chain.from_xml(CHAIN_PATH)
assert len(act_chain) == len(ref_chain)
for name in ref_chain.nuclide_dict:
# compare openmc.deplete.Nuclide objects
ref_nuc = ref_chain[name]
act_nuc = act_chain[name]
for prop in [
'name', 'half_life', 'decay_energy', 'reactions',
'decay_modes', 'yield_data', 'yield_energies',
]:
assert getattr(act_nuc, prop) == getattr(ref_nuc, prop), prop