Skip to content

Commit

Permalink
Merge pull request #1496 from visualize-admin/fix/unversioning-dimens…
Browse files Browse the repository at this point in the history
…ion-values

fix: Dimension values unversioning
  • Loading branch information
bprusinowski authored May 3, 2024
2 parents b924518 + ba246e7 commit 1b6f09c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 15 deletions.
32 changes: 17 additions & 15 deletions app/rdf/query-dimension-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,24 @@ export async function loadDimensionValuesWithMetadata(
dimensionIri
);

if (!cubeDimensions.find((d) => d.path?.value === dimensionIri)) {
const cubeDimension = cubeDimensions.find(
(d) => d.path?.value === dimensionIri
);

if (!cubeDimension) {
throw new Error(`Dimension not found: ${dimensionIri}`);
}

const isDimensionVersioned = dimensionIsVersioned(cubeDimension);
const query = `PREFIX cube: <https://cube.link/>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX schema: <http://schema.org/>
PREFIX sh: <http://www.w3.org/ns/shacl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
CONSTRUCT {
?dimensionIri rdf:first ?value .
?value
?dimensionIri rdf:first ?maybe_unversioned_value .
?maybe_unversioned_value
schema:name ?name ;
schema:alternateName ?alternateName ;
schema:description ?description ;
Expand All @@ -133,12 +138,7 @@ CONSTRUCT {
VALUES ?dimensionIri { <${dimensionIri}> }
<${cubeIri}> cube:observationConstraint/sh:property ?dimension .
?dimension sh:path ?dimensionIri .
?dimension sh:in/rdf:rest*/rdf:first ?maybeVersionedValue .
OPTIONAL {
?dimension schema:version ?version .
?maybeVersionedValue schema:sameAs ?maybeUnversionedValue .
}
BIND(COALESCE(?maybeUnversionedValue, ?maybeVersionedValue) as ?value)
?dimension sh:in/rdf:rest*/rdf:first ?value .
}
} UNION`
} {
Expand All @@ -155,16 +155,12 @@ CONSTRUCT {
}
${queryFilters ? `VALUES ?dimensionIri { <${dimensionIri}> }` : ""}
<${cubeIri}> cube:observationSet/cube:observation ?observation .
?observation ?dimensionIri ?maybeVersionedValue .
OPTIONAL {
?dimension schema:version ?version .
?maybeVersionedValue schema:sameAs ?maybeUnversionedValue .
}
BIND(COALESCE(?maybeUnversionedValue, ?maybeVersionedValue) as ?value)
?observation ?dimensionIri ?value .
${queryFilters}
}
}
}
# Metadata is only attached to versioned values
${buildLocalizedSubQuery("value", "schema:name", "name", {
locale,
})}
Expand All @@ -180,6 +176,12 @@ CONSTRUCT {
OPTIONAL { ?value geo:hasGeometry ?geometry . }
OPTIONAL { ?value schema:latitude ?latitude . }
OPTIONAL { ?value schema:longitude ?longitude . }
${
isDimensionVersioned
? `OPTIONAL { ?value schema:sameAs ?unversioned_value . }`
: ""
}
BIND(COALESCE(?unversioned_value, ?value) AS ?maybe_unversioned_value)
}`;

return await executeWithCache(
Expand Down
52 changes: 52 additions & 0 deletions e2e/named-nodes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { createClient } from "urql";

import {
DataCubeComponentsDocument,
DataCubeComponentsQuery,
DataCubeComponentsQueryVariables,
} from "../app/graphql/query-hooks";

import { setup } from "./common";

const { test, expect } = setup();

const cubeIri = "https://environment.ld.admin.ch/foen/BFS_cofog_national/2";
const versionedDimensionIri =
"https://environment.ld.admin.ch/foen/BFS_cofog_national/sector";

test("it should be possible to query dimension values from versioned dimension correctly", async ({
page,
}) => {
await page.goto("/");
const pageUrl = page.url().split("?")[0]; // remove query params
const client = createClient({ url: pageUrl + "/api/graphql" });
const { data } = await client
.query<DataCubeComponentsQuery, DataCubeComponentsQueryVariables>(
DataCubeComponentsDocument,
{
sourceType: "sparql",
sourceUrl: "https://lindas.admin.ch/query",
locale: "en",
cubeFilter: { iri: cubeIri, loadValues: true },
}
)
.toPromise();
const identifiers = data.dataCubeComponents.dimensions
.find((d) => d.iri === versionedDimensionIri)
.values.map((v) => v.identifier);

expect(
identifiers.length,
"There should be some identifiers."
).toBeGreaterThan(0);
expect(
identifiers.every((i) => typeof i === "string"),
"Every identifier must be a string."
).toBe(true);
expect
.soft(
identifiers,
"The identifiers should be equal to this list; except if the cube has been updated."
)
.toMatchObject(["S13", "S1311", "S1312", "S1313", "S1314"]);
});

0 comments on commit 1b6f09c

Please sign in to comment.