-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: deduplicate backend unit tests
- Loading branch information
Showing
12 changed files
with
149 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// https://stackoverflow.com/a/64174790/621176 | ||
export const BackendNames = ['parquet-tools', 'parquets', 'arrow'] as const; | ||
export type BackendName = typeof BackendNames[number]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { ArrowBackend } from './arrow-backend'; | ||
import { BackendName } from './backend-name'; | ||
import { ParquetToolsBackend } from './parquet-tools-backend'; | ||
import { ParquetsBackend } from './parquets-backend'; | ||
|
||
export function createParquetBackend(backend: BackendName) { | ||
switch (backend) { | ||
case 'parquet-tools': | ||
return new ParquetToolsBackend; | ||
case 'parquets': | ||
return new ParquetsBackend; | ||
default: | ||
return new ArrowBackend; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
import { jest } from '@jest/globals'; | ||
import * as path from 'path'; | ||
|
||
const testDir = path.join(__dirname, '..', '..'); | ||
|
||
module.exports = { | ||
workspace: { | ||
getConfiguration: jest.fn().mockReturnValue({ | ||
get: jest.fn(name => { | ||
switch (name) { | ||
case 'parquetToolsPath': | ||
return path.join(testDir, 'workspace', 'parquet-tools-1.12.0-SNAPSHOT.jar'); | ||
default: | ||
return undefined; | ||
} | ||
}) | ||
}), | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import toArray from '@async-generators/to-array'; | ||
import { afterEach, describe, expect, jest, test } from '@jest/globals'; | ||
import { createReadStream } from 'fs'; | ||
import * as path from 'path'; | ||
import { createInterface } from 'readline'; | ||
import { CancellationToken } from 'vscode'; | ||
import { BackendNames } from '../../src/backend-name'; | ||
import { createParquetBackend } from '../../src/parquet-backend-factory'; | ||
import * as vscode from 'vscode'; | ||
|
||
jest.setTimeout(60000); | ||
|
||
const rootDir = path.join(__dirname, '..', '..'); | ||
|
||
describe.each(BackendNames)("%s backend tests", (backendName) => { | ||
const backend = createParquetBackend(backendName); | ||
const workspace = path.join(rootDir, 'test', 'workspace'); | ||
|
||
const testFiles = { | ||
'parquet-tools': [ | ||
['small', 'small'], | ||
['large', 'large'], | ||
], | ||
'parquets': [ | ||
['small', 'small'], | ||
['large', 'large'], | ||
], | ||
'arrow': [ | ||
['small', 'small'], | ||
['large', 'large.arrow'], | ||
] | ||
}; | ||
|
||
test.each( | ||
testFiles[backendName] | ||
)('Converts %s parquet to JSON', async function (input: string, expectedFile: string) { | ||
const actual = (await toArray(backend.toJson(path.join(workspace, `${input}.parquet`)))).map(line => line.trim()); | ||
const expected = await toArray(createInterface({ input: createReadStream(path.join(workspace, `${expectedFile}.json`)) })); | ||
|
||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
test("Error on not existing file", async function () { | ||
const error = (() => { | ||
switch (backendName) { | ||
case 'arrow': | ||
return "Failed to open no-such-file: Failed to open local file 'no-such-file'"; | ||
case 'parquets': | ||
return /ENOENT: no such file or directory, stat '.*no-such-file'/; | ||
case 'parquet-tools': | ||
return /parquet-tools exited with code 1\n.*java.io.FileNotFoundException: File no-such-file does not exist/s; | ||
} | ||
})(); | ||
await expect(toArray(backend.toJson("no-such-file"))).rejects.toThrow(error); | ||
}); | ||
|
||
test("cancellation", async function () { | ||
const token = { | ||
get isCancellationRequested() { | ||
return this.isCancellationRequestedMock(); | ||
}, | ||
isCancellationRequestedMock: jest.fn().mockReturnValueOnce(false).mockReturnValue(true), | ||
onCancellationRequested: jest.fn() | ||
}; | ||
expect(await toArray(backend.toJson(path.join(workspace, `small.parquet`), token as CancellationToken))).toHaveLength(1); | ||
expect(token.isCancellationRequestedMock).toBeCalledTimes(2); | ||
}); | ||
|
||
describe('mocked jsonSpace', () => { | ||
const workspaceConfig = vscode.workspace.getConfiguration(); | ||
const parquetToolsPath = workspaceConfig.get('parquetToolsPath'); | ||
let mockedSpaced: jest.Mocked<typeof workspaceConfig.get>; | ||
|
||
test.each([0, 2, 10, "\t", "###"])('Test space %s', async function (space: number | string) { | ||
mockedSpaced = jest.mocked(workspaceConfig.get).mockImplementation(name => { | ||
return { | ||
'jsonSpace': space, | ||
'parquetToolsPath': parquetToolsPath | ||
}[name]; | ||
}); | ||
|
||
const json = (await toArray(backend.toJson(path.join(workspace, `small.parquet`)))).map(line => line.trim()); | ||
const records = await toArray(createInterface({ input: createReadStream(path.join(workspace, `small.json`)) })); | ||
const expected = records.map(record => JSON.stringify(JSON.parse(record), null, space)); | ||
|
||
expect(json).toEqual(expected); | ||
}); | ||
|
||
afterEach(() => { | ||
mockedSpaced.mockRestore(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,9 @@ | ||
import {describe, expect, test, jest} from '@jest/globals'; | ||
import toArray from '@async-generators/to-array'; | ||
import { createReadStream } from 'fs'; | ||
import * as path from 'path'; | ||
import { createInterface } from "readline"; | ||
import { describe, expect, test } from '@jest/globals'; | ||
import { ParquetToolsBackend } from "../../src/parquet-tools-backend"; | ||
import { CancellationToken } from 'vscode'; | ||
|
||
const rootDir = path.join(__dirname, '..', '..'); | ||
|
||
jest.setTimeout(60000); | ||
|
||
jest.mock('vscode', () => { | ||
const getConfigurationMock = jest.fn(); | ||
getConfigurationMock.mockReturnValue({ | ||
get: jest.fn(name => { | ||
switch (name) { | ||
case 'parquetToolsPath': | ||
return path.join(rootDir, 'test', 'workspace', 'parquet-tools-1.12.0-SNAPSHOT.jar'); | ||
default: | ||
return undefined; | ||
} | ||
}) | ||
}); | ||
return { | ||
workspace: { | ||
getConfiguration: getConfigurationMock, | ||
} | ||
}; | ||
}); | ||
|
||
describe("ParquetToolsBackend tests", () => { | ||
const backend = new ParquetToolsBackend(); | ||
const workspace = path.join(rootDir, 'test', 'workspace'); | ||
|
||
test.each( | ||
["small", "large"] | ||
)('Converts %s parquet to JSON', async function (name: string) { | ||
const json = (await toArray(backend.toJson(path.join(workspace, `${name}.parquet`)))); | ||
const expected = await toArray(createInterface({ input: createReadStream(path.join(workspace, `${name}.json`)) })); | ||
|
||
expect(json).toEqual(expected); | ||
}); | ||
|
||
test("Error on not existing file", async function () { | ||
await expect(toArray(backend.toJson("no-such-file"))).rejects.toThrow( | ||
/parquet-tools exited with code 1\n.*java.io.FileNotFoundException: File no-such-file does not exist/s); | ||
}); | ||
|
||
test("-h works", async function () { | ||
const stdout = (await ParquetToolsBackend.spawnParquetTools(['-h']).next()).value; | ||
expect(stdout).toContain('parquet-tools cat:'); | ||
}); | ||
|
||
test("cancellation", async function () { | ||
const token = { | ||
get isCancellationRequested() { | ||
return this.isCancellationRequestedMock(); | ||
}, | ||
isCancellationRequestedMock: jest.fn().mockReturnValueOnce(false).mockReturnValue(true), | ||
onCancellationRequested: jest.fn() | ||
}; | ||
expect(await toArray(backend.toJson(path.join(workspace, `small.parquet`), token as CancellationToken))).toHaveLength(1); | ||
expect(token.isCancellationRequestedMock).toBeCalledTimes(2); | ||
}); | ||
}); |
Oops, something went wrong.