Skip to content

Commit

Permalink
Improved Code Coverage Database3 (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
john-science committed Aug 4, 2022
1 parent 1660f9f commit aa16faa
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 35 deletions.
3 changes: 1 addition & 2 deletions armi/bookkeeping/db/database3.py
Original file line number Diff line number Diff line change
Expand Up @@ -1307,8 +1307,7 @@ def _writeParams(self, h5group, comps):
# flatten, store the data offsets and array shapes, and None locations
# as attrs
# - If not jagged, all top-level ndarrays are the same shape, so it is
# probably easier to replace Nones with ndarrays filled with special
# values.
# easier to replace Nones with ndarrays filled with special values.
if parameters.NoDefault in data:
data = None
else:
Expand Down
43 changes: 42 additions & 1 deletion armi/bookkeeping/db/tests/test_database3.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@

r""" Tests for the Database3 class
"""
# pylint: disable=missing-function-docstring,missing-class-docstring,abstract-method,protected-access,no-member,disallowed-name,invalid-name
import subprocess
import unittest

import h5py
import numpy

from armi.bookkeeping.db import database3
from armi.bookkeeping.db import _getH5File, database3
from armi.reactor import grids
from armi.reactor import parameters
from armi.reactor.tests import test_reactors
from armi.tests import TEST_ROOT
from armi.utils import getPreviousTimeNode
Expand Down Expand Up @@ -50,6 +52,45 @@ def tearDown(self):
self.stateRetainer.__exit__()
self.td.__exit__(None, None, None)

def test_writeToDB(self):
self.r.p.cycle = 0
self.r.p.timeNode = 0
self.r.p.cycleLength = 0

# Adding some nonsense in, to test NoDefault params
self.r.p.availabilityFactor = parameters.NoDefault

# validate that the H5 file gets bigger after the write
self.assertEqual(list(self.db.h5db.keys()), ["inputs"])
self.db.writeToDB(self.r)
self.assertEqual(sorted(self.db.h5db.keys()), ["c00n00", "inputs"])

keys = [
"Circle",
"Core",
"DerivedShape",
"Helix",
"HexAssembly",
"HexBlock",
"Hexagon",
"Reactor",
"layout",
]
self.assertEqual(sorted(self.db.h5db["c00n00"].keys()), sorted(keys))

# validate availabilityFactor did not make it into the H5 file
rKeys = ["cycle", "cycleLength", "flags", "serialNum", "timeNode"]
self.assertEqual(
sorted(self.db.h5db["c00n00"]["Reactor"].keys()), sorted(rKeys)
)

def test_getH5File(self):
with self.assertRaises(TypeError):
_getH5File(None)

h5 = _getH5File(self.db)
self.assertEqual(type(h5), h5py.File)

def makeHistory(self):
"""Walk the reactor through a few time steps and write them to the db."""
for cycle, node in ((cycle, node) for cycle in range(3) for node in range(3)):
Expand Down
56 changes: 24 additions & 32 deletions armi/utils/tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,40 @@ class TestPlotting(unittest.TestCase):
demonstrate how they are meant to be called.
"""

# Change to False when you want to inspect the plots. Change back please.
removeFiles = True

@classmethod
def setUpClass(cls):
cls.o, cls.r = test_reactors.loadTestReactor()

def test_plotDepthMap(self): # indirectly tests plot face map
# set some params to visualize
for i, b in enumerate(self.o.r.core.getBlocks()):
b.p.percentBu = i / 100
fName = plotting.plotBlockDepthMap(
self.r.core, param="percentBu", fName="depthMapPlot.png", depthIndex=2
)
self._checkExists(fName)
with TemporaryDirectoryChanger():
# set some params to visualize
for i, b in enumerate(self.o.r.core.getBlocks()):
b.p.percentBu = i / 100
fName = plotting.plotBlockDepthMap(
self.r.core, param="percentBu", fName="depthMapPlot.png", depthIndex=2
)
self._checkExists(fName)

def test_plotAssemblyTypes(self):
plotPath = "coreAssemblyTypes1.png"
plotting.plotAssemblyTypes(self.r.core.parent.blueprints, plotPath)
self._checkExists(plotPath)

plotPath = "coreAssemblyTypes2.png"
plotting.plotAssemblyTypes(
self.r.core.parent.blueprints, plotPath, yAxisLabel="y axis", title="title"
)
self._checkExists(plotPath)
with TemporaryDirectoryChanger():
plotPath = "coreAssemblyTypes1.png"
plotting.plotAssemblyTypes(self.r.core.parent.blueprints, plotPath)
self._checkExists(plotPath)

plotPath = "coreAssemblyTypes2.png"
plotting.plotAssemblyTypes(
self.r.core.parent.blueprints,
plotPath,
yAxisLabel="y axis",
title="title",
)
self._checkExists(plotPath)

with self.assertRaises(ValueError):
plotting.plotAssemblyTypes(None, plotPath, None)
with self.assertRaises(ValueError):
plotting.plotAssemblyTypes(None, plotPath, None)

def test_plotBlockFlux(self):
try:
with TemporaryDirectoryChanger():
xslib = isotxs.readBinary(ISOAA_PATH)
self.r.core.lib = xslib

Expand All @@ -87,14 +89,6 @@ def test_plotBlockFlux(self):
bList2=blockList,
)
self.assertTrue(os.path.exists("bList2.png"))
# can't test adjoint at the moment, testBlock doesn't like to .getMgFlux(adjoint=True)
finally:
os.remove("flux.txt") # secondarily created during the call.
os.remove("flux.png") # created during the call.
os.remove("peak.txt") # csecondarily reated during the call.
os.remove("peak.png") # created during the call.
os.remove("bList2.txt") # secondarily created during the call.
os.remove("bList2.png") # created during the call.

def test_plotHexBlock(self):
with TemporaryDirectoryChanger():
Expand Down Expand Up @@ -123,8 +117,6 @@ def test_plotCartesianBlock(self):

def _checkExists(self, fName):
self.assertTrue(os.path.exists(fName))
if self.removeFiles:
os.remove(fName)


if __name__ == "__main__":
Expand Down

0 comments on commit aa16faa

Please sign in to comment.