|
1 | | -import { describe, expect, test } from 'vitest'; |
2 | | -import { UnscopedParameterLookup, SqlParameterQuery, SourceTableInterface } from '../../src/index.js'; |
| 1 | +import { beforeEach, describe, expect, test } from 'vitest'; |
| 2 | +import { |
| 3 | + UnscopedParameterLookup, |
| 4 | + SqlParameterQuery, |
| 5 | + SourceTableInterface, |
| 6 | + debugHydratedMergedSource, |
| 7 | + BucketParameterQuerier, |
| 8 | + QuerierError, |
| 9 | + GetQuerierOptions, |
| 10 | + RequestParameters, |
| 11 | + ScopedParameterLookup, |
| 12 | + mergeParameterIndexLookupCreators |
| 13 | +} from '../../src/index.js'; |
3 | 14 | import { StaticSqlParameterQuery } from '../../src/StaticSqlParameterQuery.js'; |
4 | 15 | import { BASIC_SCHEMA, EMPTY_DATA_SOURCE, normalizeTokenParameters, PARSE_OPTIONS } from './util.js'; |
| 16 | +import { HydrationState } from '../../src/HydrationState.js'; |
5 | 17 |
|
6 | 18 | describe('parameter queries', () => { |
7 | 19 | const table = (name: string): SourceTableInterface => ({ |
@@ -855,6 +867,85 @@ describe('parameter queries', () => { |
855 | 867 | expect(query.getLookups(requestParams)).toEqual([UnscopedParameterLookup.normalized(['user1'])]); |
856 | 868 | }); |
857 | 869 |
|
| 870 | + describe('custom hydrationState', function () { |
| 871 | + const hydrationState: HydrationState = { |
| 872 | + getBucketSourceScope(source) { |
| 873 | + return { bucketPrefix: `${source.uniqueName}-test` }; |
| 874 | + }, |
| 875 | + getParameterIndexLookupScope(source) { |
| 876 | + return { |
| 877 | + lookupName: `${source.defaultLookupScope.lookupName}.test`, |
| 878 | + queryId: `${source.defaultLookupScope.queryId}.test` |
| 879 | + }; |
| 880 | + } |
| 881 | + }; |
| 882 | + |
| 883 | + let query: SqlParameterQuery; |
| 884 | + |
| 885 | + beforeEach(() => { |
| 886 | + const sql = 'SELECT id as group_id FROM groups WHERE token_parameters.user_id IN groups.user_ids'; |
| 887 | + query = SqlParameterQuery.fromSql( |
| 888 | + 'mybucket', |
| 889 | + sql, |
| 890 | + PARSE_OPTIONS, |
| 891 | + 'myquery', |
| 892 | + EMPTY_DATA_SOURCE |
| 893 | + ) as SqlParameterQuery; |
| 894 | + |
| 895 | + expect(query.errors).toEqual([]); |
| 896 | + }); |
| 897 | + |
| 898 | + test('for lookups', function () { |
| 899 | + const merged = mergeParameterIndexLookupCreators(hydrationState, [query]); |
| 900 | + const result = merged.evaluateParameterRow(TABLE_GROUPS, { |
| 901 | + id: 'group1', |
| 902 | + user_ids: JSON.stringify(['test-user', 'other-user']) |
| 903 | + }); |
| 904 | + expect(result).toEqual([ |
| 905 | + { |
| 906 | + lookup: ScopedParameterLookup.direct({ lookupName: 'mybucket.test', queryId: 'myquery.test' }, ['test-user']), |
| 907 | + bucketParameters: [{ group_id: 'group1' }] |
| 908 | + }, |
| 909 | + { |
| 910 | + lookup: ScopedParameterLookup.direct({ lookupName: 'mybucket.test', queryId: 'myquery.test' }, [ |
| 911 | + 'other-user' |
| 912 | + ]), |
| 913 | + bucketParameters: [{ group_id: 'group1' }] |
| 914 | + } |
| 915 | + ]); |
| 916 | + }); |
| 917 | + |
| 918 | + test('for queries', function () { |
| 919 | + const hydrated = query.createParameterQuerierSource({ hydrationState }); |
| 920 | + |
| 921 | + const queriers: BucketParameterQuerier[] = []; |
| 922 | + const errors: QuerierError[] = []; |
| 923 | + const pending = { queriers, errors }; |
| 924 | + |
| 925 | + const querierOptions: GetQuerierOptions = { |
| 926 | + hasDefaultStreams: true, |
| 927 | + globalParameters: new RequestParameters( |
| 928 | + { |
| 929 | + sub: 'test-user' |
| 930 | + }, |
| 931 | + {} |
| 932 | + ), |
| 933 | + streams: {} |
| 934 | + }; |
| 935 | + |
| 936 | + hydrated.pushBucketParameterQueriers(pending, querierOptions); |
| 937 | + |
| 938 | + expect(errors).toEqual([]); |
| 939 | + expect(queriers.length).toBe(1); |
| 940 | + |
| 941 | + const querier = queriers[0]; |
| 942 | + |
| 943 | + expect(querier.parameterQueryLookups).toEqual([ |
| 944 | + ScopedParameterLookup.direct({ lookupName: 'mybucket.test', queryId: 'myquery.test' }, ['test-user']) |
| 945 | + ]); |
| 946 | + }); |
| 947 | + }); |
| 948 | + |
858 | 949 | test('invalid OR in parameter queries', () => { |
859 | 950 | // Supporting this case is more tricky. We can do this by effectively denormalizing the OR clause |
860 | 951 | // into separate queries, but it's a significant change. For now, developers should do that manually. |
|
0 commit comments