@@ -37,9 +37,26 @@ def load_sample_data(self) -> None:
3737 This helper abstracts the data loading flow, making tests immune to
3838 changes in the data loading implementation.
3939 """
40+ import time
41+
4042 try :
4143 # Click the Load Sample Data button using its stable key with increased timeout
42- self .at .button (key = "load_sample_data_button" ).click ().run (timeout = 10 )
44+ self .at .button (key = "load_sample_data_button" ).click ().run (timeout = 15 )
45+
46+ # Wait for data loading to complete by checking session state
47+ max_retries = 5
48+ for attempt in range (max_retries ):
49+ if (
50+ hasattr (self .at .session_state , "events_data" )
51+ and self .at .session_state .events_data is not None
52+ and len (self .at .session_state .events_data ) > 0
53+ ):
54+ # Data loaded successfully, now wait for event_statistics
55+ break
56+ time .sleep (0.5 ) # Wait 500ms between retries
57+ if attempt < max_retries - 1 : # Don't run on last attempt
58+ self .at .run (timeout = 10 ) # Re-run to refresh state
59+
4360 except Exception as e :
4461 # If button interaction fails, manually load sample data for testing
4562 from datetime import datetime , timedelta
@@ -67,10 +84,52 @@ def load_sample_data(self) -> None:
6784
6885 self .at .session_state .events_data = pd .DataFrame (data )
6986
87+ # CRITICAL FIX: Calculate event_statistics manually when using fallback
88+ # Import the function from app.py
89+ import os
90+ import sys
91+
92+ sys .path .insert (0 , os .path .dirname (os .path .dirname (__file__ )))
93+ from app import get_event_statistics
94+
95+ self .at .session_state .event_statistics = get_event_statistics (
96+ self .at .session_state .events_data
97+ )
98+
7099 # Verify data was loaded by checking session state
71100 assert self .at .session_state .events_data is not None , "Sample data should be loaded"
72101 assert len (self .at .session_state .events_data ) > 0 , "Sample data should not be empty"
73102
103+ # Wait for event_statistics to be calculated (give it more time)
104+ max_retries = 10
105+ for attempt in range (max_retries ):
106+ if (
107+ hasattr (self .at .session_state , "event_statistics" )
108+ and len (self .at .session_state .event_statistics ) > 0
109+ ):
110+ break
111+ time .sleep (0.3 ) # Wait 300ms between retries
112+ if attempt < max_retries - 1 : # Don't run on last attempt
113+ self .at .run (
114+ timeout = 10
115+ ) # Re-run to refresh state and trigger statistics calculation
116+
117+ # Final verification that event_statistics was calculated
118+ if (
119+ not hasattr (self .at .session_state , "event_statistics" )
120+ or len (self .at .session_state .event_statistics ) == 0
121+ ):
122+ # Last resort: manually calculate if still not present
123+ import os
124+ import sys
125+
126+ sys .path .insert (0 , os .path .dirname (os .path .dirname (__file__ )))
127+ from app import get_event_statistics
128+
129+ self .at .session_state .event_statistics = get_event_statistics (
130+ self .at .session_state .events_data
131+ )
132+
74133 def build_funnel (self , steps : List [str ]) -> None :
75134 """
76135 Build a funnel by selecting the specified steps.
0 commit comments