Skip to content

Commit

Permalink
fix: Observations creation in dataset preview
Browse files Browse the repository at this point in the history
...when dimension has a generic path, like schema:name.
  • Loading branch information
bprusinowski committed Feb 13, 2024
1 parent d851598 commit 59aa51f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
20 changes: 14 additions & 6 deletions app/rdf/query-cube-preview.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jest.mock("@zazuko/cube-hierarchy-query/index", () => ({}));

describe("dataset preview", () => {
const dim = rdf.blankNode();
const genericDim = rdf.blankNode();
const measure = rdf.blankNode();
const observation = rdf.namedNode(
"https://environment.ld.admin.ch/foen/gefahren-waldbrand-warnung/observation/336>"
Expand All @@ -23,6 +24,8 @@ describe("dataset preview", () => {
)
),
rdf.quad(dim, ns.schema.name, rdf.literal("Region")),
rdf.quad(genericDim, ns.sh.path, ns.schema.name),
rdf.quad(genericDim, ns.schema.name, rdf.literal("Name")),
rdf.quad(
measure,
ns.sh.path,
Expand Down Expand Up @@ -87,14 +90,18 @@ describe("dataset preview", () => {
latest: true,
}
);
const dim = dimensions[0];
const dim = dimensions.find(
(d) =>
d.iri ===
"https://environment.ld.admin.ch/foen/gefahren-waldbrand-warnung/region"
);

expect(dim.iri).toEqual(
expect(dim?.iri).toEqual(
"https://environment.ld.admin.ch/foen/gefahren-waldbrand-warnung/region"
);
expect(dim.label).toEqual("Region");
expect(dim.values).toHaveLength(1);
expect(dim.values[0].position).toEqual(3);
expect(dim?.label).toEqual("Region");
expect(dim?.values).toHaveLength(1);
expect(dim?.values[0].position).toEqual(3);

const measure = measures[0];

Expand All @@ -103,9 +110,10 @@ describe("dataset preview", () => {
);
expect(measure.label).toEqual("Danger ratings");

expect(observations).toHaveLength(1);
const obs = observations[0];

expect(obs[dim.iri]).toEqual("Bern");
expect(obs[dim?.iri ?? ""]).toEqual("Bern");
expect(obs[measure.iri]).toEqual("considerable danger");
});
});
7 changes: 6 additions & 1 deletion app/rdf/query-cube-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ CONSTRUCT {
}, {} as Record<string, { values: DimensionValue[]; dataType: NamedNode }>);
// Only take quads that use dimension iris as predicates (observation values)
const qUniqueObservations = uniqBy(
qs.filter(({ predicate: p }) => qsDims.some((q) => q.object.equals(p))),
qs.filter(
({ subject: s, predicate: p }) =>
// Exclude situations where the subject is a blank node (e.g. dimension IRI
// is not unique, but something like ns.schema.name)
s.termType !== "BlankNode" && qsDims.some((q) => q.object.equals(p))
),
({ subject: s }) => s.value
);
qUniqueObservations.forEach(({ subject: s }) => {
Expand Down

0 comments on commit 59aa51f

Please sign in to comment.