Skip to content

Commit

Permalink
Add tests and improve supported functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-scheer committed Feb 1, 2019
1 parent b9e5fa3 commit 885b61c
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 40 deletions.
74 changes: 73 additions & 1 deletion packages/apollo/src/commands/service/__tests__/check.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,76 @@
it("is turned on after summit", () => {});
import { validateHistoricParams } from "../check";

describe("Service Check", () => {
describe("Historic param validation", () => {
it("Returns valid results for valid input", () => {
const result = {
to: -0,
from: -86400,
queryCountThreshold: 1,
queryCountThresholdPercentage: 0.99
};

expect(
validateHistoricParams({
validationPeriod: "P1D",
queryCountThreshold: 1,
queryCountThresholdPercentage: 99
})
).toEqual(result);

expect(
validateHistoricParams({
validationPeriod: "1",
queryCountThreshold: 1,
queryCountThresholdPercentage: 99
})
).toEqual(result);
});

it("Throws on invalid validationPeriod", () => {
const getInvalidPeriod = validationPeriod => () =>
validateHistoricParams({
validationPeriod,
queryCountThreshold: 1,
queryCountThresholdPercentage: 99
});

// validationPeriod must be a parseable positive integer or of the ISO8601 format i.e. "P30D"
expect(getInvalidPeriod("invalid")).toThrow();
expect(getInvalidPeriod("1D")).toThrow();
});

it("Throws on invalid queryCountThreshold", () => {
const getInvalidThreshold = threshold => () =>
validateHistoricParams({
validationPeriod: "P1D",
queryCountThreshold: threshold,
queryCountThresholdPercentage: 99
});

// queryCountThreshold must be a positive integer
expect(getInvalidThreshold(0.1)).toThrow();
expect(getInvalidThreshold(0)).toThrow();
expect(getInvalidThreshold(-1)).toThrow();
expect(getInvalidThreshold("invalid")).toThrow();
});

it("Throws on invalid queryCountThresholdPercentage", () => {
const getInvalidThresholdPercentage = percentage => () =>
validateHistoricParams({
validationPeriod: "P1D",
queryCountThreshold: 1,
queryCountThresholdPercentage: percentage
});

// queryCountThresholdPercentage must be a number between 0 and 100 inclusive
expect(getInvalidThresholdPercentage(-1)).toThrow();
expect(getInvalidThresholdPercentage("invalid")).toThrow();
});
});
});

// it("is turned on after summit", () => {});

// jest.mock("apollo-codegen-core/lib/localfs", () => {
// return require("../../../__mocks__/localfs");
Expand Down
90 changes: 51 additions & 39 deletions packages/apollo/src/commands/service/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export default class ServiceCheck extends ProjectCommand {
}),
validationPeriod: flags.string({
description:
"The size of the time window with which to validate the schema against. Format should be a duration in ISO8601, see: https://en.wikipedia.org/wiki/ISO_8601#Durations",
default: "P1D"
"The size of the time window with which to validate the schema against. You may provide a number (in days), or an ISO8601 format duration for more granularity (see: https://en.wikipedia.org/wiki/ISO_8601#Durations)",
default: "1"
}),
queryCountThreshold: flags.integer({
description:
Expand Down Expand Up @@ -49,7 +49,7 @@ export default class ServiceCheck extends ProjectCommand {
const schema = await project.resolveSchema({ tag });
ctx.gitContext = await gitInfo();

const historicParameters = this.validateHistoricParams({
const historicParameters = validateHistoricParams({
validationPeriod: flags.validationPeriod,
queryCountThreshold: flags.queryCountThreshold,
queryCountThresholdPercentage: flags.queryCountThresholdPercentage
Expand Down Expand Up @@ -92,46 +92,58 @@ export default class ServiceCheck extends ProjectCommand {
}
return;
}
}

validateHistoricParams({
validationPeriod,
queryCountThreshold,
queryCountThresholdPercentage
}: {
validationPeriod: string;
queryCountThreshold: number;
queryCountThresholdPercentage: number;
}): HistoricQueryParameters {
const from = -1 * duration(validationPeriod).asSeconds();

if (from >= 0) {
throw new Error(
"Please provide a valid duration for the --validationPeriod flag. Valid durations are represented in ISO 8601, see: https://en.wikipedia.org/wiki/ISO_8601#Durations."
);
}
// Validates flag variables and:
// 1) returns an input object for the checkSchema query (HistoricQueryParameters)
// 2) or throws an error
export function validateHistoricParams({
validationPeriod,
queryCountThreshold,
queryCountThresholdPercentage
}: {
validationPeriod: string;
queryCountThreshold: number;
queryCountThresholdPercentage: number;
}): HistoricQueryParameters {
// Is provided string a parseable number?
const isNumber = !Number.isNaN(Number(validationPeriod));

if (!Number.isInteger(queryCountThreshold) || queryCountThreshold < 1) {
throw new Error(
"Please provide a valid number for the --queryCountThreshold flag. Valid numbers are integers in the range x >= 1."
);
}
// If it's a number, they gave us the count in days. Else, let moment parse the "P30D"
const windowInSeconds = isNumber
? duration(Number(validationPeriod), "days").asSeconds()
: duration(validationPeriod).asSeconds();

if (
queryCountThresholdPercentage < 0 ||
queryCountThresholdPercentage > 100
) {
throw new Error(
"Please provide a valid number for the --queryCountThresholdPercentage flag. Valid numbers are in the range 0 <= x <= 100."
);
}
const from = -1 * windowInSeconds;

const asPercentage = queryCountThresholdPercentage / 100;
if (from >= 0) {
throw new Error(
"Please provide a valid duration for the --validationPeriod flag. Valid durations are represented either as a number (in days) or in ISO 8601 (see: https://en.wikipedia.org/wiki/ISO_8601#Durations)."
);
}

return {
to: -0,
from,
queryCountThreshold,
queryCountThresholdPercentage: asPercentage
};
if (!Number.isInteger(queryCountThreshold) || queryCountThreshold < 1) {
throw new Error(
"Please provide a valid number for the --queryCountThreshold flag. Valid numbers are integers in the range x >= 1."
);
}

if (
Number.isNaN(Number(queryCountThresholdPercentage)) ||
queryCountThresholdPercentage < 0 ||
queryCountThresholdPercentage > 100
) {
throw new Error(
"Please provide a valid number for the --queryCountThresholdPercentage flag. Valid numbers are in the range 0 <= x <= 100."
);
}

const asPercentage = queryCountThresholdPercentage / 100;

return {
to: -0,
from,
queryCountThreshold,
queryCountThresholdPercentage: asPercentage
};
}

0 comments on commit 885b61c

Please sign in to comment.