|
| 1 | +import { Utf8, Int64, Bool, List, Field, Float64, DataType } from '@apache-arrow/esnext-esm'; |
| 2 | +import test from 'ava'; |
| 3 | + |
| 4 | +import { Column, createColumn } from '../schema/column.js'; |
| 5 | +import { JSONType } from '../types/json.js'; |
| 6 | + |
| 7 | +import { objectToColumns } from './transform.js'; |
| 8 | + |
| 9 | +test('should parse object as expected', (t) => { |
| 10 | + const expectedColumns: Column[] = [ |
| 11 | + createColumn({ |
| 12 | + name: 'string', |
| 13 | + type: new Utf8(), |
| 14 | + }), |
| 15 | + createColumn({ |
| 16 | + name: 'number', |
| 17 | + type: new Int64(), |
| 18 | + }), |
| 19 | + createColumn({ |
| 20 | + name: 'float', |
| 21 | + type: new Float64(), |
| 22 | + }), |
| 23 | + createColumn({ |
| 24 | + name: 'boolean', |
| 25 | + type: new Bool(), |
| 26 | + }), |
| 27 | + createColumn({ |
| 28 | + name: 'object', |
| 29 | + type: new JSONType(), |
| 30 | + }), |
| 31 | + createColumn({ |
| 32 | + name: 'array', |
| 33 | + type: new List(new Field('element', new Utf8())), |
| 34 | + }), |
| 35 | + ]; |
| 36 | + |
| 37 | + const columns = objectToColumns({ |
| 38 | + string: 'test', |
| 39 | + number: 1, |
| 40 | + float: 3.14, |
| 41 | + boolean: false, |
| 42 | + object: { inner: 'foo' }, |
| 43 | + array: ['foo', 'bar'], |
| 44 | + }); |
| 45 | + t.deepEqual(columns, expectedColumns); |
| 46 | +}); |
| 47 | + |
| 48 | +test('should parse object with custom types', (t) => { |
| 49 | + const expectedColumns: Column[] = [ |
| 50 | + createColumn({ |
| 51 | + name: 'string', |
| 52 | + type: new Utf8(), |
| 53 | + }), |
| 54 | + createColumn({ |
| 55 | + name: 'float', |
| 56 | + type: new Float64(), |
| 57 | + }), |
| 58 | + ]; |
| 59 | + |
| 60 | + const columns = objectToColumns( |
| 61 | + { |
| 62 | + string: 'test', |
| 63 | + float: 1, |
| 64 | + }, |
| 65 | + { |
| 66 | + getTypeFromValue: function (key: string, value: unknown): DataType | null | undefined { |
| 67 | + if (key === 'float') return new Float64(); |
| 68 | + return undefined; |
| 69 | + }, |
| 70 | + }, |
| 71 | + ); |
| 72 | + t.deepEqual(columns, expectedColumns); |
| 73 | +}); |
| 74 | + |
| 75 | +test('should parse object with custom types and allow skip columns in type transformer', (t) => { |
| 76 | + const expectedColumns: Column[] = [ |
| 77 | + createColumn({ |
| 78 | + name: 'string', |
| 79 | + type: new Utf8(), |
| 80 | + }), |
| 81 | + ]; |
| 82 | + |
| 83 | + const columns = objectToColumns( |
| 84 | + { |
| 85 | + string: 'test', |
| 86 | + skip: 'test', |
| 87 | + }, |
| 88 | + { |
| 89 | + getTypeFromValue: function (key: string, value: unknown): DataType | null | undefined { |
| 90 | + return key === 'skip' ? null : undefined; |
| 91 | + }, |
| 92 | + }, |
| 93 | + ); |
| 94 | + t.deepEqual(columns, expectedColumns); |
| 95 | +}); |
| 96 | + |
| 97 | +test('should parse object and skip columns', (t) => { |
| 98 | + const expectedColumns: Column[] = [ |
| 99 | + createColumn({ |
| 100 | + name: 'string', |
| 101 | + type: new Utf8(), |
| 102 | + }), |
| 103 | + ]; |
| 104 | + |
| 105 | + const columns = objectToColumns( |
| 106 | + { |
| 107 | + string: 'test', |
| 108 | + skip: 'test', |
| 109 | + }, |
| 110 | + { |
| 111 | + skipColumns: ['skip'], |
| 112 | + }, |
| 113 | + ); |
| 114 | + t.deepEqual(columns, expectedColumns); |
| 115 | +}); |
| 116 | + |
| 117 | +test('should parse object and set PKs', (t) => { |
| 118 | + const expectedColumns: Column[] = [ |
| 119 | + createColumn({ |
| 120 | + name: 'id', |
| 121 | + type: new Utf8(), |
| 122 | + primaryKey: true, |
| 123 | + }), |
| 124 | + createColumn({ |
| 125 | + name: 'string', |
| 126 | + type: new Utf8(), |
| 127 | + }), |
| 128 | + ]; |
| 129 | + |
| 130 | + const columns = objectToColumns( |
| 131 | + { |
| 132 | + id: 'the-id', |
| 133 | + string: 'test', |
| 134 | + }, |
| 135 | + { |
| 136 | + primaryKeys: ['id'], |
| 137 | + }, |
| 138 | + ); |
| 139 | + t.deepEqual(columns, expectedColumns); |
| 140 | +}); |
| 141 | + |
| 142 | +test('should allow direct overrides', (t) => { |
| 143 | + const expectedColumns: Column[] = [ |
| 144 | + createColumn({ |
| 145 | + name: 'id', |
| 146 | + type: new Utf8(), |
| 147 | + notNull: true, |
| 148 | + unique: true, |
| 149 | + }), |
| 150 | + createColumn({ |
| 151 | + name: 'string', |
| 152 | + type: new Utf8(), |
| 153 | + }), |
| 154 | + ]; |
| 155 | + |
| 156 | + const columns = objectToColumns( |
| 157 | + { |
| 158 | + id: 'the-id', |
| 159 | + string: 'test', |
| 160 | + }, |
| 161 | + { |
| 162 | + overrides: new Map<string, Column>([ |
| 163 | + [ |
| 164 | + 'id', |
| 165 | + createColumn({ |
| 166 | + name: 'id', |
| 167 | + type: new Utf8(), |
| 168 | + notNull: true, |
| 169 | + unique: true, |
| 170 | + }), |
| 171 | + ], |
| 172 | + ]), |
| 173 | + }, |
| 174 | + ); |
| 175 | + t.deepEqual(columns, expectedColumns); |
| 176 | +}); |
0 commit comments