|
| 1 | +import pytest |
| 2 | +import json |
| 3 | +from policyengine_api.services.tracer_analysis_service import ( |
| 4 | + TracerAnalysisService, |
| 5 | +) |
| 6 | +from werkzeug.exceptions import NotFound |
| 7 | + |
| 8 | +from tests.fixtures.services.tracer_analysis_service import ( |
| 9 | + sample_tracer_data, |
| 10 | + sample_expected_segment, |
| 11 | + mock_get_tracer, |
| 12 | + mock_get_existing_analysis, |
| 13 | + mock_parse_tracer_output, |
| 14 | + mock_trigger_ai_analysis, |
| 15 | +) |
| 16 | + |
| 17 | +service = TracerAnalysisService() |
| 18 | +country_id = "us" |
| 19 | +household_id = "71424" |
| 20 | +policy_id = "2" |
| 21 | +target_variable = "takes_up_snap_if_eligible" |
| 22 | + |
| 23 | + |
| 24 | +class TestExecuteAnalysis: |
| 25 | + def test_execute_analysis_static( |
| 26 | + self, |
| 27 | + mock_get_tracer, |
| 28 | + mock_parse_tracer_output, |
| 29 | + mock_get_existing_analysis, |
| 30 | + ): |
| 31 | + """ |
| 32 | + GIVEN a valid tracer data and an expected parsed segment (included as fixture), |
| 33 | + AND get_existing_analysis returns a static analysis (included as fixture), |
| 34 | + WHEN execute_analysis is called, |
| 35 | + THEN then a static analysis with the "static" flag should be returned. |
| 36 | + """ |
| 37 | + |
| 38 | + analysis, analysis_type = service.execute_analysis( |
| 39 | + country_id, household_id, policy_id, target_variable |
| 40 | + ) |
| 41 | + |
| 42 | + assert analysis == "Existing static analysis" |
| 43 | + assert analysis_type == "static" |
| 44 | + |
| 45 | + def test_execute_analysis_streaming( |
| 46 | + self, |
| 47 | + mock_get_tracer, |
| 48 | + mock_parse_tracer_output, |
| 49 | + mock_get_existing_analysis, |
| 50 | + mock_trigger_ai_analysis, |
| 51 | + ): |
| 52 | + """ |
| 53 | + GIVEN a valid tracer data and an expected parsed segment, |
| 54 | + AND get_existing_analysis returns None, |
| 55 | + WHEN execute_analysis is called, |
| 56 | + THEN trigger_ai_analysis is called and returns a generator with the "streaming" flag. |
| 57 | + """ |
| 58 | + |
| 59 | + # When existing analysis value is None |
| 60 | + mock_get_existing_analysis.return_value = None |
| 61 | + |
| 62 | + analysis, analysis_type = service.execute_analysis( |
| 63 | + country_id, household_id, policy_id, target_variable |
| 64 | + ) |
| 65 | + |
| 66 | + expected_streaming_output = ["stream chunk 1", "stream chunk 2"] |
| 67 | + streaming_output = list(analysis) |
| 68 | + assert streaming_output == expected_streaming_output |
| 69 | + assert analysis_type == "streaming" |
0 commit comments