| 1 |
import argparse |
| 2 |
from typing import Any |
| 3 |
import unittest |
| 4 |
from unittest.mock import MagicMock, mock_open, patch |
| 5 |
from con_duct import cli, pprint_json |
| 6 |
from con_duct._formatter import SummaryFormatter |
| 7 |
|
| 8 |
|
| 9 |
class TestPPrint: |
| 10 |
|
| 11 |
@patch("con_duct.pprint_json.pprint") |
| 12 |
def test_pprint_json(self, mock_pprint: MagicMock, tmp_path: Any) -> None: |
| 13 |
json_file = tmp_path / "test.json" |
| 14 |
json_file.write_text('{"mock_key": "mock_value"}') |
| 15 |
|
| 16 |
args = argparse.Namespace( |
| 17 |
command="pp", |
| 18 |
file_path=str(json_file), |
| 19 |
func=pprint_json.pprint_json, |
| 20 |
log_level="INFO", |
| 21 |
humanize=False, |
| 22 |
) |
| 23 |
assert cli.execute(args) == 0 |
| 24 |
mock_pprint.assert_called_once_with({"mock_key": "mock_value"}) |
| 25 |
|
| 26 |
@patch("con_duct.pprint_json.pprint") |
| 27 |
def test_pprint_jsonl(self, mock_pprint: MagicMock, tmp_path: Any) -> None: |
| 28 |
jsonl_file = tmp_path / "test.jsonl" |
| 29 |
jsonl_file.write_text('{"line": 1}\n{"line": 2}\n') |
| 30 |
|
| 31 |
args = argparse.Namespace( |
| 32 |
command="pp", |
| 33 |
file_path=str(jsonl_file), |
| 34 |
func=pprint_json.pprint_json, |
| 35 |
log_level="INFO", |
| 36 |
humanize=False, |
| 37 |
) |
| 38 |
assert cli.execute(args) == 0 |
| 39 |
mock_pprint.assert_called_once_with([{"line": 1}, {"line": 2}]) |
| 40 |
|
| 41 |
def test_file_not_found(self) -> None: |
| 42 |
args = argparse.Namespace( |
| 43 |
command="pp", |
| 44 |
file_path="/nonexistent/path.json", |
| 45 |
func=pprint_json.pprint_json, |
| 46 |
log_level="INFO", |
| 47 |
humanize=False, |
| 48 |
) |
| 49 |
assert cli.execute(args) == 1 |
| 50 |
|
| 51 |
@patch("con_duct.pprint_json.pprint") |
| 52 |
def test_pprint_invalid_json(self, mock_pprint: MagicMock, tmp_path: Any) -> None: |
| 53 |
json_file = tmp_path / "invalid.json" |
| 54 |
json_file.write_text('{"invalid": "json"') |
| 55 |
|
| 56 |
args = argparse.Namespace( |
| 57 |
command="pp", |
| 58 |
file_path=str(json_file), |
| 59 |
func=pprint_json.pprint_json, |
| 60 |
log_level="INFO", |
| 61 |
humanize=False, |
| 62 |
) |
| 63 |
assert cli.execute(args) == 1 |
| 64 |
mock_pprint.assert_not_called() |
| 65 |
|
| 66 |
|
| 67 |
class TestPPrintHumanization(unittest.TestCase): |
| 68 |
"""Test humanization functionality in pprint_json module""" |
| 69 |
|
| 70 |
def test_apply_conversion_with_numbers(self) -> None: |
| 71 |
"""Test _apply_conversion with numeric values""" |
| 72 |
|
| 73 |
formatter = SummaryFormatter() |
| 74 |
field_mapping = pprint_json.get_field_conversion_mapping() |
| 75 |
|
| 76 |
result = pprint_json._apply_conversion( |
| 77 |
"average_pcpu", 85.567, field_mapping, formatter |
| 78 |
) |
| 79 |
assert result == "85.57%" |
| 80 |
|
| 81 |
result = pprint_json._apply_conversion( |
| 82 |
"peak_rss", 1024000, field_mapping, formatter |
| 83 |
) |
| 84 |
assert result == "1.0 MB" |
| 85 |
|
| 86 |
result = pprint_json._apply_conversion( |
| 87 |
"memory_total", 1081801371648, field_mapping, formatter |
| 88 |
) |
| 89 |
assert result == "1.1 TB" |
| 90 |
|
| 91 |
result = pprint_json._apply_conversion( |
| 92 |
"wall_clock_time", 3661.5, field_mapping, formatter |
| 93 |
) |
| 94 |
assert result == "1h 1m 1.5s" |
| 95 |
|
| 96 |
result = pprint_json._apply_conversion( |
| 97 |
"start_time", 1625400000, field_mapping, formatter |
| 98 |
) |
| 99 |
assert "Jul" in result and "2021" in result |
| 100 |
|
| 101 |
def test_apply_conversion_with_non_numbers(self) -> None: |
| 102 |
"""Test _apply_conversion with non-numeric values""" |
| 103 |
|
| 104 |
formatter = SummaryFormatter() |
| 105 |
field_mapping = pprint_json.get_field_conversion_mapping() |
| 106 |
|
| 107 |
|
| 108 |
result = pprint_json._apply_conversion( |
| 109 |
"average_pcpu", "unknown", field_mapping, formatter |
| 110 |
) |
| 111 |
assert result == "unknown" |
| 112 |
|
| 113 |
|
| 114 |
result = pprint_json._apply_conversion( |
| 115 |
"average_pcpu", None, field_mapping, formatter |
| 116 |
) |
| 117 |
assert result is None |
| 118 |
|
| 119 |
def test_apply_conversion_unmapped_fields(self) -> None: |
| 120 |
"""Test _apply_conversion with unmapped field names""" |
| 121 |
|
| 122 |
formatter = SummaryFormatter() |
| 123 |
field_mapping = pprint_json.get_field_conversion_mapping() |
| 124 |
|
| 125 |
|
| 126 |
result = pprint_json._apply_conversion( |
| 127 |
"unknown_field", 42.0, field_mapping, formatter |
| 128 |
) |
| 129 |
assert result == 42.0 |
| 130 |
|
| 131 |
|
| 132 |
result = pprint_json._apply_conversion( |
| 133 |
"unknown_field", "some_string", field_mapping, formatter |
| 134 |
) |
| 135 |
assert result == "some_string" |
| 136 |
|
| 137 |
def test_humanize_data_simple_dict(self) -> None: |
| 138 |
"""Test humanize_data with a simple dictionary""" |
| 139 |
|
| 140 |
data = { |
| 141 |
"average_pcpu": 85.567, |
| 142 |
"average_rss": 1024000, |
| 143 |
"wall_clock_time": 150.75, |
| 144 |
"unknown_field": 42, |
| 145 |
"string_field": "test", |
| 146 |
} |
| 147 |
|
| 148 |
formatter = SummaryFormatter() |
| 149 |
result = pprint_json.humanize_data(data, formatter) |
| 150 |
|
| 151 |
assert result["average_pcpu"] == "85.57%" |
| 152 |
assert result["average_rss"] == "1.0 MB" |
| 153 |
assert result["wall_clock_time"] == "2m 30.8s" |
| 154 |
assert result["unknown_field"] == 42 |
| 155 |
assert result["string_field"] == "test" |
| 156 |
|
| 157 |
def test_humanize_data_nested_structures(self) -> None: |
| 158 |
"""Test humanize_data with nested dictionaries and lists""" |
| 159 |
|
| 160 |
data = { |
| 161 |
"process": {"peak_pcpu": 75.0, "average_vsz": 512000}, |
| 162 |
"samples": [ |
| 163 |
{"average_pcpu": 80.5, "peak_rss": 2048000}, |
| 164 |
{"average_pcpu": 90.0, "peak_rss": 3072000}, |
| 165 |
], |
| 166 |
} |
| 167 |
|
| 168 |
formatter = SummaryFormatter() |
| 169 |
result = pprint_json.humanize_data(data, formatter) |
| 170 |
|
| 171 |
|
| 172 |
assert result["process"]["peak_pcpu"] == "75.00%" |
| 173 |
assert result["process"]["average_vsz"] == "512.0 kB" |
| 174 |
|
| 175 |
|
| 176 |
assert result["samples"][0]["average_pcpu"] == "80.50%" |
| 177 |
assert result["samples"][0]["peak_rss"] == "2.0 MB" |
| 178 |
assert result["samples"][1]["average_pcpu"] == "90.00%" |
| 179 |
assert result["samples"][1]["peak_rss"] == "3.1 MB" |
| 180 |
|
| 181 |
@patch( |
| 182 |
"builtins.open", |
| 183 |
new_callable=mock_open, |
| 184 |
read_data='{"average_pcpu": 85.567, "average_rss": 1024000, "wall_clock_time": 150.75}', |
| 185 |
) |
| 186 |
@patch("con_duct.pprint_json.pprint") |
| 187 |
def test_pprint_json_with_humanize( |
| 188 |
self, mock_pprint: MagicMock, mock_open: MagicMock |
| 189 |
) -> None: |
| 190 |
"""Test pprint_json with humanize=True""" |
| 191 |
args = argparse.Namespace( |
| 192 |
command="pp", |
| 193 |
file_path="dummy.json", |
| 194 |
func=pprint_json.pprint_json, |
| 195 |
log_level="INFO", |
| 196 |
humanize=True, |
| 197 |
) |
| 198 |
|
| 199 |
assert cli.execute(args) == 0 |
| 200 |
|
| 201 |
mock_open.assert_called_with("dummy.json", "r") |
| 202 |
|
| 203 |
|
| 204 |
call_args = mock_pprint.call_args[0][0] |
| 205 |
assert call_args["average_pcpu"] == "85.57%" |
| 206 |
assert call_args["average_rss"] == "1.0 MB" |
| 207 |
assert call_args["wall_clock_time"] == "2m 30.8s" |