Skip to content

Commit 4d9ad29

Browse files
committed
clean up and improve code in test"
1 parent 3c5545a commit 4d9ad29

File tree

3 files changed

+25
-34
lines changed

3 files changed

+25
-34
lines changed

tests/test_data_normalisation.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import csv
2-
import os
3-
import tempfile
4-
import builtins
5-
import pytest
6-
72
import nifipulse.config as config
83
from nifipulse.data_normalisation import process_data
9-
4+
from types import SimpleNamespace
105

116

127
def create_csv(path, rows):
@@ -52,11 +47,8 @@ def test_process_data(tmp_path, monkeypatch):
5247

5348
create_csv(input_csv, rows)
5449

55-
56-
monkeypatch.setattr(config, "env", type("E", (), {
57-
"CSV_SINK": str(input_csv),
58-
"CLEAN_DATA": str(output_csv)
59-
}))
50+
cfg = SimpleNamespace(CSV_SINK=str(input_csv), CLEAN_DATA=str(output_csv))
51+
monkeypatch.setattr(config, "env", cfg, raising=False)
6052

6153
# Run the function
6254
process_data()

tests/test_extract_metrics.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
from unittest.mock import patch, MagicMock
33
import nifipulse.extract_metrics as em
44
import 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

2725
def 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

3532
def 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

6261
def 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

7474
def 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

tests/test_load_postgre.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import pytest
21
import pandas as pd
32
from unittest.mock import patch, MagicMock
3+
from sqlalchemy.sql.elements import TextClause
44
from nifipulse.load_postgres import load_postgres
55

66

77
def test_load_postgres_success(tmp_path):
8-
98
# Create fake CSV
109
csv_file = tmp_path / "sample.csv"
1110
csv_file.write_text(
@@ -62,14 +61,18 @@ def test_load_postgres_success(tmp_path):
6261

6362
with patch("nifipulse.load_postgres.create_engine", return_value=mock_engine), \
6463
patch("nifipulse.load_postgres.pd.read_sql", side_effect=read_sql_side_effect):
65-
6664
load_postgres(str(csv_file))
6765

68-
# Assertions
69-
calls = [c.args[0].text for c in mock_conn.execute.call_args_list]
66+
# rom sqlalchemy.sql.elements import TextClause
67+
texts = []
68+
for c in mock_conn.execute.call_args_list:
69+
arg0 = c.args[0]
70+
if isinstance(arg0, TextClause):
71+
texts.append(arg0.text)
7072

71-
assert any("INSERT INTO dim_instance" in sql for sql in calls)
72-
assert any("INSERT INTO dim_metric" in sql for sql in calls)
73-
assert any("INSERT INTO dim_component" in sql for sql in calls)
74-
assert any("INSERT INTO dim_date" in sql for sql in calls)
75-
assert any("INSERT INTO fact_metrics" in sql for sql in calls)
73+
assert any("SELECT 1 FROM dim_instance" in sql for sql in texts), "Schema check missing"
74+
assert any("INSERT INTO dim_instance" in sql for sql in texts)
75+
assert any("INSERT INTO dim_metric" in sql for sql in texts)
76+
assert any("INSERT INTO dim_component" in sql for sql in texts)
77+
assert any("INSERT INTO dim_date" in sql for sql in texts)
78+
assert any("INSERT INTO fact_metrics" in sql for sql in texts), "Fact insert missing"

0 commit comments

Comments
 (0)