22from unittest .mock import patch , MagicMock
33import nifipulse .extract_metrics as em
44import nifipulse .config as config
5- import os
6-
75
86# FIXTURE POUR MOCKER config.env + CRÉER results/
97@pytest .fixture
@@ -25,65 +23,63 @@ class Env:
2523# TESTS poll_metrics()
2624
2725def test_poll_metrics_no_metrics (mock_env ):
28-
2926 with patch ("builtins.print" ) as mock_print :
3027 em .poll_metrics (metrics = [])
3128 mock_print .assert_called_with ("No metrics provided to poll." )
3229
3330
3431
3532def test_poll_metrics_calls_prometheus_and_writes_csv (mock_env ):
36-
3733 metrics = ["nifi_metric" ]
3834 # Mock réponse Prometheus
3935 fake_resp = MagicMock ()
36+ fake_resp .raise_for_status .return_value = None
4037 fake_resp .json .return_value = {
4138 "data" : {
4239 "result" : [{
43- "metric" : {"instance" : "nifi:9092" , "component_name" : "UpdateAttribute" },
44- "value" : ["0" , "5" ]
40+ "metric" : {"instance" : "nifi:9092" , "component_name" : "UpdateAttribute" , "component_id" : "cid1" , "component_type" : "PROCESSOR" },
41+ "value" : [1700000000 , "5" ]
4542 }]
4643 }
4744 }
4845
49- with patch ("requests.get" , return_value = fake_resp ):
46+ # Patch where it's used
47+ with patch ("nifipulse.extract_metrics.requests.get" , return_value = fake_resp ):
5048 em .poll_metrics (metrics = metrics , interval = 0 , count = 1 )
5149
5250 # Vérifier écriture du CSV
5351 with open (config .env .CSV_SINK ) as f :
5452 lines = f .readlines ()
5553
5654 assert len (lines ) == 2 # header + data line
57-
55+ assert "UpdateAttribute" in lines [1 ]
56+ assert ",5" in lines [1 ]
5857
5958
6059# TESTS nifipulse()
6160
6261def test_nifipulse_no_rows (mock_env ):
63-
6462 with patch ("nifipulse.extract_metrics.path_tofolder" , return_value = True ), \
6563 patch ("nifipulse.extract_metrics.files" ) as mock_files , \
64+ patch ("nifipulse.extract_metrics.poll_metrics" ) as mock_poll , \
6665 patch ("nifipulse.extract_metrics._csv_has_rows" , return_value = False ), \
6766 patch ("builtins.print" ) as mock_print :
6867 mock_files .return_value .joinpath .return_value .read_text .return_value = "metric1"
6968 em .nifipulse (poll_count = 1 , interval = 0 )
69+ mock_poll .assert_called_once ()
7070 mock_print .assert_any_call ("No polled rows written; skipping normalization." )
7171
7272
7373
7474def test_nifipulse_full_flow (mock_env ):
75-
7675 with patch ("nifipulse.extract_metrics.path_tofolder" , return_value = True ), \
7776 patch ("nifipulse.extract_metrics.files" ) as mock_files , \
7877 patch ("nifipulse.extract_metrics._csv_has_rows" , side_effect = [True , True ]), \
7978 patch ("nifipulse.extract_metrics.process_data" ) as mock_process , \
8079 patch ("nifipulse.extract_metrics.load_postgres" ) as mock_pg , \
8180 patch ("nifipulse.extract_metrics.poll_metrics" ):
82-
8381 mock_files .return_value .joinpath .return_value .read_text .return_value = "metricX"
84-
8582 em .nifipulse (poll_count = 1 , interval = 0 )
86-
8783 mock_process .assert_called_once ()
8884 mock_pg .assert_called_once ()
8985
0 commit comments