-
Notifications
You must be signed in to change notification settings - Fork 428
Expand file tree
/
Copy pathsensible-pg-defaults.test.ts
More file actions
132 lines (117 loc) · 3.63 KB
/
Copy pathsensible-pg-defaults.test.ts
File metadata and controls
132 lines (117 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { Pool } from 'pg'
import { Generated, Kysely, PostgresDialect, sql } from '../../../'
import { jsonObjectFrom, SENSIBLE_TYPES } from '../../../helpers/postgres'
import {
destroyTest,
initTest,
TestContext,
expect,
Database,
insertDefaultDataSet,
clearDatabase,
DIALECTS,
DIALECT_CONFIGS,
PLUGINS,
} from './test-setup.js'
interface Values {
id: Generated<number>
bigint: string
timestamptz: string
timestamp: string
date: string
time: string
timetz: string
array: number[]
// @todo: bytea
// @todo: money
// @todo: range types
}
if (DIALECTS.includes('postgres')) {
const dialect = 'postgres'
describe(`${dialect} sensible defaults`, () => {
let ctx: TestContext
let db: Kysely<Database & { values: Values }>
before(async function () {
ctx = await initTest(this, dialect, {})
await ctx.db.schema
.createTable('values')
.addColumn('id', 'serial', (col) => col.primaryKey())
.addColumn('bigint', 'bigint', (col) => col.notNull())
.addColumn('timestamptz', 'timestamptz', (col) => col.notNull())
.addColumn('timestamp', 'timestamp', (col) => col.notNull())
.addColumn('date', 'date', (col) => col.notNull())
.addColumn('time', 'time', (col) => col.notNull())
.addColumn('timetz', 'timetz', (col) => col.notNull())
.addColumn('array', sql`integer[]`, (col) => col.notNull())
.execute()
db = new Kysely<Database & { values: Values }>({
dialect: new PostgresDialect({
pool: async () => new Pool(DIALECT_CONFIGS.postgres),
types: SENSIBLE_TYPES,
}),
plugins: PLUGINS,
})
})
beforeEach(async () => {
await insertDefaultDataSet(ctx)
await db
.insertInto('values')
.values({
bigint: '9223372036854775807',
timestamptz: '2025-08-10 14:44:40.687342+02',
timestamp: '2025-08-10 14:44:40.687342Z',
date: '2025-08-10',
time: '14:44:40.687342',
timetz: '14:44:40.687342+02',
array: [1, 2, 3],
})
.execute()
})
afterEach(async () => {
await db.deleteFrom('values').execute()
await clearDatabase(ctx)
})
after(async () => {
await ctx.db.schema.dropTable('values').ifExists().execute()
await destroyTest(ctx)
})
it('regular selects should return the same values as JSON serialized values', async () => {
const columns: (keyof Values)[] = [
'timestamptz',
'timestamp',
'date',
'timetz',
'time',
'array',
]
const rawValues = await db
.selectFrom('values')
.select(columns)
.executeTakeFirstOrThrow()
const { value: jsonValues } = await db
.selectNoFrom((eb) =>
jsonObjectFrom(eb.selectFrom('values').select(columns))
.$notNull()
.as('value'),
)
.executeTakeFirstOrThrow()
expect(rawValues).to.eql(jsonValues)
})
it('to prevent data loss some types should not have the same value as their JSON serialized equivalent', async () => {
const columns: (keyof Values)[] = ['bigint']
const rawValues = await db
.selectFrom('values')
.select(columns)
.executeTakeFirstOrThrow()
const { value: jsonValues } = await db
.selectNoFrom((eb) =>
jsonObjectFrom(eb.selectFrom('values').select(columns))
.$notNull()
.as('value'),
)
.executeTakeFirstOrThrow()
expect(rawValues.bigint).to.eql('9223372036854775807')
expect(jsonValues.bigint).to.eql(9223372036854776000)
})
})
}