|
| 1 | +"""Tests for coverage analyzer model and query generation.""" |
| 2 | + |
| 3 | +from sidemantic import SemanticLayer |
| 4 | +from sidemantic.core.coverage_analyzer import CoverageAnalyzer |
| 5 | + |
| 6 | + |
| 7 | +def test_generate_models_from_queries(): |
| 8 | + """Test generating model definitions from queries.""" |
| 9 | + layer = SemanticLayer(auto_register=False) |
| 10 | + analyzer = CoverageAnalyzer(layer) |
| 11 | + |
| 12 | + queries = [ |
| 13 | + """ |
| 14 | + SELECT status, region, SUM(amount), COUNT(*) |
| 15 | + FROM orders |
| 16 | + GROUP BY status, region |
| 17 | + """, |
| 18 | + """ |
| 19 | + SELECT category, AVG(price), COUNT(DISTINCT product_id) |
| 20 | + FROM products |
| 21 | + GROUP BY category |
| 22 | + """, |
| 23 | + ] |
| 24 | + |
| 25 | + report = analyzer.analyze_queries(queries) |
| 26 | + models = analyzer.generate_models(report) |
| 27 | + |
| 28 | + # Should generate 2 models |
| 29 | + assert len(models) == 2 |
| 30 | + assert "orders" in models |
| 31 | + assert "products" in models |
| 32 | + |
| 33 | + # Check orders model |
| 34 | + orders = models["orders"] |
| 35 | + assert orders["model"]["name"] == "orders" |
| 36 | + assert orders["model"]["table"] == "orders" |
| 37 | + |
| 38 | + # Check orders dimensions |
| 39 | + assert len(orders["dimensions"]) == 2 |
| 40 | + dim_names = {d["name"] for d in orders["dimensions"]} |
| 41 | + assert "status" in dim_names |
| 42 | + assert "region" in dim_names |
| 43 | + |
| 44 | + # Check orders metrics |
| 45 | + assert len(orders["metrics"]) == 2 |
| 46 | + metric_names = {m["name"] for m in orders["metrics"]} |
| 47 | + assert "sum_amount" in metric_names |
| 48 | + assert "count" in metric_names |
| 49 | + |
| 50 | + # Check products model |
| 51 | + products = models["products"] |
| 52 | + assert products["model"]["name"] == "products" |
| 53 | + |
| 54 | + # Check products dimensions |
| 55 | + assert len(products["dimensions"]) == 1 |
| 56 | + assert products["dimensions"][0]["name"] == "category" |
| 57 | + |
| 58 | + # Check products metrics |
| 59 | + assert len(products["metrics"]) == 2 |
| 60 | + metric_names = {m["name"] for m in products["metrics"]} |
| 61 | + assert "avg_price" in metric_names |
| 62 | + assert "product_id_count" in metric_names |
| 63 | + |
| 64 | + |
| 65 | +def test_generate_models_count_distinct(): |
| 66 | + """Test COUNT(DISTINCT col) generates correct metric.""" |
| 67 | + layer = SemanticLayer(auto_register=False) |
| 68 | + analyzer = CoverageAnalyzer(layer) |
| 69 | + |
| 70 | + queries = [ |
| 71 | + """ |
| 72 | + SELECT status, COUNT(DISTINCT customer_id) |
| 73 | + FROM orders |
| 74 | + GROUP BY status |
| 75 | + """ |
| 76 | + ] |
| 77 | + |
| 78 | + report = analyzer.analyze_queries(queries) |
| 79 | + models = analyzer.generate_models(report) |
| 80 | + |
| 81 | + orders = models["orders"] |
| 82 | + metrics = {m["name"]: m for m in orders["metrics"]} |
| 83 | + |
| 84 | + assert "customer_id_count" in metrics |
| 85 | + assert metrics["customer_id_count"]["agg"] == "count_distinct" |
| 86 | + assert metrics["customer_id_count"]["sql"] == "customer_id" |
| 87 | + |
| 88 | + |
| 89 | +def test_generate_models_no_duplicate_metrics(): |
| 90 | + """Test that duplicate metrics are not generated.""" |
| 91 | + layer = SemanticLayer(auto_register=False) |
| 92 | + analyzer = CoverageAnalyzer(layer) |
| 93 | + |
| 94 | + queries = [ |
| 95 | + "SELECT status, SUM(amount) FROM orders GROUP BY status", |
| 96 | + "SELECT region, SUM(amount) FROM orders GROUP BY region", |
| 97 | + ] |
| 98 | + |
| 99 | + report = analyzer.analyze_queries(queries) |
| 100 | + models = analyzer.generate_models(report) |
| 101 | + |
| 102 | + orders = models["orders"] |
| 103 | + metric_names = [m["name"] for m in orders["metrics"]] |
| 104 | + |
| 105 | + # sum_amount should only appear once |
| 106 | + assert metric_names.count("sum_amount") == 1 |
| 107 | + |
| 108 | + |
| 109 | +def test_generate_rewritten_queries(): |
| 110 | + """Test generating rewritten queries.""" |
| 111 | + layer = SemanticLayer(auto_register=False) |
| 112 | + analyzer = CoverageAnalyzer(layer) |
| 113 | + |
| 114 | + queries = [ |
| 115 | + """ |
| 116 | + SELECT status, SUM(amount), COUNT(*) |
| 117 | + FROM orders |
| 118 | + GROUP BY status |
| 119 | + """ |
| 120 | + ] |
| 121 | + |
| 122 | + report = analyzer.analyze_queries(queries) |
| 123 | + rewritten = analyzer.generate_rewritten_queries(report) |
| 124 | + |
| 125 | + # Should generate 1 rewritten query |
| 126 | + assert len(rewritten) == 1 |
| 127 | + assert "query_1" in rewritten |
| 128 | + |
| 129 | + sql = rewritten["query_1"] |
| 130 | + |
| 131 | + # Check it's SQL format |
| 132 | + assert "SELECT" in sql |
| 133 | + assert "FROM orders" in sql |
| 134 | + |
| 135 | + # Check it uses semantic layer syntax (model.dimension, model.metric) |
| 136 | + assert "orders.status" in sql |
| 137 | + assert "orders.count" in sql |
| 138 | + assert "orders.sum_amount" in sql |
| 139 | + |
| 140 | + |
| 141 | +def test_generate_rewritten_queries_with_filter(): |
| 142 | + """Test generating rewritten queries with WHERE clause.""" |
| 143 | + layer = SemanticLayer(auto_register=False) |
| 144 | + analyzer = CoverageAnalyzer(layer) |
| 145 | + |
| 146 | + queries = [ |
| 147 | + """ |
| 148 | + SELECT status, SUM(amount) |
| 149 | + FROM orders |
| 150 | + WHERE status = 'completed' |
| 151 | + GROUP BY status |
| 152 | + """ |
| 153 | + ] |
| 154 | + |
| 155 | + report = analyzer.analyze_queries(queries) |
| 156 | + rewritten = analyzer.generate_rewritten_queries(report) |
| 157 | + |
| 158 | + sql = rewritten["query_1"] |
| 159 | + |
| 160 | + # Check it includes WHERE clause |
| 161 | + assert "WHERE" in sql |
| 162 | + assert "status = 'completed'" in sql or "status='completed'" in sql |
| 163 | + |
| 164 | + |
| 165 | +def test_generate_rewritten_queries_skips_unparseable(): |
| 166 | + """Test that unparseable queries are skipped.""" |
| 167 | + layer = SemanticLayer(auto_register=False) |
| 168 | + analyzer = CoverageAnalyzer(layer) |
| 169 | + |
| 170 | + queries = [ |
| 171 | + "SELECT FROM WHERE", # Invalid |
| 172 | + "SELECT status, COUNT(*) FROM orders GROUP BY status", # Valid |
| 173 | + ] |
| 174 | + |
| 175 | + report = analyzer.analyze_queries(queries) |
| 176 | + rewritten = analyzer.generate_rewritten_queries(report) |
| 177 | + |
| 178 | + # Should only generate 1 query (skip the invalid one) |
| 179 | + assert len(rewritten) == 1 |
| 180 | + |
| 181 | + |
| 182 | +def test_write_model_files(tmp_path): |
| 183 | + """Test writing model files to disk.""" |
| 184 | + layer = SemanticLayer(auto_register=False) |
| 185 | + analyzer = CoverageAnalyzer(layer) |
| 186 | + |
| 187 | + queries = [ |
| 188 | + "SELECT status, SUM(amount) FROM orders GROUP BY status", |
| 189 | + ] |
| 190 | + |
| 191 | + report = analyzer.analyze_queries(queries) |
| 192 | + models = analyzer.generate_models(report) |
| 193 | + |
| 194 | + output_dir = tmp_path / "models" |
| 195 | + analyzer.write_model_files(models, str(output_dir)) |
| 196 | + |
| 197 | + # Check file was created |
| 198 | + orders_file = output_dir / "orders.yml" |
| 199 | + assert orders_file.exists() |
| 200 | + |
| 201 | + # Check file contents |
| 202 | + import yaml |
| 203 | + |
| 204 | + with open(orders_file) as f: |
| 205 | + data = yaml.safe_load(f) |
| 206 | + |
| 207 | + assert data["model"]["name"] == "orders" |
| 208 | + assert len(data["dimensions"]) == 1 |
| 209 | + assert len(data["metrics"]) == 1 |
| 210 | + |
| 211 | + |
| 212 | +def test_write_rewritten_queries(tmp_path): |
| 213 | + """Test writing rewritten queries to disk.""" |
| 214 | + layer = SemanticLayer(auto_register=False) |
| 215 | + analyzer = CoverageAnalyzer(layer) |
| 216 | + |
| 217 | + queries = [ |
| 218 | + "SELECT status, COUNT(*) FROM orders GROUP BY status", |
| 219 | + ] |
| 220 | + |
| 221 | + report = analyzer.analyze_queries(queries) |
| 222 | + rewritten = analyzer.generate_rewritten_queries(report) |
| 223 | + |
| 224 | + output_dir = tmp_path / "queries" |
| 225 | + analyzer.write_rewritten_queries(rewritten, str(output_dir)) |
| 226 | + |
| 227 | + # Check file was created |
| 228 | + query_file = output_dir / "query_1.sql" |
| 229 | + assert query_file.exists() |
| 230 | + |
| 231 | + # Check file contents |
| 232 | + content = query_file.read_text() |
| 233 | + assert "SELECT" in content |
| 234 | + assert "FROM orders" in content |
| 235 | + assert "orders.status" in content |
| 236 | + assert "orders.count" in content |
| 237 | + |
| 238 | + |
| 239 | +def test_generate_models_multiple_aggregations_same_column(): |
| 240 | + """Test handling multiple aggregation types on same column.""" |
| 241 | + layer = SemanticLayer(auto_register=False) |
| 242 | + analyzer = CoverageAnalyzer(layer) |
| 243 | + |
| 244 | + queries = [ |
| 245 | + """ |
| 246 | + SELECT |
| 247 | + status, |
| 248 | + SUM(amount), |
| 249 | + AVG(amount), |
| 250 | + MIN(amount), |
| 251 | + MAX(amount) |
| 252 | + FROM orders |
| 253 | + GROUP BY status |
| 254 | + """ |
| 255 | + ] |
| 256 | + |
| 257 | + report = analyzer.analyze_queries(queries) |
| 258 | + models = analyzer.generate_models(report) |
| 259 | + |
| 260 | + orders = models["orders"] |
| 261 | + metric_names = {m["name"] for m in orders["metrics"]} |
| 262 | + |
| 263 | + assert "sum_amount" in metric_names |
| 264 | + assert "avg_amount" in metric_names |
| 265 | + assert "min_amount" in metric_names |
| 266 | + assert "max_amount" in metric_names |
0 commit comments