Skip to content

Commit

Permalink
Merge 784cf5c into b179558
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed Dec 2, 2019
2 parents b179558 + 784cf5c commit 5994430
Show file tree
Hide file tree
Showing 13 changed files with 1,990 additions and 10 deletions.
23 changes: 16 additions & 7 deletions __tests__/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@ import { Dimension } from "../src/components";
import { DataCube } from "../src/datacube";
import { fetch } from "./utils/fetch-mock";

export function extractKeyword(keyword: string, sparql: string): string {
const lastMatchingLine = sparql
export function extractKeywords(keyword: string, sparql: string, includes = false): string[] {
return sparql
.split("\n")
.filter((line: string) => line.trim().startsWith(keyword))
.filter((line: string) => {
if (includes) {
return line.trim().includes(keyword);
}
return line.trim().startsWith(keyword);
})
.map((line) => line.trim());
}
export function extractKeyword(keyword: string, sparql: string, includes = false): string {
const lastMatchingLine = extractKeywords(keyword, sparql, includes)
.slice(-1)[0];
if (lastMatchingLine) {
return lastMatchingLine.trim();
}
return "";
return lastMatchingLine || "";
}
export function extractSelect(sparql: string) {
return extractKeyword("SELECT", sparql);
}
export function extractFilter(sparql: string) {
return extractKeyword("FILTER", sparql);
Expand Down
362 changes: 362 additions & 0 deletions __tests__/query.broader.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,362 @@
// tslint:disable: no-shadowed-variable

import { literal, namedNode } from "@rdfjs/data-model";
import { Attribute, Component, Dimension, Measure } from "../src/components";
import { DataCube } from "../src/datacube";
import { DataCubeEntryPoint } from "../src/entrypoint";
import {
extractFilter,
extractKeyword,
extractKeywords,
extractLimit,
extractSelect,
} from "./filter.test";
import { fetch } from "./utils/fetch-mock";

const entryPoint = new DataCubeEntryPoint(
"https://trifid-lindas.test.cluster.ldbar.ch/query",
{
languages: ["de"],
fetcher: {
fetch,
},
},
);

let dataCube: DataCube;
let animalType: Component;
let epidemic: Component;
let epidemicSpecification: Component;
let reason: Component;

describe("broader", () => {
beforeAll(async () => {
[dataCube] = await entryPoint.dataCubesByGraphIri(
"https://linked.opendata.swiss/graph/blv/animalpest",
);
});
beforeEach(async () => {
[
animalType,
epidemic,
epidemicSpecification,
reason,
] = await dataCube.dimensions();
});

test("one level loose", async () => {
const query = dataCube.query().select({
animalType,
animalGroup: animalType.broader(),
epidemic,
epidemicGroup: epidemic.broader(),
epidemicSpecification,
reason,
});
const sparql = await query.toSparql();
const select = extractSelect(sparql).split(" ");
expect(select).toMatchInlineSnapshot(`
Array [
"SELECT",
"?animalType",
"?animalTypeLabel",
"?epidemic",
"?epidemicLabel",
"?epidemicSpecification",
"?epidemicSpecificationLabel",
"?reason",
"?reasonLabel",
"?animalGroup",
"?animalGroupLabel",
"?epidemicGroup",
"?epidemicGroupLabel",
"FROM",
"<https://linked.opendata.swiss/graph/blv/animalpest>",
]
`);
const broader = extractKeywords("skos:broader", sparql, true);
expect(broader).toMatchInlineSnapshot(`
Array [
"?animalType skos:broader ?animalGroup.",
"?epidemic skos:broader ?epidemicGroup.",
]
`);
});

test("one level pinned", async () => {
const query = dataCube.query().select({
animalType,
animalGroup: animalType.broader(
"http://ld.zazuko.com/animalpest/species/group/bunny",
),
epidemic,
epidemicSpecification,
reason,
});
const sparql = await query.toSparql();
const select = extractSelect(sparql).split(" ");
expect(select).toMatchInlineSnapshot(`
Array [
"SELECT",
"?animalType",
"?animalTypeLabel",
"?epidemic",
"?epidemicLabel",
"?epidemicSpecification",
"?epidemicSpecificationLabel",
"?reason",
"?reasonLabel",
"?animalGroup",
"?animalGroupLabel",
"FROM",
"<https://linked.opendata.swiss/graph/blv/animalpest>",
]
`);
const broader = extractKeywords("skos:broader", sparql, true);
expect(broader).toMatchInlineSnapshot(`
Array [
"?animalType skos:broader <http://ld.zazuko.com/animalpest/species/group/bunny>.",
]
`);
});

test("one level both", async () => {
const query = dataCube.query().select({
animalType,
animalGroup: animalType.broader(
"http://ld.zazuko.com/animalpest/species/group/bunny",
),
epidemic,
epidemicGroup: epidemic.broader(),
epidemicSpecification,
reason,
});
const sparql = await query.toSparql();
const select = extractSelect(sparql).split(" ");
expect(select).toMatchInlineSnapshot(`
Array [
"SELECT",
"?animalType",
"?animalTypeLabel",
"?epidemic",
"?epidemicLabel",
"?epidemicSpecification",
"?epidemicSpecificationLabel",
"?reason",
"?reasonLabel",
"?animalGroup",
"?animalGroupLabel",
"?epidemicGroup",
"?epidemicGroupLabel",
"FROM",
"<https://linked.opendata.swiss/graph/blv/animalpest>",
]
`);
const broader = extractKeywords("skos:broader", sparql, true);
expect(broader).toMatchInlineSnapshot(`
Array [
"?animalType skos:broader <http://ld.zazuko.com/animalpest/species/group/bunny>.",
"?epidemic skos:broader ?epidemicGroup.",
]
`);
});

test("each loose level", async () => {
const entryPoint = new DataCubeEntryPoint(
"https://ld.stadt-zuerich.ch/query",
{
fetcher: { fetch },
languages: ["de"],
},
);
const dataCubes = await entryPoint.dataCubes();
const dataCube = dataCubes.find((cube) => cube.iri.endsWith("BEW-RAUM-ZEIT"));

const dimensions = await dataCube.dimensions();
const zeit = dimensions.find((dimension) =>
dimension.iri.value.endsWith("/ZEIT"),
);
const raum = dimensions.find((dimension) =>
dimension.iri.value.endsWith("/RAUM"),
);

const raumIn = raum.broader();
const quartier = raumIn.broader();
const kreis = quartier.broader();
const gemeinde = kreis.broader();
const kanton = gemeinde.broader();
const land = kanton.broader();
const kontinent = land.broader();
const query = dataCube.query().select({
zeit,
raum,
raumIn,
quartier,
kreis,
gemeinde,
kanton,
land,
kontinent,
});
const sparql = await query.toSparql();
const select = extractSelect(sparql).split(" ");
expect(select).toMatchInlineSnapshot(`
Array [
"SELECT",
"?zeit",
"?zeitLabel",
"?raum",
"?raumLabel",
"?raumIn",
"?raumInLabel",
"?quartier",
"?quartierLabel",
"?kreis",
"?kreisLabel",
"?gemeinde",
"?gemeindeLabel",
"?kanton",
"?kantonLabel",
"?land",
"?landLabel",
"?kontinent",
"?kontinentLabel",
"FROM",
"<https://linked.opendata.swiss/graph/zh/statistics>",
]
`);
const broader = extractKeywords("skos:broader", sparql, true);
expect(broader).toMatchInlineSnapshot(`
Array [
"?raum skos:broader ?raumIn.",
"?raumIn skos:broader ?quartier.",
"?quartier skos:broader ?kreis.",
"?kreis skos:broader ?gemeinde.",
"?gemeinde skos:broader ?kanton.",
"?kanton skos:broader ?land.",
"?land skos:broader ?kontinent.",
]
`);
});

test("one deep loose level", async () => {
const entryPoint = new DataCubeEntryPoint(
"https://ld.stadt-zuerich.ch/query",
{
fetcher: { fetch },
languages: ["de"],
},
);
const dataCubes = await entryPoint.dataCubes();
const dataCube = dataCubes.find((cube) => cube.iri.endsWith("BEW-RAUM-ZEIT"));

const dimensions = await dataCube.dimensions();
const zeit = dimensions.find((dimension) =>
dimension.iri.value.endsWith("/ZEIT"),
);
const raum = dimensions.find((dimension) =>
dimension.iri.value.endsWith("/RAUM"),
);

const query = dataCube.query().select({
zeit,
raum,
kontinent: raum
.broader()
.broader()
.broader()
.broader()
.broader()
.broader()
.broader(),
});
const sparql = await query.toSparql();
const select = extractSelect(sparql).split(" ");
expect(select).toMatchInlineSnapshot(`
Array [
"SELECT",
"?zeit",
"?zeitLabel",
"?raum",
"?raumLabel",
"?kontinent",
"?kontinentLabel",
"FROM",
"<https://linked.opendata.swiss/graph/zh/statistics>",
]
`);
const broader = extractKeywords("skos:broader", sparql, true);
expect(broader).toMatchInlineSnapshot(`
Array [
"?raum skos:broader ?raum1.",
"?raum1 skos:broader ?raum2.",
"?raum2 skos:broader ?raum3.",
"?raum3 skos:broader ?raum4.",
"?raum4 skos:broader ?raum5.",
"?raum5 skos:broader ?raum6.",
"?raum6 skos:broader ?kontinent.",
]
`);
});

test("one deep pinned level", async () => {
const entryPoint = new DataCubeEntryPoint(
"https://ld.stadt-zuerich.ch/query",
{
fetcher: { fetch },
languages: ["de"],
},
);
const dataCubes = await entryPoint.dataCubes();
const dataCube = dataCubes.find((cube) => cube.iri.endsWith("BEW-RAUM-ZEIT"));

const dimensions = await dataCube.dimensions();
const zeit = dimensions.find((dimension) =>
dimension.iri.value.endsWith("/ZEIT"),
);
const raum = dimensions.find((dimension) =>
dimension.iri.value.endsWith("/RAUM"),
);
const kontinent = raum
.broader()
.broader()
.broader()
.broader()
.broader()
.broader()
.broader("https://ld.stadt-zuerich.ch/statistics/code/Kontinent");

const query = dataCube.query().select({
zeit,
raum,
kontinent,
});
const sparql = await query.toSparql();
const select = extractSelect(sparql).split(" ");
expect(select).toMatchInlineSnapshot(`
Array [
"SELECT",
"?zeit",
"?zeitLabel",
"?raum",
"?raumLabel",
"?kontinent",
"?kontinentLabel",
"FROM",
"<https://linked.opendata.swiss/graph/zh/statistics>",
]
`);
const broader = extractKeywords("skos:broader", sparql, true);
expect(broader).toMatchInlineSnapshot(`
Array [
"?raum skos:broader ?raum1.",
"?raum1 skos:broader ?raum2.",
"?raum2 skos:broader ?raum3.",
"?raum3 skos:broader ?raum4.",
"?raum4 skos:broader ?raum5.",
"?raum5 skos:broader ?raum6.",
"?raum6 skos:broader <https://ld.stadt-zuerich.ch/statistics/code/Kontinent>.",
]
`);
});
});

0 comments on commit 5994430

Please sign in to comment.