@@ -64,17 +64,80 @@ def test_eventing_app(self):
6464 self .assertEqual (state .title , "eventing_app" )
6565
6666 jobs = self .service .jobs
67+
68+ base_search = self ._create_test_data_search (count = 100 )
69+ full_search = (
70+ base_search
71+ + '''
72+ | eventingcsc status=200
73+ | head 10
74+ '''
75+ )
76+
77+ stream = jobs .oneshot (full_search , output_mode = 'json' )
78+
79+ reader = results .JSONResultsReader (stream )
80+ items = list (reader )
81+
82+ actual_results = [item for item in items if isinstance (item , dict )]
83+ informational_messages = [
84+ item for item in items if isinstance (item , results .Message )
85+ ]
86+
87+ self .assertTrue (len (actual_results ) > 0 )
88+
89+ fatal_messages = [
90+ msg for msg in informational_messages if msg .type in ['FATAL' , 'ERROR' ]
91+ ]
92+ self .assertEqual (
93+ len (fatal_messages ),
94+ 0 ,
95+ f"Should not have FATAL/ERROR messages, but got: { [msg .message for msg in fatal_messages ]} " ,
96+ )
97+
98+ first_result = actual_results [0 ]
99+ self .assertIn ('status' , first_result )
100+ self .assertEqual (first_result ['status' ], '200' )
101+
102+ def test_eventing_app_malformed_query_missing_field (self ):
103+ app_name = "eventing_app"
104+ # Fetch the app
105+ app = self .service .apps [app_name ]
106+ app .refresh ()
107+
108+ jobs = self .service .jobs
109+ # wrong search query because `status` field is not being passed to eventing app
67110 stream = jobs .oneshot (
68111 'search index="_internal" | head 4000 | eventingcsc status=200 | head 10' ,
69- output_mode = " json" ,
112+ output_mode = ' json' ,
70113 )
71- result = results .JSONResultsReader (stream )
72- ds = list (result )
114+ reader = results .JSONResultsReader (stream )
115+ items = list (reader )
116+
117+ self .assertTrue (len (items ) > 0 , "Should have at least one item (error message)" )
118+
119+ actual_results = [item for item in items if isinstance (item , dict )]
120+ informational_messages = [
121+ item for item in items if isinstance (item , results .Message )
122+ ]
73123
74- self .assertEqual (result .is_preview , False )
75- self .assertTrue (isinstance (ds [0 ], (dict , results .Message )))
76- nonmessages = [d for d in ds if isinstance (d , dict )]
77- self .assertTrue (len (nonmessages ) <= 10 )
124+ self .assertTrue (len (informational_messages ) > 0 )
125+
126+ error_messages = [
127+ msg for msg in informational_messages if msg .type in ['FATAL' , 'ERROR' ]
128+ ]
129+ self .assertTrue (len (error_messages ) > 0 )
130+
131+ error_message = error_messages [0 ]
132+ self .assertIn ('eventingcsc' , error_message .message )
133+ self .assertIn ('status' , error_message .message )
134+ self .assertIn ('KeyError' , error_message .message )
135+
136+ self .assertEqual (
137+ len (actual_results ),
138+ 0 ,
139+ f"Should not have actual results, but got: { actual_results } " ,
140+ )
78141
79142 def test_generating_app (self ):
80143 app_name = "generating_app"
@@ -257,6 +320,22 @@ def test_streaming_app(self):
257320 self .assertTrue (ds [0 ]["fahrenheit" ] == "95.0" )
258321 self .assertTrue (len (ds ) == 5 )
259322
323+ def _create_test_data_search (self , count = 100 ):
324+ """Helper to create deterministic test data using Splunk search commands."""
325+ return f'''
326+ | makeresults count={ count }
327+ | streamstats count as row_num
328+ | eval _time=_time - (row_num * 60)
329+ | eval status=case(
330+ (row_num % 10) < 7, 200,
331+ (row_num % 10) < 9, 404,
332+ 1=1, 500
333+ )
334+ | eval response_time=100 + ((row_num * 37) % 1000)
335+ | eval user_id="user" + tostring(row_num % 50)
336+ | eval _raw=strftime(_time, "%Y-%m-%d %H:%M:%S") + " status=" + tostring(status) + " response_time=" + tostring(response_time) + "ms user=" + user_id
337+ '''
338+
260339
261340if __name__ == "__main__" :
262341 unittest .main ()
0 commit comments