Skip to content

Commit

Permalink
factor tests with it.for()
Browse files Browse the repository at this point in the history
  • Loading branch information
severo committed Dec 20, 2024
1 parent 90f8a3b commit 00023d7
Showing 1 changed file with 9 additions and 60 deletions.
69 changes: 9 additions & 60 deletions test/column.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
import { compressors } from 'hyparquet-compressors'
import { describe, expect, it } from 'vitest'
import { getColumnRange, readColumn } from '../src/column.js'
import { parquetMetadata } from '../src/hyparquet.js'
import { getSchemaPath } from '../src/schema.js'
import { getColumnRange, readColumn } from '../src/column.js'
import { asyncBufferFromFile } from '../src/utils.js'

describe('readColumn', () => {
it('read columns when rowLimit is undefined', async () => {
const testFile = 'test/files/float16_nonzeros_and_nans.parquet'
const asyncBuffer = await asyncBufferFromFile(testFile)
const arrayBuffer = await asyncBuffer.slice(0)
const metadata = parquetMetadata(arrayBuffer)
const values = [null, 1, -2, NaN, 0, -1, -0, 2]

const column = metadata.row_groups[0].columns[0]
if (!column.meta_data) throw new Error(`No column metadata for ${testFile}`)
const [columnStartByte, columnEndByte] = getColumnRange(column.meta_data).map(Number)
const columnArrayBuffer = arrayBuffer.slice(columnStartByte, columnEndByte)
const schemaPath = getSchemaPath(metadata.schema, column.meta_data?.path_in_schema ?? [])
const reader = { view: new DataView(columnArrayBuffer), offset: 0 }

const rowLimit = undefined
const result = readColumn(reader, rowLimit, column.meta_data, schemaPath, { file: asyncBuffer, compressors })
const expected = [null, 1, -2, NaN, 0, -1, -0, 2]
expect(result).toEqual(expected)
})

it('read columns when rowLimit is Infinity', async () => {
describe('readColumn', () => {
it.for([
{ rowLimit: undefined, expected: values },
{ rowLimit: Infinity, expected: values },
{ rowLimit: 2, expected: values.slice(0, 2) },
{ rowLimit: 0, expected: [] },
])('readColumn with rowLimit %p', async ({ rowLimit, expected }) => {
const testFile = 'test/files/float16_nonzeros_and_nans.parquet'
const asyncBuffer = await asyncBufferFromFile(testFile)
const arrayBuffer = await asyncBuffer.slice(0)
Expand All @@ -38,46 +26,7 @@ describe('readColumn', () => {
const schemaPath = getSchemaPath(metadata.schema, column.meta_data?.path_in_schema ?? [])
const reader = { view: new DataView(columnArrayBuffer), offset: 0 }

const rowLimit = Infinity
const result = readColumn(reader, rowLimit, column.meta_data, schemaPath, { file: asyncBuffer, compressors })
const expected = [null, 1, -2, NaN, 0, -1, -0, 2]
expect(result).toEqual(expected)
})

it('read columns when rowLimit is defined', async () => {
const testFile = 'test/files/float16_nonzeros_and_nans.parquet'
const asyncBuffer = await asyncBufferFromFile(testFile)
const arrayBuffer = await asyncBuffer.slice(0)
const metadata = parquetMetadata(arrayBuffer)

const column = metadata.row_groups[0].columns[0]
if (!column.meta_data) throw new Error(`No column metadata for ${testFile}`)
const [columnStartByte, columnEndByte] = getColumnRange(column.meta_data).map(Number)
const columnArrayBuffer = arrayBuffer.slice(columnStartByte, columnEndByte)
const schemaPath = getSchemaPath(metadata.schema, column.meta_data?.path_in_schema ?? [])
const reader = { view: new DataView(columnArrayBuffer), offset: 0 }

const rowLimit = 2
const result = readColumn(reader, rowLimit, column.meta_data, schemaPath, { file: asyncBuffer, compressors })
expect(result.length).toBe(rowLimit)
})

it('read columns when rowLimit is 0', async () => {
const testFile = 'test/files/float16_nonzeros_and_nans.parquet'
const asyncBuffer = await asyncBufferFromFile(testFile)
const arrayBuffer = await asyncBuffer.slice(0)
const metadata = parquetMetadata(arrayBuffer)

const column = metadata.row_groups[0].columns[0]
if (!column.meta_data) throw new Error(`No column metadata for ${testFile}`)
const [columnStartByte, columnEndByte] = getColumnRange(column.meta_data).map(Number)
const columnArrayBuffer = arrayBuffer.slice(columnStartByte, columnEndByte)
const schemaPath = getSchemaPath(metadata.schema, column.meta_data?.path_in_schema ?? [])
const reader = { view: new DataView(columnArrayBuffer), offset: 0 }

const rowLimit = 0
const result = readColumn(reader, rowLimit, column.meta_data, schemaPath, { file: asyncBuffer, compressors })
expect(result.length).toBe(rowLimit)
})

})

0 comments on commit 00023d7

Please sign in to comment.