Coverage for tests/test_oneRoom_SimpleMPC.py: 94%

54 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2025-10-20 14:09 +0000

1import pytest 

2import pandas as pd 

3import os 

4import sys 

5from pathlib import Path 

6import importlib.util 

7import json 

8from util import module_cleanup, round_floats_in_structure 

9 

10# Add the project root to the Python path to allow for absolute imports 

11# This helps in locating the agentlib_flexquant package if needed 

12root_path = Path(__file__).parent.parent 

13sys.path.insert(0, str(root_path)) 

14 

15 

16def create_dataframe_summary(df: pd.DataFrame, precision: int = 6) -> dict: 

17 """Create a robust, compact summary of a DataFrame for snapshotting. 

18 

19 This summary is designed to be insensitive to minor floating-point differences 

20 while being highly sensitive to meaningful data changes. 

21 

22 Args: 

23 df: The pandas DataFrame to summarize. 

24 precision: The number of decimal places to round float values to. 

25 

26 Returns: 

27 A dictionary containing the summary. 

28 

29 """ 

30 if df is None or df.empty: 

31 return {"error": "DataFrame is empty or None"} 

32 

33 # Get descriptive statistics and round them to handle float precision issues 

34 summary_stats = df.describe().round(precision) 

35 

36 # Convert the stats DataFrame to a dictionary. This may have tuple keys. 

37 stats_dict_raw = summary_stats.to_dict() 

38 

39 # Create a new dictionary, converting any tuple keys into strings. 

40 # e.g., ('lower', 'P_el') becomes 'lower.P_el' 

41 stats_dict_clean = { 

42 ".".join(map(str, k)) if isinstance(k, tuple) else str(k): v 

43 for k, v in stats_dict_raw.items() 

44 } 

45 

46 # Create the final summary object 

47 summary = { 

48 "shape": df.shape, 

49 "columns": df.columns.tolist(), 

50 "index_start": str(df.index.min()), 

51 "index_end": str(df.index.max()), 

52 "statistics": stats_dict_clean, 

53 "head_5_rows": df.head(5).round(precision).to_dict(orient='split'), 

54 "tail_5_rows": df.tail(5).round(precision).to_dict(orient='split'), 

55 } 

56 return summary 

57 

58def assert_frame_matches_summary_snapshot(snapshot, df: pd.DataFrame, 

59 snapshot_name: str): 

60 """Assert that a DataFrame's summary matches a stored snapshot. 

61 

62 This function creates a summary of the dataframe and uses pytest-snapshot 

63 to compare it against a stored version. 

64 

65 """ 

66 # Create a summary of the dataframe 

67 summary = create_dataframe_summary(df) 

68 

69 # Round all numbers in the summary to handle cross-platform differences 

70 rounded_summary = round_floats_in_structure(summary, precision=5) 

71 

72 # Convert the summary dictionary to a formatted JSON string 

73 summary_json = json.dumps(rounded_summary, indent=2, sort_keys=True) 

74 

75 # Use snapshot.assert_match on the small, stable JSON string 

76 snapshot.assert_match(summary_json, snapshot_name) 

77 

78def run_example_from_path(example_path: Path): 

79 """Dynamically import and run the 'run_example' function from a script 

80 in the specified directory. 

81 

82 This function robustly handles changing the working directory AND the 

83 Python import path, ensuring the script can find both its local files 

84 and its local modules. 

85 

86 """ 

87 run_script_path = example_path / 'main_one_room_flex.py' 

88 if not run_script_path.is_file(): 

89 raise FileNotFoundError( 

90 f"Could not find the run script at {run_script_path}. " 

91 "Please ensure it is named 'run.py' or adjust the test code." 

92 ) 

93 

94 # --- SETUP: Store original paths before changing them --- 

95 original_cwd = Path.cwd() 

96 original_sys_path = sys.path[:] # Create a copy of the sys.path list 

97 

98 module_name = f"agentlib_flexquant.tests.examples.{example_path.name}" 

99 

100 try: 

101 # --- STEP 1: Change CWD for file access (e.g., config.json) --- 

102 os.chdir(example_path) 

103 

104 # --- STEP 2: Add example dir to sys.path for module imports --- 

105 sys.path.insert(0, str(example_path)) 

106 

107 # Dynamically import the run_example function from the script 

108 spec = importlib.util.spec_from_file_location(module_name, run_script_path) 

109 run_module = importlib.util.module_from_spec(spec) 

110 sys.modules[module_name] = run_module 

111 spec.loader.exec_module(run_module) 

112 

113 if not hasattr(run_module, 'run_example'): 

114 raise AttributeError( 

115 "The 'run.py' script must contain a 'run_example' function.") 

116 

117 # Execute the function and get the results 

118 results = run_module.run_example(until=3600) 

119 return results 

120 

121 finally: 

122 # --- TEARDOWN: Always restore original paths to avoid side-effects --- 

123 os.chdir(original_cwd) 

124 sys.path[:] = original_sys_path # Restore the original sys.path 

125 

126def test_oneroom_simple_mpc(snapshot, module_cleanup): 

127 """Unit test for the oneroom_simpleMPC example using snapshot testing. 

128 

129 This test runs the example via its own run script and compares the 

130 full resulting dataframes against stored snapshots. 

131 

132 """ 

133 # Define the path to the example directory 

134 example_path = root_path / 'examples' / 'OneRoom_SimpleMPC' 

135 

136 # Run the example and get the results object 

137 res = run_example_from_path(example_path) 

138 

139 # Extract the full resulting dataframes as requested 

140 df_neg_flex_res = res["NegFlexMPC"]["NegFlexMPC"] 

141 df_pos_flex_res = res["PosFlexMPC"]["PosFlexMPC"] 

142 df_baseline_res = res["FlexModel"]["Baseline"] 

143 df_indicator_res = res["FlexibilityIndicator"]["FlexibilityIndicator"] 

144 

145 # Assert that a summary of each result DataFrame matches its snapshot 

146 assert_frame_matches_summary_snapshot( 

147 snapshot, 

148 df_neg_flex_res, 

149 'oneroom_simpleMPC_neg_flex_summary.json' 

150 ) 

151 assert_frame_matches_summary_snapshot( 

152 snapshot, 

153 df_pos_flex_res, 

154 'oneroom_simpleMPC_pos_flex_summary.json' 

155 ) 

156 assert_frame_matches_summary_snapshot( 

157 snapshot, 

158 df_baseline_res, 

159 'oneroom_simpleMPC_baseline_summary.json' 

160 ) 

161 assert_frame_matches_summary_snapshot( 

162 snapshot, 

163 df_indicator_res, 

164 'oneroom_simpleMPC_indicator_summary.json' 

165 )