Skip to content

Commit

Permalink
feat(query): many filters can be applied at once
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed Oct 30, 2019
1 parent c110884 commit 63b867b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 14 deletions.
82 changes: 72 additions & 10 deletions __tests__/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,25 @@ describe("limit", () => {
});

test("uses provided value", async () => {
const query = dataCube.query().select({
raum: raumDimension,
bep: beschaeftigteMeasure.avg(),
})
.limit(12377);
const query = dataCube
.query()
.select({
raum: raumDimension,
bep: beschaeftigteMeasure.avg(),
})
.limit(12377);
const sparql = await query.toSparql();
expect(extractLimit(sparql)).toMatchInlineSnapshot(`"LIMIT 12377"`);
});

test("can be explicitly removed using null", async () => {
const query = dataCube.query().select({
raum: raumDimension,
bep: beschaeftigteMeasure.avg(),
})
.limit(null);
const query = dataCube
.query()
.select({
raum: raumDimension,
bep: beschaeftigteMeasure.avg(),
})
.limit(null);
const sparql = await query.toSparql();
expect(extractLimit(sparql)).toBe("");
});
Expand Down Expand Up @@ -437,6 +441,34 @@ describe("handles languages", () => {
});

describe("filter", () => {
it("applies 1 filter", async () => {
const base = dataCube.query().select({
raum: raumDimension,
bep: beschaeftigteMeasure,
});
const query = base.filter(raumDimension.gte(literal("12")));
const sparql = await query.toSparql();
expect(extractFilter(sparql)).toMatchInlineSnapshot(
`"FILTER(?raum >= \\"12\\"^^xsd:string)"`,
);
});

it("applies 1 filter as array", async () => {
const base = dataCube.query().select({
raum: raumDimension,
bep: beschaeftigteMeasure,
});
const query = base.filter([raumDimension.gte(literal("12"))]);
const sparql = await query.toSparql();
expect(extractFilter(sparql)).toMatchInlineSnapshot(
`"FILTER(?raum >= \\"12\\"^^xsd:string)"`,
);

const query2 = base.filter(raumDimension.gte(literal("12")));
const sparql2 = await query2.toSparql();
expect(sparql).toBe(sparql2);
});

it("applies 3 filters", async () => {
const base = dataCube.query().select({
raum: raumDimension,
Expand All @@ -462,6 +494,16 @@ describe("filter", () => {
.filter(beschaeftigteMeasure.lt(literal("120")));
const sparql = await query.toSparql();
expect(extractFilter(sparql).split("&&")).toHaveLength(4);

const query2 = base
.filter([
raumDimension.gte(literal("12")),
raumDimension.lte(literal("120")),
beschaeftigteMeasure.gte(literal("12")),
beschaeftigteMeasure.lt(literal("120")),
]);
const sparql2 = await query2.toSparql();
expect(sparql2).toBe(sparql);
});

it("builds filters in the order they were given", async () => {
Expand All @@ -482,6 +524,26 @@ describe("filter", () => {
expect(part).toContain(filters[index]);
});
});

it("builds filters as an array in the order they were given", async () => {
const base = dataCube.query().select({
raum: raumDimension,
bep: beschaeftigteMeasure,
});
const filters = ["aaa", "bbb", "ccc", "ddd"];
const query = base.filter([
raumDimension.gte(literal(filters[0])),
raumDimension.lte(literal(filters[1])),
beschaeftigteMeasure.gte(literal(filters[2])),
beschaeftigteMeasure.lt(literal(filters[3])),
]);
const sparql = await query.toSparql();
extractFilter(sparql)
.split("&&")
.forEach((part, index) => {
expect(part).toContain(filters[index]);
});
});
});

describe("auto names variables", () => {
Expand Down
18 changes: 14 additions & 4 deletions src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,25 @@ export class Query {
* .filter(({ someDate }) => someDate.not.equals("2019-08-29T07:27:56.241Z"));
* // syntax 2:
* .filter(dateDimension.not.equals("2019-08-29T07:27:56.241Z"));
* // syntax 3:
* .filter([dateDimension.not.equals("2019-08-29T07:27:56.241Z"), secondFilter, thirdFilter, moreFilters]);
* ```
* @param filter
*/
public filter(filter: IExpr | FilterFunction) {
public filter(filter: IExpr | FilterFunction | Array<IExpr | FilterFunction>) {
const self = this.clone();
if (typeof filter === "function") {
filter = filter(this.state.selects);
let newFilters = [];
if (Array.isArray(filter)) {
newFilters = filter;
} else {
newFilters.push(filter);
}
self.state.filters.push(filter);
newFilters.forEach((newFilter) => {
if (typeof filter === "function") {
newFilter = filter(this.state.selects);
}
self.state.filters.push(newFilter);
});
return self;
}

Expand Down

0 comments on commit 63b867b

Please sign in to comment.