@@ -314,3 +314,115 @@ def test_compare_normalizations_with_invalid_json(self) -> None:
314314 Path (current_file .name ).unlink (missing_ok = True )
315315 Path (previous_file .name ).unlink (missing_ok = True )
316316 Path (output_file .name ).unlink (missing_ok = True )
317+
318+ def test_increased_patterns_contains_required_fields (
319+ self , sample_current_data : Dict [str , Any ], sample_previous_data : Dict [str , Any ]
320+ ) -> None :
321+ """Test that increased patterns contain all fields required by the email generation."""
322+ increased_patterns = _find_increased_patterns (sample_current_data , sample_previous_data )
323+
324+ # There should be one increased pattern based on our test data
325+ assert len (increased_patterns ) == 1
326+ pattern = increased_patterns [0 ]
327+
328+ # The increased pattern should have proper pattern text
329+ assert pattern ["pattern" ] == "Error message 1"
330+
331+ # Verify it contains all the fields required by email generation
332+ assert "count" in pattern , "Pattern is missing 'count' field"
333+ assert pattern ["count" ] == 10 , "Pattern has incorrect current count"
334+
335+ # These fields are expected by step9_generate_email_bodies.py but are missing
336+ assert "current_count" in pattern , "Pattern is missing 'current_count' field"
337+ assert pattern ["current_count" ] == 10 , "Pattern has incorrect current count"
338+ assert "previous_count" in pattern , "Pattern is missing 'previous_count' field"
339+ assert pattern ["previous_count" ] == 5 , "Pattern has incorrect previous count"
340+ assert "absolute_change" in pattern , "Pattern is missing 'absolute_change' field"
341+ assert pattern ["absolute_change" ] == 5 , "Pattern has incorrect absolute change"
342+ assert "percent_change" in pattern , "Pattern is missing 'percent_change' field"
343+ assert pattern ["percent_change" ] == 100 , "Pattern has incorrect percent change"
344+
345+ def test_decreased_patterns_contains_required_fields (
346+ self , sample_current_data : Dict [str , Any ], sample_previous_data : Dict [str , Any ]
347+ ) -> None :
348+ """Test that decreased patterns contain all fields required by the email generation."""
349+ decreased_patterns = _find_decreased_patterns (sample_current_data , sample_previous_data )
350+
351+ # There should be one decreased pattern based on our test data
352+ assert len (decreased_patterns ) == 1
353+ pattern = decreased_patterns [0 ]
354+
355+ # The decreased pattern should have proper pattern text
356+ assert pattern ["pattern" ] == "Error message 2"
357+
358+ # Verify it contains all the fields required by email generation
359+ assert "count" in pattern , "Pattern is missing 'count' field"
360+ assert pattern ["count" ] == 5 , "Pattern has incorrect current count"
361+
362+ # These fields are expected by step9_generate_email_bodies.py but are missing
363+ assert "current_count" in pattern , "Pattern is missing 'current_count' field"
364+ assert pattern ["current_count" ] == 5 , "Pattern has incorrect current count"
365+ assert "previous_count" in pattern , "Pattern is missing 'previous_count' field"
366+ assert pattern ["previous_count" ] == 10 , "Pattern has incorrect previous count"
367+ assert "absolute_change" in pattern , "Pattern is missing 'absolute_change' field"
368+ assert pattern ["absolute_change" ] == 5 , "Pattern has incorrect absolute change"
369+ assert "percent_change" in pattern , "Pattern is missing 'percent_change' field"
370+ assert pattern ["percent_change" ] == 50 , "Pattern has incorrect percent change"
371+
372+ def test_no_patterns_with_zero_change (self ) -> None :
373+ """Test that patterns with zero change are not included in increased or decreased lists."""
374+ # Create test data with patterns that have the same count
375+ current_data = {
376+ "patterns" : [
377+ {"cluster_id" : 1 , "count" : 10 , "pattern" : "Same count pattern" , "sample_doc_references" : ["index:id1" ]}
378+ ]
379+ }
380+
381+ previous_data = {
382+ "patterns" : [
383+ {"cluster_id" : 1 , "count" : 10 , "pattern" : "Same count pattern" , "sample_doc_references" : ["index:id1" ]}
384+ ]
385+ }
386+
387+ # There should be no increased patterns
388+ increased_patterns = _find_increased_patterns (current_data , previous_data )
389+ assert len (increased_patterns ) == 0 , "Patterns with no increase should not be in increased_patterns"
390+
391+ # There should be no decreased patterns
392+ decreased_patterns = _find_decreased_patterns (current_data , previous_data )
393+ assert len (decreased_patterns ) == 0 , "Patterns with no decrease should not be in decreased_patterns"
394+
395+ def test_zero_counts_handled_correctly (self ) -> None :
396+ """Test that zero counts are handled correctly in comparison."""
397+ # Create test data with patterns that have zero count
398+ current_data = {
399+ "patterns" : [
400+ {"cluster_id" : 1 , "count" : 0 , "pattern" : "Zero count current" , "sample_doc_references" : ["index:id1" ]},
401+ {"cluster_id" : 2 , "count" : 5 , "pattern" : "Zero count previous" , "sample_doc_references" : ["index:id2" ]},
402+ ]
403+ }
404+
405+ previous_data = {
406+ "patterns" : [
407+ {"cluster_id" : 1 , "count" : 5 , "pattern" : "Zero count current" , "sample_doc_references" : ["index:id1" ]},
408+ {"cluster_id" : 2 , "count" : 0 , "pattern" : "Zero count previous" , "sample_doc_references" : ["index:id2" ]},
409+ ]
410+ }
411+
412+ # "Zero count current" should be in decreased patterns
413+ decreased_patterns = _find_decreased_patterns (current_data , previous_data )
414+ assert len (decreased_patterns ) == 1
415+ assert decreased_patterns [0 ]["pattern" ] == "Zero count current"
416+ assert "current_count" in decreased_patterns [0 ], "Pattern is missing 'current_count' field"
417+ assert "previous_count" in decreased_patterns [0 ], "Pattern is missing 'previous_count' field"
418+ assert "absolute_change" in decreased_patterns [0 ], "Pattern is missing 'absolute_change' field"
419+ assert "percent_change" in decreased_patterns [0 ], "Pattern is missing 'percent_change' field"
420+
421+ # "Zero count previous" should be in increased patterns
422+ increased_patterns = _find_increased_patterns (current_data , previous_data )
423+ assert len (increased_patterns ) == 1
424+ assert increased_patterns [0 ]["pattern" ] == "Zero count previous"
425+ assert "current_count" in increased_patterns [0 ], "Pattern is missing 'current_count' field"
426+ assert "previous_count" in increased_patterns [0 ], "Pattern is missing 'previous_count' field"
427+ assert "absolute_change" in increased_patterns [0 ], "Pattern is missing 'absolute_change' field"
428+ assert "percent_change" in increased_patterns [0 ], "Pattern is missing 'percent_change' field"
0 commit comments