@rockset/cli
Version:
Official Rockset CLI
157 lines (156 loc) • 6.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
const resolve_1 = require("../../commands/local/resolve");
const init_1 = require("../../commands/local/init");
const core_1 = require("@rockset/core");
const add_1 = require("../../commands/local/queryLambda/add");
const path = require("path");
const delete_1 = require("../../commands/local/queryLambda/delete");
const list_1 = require("../../commands/local/queryLambda/list");
const _ = require("lodash");
const os = require("os");
const fse = require("fs-extra");
const types_1 = require("@rockset/core/dist/types");
/**
* NOTE: all tests in this file are run on the user's real file system
* Changes should be limited to the testLambdas directory
*
* DO NOT invoke any auth commands here, as they will actually modify the user's real auth configuration file
*/
const expectToExist = async (p) => expect(await core_1.fileutil.exists(p)).toBeTruthy();
const expectToNotExist = async (p) => expect(await core_1.fileutil.exists(p)).toBeFalsy;
const expectError = async (f) => {
try {
await f();
fail('This should have thrown an exception');
}
catch (error) { }
};
const testAdd = async (name, expectedPath) => {
await expectToNotExist(expectedPath);
await add_1.default.run([name]);
await expectToExist(expectedPath);
const sqlPath = await core_1.fileutil.getLambdaSqlPathFromQualifiedName(core_1.types.parseLambdaQualifiedName(name));
await expectToExist(sqlPath);
// This should fail
await expectError(async () => add_1.default.run([name]));
};
const testDelete = async (name, expectedPath) => {
await expectToExist(expectedPath);
await delete_1.default.run([name, '-y']);
await expectToNotExist(expectedPath);
};
const testResolve = async ([name, file]) => {
const spy = jest.spyOn(process.stdout, 'write');
spy.mockClear();
await resolve_1.default.run([name]);
expect(spy).toHaveBeenCalledWith(file + '\n');
spy.mockClear();
const { ws } = core_1.pathutil.getWsNamePair(core_1.types.parseLambdaQualifiedName(name));
const wsPath = path.dirname(file);
await resolve_1.default.run([ws, '-e', 'workspace']);
expect(spy).toHaveBeenCalledWith(wsPath + '\n');
};
const testResolveFailure = async ([name, file]) => {
await expectError(async () => resolve_1.default.run([name]));
await expectError(async () => resolve_1.default.run([name, '--sql']));
const spy = jest.spyOn(process.stdout, 'write');
spy.mockClear();
await resolve_1.default.run([name, '--no-exists']);
expect(spy).toHaveBeenCalledWith(file + '\n');
spy.mockClear();
const { ws } = core_1.pathutil.getWsNamePair(core_1.types.parseLambdaQualifiedName(name));
const wsPath = path.dirname(file);
await resolve_1.default.run([ws, '-e', 'workspace']);
expect(spy).toHaveBeenCalledWith(wsPath + '\n');
};
const relative = (p) => path.join(process.cwd(), init_1.DEFAULT_SOURCE_ROOT, p);
describe('local command test suite', () => {
let TEST_DIR;
let all;
let lambdas;
beforeAll(async () => {
TEST_DIR = path.join(os.tmpdir(), 'rockset_cli_test', 'root');
await fse.emptyDir(TEST_DIR);
process.chdir(TEST_DIR);
all = _.sortBy([
['commons.foo', relative('commons/foo.lambda.json')],
['commons.sub.sub2.foo', relative('commons/sub/sub2/foo.lambda.json')],
['a.b.c.d.e.f.g.h', relative('a/b/c/d/e/f/g/h.lambda.json')],
[
'commons.hello-world.helloWorld.hello_world',
relative('commons/hello-world/helloWorld/hello_world.lambda.json'),
],
], (x) => x[0]);
lambdas = all.map(([name]) => name);
});
afterAll(async () => {
await fse.emptyDir(TEST_DIR);
});
// ******* Helpers *********
const mapAll = (f) => {
return Promise.all(all.map((x) => f(x).catch((error) => {
throw new Error(`Failed at ${x}, ${error}`);
})));
};
// ****** Start tests here ******
// Initialize
test('initialize', async () => {
await init_1.default.run(['-y']);
expect(await core_1.fileutil.exists(core_1.types.ROOT_CONFIG)).toBe(true);
});
// Add Query Lambdas failure
test('Add Query Lambda without ws failure', async () => {
const name = 'commons';
await expectError(async () => {
await add_1.default.run([name]);
});
});
// Add Query Lambdas
test('Add Query Lambda successful', async () => {
await mapAll(([name, file]) => testAdd(name, file));
});
// List Query Lambdas
test('List Query Lambda successful', async () => {
const spy = jest.spyOn(process.stdout, 'write');
await list_1.default.run();
const result = lambdas.join('\n') + '\n';
expect(spy).toHaveBeenCalledWith(result);
});
test('Resolve Query Lambdas', async () => {
await mapAll(testResolve);
});
// Delete all Query Lambdas by lambda (-l flag)
test('Delete Query Lambdas', async () => {
await mapAll(([name, file]) => testDelete(name, file));
});
// All lambdas should fail to resolve (they've been deleted), but ws should still resolve
test('Resolve Query Lambdas', async () => {
await mapAll(testResolveFailure);
});
// List Query Lambdas: should list nothing
test('List Query Lambda successful', async () => {
const spy = jest.spyOn(process.stdout, 'write');
await list_1.default.run();
expect(spy).toHaveBeenCalledWith('\n');
});
// Add back Query Lambdas
test('Add Query Lambda successful', async () => {
await mapAll(([name, file]) => testAdd(name, file));
});
test('Add query lambda with description', async () => {
const expectedPath = relative('description/commons/test.lambda.json');
const name = 'description.commons.test';
const description = 'my description';
await expectToNotExist(expectedPath);
await add_1.default.run([name, '-d', 'my description']);
await expectToExist(expectedPath);
const sqlPath = await core_1.fileutil.getLambdaSqlPathFromQualifiedName(core_1.types.parseLambdaQualifiedName(name));
await expectToExist(sqlPath);
const lambdaEntity = await core_1.fileutil.readLambda(types_1.parseLambdaQualifiedName(name), types_1.parseAbsolutePath(expectedPath));
expect(lambdaEntity.config.description).toEqual(description);
// This should fail
await expectError(async () => add_1.default.run([name]));
});
});