Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON schema and validation #182

Merged
merged 21 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update tests and data validation
  • Loading branch information
humitos committed Nov 14, 2023
commit e135e8bf9ec3a9b12a0e08f66452ea9446b636c7
6 changes: 3 additions & 3 deletions dist/readthedocs-addons.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/readthedocs-addons.js.map

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion src/data-validation.js
humitos marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note here, I didn't inspect the actual schemas here deeply. I think we can adjust if they don't match what we are outputting from the application and don't need to be strict about ensuring they are strictly accurate yet.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we planning to add some backend tests to see if the API response conforms to the schema(s)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agjohnson

Just a note here, I didn't inspect the actual schemas here deeply. I think we can adjust if they don't match what we are outputting from the application and don't need to be strict about ensuring they are strictly accurate yet.

Yes. Currently, they are "good enough" but not perfect. I've been finding issues while writing the test cases, so I expect to find more as we keep working on the addons. We already have the pattern defined, so updating them shouldn't be too hard

@zanderle

Are we planning to add some backend tests to see if the API response conforms to the schema(s)?

This is something that I'd eventually want but we can't prioritize right now. We will need to find a way to share the JSON schema between the two repositories and execute the tests in both platforms with the same schema. "Sharing the schema" probably means writing the schema in JSON instead of JS that's something I tried to avoid because JSON doesn't support comments among others painful things, like not supporting trailing comma 🙃

Do you have experience doing this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way I did it in the past was to have the schema in json and then use description and $comment keywords to provide context where needed.

It's been a few years since I did this, so I can't remember how exactly I shared it between the frontend and backend repos. Let me try to find that information actually.

Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const addons_ethicalads = {
const addons_flyout = {
$id: "https://v1.schemas.readthedocs.org/addons.flyout.json",
type: "object",
required: ["addons"],
required: ["addons", "domains", "projects", "versions"],
properties: {
addons: {
type: "object",
Expand Down Expand Up @@ -156,15 +156,18 @@ const addons_flyout = {
},
domains: {
type: "object",
required: ["dashboard"],
properties: {
dashboard: { type: "string" },
},
},
projects: {
type: "object",
required: ["current"],
properties: {
current: {
type: "object",
required: ["slug", "single_version"],
properties: {
slug: { type: "string" },
single_version: { type: "boolean" },
Expand All @@ -174,9 +177,11 @@ const addons_flyout = {
},
versions: {
type: "object",
required: ["current"],
properties: {
current: {
type: "object",
required: ["slug"],
properties: {
slug: { type: "string" },
},
Expand Down
2 changes: 1 addition & 1 deletion src/ethicalads.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class EthicalAdsAddon extends AddonBase {

static isEnabled(config) {
return (
super.isConfigValid(config) && config.addons.ethicalads.ad_free === false
super.isEnabled(config) && config.addons.ethicalads.ad_free === false
);
}
}
54 changes: 51 additions & 3 deletions tests/analytics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,70 @@ import { expect, assert, fixture, html } from "@open-wc/testing";
import { AnalyticsAddon } from "../src/analytics";

describe("Analytics addon", () => {
it("is disabled by default", () => {
it("invalid configuration disables the addon", () => {
expect(
AnalyticsAddon.isEnabled({
addons: {
analytics: {},
analytics: {
enabled: true,
},
},
}),
).to.be.false;
});

it("is enabled with configuration", () => {
it("is disabled", () => {
expect(
AnalyticsAddon.isEnabled({
addons: {
analytics: {
enabled: false,
code: null,
},
},
projects: {
current: {
slug: "project",
language: {
code: "es",
},
programming_language: {
code: "py",
},
},
},
versions: {
current: {
slug: "project",
},
},
}),
).to.be.false;
});

it("valid data and enabled", () => {
expect(
AnalyticsAddon.isEnabled({
addons: {
analytics: {
enabled: true,
code: "UA-12345",
},
},
projects: {
current: {
slug: "project",
language: {
code: "es",
},
programming_language: {
code: "py",
},
},
},
versions: {
current: {
slug: "project",
},
},
}),
Expand Down
22 changes: 19 additions & 3 deletions tests/docdiff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,38 @@ import { expect, assert, fixture, html } from "@open-wc/testing";
import { DocDiffAddon, DocDiffElement } from "../src/docdiff";

describe("Doc diff addon", () => {
it("is disabled by default", () => {
it("invalid configuration disables the addon", () => {
expect(
DocDiffAddon.isEnabled({
addons: {
doc_diff: {},
doc_diff: {
enabled: true,
},
},
}),
).to.be.false;
});

it("is disabled with valid data", () => {
expect(
DocDiffAddon.isEnabled({
addons: {
doc_diff: {
enabled: false,
base_url: "https://project.readthedocs.io/en/latest/index.html",
},
},
}),
).to.be.false;
});

it("is enabled with configuration", () => {
it("is enabled with valid data", () => {
expect(
DocDiffAddon.isEnabled({
addons: {
doc_diff: {
enabled: true,
base_url: "https://project.readthedocs.io/en/latest/index.html",
},
},
}),
Expand Down
30 changes: 27 additions & 3 deletions tests/ethicalads.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,43 @@ import { expect, assert, fixture, html } from "@open-wc/testing";
import { EthicalAdsAddon } from "../src/ethicalads";

describe("EthicalAds addon", () => {
it("is disabled by default", () => {
it("invalid configuration disables the addon", () => {
expect(
EthicalAdsAddon.isEnabled({
addons: {
ethicalads: {},
ethicalads: {
enabled: true,
},
},
}),
).to.be.false;
});

it("is enabled with configuration", () => {
it("is disabled with valid data", () => {
expect(
EthicalAdsAddon.isEnabled({
addons: {
ethicalads: {
enabled: false,
ad_free: false,
campaign_types: ["community", "paid"],
keywords: ["docs", "data-science"],
publisher: "readthedocs",
},
},
}),
).to.be.false;
});

it("is enabled with valid data", () => {
expect(
EthicalAdsAddon.isEnabled({
addons: {
ethicalads: {
enabled: true,
ad_free: false,
campaign_types: ["community", "paid"],
keywords: ["docs", "data-science"],
publisher: "readthedocs",
},
},
Expand All @@ -32,6 +53,9 @@ describe("EthicalAds addon", () => {
ethicalads: {
enabled: true,
ad_free: true,
campaign_types: ["community", "paid"],
keywords: ["docs", "data-science"],
publisher: "readthedocs",
},
},
}),
Expand Down
54 changes: 51 additions & 3 deletions tests/flyout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,70 @@ import { expect, assert, fixture, html } from "@open-wc/testing";
import { FlyoutAddon, FlyoutElement } from "../src/flyout";

describe("Flyout addon", () => {
it("is disabled by default", () => {
it("invalid configuration disables the addon", () => {
expect(
FlyoutAddon.isEnabled({
addons: {
flyout: {},
flyout: {
enabled: true,
},
},
}),
).to.be.false;
});

it("is enabled with configuration", () => {
it("is disabled with valid data", () => {
expect(
FlyoutAddon.isEnabled({
addons: {
flyout: {
enabled: false,
downloads: [],
translations: [],
versions: [],
},
},
domains: {
dashboard: "readthedocs.org",
},
projects: {
current: {
slug: "project",
single_version: false,
},
},
versions: {
current: {
slug: "latest",
},
},
}),
).to.be.false;
});

it("is enabled with valid data", () => {
expect(
FlyoutAddon.isEnabled({
addons: {
flyout: {
enabled: true,
downloads: [],
translations: [],
versions: [],
},
},
domains: {
dashboard: "readthedocs.org",
},
projects: {
current: {
slug: "project",
single_version: false,
},
},
versions: {
current: {
slug: "latest",
},
},
}),
Expand Down
36 changes: 33 additions & 3 deletions tests/hotkeys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,57 @@ import { expect, assert, fixture, html } from "@open-wc/testing";
import { HotKeysElement, HotKeysAddon } from "../src/hotkeys";

describe("Hotkeys addon", () => {
it("is disabled by default", () => {
it("invalid configuration disables the addon", () => {
expect(
HotKeysAddon.isEnabled({
addons: {
hotkeys: {},
hotkeys: {
enabled: true,
},
},
}),
).to.be.false;
});

it("is enabled with configuration", () => {
it("is enabled", () => {
expect(
HotKeysAddon.isEnabled({
addons: {
hotkeys: {
enabled: true,
doc_diff: {
enabled: true,
trigger: "Key D",
},
search: {
enabled: true,
trigger: "Slash",
},
},
},
}),
).to.be.true;
});

it("is disabled", () => {
expect(
HotKeysAddon.isEnabled({
addons: {
hotkeys: {
enabled: false,
doc_diff: {
enabled: true,
trigger: "Key D",
},
search: {
enabled: true,
trigger: "Slash",
},
},
},
}),
).to.be.false;
});
});

describe("Hotkeys element", () => {
Expand Down