1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18import { validateVector } from './utils';
19import { Vector, DateDay, DateMillisecond } from '../../Arrow';
20import {
21    encodeAll,
22    encodeEach,
23    encodeEachDOM,
24    encodeEachNode,
25    date32sNoNulls,
26    date64sNoNulls,
27    date32sWithNulls,
28    date64sWithNulls
29} from './utils';
30
31const testDOMStreams = process.env.TEST_DOM_STREAMS === 'true';
32const testNodeStreams = process.env.TEST_NODE_STREAMS === 'true';
33
34describe('DateDayBuilder', () => {
35
36    runTestsWithEncoder('encodeAll', encodeAll(() => new DateDay()));
37    runTestsWithEncoder('encodeEach: 5', encodeEach(() => new DateDay(), 5));
38    runTestsWithEncoder('encodeEach: 25', encodeEach(() => new DateDay(), 25));
39    runTestsWithEncoder('encodeEach: undefined', encodeEach(() => new DateDay()));
40    testDOMStreams && runTestsWithEncoder('encodeEachDOM: 25', encodeEachDOM(() => new DateDay(), 25));
41    testNodeStreams && runTestsWithEncoder('encodeEachNode: 25', encodeEachNode(() => new DateDay(), 25));
42
43    function runTestsWithEncoder(name: string, encode: (vals: (Date | null)[], nullVals?: any[]) => Promise<Vector<DateDay>>) {
44        describe(`${encode.name} ${name}`, () => {
45            it(`encodes dates no nulls`, async () => {
46                const vals = date32sNoNulls(20);
47                validateVector(vals, await encode(vals, []), []);
48            });
49            it(`encodes dates with nulls`, async () => {
50                const vals = date32sWithNulls(20);
51                validateVector(vals, await encode(vals, [null]), [null]);
52            });
53        });
54    }
55});
56
57describe('DateMillisecondBuilder', () => {
58
59    runTestsWithEncoder('encodeAll', encodeAll(() => new DateMillisecond()));
60    runTestsWithEncoder('encodeEach: 5', encodeEach(() => new DateMillisecond(), 5));
61    runTestsWithEncoder('encodeEach: 25', encodeEach(() => new DateMillisecond(), 25));
62    runTestsWithEncoder('encodeEach: undefined', encodeEach(() => new DateMillisecond()));
63    testDOMStreams && runTestsWithEncoder('encodeEachDOM: 25', encodeEachDOM(() => new DateMillisecond(), 25));
64    testNodeStreams && runTestsWithEncoder('encodeEachNode: 25', encodeEachNode(() => new DateMillisecond(), 25));
65
66    function runTestsWithEncoder(name: string, encode: (vals: (Date | null)[], nullVals?: any[]) => Promise<Vector<DateMillisecond>>) {
67        describe(`${encode.name} ${name}`, () => {
68            it(`encodes dates no nulls`, async () => {
69                const vals = date64sNoNulls(20);
70                validateVector(vals, await encode(vals, []), []);
71            });
72            it(`encodes dates with nulls`, async () => {
73                const vals = date64sWithNulls(20);
74                validateVector(vals, await encode(vals, [null]), [null]);
75            });
76        });
77    }
78});
79
80describe('DateMillisecondBuilder', () => {
81    const encode = encodeAll(() => new DateMillisecond());
82    const dates = [
83        null,
84        '2019-03-19T13:40:14.746Z',
85        '2019-03-06T21:12:50.912Z',
86        '2019-03-22T12:50:56.854Z',
87        '2019-02-25T03:34:30.916Z',
88        null,
89        null,
90        null,
91        null,
92        null,
93        null,
94        '2019-03-18T18:12:37.293Z',
95        '2019-03-26T21:58:35.307Z',
96        '2019-04-02T03:03:46.464Z',
97        '2019-03-24T18:45:25.763Z',
98        null,
99        '2019-03-19T01:10:59.189Z',
100        '2019-03-10T21:15:32.237Z',
101        '2019-03-21T07:25:34.864Z',
102        null
103    ].map((x) => x === null ? x : new Date(x));
104    it(`encodes dates with nulls`, async () => {
105        const vals = dates.slice();
106        validateVector(vals, await encode(vals, [null]), [null]);
107    });
108});
109