-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdecode.test.ts
32 lines (28 loc) · 1.06 KB
/
decode.test.ts
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
import type { Field } from '../src'
import { ColumnType, Decoders } from '../src/config'
import { cast } from '../src/decode'
describe('decode', () => {
describe('cast', () => {
test('use default decode method if config not provided', () => {
expect(cast(makeField(ColumnType.BIGINT), '21412421321421521321', {})).toEqual('21412421321421521321')
expect(cast(makeField(ColumnType.FLOAT), '1.2', {})).toEqual(1.2)
expect(cast(makeField(ColumnType.INT), '1', {})).toEqual(1)
})
test('override default decode method if config provided', () => {
const config: Decoders = {
[ColumnType.BIGINT]: BigInt,
[ColumnType.FLOAT]: String
}
expect(cast(makeField(ColumnType.BIGINT), '21412421321421521321', config)).toEqual(21412421321421521321n)
expect(cast(makeField(ColumnType.FLOAT), '1.2', config)).toEqual('1.2')
expect(cast(makeField(ColumnType.INT), '1', config)).toEqual(1)
})
})
})
function makeField(type: ColumnType): Field {
return {
name: 'foo',
type: type,
nullable: false
}
}