|
| 1 | +"""Tests for CLI functions.""" |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from alpacloud.promls.cli import PrintMode, do_print |
| 8 | +from alpacloud.promls.filter import MetricsTree |
| 9 | +from alpacloud.promls.metrics import Metric |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def test_metrics(): |
| 14 | + """Common test data for all PrintMode tests.""" |
| 15 | + return [ |
| 16 | + Metric(name="http_requests_total", help="Total HTTP requests", type="counter"), |
| 17 | + Metric(name="http_requests_failed", help="Failed HTTP requests", type="counter"), |
| 18 | + Metric(name="http_response_time_seconds", help="HTTP response time", type="histogram"), |
| 19 | + Metric(name="database_connections_active", help="Active database connections", type="gauge"), |
| 20 | + Metric(name="database_queries_total", help="Total database queries", type="counter"), |
| 21 | + ] |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def test_tree(test_metrics): |
| 26 | + """Create a MetricsTree from test metrics.""" |
| 27 | + return MetricsTree.mk_tree(test_metrics) |
| 28 | + |
| 29 | + |
| 30 | +class TestPrintModeFlat: |
| 31 | + """Tests for PrintMode.flat""" |
| 32 | + |
| 33 | + def test_flat_output_contains_all_metrics(self, test_tree, test_metrics): |
| 34 | + """Test that flat mode contains all metric names.""" |
| 35 | + result = do_print(test_tree, PrintMode.flat) |
| 36 | + |
| 37 | + for metric in test_metrics: |
| 38 | + assert metric.name in result |
| 39 | + |
| 40 | + def test_flat_output_contains_types(self, test_tree, test_metrics): |
| 41 | + """Test that flat mode contains metric types.""" |
| 42 | + result = do_print(test_tree, PrintMode.flat) |
| 43 | + |
| 44 | + for metric in test_metrics: |
| 45 | + assert metric.type in result |
| 46 | + |
| 47 | + def test_flat_output_contains_help(self, test_tree, test_metrics): |
| 48 | + """Test that flat mode contains help text.""" |
| 49 | + result = do_print(test_tree, PrintMode.flat) |
| 50 | + |
| 51 | + for metric in test_metrics: |
| 52 | + if metric.help: |
| 53 | + assert metric.help in result |
| 54 | + |
| 55 | + def test_flat_output_format(self, test_tree): |
| 56 | + """Test that flat mode follows expected format.""" |
| 57 | + result = do_print(test_tree, PrintMode.flat) |
| 58 | + lines = result.split("\n") |
| 59 | + |
| 60 | + # Should have one line per metric |
| 61 | + assert len(lines) == len(test_tree.metrics) |
| 62 | + |
| 63 | + # Each line should contain parentheses (for type) |
| 64 | + for line in lines: |
| 65 | + assert "(" in line and ")" in line |
| 66 | + |
| 67 | + def test_flat_empty_tree(self): |
| 68 | + """Test flat mode with empty tree.""" |
| 69 | + empty_tree = MetricsTree({}) |
| 70 | + result = do_print(empty_tree, PrintMode.flat) |
| 71 | + assert result == "" |
| 72 | + |
| 73 | + def test_flat_metric_without_help(self): |
| 74 | + """Test flat mode with metric without help text.""" |
| 75 | + metric = Metric(name="test_metric", help="", type="counter") |
| 76 | + tree = MetricsTree.mk_tree([metric]) |
| 77 | + result = do_print(tree, PrintMode.flat) |
| 78 | + |
| 79 | + assert "test_metric" in result |
| 80 | + assert "(counter)" in result |
| 81 | + |
| 82 | + |
| 83 | +class TestPrintModeFull: |
| 84 | + """Tests for PrintMode.full""" |
| 85 | + |
| 86 | + def test_full_output_contains_help_comments(self, test_tree, test_metrics): |
| 87 | + """Test that full mode contains HELP comments.""" |
| 88 | + result = do_print(test_tree, PrintMode.full) |
| 89 | + |
| 90 | + for metric in test_metrics: |
| 91 | + assert f"# HELP {metric.name} {metric.help}" in result |
| 92 | + |
| 93 | + def test_full_output_contains_type_comments(self, test_tree, test_metrics): |
| 94 | + """Test that full mode contains TYPE comments.""" |
| 95 | + result = do_print(test_tree, PrintMode.full) |
| 96 | + |
| 97 | + for metric in test_metrics: |
| 98 | + assert f"# TYPE {metric.name} {metric.type}" in result |
| 99 | + |
| 100 | + def test_full_output_contains_metric_names(self, test_tree, test_metrics): |
| 101 | + """Test that full mode contains metric names.""" |
| 102 | + result = do_print(test_tree, PrintMode.full) |
| 103 | + |
| 104 | + for metric in test_metrics: |
| 105 | + # Each metric name should appear at least 3 times (HELP, TYPE, and standalone) |
| 106 | + assert result.count(metric.name) >= 3 |
| 107 | + |
| 108 | + def test_full_output_format(self, test_tree): |
| 109 | + """Test that full mode follows expected format.""" |
| 110 | + result = do_print(test_tree, PrintMode.full) |
| 111 | + lines = result.split("\n") |
| 112 | + |
| 113 | + # Should have 3 lines per metric (HELP, TYPE, name) |
| 114 | + assert len(lines) == len(test_tree.metrics) * 3 |
| 115 | + |
| 116 | + def test_full_empty_tree(self): |
| 117 | + """Test full mode with empty tree.""" |
| 118 | + empty_tree = MetricsTree({}) |
| 119 | + result = do_print(empty_tree, PrintMode.full) |
| 120 | + assert result == "" |
| 121 | + |
| 122 | + def test_full_metric_without_help(self): |
| 123 | + """Test full mode with metric without help text.""" |
| 124 | + metric = Metric(name="test_metric", help="", type="counter") |
| 125 | + tree = MetricsTree.mk_tree([metric]) |
| 126 | + result = do_print(tree, PrintMode.full) |
| 127 | + |
| 128 | + assert "# HELP test_metric " in result |
| 129 | + assert "# TYPE test_metric counter" in result |
| 130 | + assert "test_metric" in result |
| 131 | + |
| 132 | + |
| 133 | +class TestPrintModeTree: |
| 134 | + """Tests for PrintMode.tree""" |
| 135 | + |
| 136 | + def test_tree_output_contains_all_metrics(self, test_tree, test_metrics): |
| 137 | + """Test that tree mode contains all metric names.""" |
| 138 | + result = do_print(test_tree, PrintMode.tree) |
| 139 | + |
| 140 | + for metric in test_metrics: |
| 141 | + assert metric.name in result or metric.name.split("_")[-1] in result |
| 142 | + |
| 143 | + def test_tree_output_uses_tabs(self, test_tree): |
| 144 | + """Test that tree mode uses tabs for indentation.""" |
| 145 | + result = do_print(test_tree, PrintMode.tree) |
| 146 | + |
| 147 | + # Should contain tabs if there's any hierarchy |
| 148 | + if len(test_tree.metrics) > 0: |
| 149 | + assert "\t" in result |
| 150 | + |
| 151 | + def test_tree_output_hierarchical_structure(self, test_tree): |
| 152 | + """Test that tree mode creates hierarchical structure.""" |
| 153 | + result = do_print(test_tree, PrintMode.tree) |
| 154 | + lines = result.split("\n") |
| 155 | + |
| 156 | + # Should have lines with different indentation levels |
| 157 | + indent_levels = set() |
| 158 | + for line in lines: |
| 159 | + if line: |
| 160 | + indent = len(line) - len(line.lstrip("\t")) |
| 161 | + indent_levels.add(indent) |
| 162 | + |
| 163 | + # With metrics that have underscores, we should have multiple indent levels |
| 164 | + assert len(indent_levels) > 1 |
| 165 | + |
| 166 | + def test_tree_output_contains_types(self, test_tree, test_metrics): |
| 167 | + """Test that tree mode contains metric types.""" |
| 168 | + result = do_print(test_tree, PrintMode.tree) |
| 169 | + |
| 170 | + for metric in test_metrics: |
| 171 | + assert metric.type in result |
| 172 | + |
| 173 | + def test_tree_empty_tree(self): |
| 174 | + """Test tree mode with empty tree.""" |
| 175 | + empty_tree = MetricsTree({}) |
| 176 | + result = do_print(empty_tree, PrintMode.tree) |
| 177 | + assert result == "" |
| 178 | + |
| 179 | + def test_tree_metric_without_underscore(self): |
| 180 | + """Test tree mode with metric without underscore.""" |
| 181 | + metric = Metric(name="simplemetric", help="A simple metric", type="gauge") |
| 182 | + tree = MetricsTree.mk_tree([metric]) |
| 183 | + result = do_print(tree, PrintMode.tree) |
| 184 | + |
| 185 | + assert "simplemetric" in result |
| 186 | + assert "(gauge)" in result |
| 187 | + |
| 188 | + |
| 189 | +class TestPrintModeJson: |
| 190 | + """Tests for PrintMode.json""" |
| 191 | + |
| 192 | + def test_json_output_is_valid_json(self, test_tree): |
| 193 | + """Test that json mode produces valid JSON.""" |
| 194 | + result = do_print(test_tree, PrintMode.json) |
| 195 | + |
| 196 | + # Should be parseable as JSON |
| 197 | + parsed = json.loads(result) |
| 198 | + assert isinstance(parsed, dict) |
| 199 | + |
| 200 | + def test_json_output_contains_all_metrics(self, test_tree, test_metrics): |
| 201 | + """Test that json mode contains all metrics.""" |
| 202 | + result = do_print(test_tree, PrintMode.json) |
| 203 | + parsed = json.loads(result) |
| 204 | + |
| 205 | + for metric in test_metrics: |
| 206 | + assert metric.name in parsed |
| 207 | + |
| 208 | + def test_json_output_contains_metric_attributes(self, test_tree, test_metrics): |
| 209 | + """Test that json mode contains all metric attributes.""" |
| 210 | + result = do_print(test_tree, PrintMode.json) |
| 211 | + parsed = json.loads(result) |
| 212 | + |
| 213 | + for metric in test_metrics: |
| 214 | + metric_data = parsed[metric.name] |
| 215 | + assert metric_data["name"] == metric.name |
| 216 | + assert metric_data["help"] == metric.help |
| 217 | + assert metric_data["type"] == metric.type |
| 218 | + |
| 219 | + def test_json_output_is_indented(self, test_tree): |
| 220 | + """Test that json mode uses indentation.""" |
| 221 | + result = do_print(test_tree, PrintMode.json) |
| 222 | + |
| 223 | + # Indented JSON should contain newlines |
| 224 | + assert "\n" in result |
| 225 | + # Should contain 2-space indentation |
| 226 | + assert " " in result |
| 227 | + |
| 228 | + def test_json_empty_tree(self): |
| 229 | + """Test json mode with empty tree.""" |
| 230 | + empty_tree = MetricsTree({}) |
| 231 | + result = do_print(empty_tree, PrintMode.json) |
| 232 | + parsed = json.loads(result) |
| 233 | + assert parsed == {} |
| 234 | + |
| 235 | + def test_json_metric_structure(self): |
| 236 | + """Test json mode metric structure.""" |
| 237 | + metric = Metric(name="test_metric", help="Test help", type="counter") |
| 238 | + tree = MetricsTree.mk_tree([metric]) |
| 239 | + result = do_print(tree, PrintMode.json) |
| 240 | + parsed = json.loads(result) |
| 241 | + |
| 242 | + assert "test_metric" in parsed |
| 243 | + assert parsed["test_metric"]["name"] == "test_metric" |
| 244 | + assert parsed["test_metric"]["help"] == "Test help" |
| 245 | + assert parsed["test_metric"]["type"] == "counter" |
0 commit comments