UNPKG

@evidence-dev/sqlite

Version:

SQLite driver for Evidence projects

70 lines (62 loc) 2.43 kB
import { test } from 'uvu'; import * as assert from 'uvu/assert'; import runQuery from '../index.cjs'; import { batchedAsyncGeneratorToArray, TypeFidelity } from '@evidence-dev/db-commons'; import 'dotenv/config'; test('query runs', async () => { try { const { rows: row_generator, columnTypes } = await runQuery( "select null as number_col, null as date_col, null as timestamp_col, null as string_col, null as bool_col union all select 100 as number_col, DATE('now') as date_col, current_timestamp as timestamp_col, 'Evidence' as string_col, false as bool_col", { filename: ':memory:' } ); const rows = await batchedAsyncGeneratorToArray(row_generator); assert.instance(rows, Array); assert.instance(columnTypes, Array); assert.type(rows[1], 'object'); assert.equal(rows[1].number_col, 100); let actualColumnTypes = columnTypes.map((columnType) => columnType.evidenceType); let actualColumnNames = columnTypes.map((columnType) => columnType.name); let actualTypePrecisions = columnTypes.map((columnType) => columnType.typeFidelity); let expectedColumnTypes = ['number', 'date', 'date', 'string', 'number']; let expectedColumnNames = ['number_col', 'date_col', 'timestamp_col', 'string_col', 'bool_col']; let expectedTypePrecision = Array(5).fill(TypeFidelity.INFERRED); assert.equal( true, expectedColumnTypes.length === actualColumnTypes.length && expectedColumnTypes.every((value, index) => value === actualColumnTypes[index]) ); assert.equal( true, expectedColumnNames.length === actualColumnNames.length && expectedColumnNames.every((value, index) => value === actualColumnNames[index]) ); assert.equal( true, expectedTypePrecision.length === actualTypePrecisions.length && expectedTypePrecision.every((value, index) => value === actualTypePrecisions[index]) ); } catch (e) { throw Error(e); } }); test('query batches results properly', async () => { try { const { rows, expectedRowCount } = await runQuery( 'select 1 union all select 2 union all select 3 union all select 4 union all select 5', { filename: ':memory:' }, 2 ); const arr = []; for await (const batch of rows()) { arr.push(batch); } for (const batch of arr.slice(0, -1)) { assert.equal(batch.length, 2); } assert.equal(arr[arr.length - 1].length, 1); assert.equal(expectedRowCount, 5); } catch (e) { throw Error(e); } }); test.run();