|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#20987] `jsonMembershipCandidates` and `jsonMembershipPredicate` — the one |
| 5 | + * `$contains` membership construct `driver-sql` and the analytics read scope |
| 6 | + * and `where` ask, moved here from `driver-sql` (#17590). |
| 7 | + * |
| 8 | + * What each face does with the SQL is pinned in its own package (the driver's |
| 9 | + * move proof `sql-driver-20987-json-membership-move.test.ts`, its executed |
| 10 | + * three-dialect suite `sql-driver-17590-json-column-membership.test.ts`, and |
| 11 | + * the analytics `contains-membership.test.ts`). This file pins the function's |
| 12 | + * own contract: the candidate set, the construct per dialect, and that the |
| 13 | + * emitters are called left to right, in placeholder order, or not at all. |
| 14 | + */ |
| 15 | + |
| 16 | +import { describe, it, expect } from 'vitest'; |
| 17 | +import { jsonMembershipCandidates, jsonMembershipPredicate, type JsonMembershipEmitters } from './json-membership-sql'; |
| 18 | + |
| 19 | +/** Emitters that render `C` for the column and `?` for a value, recording each call in order. */ |
| 20 | +function recording(): { emit: JsonMembershipEmitters; calls: string[] } { |
| 21 | + const calls: string[] = []; |
| 22 | + return { |
| 23 | + calls, |
| 24 | + emit: { |
| 25 | + column: () => { |
| 26 | + calls.push('column'); |
| 27 | + return 'C'; |
| 28 | + }, |
| 29 | + value: (v) => { |
| 30 | + calls.push(`value ${v}`); |
| 31 | + return '?'; |
| 32 | + }, |
| 33 | + }, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +describe('[#20987] jsonMembershipCandidates — the JSON scalars one comparand denotes', () => { |
| 38 | + it('a plain string denotes one JSON string', () => { |
| 39 | + expect(jsonMembershipCandidates('u1')).toEqual(['"u1"']); |
| 40 | + }); |
| 41 | + |
| 42 | + it('a string JSON must escape is escaped', () => { |
| 43 | + expect(jsonMembershipCandidates('say "hi"')).toEqual(['"say \\"hi\\""']); |
| 44 | + }); |
| 45 | + |
| 46 | + it('true / false / null also denote their JSON literal', () => { |
| 47 | + expect(jsonMembershipCandidates('true')).toEqual(['"true"', 'true']); |
| 48 | + expect(jsonMembershipCandidates('false')).toEqual(['"false"', 'false']); |
| 49 | + expect(jsonMembershipCandidates('null')).toEqual(['"null"', 'null']); |
| 50 | + }); |
| 51 | + |
| 52 | + it('a JSON number also denotes the number, canonicalised', () => { |
| 53 | + expect(jsonMembershipCandidates('7')).toEqual(['"7"', '7']); |
| 54 | + expect(jsonMembershipCandidates('1.50')).toEqual(['"1.50"', '1.5']); |
| 55 | + expect(jsonMembershipCandidates(10)).toEqual(['"10"', '10']); |
| 56 | + }); |
| 57 | + |
| 58 | + it('a string Number() accepts but the JSON number grammar refuses denotes the string only', () => { |
| 59 | + for (const text of ['0x10', ' 1 ', 'Infinity', '', '01', '1.']) { |
| 60 | + expect(jsonMembershipCandidates(text), JSON.stringify(text)).toEqual([JSON.stringify(text)]); |
| 61 | + } |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe('[#20987] jsonMembershipPredicate — the construct per dialect, through the caller\'s emitters', () => { |
| 66 | + it('sqlite: a json_each element scan, the column twice then the value, per candidate', () => { |
| 67 | + const { emit, calls } = recording(); |
| 68 | + expect(jsonMembershipPredicate('sqlite', emit, 'u1')).toBe( |
| 69 | + "(EXISTS (SELECT 1 FROM json_each(CASE WHEN json_valid(C) THEN C ELSE '[]' END) AS os_member " |
| 70 | + + "WHERE typeof(os_member.key) = 'integer' AND CASE os_member.type " |
| 71 | + + "WHEN 'true' THEN 'true' WHEN 'false' THEN 'false' WHEN 'null' THEN 'null' " |
| 72 | + + 'ELSE json_quote(os_member.value) END = ?))', |
| 73 | + ); |
| 74 | + expect(calls).toEqual(['column', 'column', 'value "u1"']); |
| 75 | + }); |
| 76 | + |
| 77 | + it('postgres: jsonb containment of the array-wrapped candidate', () => { |
| 78 | + const { emit, calls } = recording(); |
| 79 | + expect(jsonMembershipPredicate('postgres', emit, 'u1')).toBe('(C::jsonb @> ?::jsonb)'); |
| 80 | + expect(calls).toEqual(['column', 'value ["u1"]']); |
| 81 | + }); |
| 82 | + |
| 83 | + it('mysql: JSON_CONTAINS of the array-wrapped candidate', () => { |
| 84 | + const { emit, calls } = recording(); |
| 85 | + expect(jsonMembershipPredicate('mysql', emit, 'u1')).toBe('(JSON_CONTAINS(C, ?))'); |
| 86 | + expect(calls).toEqual(['column', 'value ["u1"]']); |
| 87 | + }); |
| 88 | + |
| 89 | + it('two candidates are OR-ed inside one pair of parentheses, emitted left to right', () => { |
| 90 | + const { emit, calls } = recording(); |
| 91 | + expect(jsonMembershipPredicate('postgres', emit, '1.50')).toBe('(C::jsonb @> ?::jsonb OR C::jsonb @> ?::jsonb)'); |
| 92 | + expect(calls).toEqual(['column', 'value ["1.50"]', 'column', 'value [1.5]']); |
| 93 | + }); |
| 94 | + |
| 95 | + it("'unknown' answers null and calls neither emitter", () => { |
| 96 | + const { emit, calls } = recording(); |
| 97 | + expect(jsonMembershipPredicate('unknown', emit, 'true')).toBeNull(); |
| 98 | + expect(calls).toEqual([]); |
| 99 | + }); |
| 100 | + |
| 101 | + it('a positional caller binds in placeholder order', () => { |
| 102 | + const params: unknown[] = []; |
| 103 | + const sql = jsonMembershipPredicate( |
| 104 | + 'postgres', |
| 105 | + { column: () => '"t"."tags"', value: (v) => { params.push(v); return `$${params.length}`; } }, |
| 106 | + 'true', |
| 107 | + ); |
| 108 | + expect(sql).toBe('("t"."tags"::jsonb @> $1::jsonb OR "t"."tags"::jsonb @> $2::jsonb)'); |
| 109 | + expect(params).toEqual(['["true"]', '[true]']); |
| 110 | + }); |
| 111 | +}); |
0 commit comments