Skip to content

Commit

Permalink
Update API tests to include mdims
Browse files Browse the repository at this point in the history
  • Loading branch information
rakyi committed Oct 31, 2024
1 parent 5cb2ed1 commit 74e53ae
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
128 changes: 128 additions & 0 deletions adminSiteServer/app.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import knex, { Knex } from "knex"
import { dbTestConfig } from "../db/tests/dbTestConfig.js"
import {
TransactionCloseMode,
knexRaw,

Check warning on line 46 in adminSiteServer/app.test.tsx

View workflow job for this annotation

GitHub Actions / eslint

'knexRaw' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 46 in adminSiteServer/app.test.tsx

View workflow job for this annotation

GitHub Actions / eslint

'knexRaw' is defined but never used. Allowed unused vars must match /^_/u
knexReadWriteTransaction,
setKnexInstance,
} from "../db/db.js"
Expand All @@ -51,6 +52,8 @@ import {
ChartConfigsTableName,
ChartsTableName,
DatasetsTableName,
MultiDimDataPagesTableName,
MultiDimXChartConfigsTableName,
VariablesTableName,
} from "@ourworldindata/types"
import path from "path"
Expand Down Expand Up @@ -363,6 +366,67 @@ describe("OwidAdminApp: indicator-level chart configs", () => {
},
],
}
const testMultiDimConfig = {
grapherConfigSchema:
"https://files.ourworldindata.org/schemas/grapher-schema.005.json",
title: {
title: "Energy use",
titleVariant: "by energy source",
},
views: [
{
config: { title: "Total energy use" },
dimensions: {
source: "all",
metric: "total",
},
indicators: {
y: variableId,
},
},
{
dimensions: {
metric: "per_capita",
source: "all",
},
indicators: {
y: otherVariableId,
},
},
],
dimensions: [
{
name: "Energy source",
slug: "source",
choices: [
{
name: "All sources",
slug: "all",
group: "Aggregates",
description: "Total energy use",
},
],
},
{
name: "Metric",
slug: "metric",
choices: [
{
name: "Total consumption",
slug: "total",
description:
"The amount of energy consumed nationally per year",
},
{
name: "Consumption per capita",
slug: "per_capita",
description:
"The average amount of energy each person consumes per year",
},
],
},
],
}

beforeEach(async () => {
await testKnexInstance!(DatasetsTableName).insert([dummyDataset])
Expand Down Expand Up @@ -432,6 +496,70 @@ describe("OwidAdminApp: indicator-level chart configs", () => {
...testVariableConfigAdmin,
})

// create mdim config that uses both of the variables
await makeRequestAgainstAdminApi({
method: "PUT",
path: "/multi-dim/energy",
body: JSON.stringify(testMultiDimConfig),
})
const mdim = await testKnexInstance!(MultiDimDataPagesTableName).first()
expect(mdim.slug).toBe("energy")
const savedMdimConfig = JSON.parse(mdim.config)
// variableId should be normalized to an array
expect(savedMdimConfig.views[0].indicators.y).toBeInstanceOf(Array)

const [mdxcc1, mdxcc2] = await testKnexInstance!(
MultiDimXChartConfigsTableName
)
expect(mdxcc1.multiDimId).toBe(mdim.id)
expect(mdxcc1.viewId).toBe("total__all")
expect(mdxcc1.variableId).toBe(variableId)
expect(mdxcc2.multiDimId).toBe(mdim.id)
expect(mdxcc2.viewId).toBe("per_capita__all")
expect(mdxcc2.variableId).toBe(otherVariableId)

// view config should override the variable config
const expectedMergedViewConfig = {
...mergedGrapherConfig,
title: "Total energy use",
selectedEntityNames: [], // mdims define their own default entities
}
const fullViewConfig1 = await testKnexInstance!(ChartConfigsTableName)
.where("id", mdxcc1.chartConfigId)
.first()
expect(JSON.parse(fullViewConfig1.full)).toEqual(
expectedMergedViewConfig
)

// update the admin-authored config for the variable
await makeRequestAgainstAdminApi({
method: "PUT",
path: `/variables/${variableId}/grapherConfigAdmin`,
body: JSON.stringify({
...testVariableConfigAdmin,
subtitle: "Newly updated subtitle",
}),
})
const expectedMergedViewConfigUpdated = {
...expectedMergedViewConfig,
subtitle: "Newly updated subtitle",
}
const fullViewConfig1Updated = await testKnexInstance!(
ChartConfigsTableName
)
.where("id", mdxcc1.chartConfigId)
.first()
expect(JSON.parse(fullViewConfig1Updated.full)).toEqual(
expectedMergedViewConfigUpdated
)

// clean-up the mdim tables
await testKnexInstance!(MultiDimXChartConfigsTableName).truncate()
await testKnexInstance!(MultiDimDataPagesTableName).delete()
await testKnexInstance!(ChartConfigsTableName)
.whereIn("id", [mdxcc1.chartConfigId, mdxcc2.chartConfigId])
.delete()

// delete the admin-authored grapher config we just added
// and verify that the merged config is now the same as the ETL config
await makeRequestAgainstAdminApi({
Expand Down
4 changes: 4 additions & 0 deletions db/tests/testHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
ChartRevisionsTableName,
ChartsTableName,
DatasetsTableName,
MultiDimDataPagesTableName,
MultiDimXChartConfigsTableName,
PostsGdocsTableName,
UsersTableName,
VariablesTableName,
Expand All @@ -15,6 +17,8 @@ export const TABLES_IN_USE = [
ChartDimensionsTableName,
ChartRevisionsTableName,
ChartsTableName,
MultiDimXChartConfigsTableName,
MultiDimDataPagesTableName,
VariablesTableName,
ChartConfigsTableName,
DatasetsTableName,
Expand Down

0 comments on commit 74e53ae

Please sign in to comment.