Skip to content

Commit 96ef308

Browse files
committed
add tests
1 parent 82f5666 commit 96ef308

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

packages/cubejs-schema-compiler/test/unit/base-query.test.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,6 +2033,90 @@ describe('SQL Generation', () => {
20332033
FROM
20342034
(select * from order where (type = ?)) AS "order" WHERE ("order".type = ?) AND ("order".category = ?)`);
20352035
});
2036+
2037+
it('view referencing cube with FILTER_PARAMS - multiple filters and complex query', async () => {
2038+
/** @type {Compilers} */
2039+
const viewCompiler = prepareYamlCompiler(
2040+
createSchemaYaml({
2041+
cubes: [{
2042+
name: 'Product',
2043+
sql: 'select * from products where {FILTER_PARAMS.Product.category.filter(\'category\')} and {FILTER_PARAMS.Product.status.filter(\'status\')}',
2044+
measures: [
2045+
{
2046+
name: 'count',
2047+
type: 'count',
2048+
},
2049+
{
2050+
name: 'revenue',
2051+
sql: 'price',
2052+
type: 'sum',
2053+
}
2054+
],
2055+
dimensions: [
2056+
{
2057+
name: 'category',
2058+
sql: 'category',
2059+
type: 'string'
2060+
},
2061+
{
2062+
name: 'status',
2063+
sql: 'status',
2064+
type: 'string'
2065+
},
2066+
{
2067+
name: 'name',
2068+
sql: 'name',
2069+
type: 'string'
2070+
}
2071+
]
2072+
}],
2073+
views: [{
2074+
name: 'product_analytics',
2075+
cubes: [{
2076+
join_path: 'Product',
2077+
prefix: true,
2078+
includes: [
2079+
'category',
2080+
'status',
2081+
'name',
2082+
'count',
2083+
'revenue'
2084+
]
2085+
}]
2086+
}]
2087+
})
2088+
);
2089+
2090+
await viewCompiler.compiler.compile();
2091+
const query = new PostgresQuery(viewCompiler, {
2092+
measures: ['product_analytics.Product_count', 'product_analytics.Product_revenue'],
2093+
dimensions: ['product_analytics.Product_name'],
2094+
filters: [
2095+
{
2096+
member: 'product_analytics.Product_category',
2097+
operator: 'equals',
2098+
values: ['electronics'],
2099+
},
2100+
{
2101+
member: 'product_analytics.Product_status',
2102+
operator: 'equals',
2103+
values: ['active'],
2104+
},
2105+
],
2106+
});
2107+
const queryAndParams = query.buildSqlAndParams();
2108+
const queryString = queryAndParams[0];
2109+
2110+
expect(queryString).toContain('select * from products where (category = $1) and (status = $2)');
2111+
// Test that the view query contains the expected structure
2112+
expect(queryString).toMatch(/SELECT\s+"product"\.name/);
2113+
expect(queryString).toMatch(/count\(\*\)/);
2114+
expect(queryString).toMatch(/sum\("product"\.price\)/);
2115+
// Test that view filters are properly applied
2116+
expect(queryString).toContain('WHERE ("product".category = $3) AND ("product".status = $4)');
2117+
// Test parameter values
2118+
expect(queryAndParams[1]).toEqual(['electronics', 'active', 'electronics', 'active']);
2119+
});
20362120
});
20372121

20382122
describe('FILTER_GROUP', () => {

0 commit comments

Comments
 (0)