|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#34974] `distinct()` and an `aggregate()` group key present a declared |
| 5 | + * `Field.boolean` the way `find()` presents it: the stored `0` / `'0'` / |
| 6 | + * `'false'` read as `false`, and `1` / `'1'` / `'true'` as `true`. |
| 7 | + * |
| 8 | + * `presentReadValue`'s `'boolean'` arm was `Boolean(value)`, while the row read |
| 9 | + * (`formatOutput`) has read the text spellings as their boolean since #34916. |
| 10 | + * On a column with TEXT affinity (one whose type changed to boolean without a |
| 11 | + * rebuild) the boot's storage convergence rewrites a legacy `'false'` to `0`, |
| 12 | + * which the affinity stores as the text `'0'`, and it leaves a `'0'` alone |
| 13 | + * because it already reads as `false` on a row. `Boolean('0')` is `true`, so |
| 14 | + * `distinct()` answered `[true]` for a column of un-ticked rows and a group-by |
| 15 | + * put every row in a `true` bucket. |
| 16 | + * |
| 17 | + * Two columns: an INTEGER-form boolean column the driver creates (with the |
| 18 | + * pre-#34916 text `'true'` / `'false'` written past the driver, the form a |
| 19 | + * boot that has not converged it yet still holds), and a TEXT-affinity column |
| 20 | + * after a boot. |
| 21 | + */ |
| 22 | + |
| 23 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 24 | +import type { DriverQuery } from '@objectstack/spec/contracts'; |
| 25 | +import { SqlDriver } from './sql-driver.js'; |
| 26 | + |
| 27 | +const INT_TABLE = 'bool_presenter_int_34974'; |
| 28 | +const TXT_TABLE = 'bool_presenter_txt_34974'; |
| 29 | + |
| 30 | +function sqliteDriver(): SqlDriver { |
| 31 | + return new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true } as any); |
| 32 | +} |
| 33 | + |
| 34 | +/** The group-by answer as `[flag, count]` pairs, sorted by the flag's text. */ |
| 35 | +async function groupCounts(driver: SqlDriver, table: string, groupBy: DriverQuery['groupBy'], key: string): Promise<Array<[unknown, number]>> { |
| 36 | + const rows = (await driver.aggregate(table, { |
| 37 | + groupBy, |
| 38 | + aggregations: [{ function: 'count', field: 'id', alias: 'n' }], |
| 39 | + } as DriverQuery)) as Array<Record<string, unknown>>; |
| 40 | + return rows |
| 41 | + .map((r): [unknown, number] => [r[key], Number(r.n)]) |
| 42 | + .sort((a, b) => String(a[0]).localeCompare(String(b[0]))); |
| 43 | +} |
| 44 | + |
| 45 | +describe('[#34974] driver-sql — distinct and aggregate present an INTEGER boolean column as find() does (sqlite)', () => { |
| 46 | + let driver: SqlDriver; |
| 47 | + |
| 48 | + beforeAll(async () => { |
| 49 | + driver = sqliteDriver(); |
| 50 | + await driver.initObjects([{ name: INT_TABLE, fields: { label: { type: 'string' }, flag: { type: 'boolean' } } }]); |
| 51 | + const knex = (driver as any).knex; |
| 52 | + // Written past the driver, after the table was created: the convergence |
| 53 | + // has not seen these rows, so the text spellings are still text. |
| 54 | + await knex(INT_TABLE).insert([ |
| 55 | + { id: 'int_one', label: 'int_one', flag: 1 }, |
| 56 | + { id: 'text_true', label: 'text_true', flag: 'true' }, |
| 57 | + { id: 'text_false', label: 'text_false', flag: 'false' }, |
| 58 | + ]); |
| 59 | + }); |
| 60 | + |
| 61 | + afterAll(async () => { |
| 62 | + await driver.disconnect(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('the fixture: the column holds the integer 1 and the text true / false', async () => { |
| 66 | + const rows = await (driver as any).knex.raw(`select id, typeof(flag) as t from ${INT_TABLE} order by id`); |
| 67 | + expect(Object.fromEntries(rows.map((r: any) => [r.id, r.t]))).toEqual({ int_one: 'integer', text_false: 'text', text_true: 'text' }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('find() is the reference: the text false reads as false', async () => { |
| 71 | + const rows = (await driver.find(INT_TABLE, { orderBy: [{ field: 'id', order: 'asc' }] } as DriverQuery)) as any[]; |
| 72 | + expect(rows.map((r) => [r.id, r.flag])).toEqual([['int_one', true], ['text_false', false], ['text_true', true]]); |
| 73 | + }); |
| 74 | + |
| 75 | + it('distinct() answers false and true, not true alone', async () => { |
| 76 | + const values = await driver.distinct(INT_TABLE, 'flag'); |
| 77 | + expect([...values].sort()).toEqual([false, true]); |
| 78 | + }); |
| 79 | + |
| 80 | + it('an aggregate group key presents the text false as false', async () => { |
| 81 | + // The stored values stay three groups (SQL groups the raw cells); each presents as find() does. |
| 82 | + expect(await groupCounts(driver, INT_TABLE, ['flag'], 'flag')).toEqual([[false, 1], [true, 1], [true, 1]]); |
| 83 | + }); |
| 84 | + |
| 85 | + it('an aliased group key presents the same way', async () => { |
| 86 | + expect(await groupCounts(driver, INT_TABLE, [{ field: 'flag', alias: 'ticked' }] as any, 'ticked')) |
| 87 | + .toEqual([[false, 1], [true, 1], [true, 1]]); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
| 91 | +describe('[#34974] driver-sql — distinct and aggregate present a TEXT-affinity boolean column as find() does (sqlite)', () => { |
| 92 | + let driver: SqlDriver; |
| 93 | + |
| 94 | + beforeAll(async () => { |
| 95 | + driver = sqliteDriver(); |
| 96 | + const knex = (driver as any).knex; |
| 97 | + // A column whose type changed to boolean without a rebuild keeps TEXT |
| 98 | + // affinity (a `text` field retyped to `boolean` boots onto the same column). |
| 99 | + await knex.raw(`create table ${TXT_TABLE} (id varchar(255) primary key, label varchar(255), flag text)`); |
| 100 | + await knex(TXT_TABLE).insert([ |
| 101 | + { id: 'canon_zero', label: 'canon_zero', flag: '0' }, |
| 102 | + { id: 'legacy_false_a', label: 'legacy_false_a', flag: 'false' }, |
| 103 | + { id: 'legacy_false_b', label: 'legacy_false_b', flag: 'false' }, |
| 104 | + { id: 'legacy_true', label: 'legacy_true', flag: 'true' }, |
| 105 | + ]); |
| 106 | + // The boot: the storage convergence rewrites the legacy text to `1` / `0`, |
| 107 | + // which this affinity stores as the text '1' / '0'. |
| 108 | + await driver.initObjects([{ name: TXT_TABLE, fields: { label: { type: 'string' }, flag: { type: 'boolean' } } }]); |
| 109 | + }); |
| 110 | + |
| 111 | + afterAll(async () => { |
| 112 | + await driver.disconnect(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('the fixture: after the boot, every cell is the text 0 or 1', async () => { |
| 116 | + const rows = await (driver as any).knex.raw(`select id, flag, typeof(flag) as t from ${TXT_TABLE} order by id`); |
| 117 | + expect(rows.map((r: any) => [r.id, r.flag, r.t])).toEqual([ |
| 118 | + ['canon_zero', '0', 'text'], |
| 119 | + ['legacy_false_a', '0', 'text'], |
| 120 | + ['legacy_false_b', '0', 'text'], |
| 121 | + ['legacy_true', '1', 'text'], |
| 122 | + ]); |
| 123 | + }); |
| 124 | + |
| 125 | + it('find() is the reference: the text 0 reads as false', async () => { |
| 126 | + const rows = (await driver.find(TXT_TABLE, { orderBy: [{ field: 'id', order: 'asc' }] } as DriverQuery)) as any[]; |
| 127 | + expect(rows.map((r) => [r.id, r.flag])).toEqual([ |
| 128 | + ['canon_zero', false], ['legacy_false_a', false], ['legacy_false_b', false], ['legacy_true', true], |
| 129 | + ]); |
| 130 | + }); |
| 131 | + |
| 132 | + it('distinct() answers false and true, not true alone', async () => { |
| 133 | + const values = await driver.distinct(TXT_TABLE, 'flag'); |
| 134 | + expect([...values].sort()).toEqual([false, true]); |
| 135 | + }); |
| 136 | + |
| 137 | + it('an aggregate group key counts three false and one true', async () => { |
| 138 | + expect(await groupCounts(driver, TXT_TABLE, ['flag'], 'flag')).toEqual([[false, 3], [true, 1]]); |
| 139 | + }); |
| 140 | + |
| 141 | + it('an aliased group key presents the same way', async () => { |
| 142 | + expect(await groupCounts(driver, TXT_TABLE, [{ field: 'flag', alias: 'ticked' }] as any, 'ticked')) |
| 143 | + .toEqual([[false, 3], [true, 1]]); |
| 144 | + }); |
| 145 | +}); |
0 commit comments