1515
1616from gnssanalysis .enum_meta_properties import EnumMetaProperties
1717
18- DEFAULT_DATAFRAME_HASH_BASELINE_DIR = _pathlib .Path ("./baseline_dataframe_records" )
18+ # Two options, as a convenience feature to allow invoking from the project root or the tests subdir.
19+ BASELINE_DATAFRAME_RECORDS_DIR_ROOT_RELATIVE = _pathlib .Path ("./tests/baseline_dataframe_records" )
20+ BASELINE_DATAFRAME_RECORDS_DIR_TESTS_RELATIVE = _pathlib .Path ("./baseline_dataframe_records" )
1921
2022
2123class StrictMode (metaclass = EnumMetaProperties ):
@@ -1011,22 +1013,37 @@ class DataFrameHashUtils:
10111013 @staticmethod
10121014 def get_paths_for_pickle_and_hash (
10131015 filename_prefix : str ,
1014- parent_dir : _pathlib .Path = DEFAULT_DATAFRAME_HASH_BASELINE_DIR ,
1016+ # parent_dir: _pathlib.Path = BASELINE_DATAFRAME_RECORDS_DIR_ROOT_RELATIVE ,
10151017 subdir : Optional [_pathlib .Path ] = None ,
10161018 ) -> tuple [_pathlib .Path , _pathlib .Path ]:
10171019
10181020 cwd : str = _pathlib .Path .cwd ().as_posix ()
1019- if not cwd .endswith ("/gnssanalysis/tests" ):
1021+
1022+ # The following is a quality of life feature, allowing test invocation from either:
1023+ # - the project root dir --> python -m unittest discover -v -s tests
1024+ # - the tests subdir --> python -m unittest discover -v
1025+ if cwd .endswith ("/gnssanalysis" ):
1026+ parent_dir = BASELINE_DATAFRAME_RECORDS_DIR_ROOT_RELATIVE
1027+ elif cwd .endswith ("/gnssanalysis/tests" ):
1028+ parent_dir = BASELINE_DATAFRAME_RECORDS_DIR_TESTS_RELATIVE
1029+ else :
10201030 raise ValueError (
10211031 f"DataFrameHashUtils invoked in invalid workdir: '{ cwd } '. "
1022- "It should only be run within ' gnssanalysis/ tests' "
1032+ "It should be run within the top level gnssanalysis project dir (preferred), or the tests subdir "
10231033 )
10241034
1025- dir = parent_dir / subdir if subdir is not None else parent_dir
1026- ensure_folders ([dir ]) # Create if it doesn't already exist
1035+ if not parent_dir .is_dir ():
1036+ raise ValueError (f"Test baselining dir not found at: '{ parent_dir .as_posix ()} '" )
1037+
1038+ target_dir = parent_dir / subdir if subdir is not None else parent_dir
1039+ if not target_dir .is_dir ():
1040+ # Create directory (fail if parent dirs don't exist). We take this more conservative approach because if
1041+ # the baseline directory doesn't exist *where we are looking*, that may indicate our workdir is wrong
1042+ # and we should stop.
1043+ target_dir .mkdir ()
10271044
1028- pickled_list_path = _pathlib .Path (f"{ dir } /{ filename_prefix } .pickledlist" )
1029- pickled_list_hash_path = _pathlib .Path (f"{ dir } /{ filename_prefix } .pickledlist_sha256" )
1045+ pickled_list_path = _pathlib .Path (f"{ target_dir } /{ filename_prefix } .pickledlist" )
1046+ pickled_list_hash_path = _pathlib .Path (f"{ target_dir } /{ filename_prefix } .pickledlist_sha256" )
10301047 return (pickled_list_path , pickled_list_hash_path )
10311048
10321049 @staticmethod
@@ -1105,7 +1122,7 @@ def ensure_unique_df_objects(dataframes: list[DataFrame]) -> None:
11051122 @staticmethod
11061123 def record_baseline ( # Was baseline_pickled_df_list_and_hash()
11071124 dataframes : list [DataFrame ],
1108- parent_dir : _pathlib .Path = DEFAULT_DATAFRAME_HASH_BASELINE_DIR ,
1125+ # parent_dir: _pathlib.Path = BASELINE_DATAFRAME_RECORDS_DIR_ROOT_RELATIVE ,
11091126 # Used to differentiate between multiple sets of dataframes in a single test function
11101127 # TODO can't we just bundle them:
11111128 # TODO in any case we need to detect and throw an exception when the same function calls us twice in a run...
@@ -1153,7 +1170,7 @@ def record_baseline( # Was baseline_pickled_df_list_and_hash()
11531170 DataFrameHashUtils .caller_record .add (caller_id )
11541171
11551172 pickled_objects_path , aggregate_sha256_path = DataFrameHashUtils .get_paths_for_pickle_and_hash (
1156- filename_prefix , parent_dir = parent_dir , subdir = subdir
1173+ filename_prefix , subdir = subdir
11571174 )
11581175
11591176 DataFrameHashUtils .ensure_unique_df_objects (dataframes )
@@ -1180,7 +1197,7 @@ def record_baseline( # Was baseline_pickled_df_list_and_hash()
11801197 @staticmethod
11811198 def verify ( # Was create_and_verify_pickled_df_list()
11821199 dataframes : list [DataFrame ],
1183- parent_dir : _pathlib .Path = DEFAULT_DATAFRAME_HASH_BASELINE_DIR ,
1200+ # parent_dir: _pathlib.Path = BASELINE_DATAFRAME_RECORDS_DIR_ROOT_RELATIVE ,
11841201 # Option to strictly enforce that a baseline must exist for anything this function is invoked to check:
11851202 raise_for_missing_baseline : bool = False ,
11861203 raise_rather_than_continue_for_incorrect_mode : bool = False ,
@@ -1239,7 +1256,7 @@ def verify( # Was create_and_verify_pickled_df_list()
12391256
12401257 # Determine paths on disk...
12411258 pickled_list_path , pickled_list_hash_path = DataFrameHashUtils .get_paths_for_pickle_and_hash (
1242- filename_prefix , parent_dir = parent_dir , subdir = subdir
1259+ filename_prefix , subdir = subdir
12431260 )
12441261
12451262 # Check if pickled_df_list or hash exist on disk
0 commit comments