Skip to content

Commit

Permalink
Merge pull request #118 from molgenis/feat/queryForNull
Browse files Browse the repository at this point in the history
handle undefined values for has_any any_has_any when not querying for…
  • Loading branch information
dennishendriksen authored Nov 20, 2024
2 parents 1fa7dae + c3fdf3c commit 40e8d8c
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@molgenis/vip-report-api",
"version": "6.1.1",
"version": "6.1.2",
"description": "TypeScript Report API for Variant Call Format (VCF) Report Templates",
"scripts": {
"build": "tsc --build",
Expand Down
68 changes: 66 additions & 2 deletions src/__tests__/apiClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const record0: Item<VcfRecord> = {
i: [],
r: "C",
a: ["T"],
q: null,
q: 80,
f: ["PASS"],
n: {
n_array0: ["c", null, "d", "b"],
Expand Down Expand Up @@ -79,7 +79,7 @@ const record1: Item<VcfRecord> = {
r: "G",
a: ["A"],
q: null,
f: ["PASS"],
f: [],
n: {
n_array0: ["b", "c", "a"],
n_array1: ["a", "b"],
Expand Down Expand Up @@ -393,6 +393,38 @@ test("get - records with less than query", async () => {
});
});

test("get - records with equals null query on qual", async () => {
const params: Params = {
query: {
selector: ["q"],
operator: "==",
args: null,
},
};
const records = await api.getRecords(params);
expect(records).toEqual({
items: [record1],
page: { number: 0, size: 10, totalElements: 1 },
total: 2,
});
});

test("get - records with equals empty filter", async () => {
const params: Params = {
query: {
selector: ["f"],
operator: "==",
args: [],
},
};
const records = await api.getRecords(params);
expect(records).toEqual({
items: [record1],
page: { number: 0, size: 10, totalElements: 1 },
total: 2,
});
});

test("get - records with equals null query", async () => {
const params: Params = {
query: {
Expand Down Expand Up @@ -867,6 +899,22 @@ test("get - some records using has_any undefined", async () => {
});
});

test("get - some records using has_any with undefined values", async () => {
const params: Params = {
query: {
selector: ["n", "n_array1"],
operator: "has_any",
args: ["a"],
},
};
const records = await api.getRecords(params);
expect(records).toEqual({
items: [record1],
page: { number: 0, size: 10, totalElements: 1 },
total: 2,
});
});

test("get - some records using has_any null", async () => {
const params: Params = {
query: {
Expand Down Expand Up @@ -899,6 +947,22 @@ test("get - some records using empty any_has_any", async () => {
});
});

test("get - some records using undefined any_has_any value", async () => {
const params: Params = {
query: {
selector: ["n", "n_object0", "*", "5"],
operator: "any_has_any",
args: ["a"],
},
};
const records = await api.getRecords(params);
expect(records).toEqual({
items: [],
page: { number: 0, size: 10, totalElements: 0 },
total: 2,
});
});

test("get - some records using wildcard selector part with !has_any", async () => {
const params: Params = {
query: {
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/trio.vcf
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
##FORMAT=<ID=VIPC_S,Number=.,Type=String,Description="VIP decision tree classification.">
#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT Patient Mother Father
1 10042538 . C T . PASS n_array0=c,,d,b;n_number2=1;n_object0=dummy5|c|1&2,dummy7||1&2,dummy1|d|1&2,dummy4|b|1&2;n_string0=a;n_string3=b;n_string4=b GT:DP:AD 1|0:50:45,5 0|0:10:10,0 0|0:10:10,0
1 16376412 . G A . PASS n_array0=b,c,a;n_array1=a,b;n_bool3;n_bool6;n_bool7;n_number2=0;n_object0=dummy3|b|1&2&3,dummy2|c|1&2&3,dummy6|a|;n_string0=a;n_string3=a;n_string4=A GT:DP:AD 0|1:10:0,0 1|0:11:11,0 1|0:11:11,0
1 10042538 . C T 80 PASS n_array0=c,,d,b;n_number2=1;n_object0=dummy5|c|1&2,dummy7||1&2,dummy1|d|1&2,dummy4|b|1&2;n_string0=a;n_string3=b;n_string4=b GT:DP:AD 1|0:50:45,5 0|0:10:10,0 0|0:10:10,0
1 16376412 . G A . . n_array0=b,c,a;n_array1=a,b;n_bool3;n_bool6;n_bool7;n_number2=0;n_object0=dummy3|b|1&2&3,dummy2|c|1&2&3,dummy6|a|;n_string0=a;n_string3=a;n_string4=A GT:DP:AD 0|1:10:0,0 1|0:11:11,0 1|0:11:11,0
44 changes: 31 additions & 13 deletions src/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,16 @@ function matches(query: Query, resource: Item<Resource>): boolean {

function matchesEquals(query: QueryClause, resource: Item<Resource>): boolean {
const value: unknown = select(query.selector, resource);
if (Array.isArray(query.args)) {
if (value === undefined || value === null) {
return false;
}
if ((query.args as unknown[]).length === 0) {
return (value as unknown[]).length === 0;
} else {
throw new Error(`Equals query with an array is only supported with an empty array.`);
}
}
return value === query.args;
}

Expand Down Expand Up @@ -473,20 +483,25 @@ function matchesAnyHasAny(query: QueryClause, resource: Item<Resource>): boolean
}
}
}
} else if ((query.args as unknown[]).length === 0) {
for (const item of value as unknown[]) {
if ((item as unknown[]).length === 0) {
match = true;
break;
}
}
} else {
for (const item of value as unknown[]) {
if (item !== null) {
for (const arg of query.args as unknown[]) {
if ((item as unknown[]).includes(arg)) {
match = true;
break;
if (value === undefined) {
return false;
}
if ((query.args as unknown[]).length === 0) {
for (const item of value as unknown[]) {
if ((item as unknown[]).length === 0) {
match = true;
break;
}
}
} else {
for (const item of value as unknown[]) {
if (item !== null) {
for (const arg of query.args as unknown[]) {
if (item !== undefined && (item as unknown[]).includes(arg)) {
match = true;
break;
}
}
}
}
Expand Down Expand Up @@ -514,6 +529,9 @@ function matchesHasAny(query: QueryClause, resource: Item<Resource>): boolean {
}
}
} else {
if (value === undefined) {
return false;
}
for (const arg of query.args as unknown[]) {
if ((value as unknown[]).includes(arg)) {
match = true;
Expand Down

0 comments on commit 40e8d8c

Please sign in to comment.