Skip to content

Commit ee4fae0

Browse files
committed
NPI-4453 clean up, refactor and rename things to allow easy extention to support any object type, not just DataFrames
1 parent c429792 commit ee4fae0

8 files changed

+142
-110
lines changed

gnssanalysis/gn_utils.py

Lines changed: 103 additions & 72 deletions
Large diffs are not rendered by default.

tests/test_utils.py

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pyfakefs.fake_filesystem_unittest import TestCase
66
from pathlib import Path
77

8-
from gnssanalysis.gn_utils import DataFrameHashUtils, delete_entire_directory
8+
from gnssanalysis.gn_utils import UnitTestBaseliner, delete_entire_directory
99
import gnssanalysis.gn_utils as ga_utils
1010

1111

@@ -69,30 +69,30 @@ def test_configure_logging(self):
6969
self.assertEqual(logger_not_output, None)
7070

7171

72-
class TestDataFrameHashUtils(unittest.TestCase):
72+
class TestUnitTestBaseliner(unittest.TestCase):
7373

7474
def test_verify_refusal_in_wrong_mode(self):
75-
mode_backup = DataFrameHashUtils.mode
75+
mode_backup = UnitTestBaseliner.mode
7676
try:
7777
df = DataFrame(["a", "b", "c"])
7878

7979
# Baseline (do not commit uncommented!) Note: every function needs its own baseline, becuase the
8080
# function name determines the filename, unless we override that.
81-
# DataFrameHashUtils.mode = "baseline"
82-
# DataFrameHashUtils.record_baseline([df])
81+
# UnitTestBaseliner.mode = "baseline"
82+
# UnitTestBaseliner.record_baseline([df])
8383

8484
# In baseline (write) mode, verify should be refused.
85-
DataFrameHashUtils.mode = "baseline"
85+
UnitTestBaseliner.mode = "baseline"
8686

8787
with self.assertWarns(Warning) as warning_assessor:
8888
self.assertFalse(
89-
DataFrameHashUtils.verify([df]),
90-
"DF list verification should not succeed in 'baseline' mode",
89+
UnitTestBaseliner.verify([df]),
90+
"DF / object list verification should not succeed in 'baseline' mode",
9191
)
9292
# Ensure the expected warning, and only that warning, was raised
9393
captured_warnings = warning_assessor.warnings
9494
self.assertEqual(
95-
"Refusing to run verify method while not in verify mode. Set DataframeHashUtils.mode = 'verify' first",
95+
"Refusing to run verify method while not in verify mode. Set UnitTestBaseliner.mode = 'verify' first",
9696
str(captured_warnings[0].message),
9797
)
9898
self.assertEqual(
@@ -102,22 +102,23 @@ def test_verify_refusal_in_wrong_mode(self):
102102
)
103103

104104
# Should succeed in correct mode.
105-
DataFrameHashUtils.mode = "verify"
105+
UnitTestBaseliner.mode = "verify"
106106
self.assertTrue(
107-
DataFrameHashUtils.verify([df]),
108-
"DF list verification should succeed in 'verify' mode",
107+
UnitTestBaseliner.verify([df]),
108+
"DF / object list verification should succeed in 'verify' mode",
109109
)
110110
finally:
111111
# Ensure flag reset to avoid impacts on other tests (across the whole suite)
112-
DataFrameHashUtils.mode = mode_backup
112+
UnitTestBaseliner.mode = mode_backup
113113

114114
def test_repeat_caller_rejection(self):
115115
# These functions determine what files to write/read baselines from, based on the identity of the (test)
116116
# function that called them. Therefore, calling twice from the same function would cause the *same baseline
117117
# files* to be read/written for a different part of the unit test.
118118
# That would have the effect of:
119119
# - in write mode: overwriting the baseline file for a previous part of the test function.
120-
# - in read mode: repeating verification of the same file against a different DF list (which would likely fail).
120+
# - in read mode: repeating verification of the same file against a different DF / object list (which would
121+
# likely fail).
121122

122123
# We're only testing it with the verify function below, but both verify and baseline functions use the same
123124
# caller check logic, and store the caller record statically in a class variable. ?
@@ -126,51 +127,51 @@ def test_repeat_caller_rejection(self):
126127

127128
# Baseline (every function needs its own baseline, becuase the function name determines the filename,
128129
# unless we override that)
129-
# DataFrameHashUtils.mode = "baseline"
130-
# DataFrameHashUtils.record_baseline([df])
130+
# UnitTestBaseliner.mode = "baseline"
131+
# UnitTestBaseliner.record_baseline([df])
131132

132133
self.assertTrue(
133-
DataFrameHashUtils.verify([df]),
134-
"DF list verification should succeed on *first* call from a function.",
134+
UnitTestBaseliner.verify([df]),
135+
"DF / object list verification should succeed on *first* call from a function.",
135136
)
136137
with self.assertRaises(ValueError):
137-
DataFrameHashUtils.verify([df])
138-
self.fail("DF list verification should fail on *second*/repeated calls from a function.")
138+
UnitTestBaseliner.verify([df])
139+
self.fail("DF / object list verification should fail on *second*/repeated calls from a function.")
139140

140-
def test_duplicate_df_rejection(self):
141+
def test_duplicate_object_rejection(self):
141142

142-
# List to aggregate DFs for hashing
143-
dfs_to_hash: list[DataFrame] = []
143+
# List to aggregate DFs / objects for hashing
144+
objects_to_hash: list[object] = []
144145

145146
df = DataFrame(["a", "b", "c"]) # Let's call this Dataframe 'a'
146-
dfs_to_hash.extend([df])
147+
objects_to_hash.extend([df])
147148

148149
# Overwrite local variable, as often happens in our unit tests
149150
df = DataFrame(["b", "c", "d"]) # Let's call this Dataframe 'b'
150151

151152
# This might look questionable, but is ok, because we saved a reference to dataframe 'a' to the list,
152153
# before overwriting local var 'df' to point at dataframe 'b'.
153-
dfs_to_hash.extend([df])
154+
objects_to_hash.extend([df])
154155

155156
# Baseline this test (this should only be committed commented out!)
156-
# DataFrameHashUtils.mode = "baseline"
157-
# DataFrameHashUtils.record_baseline(dfs_to_hash)
157+
# UnitTestBaseliner.mode = "baseline"
158+
# UnitTestBaseliner.record_baseline(dfs_to_hash)
158159

159160
# Will return True if verification succeeded. False if baseline missing or mode != verify
160161
self.assertTrue(
161-
DataFrameHashUtils.verify(dfs_to_hash),
162-
"DF list verification should succeed here (unless baseline files are missing, or baselining has been turned on)",
162+
UnitTestBaseliner.verify(objects_to_hash),
163+
"DF / object list verification should succeed here (unless baseline files are missing, or baselining has been turned on)",
163164
)
164165

165166
# The local variable df still points to the same DF, so now the list contains [a,b,b]. This should be an error.
166-
dfs_to_hash.extend([df])
167+
objects_to_hash.extend([df])
167168
with self.assertRaises(ValueError):
168-
DataFrameHashUtils.verify(dfs_to_hash)
169+
UnitTestBaseliner.verify(objects_to_hash)
169170

170171
def test_caller_identity_fetch(self):
171172
def wrapper_function():
172-
class_name, func_name = DataFrameHashUtils.get_grandparent_caller_id()
173-
self.assertEqual(class_name, "TestDataFrameHashUtils")
173+
class_name, func_name = UnitTestBaseliner.get_grandparent_caller_id()
174+
self.assertEqual(class_name, "TestUnitTestBaseliner")
174175
self.assertEqual(func_name, "test_caller_identity_fetch")
175176

176177
# We have to do this (create an extra stack frame) because the function looks for
@@ -187,8 +188,8 @@ def wrapper_function():
187188

188189
# os.chdir("./tests")
189190

190-
# df_hash_tests = TestDataFrameHashUtils()
191-
# df_hash_tests.test_duplicate_df_rejection()
192-
# df_hash_tests.test_verify_refusal_in_wrong_mode
193-
# df_hash_tests.test_repeat_caller_rejection()
194-
# df_hash_tests.test_caller_identity_fetch()
191+
# baseliner_tests = TestUnitTestBaseliner()
192+
# baseliner_tests.test_duplicate_object_rejection()
193+
# baseliner_tests.test_verify_refusal_in_wrong_mode
194+
# baseliner_tests.test_repeat_caller_rejection()
195+
# baseliner_tests.test_caller_identity_fetch()

tests/baseline_dataframe_records/TestDataFrameHashUtils/test_duplicate_df_rejection.pickledlist renamed to tests/unittest_baselines/TestUnitTestBaseliner/test_duplicate_object_rejection.pickledlist

File renamed without changes.

tests/baseline_dataframe_records/TestDataFrameHashUtils/test_duplicate_df_rejection.pickledlist_sha256 renamed to tests/unittest_baselines/TestUnitTestBaseliner/test_duplicate_object_rejection.pickledlist_sha256

File renamed without changes.

tests/baseline_dataframe_records/TestDataFrameHashUtils/test_repeat_caller_rejection.pickledlist renamed to tests/unittest_baselines/TestUnitTestBaseliner/test_repeat_caller_rejection.pickledlist

File renamed without changes.

tests/baseline_dataframe_records/TestDataFrameHashUtils/test_repeat_caller_rejection.pickledlist_sha256 renamed to tests/unittest_baselines/TestUnitTestBaseliner/test_repeat_caller_rejection.pickledlist_sha256

File renamed without changes.

tests/baseline_dataframe_records/TestDataFrameHashUtils/test_verify_refusal_in_wrong_mode.pickledlist renamed to tests/unittest_baselines/TestUnitTestBaseliner/test_verify_refusal_in_wrong_mode.pickledlist

File renamed without changes.

tests/baseline_dataframe_records/TestDataFrameHashUtils/test_verify_refusal_in_wrong_mode.pickledlist_sha256 renamed to tests/unittest_baselines/TestUnitTestBaseliner/test_verify_refusal_in_wrong_mode.pickledlist_sha256

File renamed without changes.

0 commit comments

Comments
 (0)