|
1 | 1 | import unittest |
2 | 2 |
|
3 | 3 | from pandasai.ee.helpers.query_builder import QueryBuilder |
4 | | -from tests.unit_tests.ee.helpers.schema import VIZ_QUERY_SCHEMA |
| 4 | +from tests.unit_tests.ee.helpers.schema import MULTI_JOIN_SCHEMA, VIZ_QUERY_SCHEMA |
5 | 5 |
|
6 | 6 |
|
7 | 7 | class TestQueryBuilder(unittest.TestCase): |
@@ -191,3 +191,40 @@ def test_sql_with_filters_with_set_filter(self): |
191 | 191 | "SELECT SUM(`orders`.`freight`) AS total_freight, `orders`.`ship_country` AS ship_country FROM `orders` GROUP BY ship_country HAVING SUM(`orders`.`freight`) IS NOT NULL ORDER BY total_freight asc", |
192 | 192 | "SELECT `orders`.`ship_country` AS ship_country, SUM(`orders`.`freight`) AS total_freight FROM `orders` GROUP BY ship_country HAVING SUM(`orders`.`freight`) IS NOT NULL ORDER BY total_freight asc", |
193 | 193 | ] |
| 194 | + |
| 195 | + def test_sql_with_filters_with_join(self): |
| 196 | + query_builder = QueryBuilder(MULTI_JOIN_SCHEMA) |
| 197 | + |
| 198 | + json_str = { |
| 199 | + "type": "bar", |
| 200 | + "dimensions": ["Engagement.activity_type"], |
| 201 | + "measures": ["Sales.total_revenue"], |
| 202 | + "timeDimensions": [], |
| 203 | + "options": { |
| 204 | + "xLabel": "Activity Type", |
| 205 | + "yLabel": "Total Revenue", |
| 206 | + "title": "Total Revenue Generated from Users who Logged in Before Purchase", |
| 207 | + "legend": {"display": True, "position": "top"}, |
| 208 | + }, |
| 209 | + "joins": [ |
| 210 | + { |
| 211 | + "name": "Engagement", |
| 212 | + "join_type": "right", |
| 213 | + "sql": "${Sales.id} = ${Engagement.id}", |
| 214 | + } |
| 215 | + ], |
| 216 | + "filters": [ |
| 217 | + { |
| 218 | + "member": "Engagement.engagement_date", |
| 219 | + "operator": "beforeDate", |
| 220 | + "values": ["${Sales.sales_date}"], |
| 221 | + } |
| 222 | + ], |
| 223 | + "order": [{"id": "Sales.total_revenue", "direction": "asc"}], |
| 224 | + } |
| 225 | + sql_query = query_builder.generate_sql(json_str) |
| 226 | + |
| 227 | + assert ( |
| 228 | + sql_query |
| 229 | + == "SELECT `engagement`.`activity_type` AS activity_type, SUM(`sales`.`revenue`) AS total_revenue FROM `sales` RIGHT JOIN `engagement` ON `engagement`.`id` = `sales`.`id` WHERE `engagement`.`engagement_date` < '${Sales.sales_date}' GROUP BY activity_type ORDER BY total_revenue asc" |
| 230 | + ) |
0 commit comments