From 47d3cc77b3f24540fcf6ae56be6c0e34b397604e Mon Sep 17 00:00:00 2001 From: John Stilley <1831479+john-science@users.noreply.github.com> Date: Fri, 12 Nov 2021 09:13:29 -0800 Subject: [PATCH] Switching form a Setting deepcopy to a copy (#476) --- armi/bookkeeping/db/tests/test_database3.py | 26 ++++++++++----------- armi/settings/caseSettings.py | 2 +- armi/settings/tests/test_settings.py | 19 +++++++++++++++ 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/armi/bookkeeping/db/tests/test_database3.py b/armi/bookkeeping/db/tests/test_database3.py index 9dd4d487b..6575c0b22 100644 --- a/armi/bookkeeping/db/tests/test_database3.py +++ b/armi/bookkeeping/db/tests/test_database3.py @@ -64,9 +64,7 @@ def makeHistory(self): self.db.writeToDB(self.r) def makeShuffleHistory(self): - """ - Walk the reactor through a few time steps with some shuffling. - """ + """Walk the reactor through a few time steps with some shuffling.""" # Serial numbers *are not stable* (i.e., they can be different between test runs # due to parallelism and test run order). However, they are the simplest way to # check correctness of location-based history tracking. So we stash the serial @@ -76,7 +74,8 @@ def makeShuffleHistory(self): self.centralTopBlockSerialNums = [] grid = self.r.core.spatialGrid - for cycle in range(3): + + for cycle in range(2): a1 = self.r.core.childrenByLocator[grid[cycle, 0, 0]] a2 = self.r.core.childrenByLocator[grid[0, 0, 0]] olda1Loc = a1.spatialLocator @@ -86,7 +85,7 @@ def makeShuffleHistory(self): self.centralAssemSerialNums.append(c.p.serialNum) self.centralTopBlockSerialNums.append(c[-1].p.serialNum) - for node in range(3): + for node in range(2): self.r.p.cycle = cycle self.r.p.timeNode = node # something that splitDatabase won't change, so that we can make sure @@ -94,12 +93,13 @@ def makeShuffleHistory(self): self.r.p.cycleLength = cycle self.db.writeToDB(self.r) + # add some more data that isnt written to the database to test the # DatabaseInterface API - self.r.p.cycle = 3 + self.r.p.cycle = 2 self.r.p.timeNode = 0 self.r.p.cycleLength = cycle - self.r.core[0].p.chargeTime = 3 + self.r.core[0].p.chargeTime = 2 # add some fake missing parameter data to test allowMissing self.db.h5db["c00n00/Reactor/missingParam"] = "i don't exist" @@ -207,7 +207,7 @@ def test_load(self) -> None: del self.db.h5db["c00n00/Reactor/missingParam"] _r = self.db.load(0, 0, allowMissing=False) - def test_history(self) -> None: + def test_history(self): self.makeShuffleHistory() grid = self.r.core.spatialGrid @@ -219,15 +219,15 @@ def test_history(self) -> None: testAssem, params=["chargeTime", "serialNum"] ) expectedSn = { - (c, n): self.centralAssemSerialNums[c] for c in range(3) for n in range(3) + (c, n): self.centralAssemSerialNums[c] for c in range(2) for n in range(2) } self.assertEqual(expectedSn, hist["serialNum"]) # test block hists = self.db.getHistoriesByLocation( - [testBlock], params=["serialNum"], timeSteps=[(0, 0), (1, 0), (2, 0)] + [testBlock], params=["serialNum"], timeSteps=[(0, 0), (1, 0)] ) - expectedSn = {(c, 0): self.centralTopBlockSerialNums[c] for c in range(3)} + expectedSn = {(c, 0): self.centralTopBlockSerialNums[c] for c in range(2)} self.assertEqual(expectedSn, hists[testBlock]["serialNum"]) # cant mix blocks and assems, since they are different distance from core @@ -238,8 +238,8 @@ def test_history(self) -> None: hist = self.dbi.getHistory( self.r.core[0], params=["chargeTime", "serialNum"], byLocation=True ) - self.assertIn((3, 0), hist["chargeTime"].keys()) - self.assertEqual(hist["chargeTime"][(3, 0)], 3) + self.assertIn((2, 0), hist["chargeTime"].keys()) + self.assertEqual(hist["chargeTime"][(2, 0)], 2) def test_replaceNones(self): """ diff --git a/armi/settings/caseSettings.py b/armi/settings/caseSettings.py index 08036f63f..679fb2477 100644 --- a/armi/settings/caseSettings.py +++ b/armi/settings/caseSettings.py @@ -146,7 +146,7 @@ def getSetting(self, key, default=None): NOTE: This is used very rarely, try to organize your code to only need a Setting value. """ if key in self.__settings: - return deepcopy(self.__settings[key]) + return copy(self.__settings[key]) elif default is not None: return default else: diff --git a/armi/settings/tests/test_settings.py b/armi/settings/tests/test_settings.py index b79343744..5186d74b0 100644 --- a/armi/settings/tests/test_settings.py +++ b/armi/settings/tests/test_settings.py @@ -284,6 +284,25 @@ def test_modified(self): cs3 = cs2.modified(newSettings={"numberofGenericParams": 7}) cs4 = cs3.modified(newSettings={"somethingElse": 123}) + def test_copySetting(self): + """Ensure that when we copy a Setting() object, the result is sound. + NOTE: In particuar, self.schema and self._customSchema on a Setting object are + removed by Setting.__getstate__, and that has been a problem in the past. + """ + # get a baseline: show how the Setting object looks to start + s1 = setting.Setting("testCopy", 765) + self.assertEquals(s1.name, "testCopy") + self.assertEquals(s1._value, 765) + self.assertTrue(hasattr(s1, "schema")) + self.assertTrue(hasattr(s1, "_customSchema")) + + # show that copy(Setting) is working correctly + s2 = copy.copy(s1) + self.assertEquals(s2._value, 765) + self.assertEquals(s2.name, "testCopy") + self.assertTrue(hasattr(s2, "schema")) + self.assertTrue(hasattr(s2, "_customSchema")) + class TestSettingsConversion(unittest.TestCase): """Make sure we can convert from old XML type settings to new Yaml settings."""