Skip to content

Commit

Permalink
add few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianmroz-allegro committed Nov 10, 2022
1 parent 2086543 commit 93851b1
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 2 deletions.
167 changes: 167 additions & 0 deletions src/server/cli/build-settings.mocha.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* Copyright 2017-2022 Allegro.pl
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { expect, use } from "chai";
import { RefExpression } from "plywood";
import sinon from "sinon";
import equivalent from "../../client/utils/test-utils/equivalent";
import * as SourcesModule from "../../common/models/app-settings/app-settings";
import { EMPTY_APP_SETTINGS } from "../../common/models/app-settings/app-settings";
import { fromConfig } from "../../common/models/cluster/cluster";
import * as AppSettingsModule from "../../common/models/sources/sources";
import { ServerSettings } from "../models/server-settings/server-settings";
import buildSettings, { settingsForDatasetFile, settingsForDruidConnection } from "./build-settings";

use(equivalent);

describe("Build Settings", () => {
describe("settingsForDatasetFile", () => {
const settings = settingsForDatasetFile("path/to/file.json", "time", {
verbose: true,
port: 42,
serverHost: "foobar",
serverRoot: "qvux"
});

it("should create empty app settings", () => {
expect(settings.appSettings).to.be.deep.equal(EMPTY_APP_SETTINGS);
});

it("should create empty cluster array", () => {
expect(settings.sources.clusters).to.be.deep.equal([]);
});

it("should pass options to server settings constructor and create object", () => {
expect(settings.serverSettings).to.be.equivalent(ServerSettings.fromJS({
verbose: true,
port: 42,
serverHost: "foobar",
serverRoot: "qvux"
}));
});

it("should create one data cube", () => {
expect(settings.sources.dataCubes.length).to.be.equal(1);
});

it("should pass path as source to data cube", () => {
expect(settings.sources.dataCubes[0].source).to.be.equal("path/to/file.json");
});

it("should set filename as data cube name", () => {
expect(settings.sources.dataCubes[0].name).to.be.equal("file");
});

it("should set data cube cluster name to 'native'", () => {
expect(settings.sources.dataCubes[0].clusterName).to.be.equal("native");
});

it("should pass time attribute to data cube", () => {
expect(settings.sources.dataCubes[0].timeAttribute).to.be.equivalent(RefExpression.fromJS({
name: "time"
}));
});
});

describe("settingsForDruidConnection", () => {
const settings = settingsForDruidConnection("http://druid-url.com", {
verbose: true,
port: 42,
serverHost: "foobar",
serverRoot: "qvux"
}, {
type: "http-basic",
password: "secret",
username: "foobar"
});

it("should create empty app settings", () => {
expect(settings.appSettings).to.be.deep.equal(EMPTY_APP_SETTINGS);
});

it("should create empty data cubes array", () => {
expect(settings.sources.dataCubes).to.be.deep.equal([]);
});

it("should pass options to server settings constructor and create object", () => {
expect(settings.serverSettings).to.be.equivalent(ServerSettings.fromJS({
verbose: true,
port: 42,
serverHost: "foobar",
serverRoot: "qvux"
}));
});

it("should create one cluster", () => {
expect(settings.sources.clusters.length).to.be.equal(1);
});

it("should pass url to cluster", () => {
expect(settings.sources.clusters[0].url).to.be.equal("http://druid-url.com");
});

it("should set 'druid' as cluster name", () => {
expect(settings.sources.clusters[0].name).to.be.equal("druid");
});

it("should pass auth to cluster", () => {
expect(settings.sources.clusters[0].auth).to.be.deep.equal({
type: "http-basic",
password: "secret",
username: "foobar"
});
});
});

describe("buildSettings", () => {
it("should pass config to AppSettings fromConfig function", () => {
const appSettingsFromConfigStub = sinon.stub(AppSettingsModule, "fromConfig").returns("app-settings");
buildSettings({ config: true }, {});
expect(appSettingsFromConfigStub.calledWith({ config: true })).to.be.true;
appSettingsFromConfigStub.restore();
});

it("should pass config to Sources fromConfig function", () => {
const sourcesFromConfigStub = sinon.stub(SourcesModule, "fromConfig").returns("sources");
buildSettings({ config: true }, {});
expect(sourcesFromConfigStub.calledWith({ config: true })).to.be.true;
sourcesFromConfigStub.restore();
});

it("should pass merged config and options to ServerSettings.fromJS", () => {
const serverSettingsFromJSSStub = sinon.stub(ServerSettings, "fromJS").returns("server-settings");
buildSettings({ config: true }, { options: true } as any);
expect(serverSettingsFromJSSStub.calledWith({ config: true, options: true })).to.be.true;
serverSettingsFromJSSStub.restore();
});

it("should pass auth object to all clusters", () => {
const makeCluster = (name: string) => fromConfig({ name, url: `https://${name}.com` });

const settings = buildSettings({
clusters: [makeCluster("foobar-1"), makeCluster("foobar-2")]
}, {}, { type: "http-basic", password: "secret", username: "foobar" });

expect(settings.sources.clusters[0].auth).to.be.deep.equal({
type: "http-basic", password: "secret", username: "foobar"
});

expect(settings.sources.clusters[1].auth).to.be.deep.equal({
type: "http-basic", password: "secret", username: "foobar"
});
});
});
});
37 changes: 37 additions & 0 deletions src/server/cli/load-config-file.mocha.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2017-2022 Allegro.pl
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { expect } from "chai";
import * as sinon from "sinon";
import * as FileModule from "../utils/file/file";
import { loadConfigFile } from "./load-config-file";

describe("loadConfigFile", () => {
it("should pass path and yml format to loadFileSync", () => {
const loadFileSync = sinon.stub(FileModule, "loadFileSync").returns("result");
loadConfigFile("path", null as any);
expect(loadFileSync.calledWith("path", "yaml")).to.be.true;
loadFileSync.restore();
});

it("should call program.error with error message if loadFileSync throws", () => {
const loadFileSync = sinon.stub(FileModule, "loadFileSync").throws(new Error("error-message"));
const programSpy = { error: sinon.spy() };
loadConfigFile("path", programSpy as any);
expect(programSpy.error.calledWithMatch("error-message")).to.be.true;
loadFileSync.restore();
});
});
66 changes: 66 additions & 0 deletions src/server/cli/utils.mocha.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2017-2022 Allegro.pl
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { expect } from "chai";
import { InvalidArgumentError } from "commander";
import { parseCredentials, parseInteger } from "./utils";

describe("CLI utils", () => {
describe("parseInteger", () => {
it("should parse valid integer", () => {
expect(parseInteger("9090")).to.be.equal(9090);
});

it("should throw InvalidArgumentError on invalid integer", () => {
expect(() => parseInteger("foobar")).to.throw(InvalidArgumentError);
});

it("should throw with message on invalid integer", () => {
expect(() => parseInteger("foobar")).to.throw("Must be an integer");
});
});

describe("parseCredentials", () => {
it("should pass undefined if both parameters are undefined", () => {
expect(parseCredentials(undefined, undefined)).to.be.undefined;
});

it("should return ClusterAuth object with username, password and type", () => {
expect(parseCredentials("foobar", "secret")).to.be.deep.equal({
username: "foobar",
password: "secret",
type: "http-basic"
});
});

it("should throw InvalidArgumentError on missing password", () => {
expect(() => parseCredentials("foobar", undefined)).to.throw(InvalidArgumentError);
});

it("should throw with message on missing password", () => {
expect(() => parseCredentials("foobar", undefined)).to.throw("Expected password for username");
});

it("should throw InvalidArgumentError on missing username", () => {
expect(() => parseCredentials(undefined, "secret")).to.throw(InvalidArgumentError);
});

it("should throw with message on missing username", () => {
expect(() => parseCredentials(undefined, "secret")).to.throw("Expected username for password");
});
});

});
4 changes: 2 additions & 2 deletions src/server/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ export function parseInteger(value: string): number {
export function parseCredentials(username: string | undefined, password: string | undefined): ClusterAuthJS | undefined {
if (isNil(password) && isNil(username)) return undefined;
if (isNil(username)) {
throw new InvalidArgumentError("You need to pass username if you pass password");
throw new InvalidArgumentError("Expected username for password");
}
if (isNil(password)) {
throw new InvalidArgumentError("You need to pass password if you pass username");
throw new InvalidArgumentError("Expected password for username");
}
return {
type: "http-basic",
Expand Down

0 comments on commit 93851b1

Please sign in to comment.