|
| 1 | +import csv |
| 2 | +import os |
| 3 | +import tempfile |
| 4 | +import builtins |
| 5 | +import pytest |
| 6 | + |
| 7 | +import nifipulse.config as config |
| 8 | +from nifipulse.data_normalisation import process_data |
| 9 | + |
| 10 | + |
| 11 | +# ----------------------------- |
| 12 | +# Helper to create temp CSV data |
| 13 | +# ----------------------------- |
| 14 | +def create_csv(path, rows): |
| 15 | + fieldnames = ["timestamp", "instance", "metric_name", "component_name", |
| 16 | + "component_type", "component_id", "value"] |
| 17 | + |
| 18 | + with open(path, "w", newline='', encoding='utf-8') as f: |
| 19 | + writer = csv.DictWriter(f, fieldnames=fieldnames) |
| 20 | + writer.writeheader() |
| 21 | + for r in rows: |
| 22 | + writer.writerow(r) |
| 23 | + |
| 24 | + |
| 25 | +# ----------------------------- |
| 26 | +# TEST: process_data filters, maps, converts and writes results |
| 27 | +# ----------------------------- |
| 28 | +def test_process_data(tmp_path, monkeypatch): |
| 29 | + # Temporary paths |
| 30 | + input_csv = tmp_path / "input.csv" |
| 31 | + output_csv = tmp_path / "clean.csv" |
| 32 | + |
| 33 | + # Fake rows |
| 34 | + rows = [ |
| 35 | + # 👎 should be filtered (value = 0 AND metric starts with nifi_amount) |
| 36 | + { |
| 37 | + "timestamp": "2025-01-01T00:00:00Z", |
| 38 | + "instance": "nifi1", |
| 39 | + "metric_name": "nifi_amount_flowfiles_received", |
| 40 | + "component_name": "ProcessorA", |
| 41 | + "component_type": "PROCESSOR", |
| 42 | + "component_id": "abc123", |
| 43 | + "value": "0" |
| 44 | + }, |
| 45 | + # 👍 should be kept and mapped |
| 46 | + { |
| 47 | + "timestamp": "2025-01-01T00:01:00Z", |
| 48 | + "instance": "nifi1", |
| 49 | + "metric_name": "nifi_amount_bytes_read", |
| 50 | + "component_name": "ProcessorB", |
| 51 | + "component_type": "PROCESSOR", |
| 52 | + "component_id": "xyz999", |
| 53 | + "value": "150" |
| 54 | + } |
| 55 | + ] |
| 56 | + |
| 57 | + create_csv(input_csv, rows) |
| 58 | + |
| 59 | + # Override config.env paths |
| 60 | + monkeypatch.setattr(config, "env", type("E", (), { |
| 61 | + "CSV_SINK": str(input_csv), |
| 62 | + "CLEAN_DATA": str(output_csv) |
| 63 | + })) |
| 64 | + |
| 65 | + # Run the function |
| 66 | + process_data() |
| 67 | + |
| 68 | + # ----------- Validate output CSV ----------- |
| 69 | + assert output_csv.exists(), "Output file should be created" |
| 70 | + |
| 71 | + with open(output_csv, encoding='utf-8') as f: |
| 72 | + reader = list(csv.DictReader(f)) |
| 73 | + |
| 74 | + # One row should remain |
| 75 | + assert len(reader) == 1 |
| 76 | + |
| 77 | + row = reader[0] |
| 78 | + |
| 79 | + # Validate correct mapping |
| 80 | + assert row["metric_name"] == "bytes_read" |
| 81 | + |
| 82 | + # Validate unit detection |
| 83 | + assert row["original_unit"] == "bytes" |
| 84 | + |
| 85 | + # Validate numeric conversion |
| 86 | + assert row["value"] == "150.0" |
| 87 | + |
| 88 | + # Validate ID propagation |
| 89 | + assert row["unique_id"] == "xyz999" |
| 90 | + |
| 91 | + # Validate timestamp copy |
| 92 | + assert row["timestamp_utc"] == "2025-01-01T00:01:00Z" |
0 commit comments