Skip to content

Commit 7e7cc07

Browse files
committed
fix group_by and add test
1 parent d46f7e7 commit 7e7cc07

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

lib/query/woqlQuery.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,9 +1538,9 @@ WOQLQuery.prototype.group_by = function (gvarlist, groupedvar, output, groupquer
15381538
this.cursor['@type'] = 'GroupBy';
15391539
this.cursor.group_by = [];
15401540

1541-
if (typeof gvarlist === 'string') gvarlist = [gvarlist];
1541+
if (typeof gvarlist === 'string' || gvarlist instanceof Var) gvarlist = [gvarlist];
15421542
this.cursor.group_by = this.rawVarList(gvarlist);
1543-
if (typeof groupedvar === 'string') groupedvar = [groupedvar];
1543+
if (typeof groupedvar === 'string' || groupedvar instanceof Var) groupedvar = [groupedvar];
15441544
this.cursor.template = this.rawVarList(groupedvar);
15451545
this.cursor.grouped = this.varj(output);
15461546
return this.addSubQuery(groupquery);

test/woql.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,19 @@ describe('woql queries', () => {
313313
expect(woqlObject.json()).to.eql(woqlJson.memberJson);
314314
});
315315

316+
it('check the group_by with variables', () => {
317+
let v = WOQL.Vars("person", "label", "eyes", "group");
318+
const query = WOQL.group_by(
319+
v.eyes,
320+
v.label,
321+
v.group,
322+
WOQL.and(WOQL.triple(v.person, "rdf:type", "@schema:People"),
323+
WOQL.triple(v.person, "label", v.label),
324+
WOQL.triple(v.person, "eye_color", v.eyes)))
325+
326+
expect(query.json()).to.eql(woqlJson.groupbyJsonWithVars);
327+
})
328+
316329
it('check the group_by method', () => {
317330
const woqlObject = WOQL.group_by(['v:A', 'v:B'], ['v:C'], 'v:New');
318331
const woqlObject01 = WOQL.group_by(['v:A', 'v:B'], ['v:C'], 'v:New').triple('v:A', 'v:B', 'v:C');

test/woqlJson/woqlJson.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,69 @@ module.exports = {
5656
variable: 'list_obj',
5757
},
5858
},
59+
groupbyJsonWithVars:{
60+
"@type": "GroupBy",
61+
"group_by": [
62+
"eyes"
63+
],
64+
"template": [
65+
"label"
66+
],
67+
"grouped": {
68+
"@type": "Value",
69+
"variable": "group"
70+
},
71+
"query": {
72+
"@type": "And",
73+
"and": [
74+
{
75+
"@type": "Triple",
76+
"subject": {
77+
"@type": "NodeValue",
78+
"variable": "person"
79+
},
80+
"predicate": {
81+
"@type": "NodeValue",
82+
"node": "rdf:type"
83+
},
84+
"object": {
85+
"@type": "Value",
86+
"node": "@schema:People"
87+
}
88+
},
89+
{
90+
"@type": "Triple",
91+
"subject": {
92+
"@type": "NodeValue",
93+
"variable": "person"
94+
},
95+
"predicate": {
96+
"@type": "NodeValue",
97+
"node": "label"
98+
},
99+
"object": {
100+
"@type": "Value",
101+
"variable": "label"
102+
}
103+
},
104+
{
105+
"@type": "Triple",
106+
"subject": {
107+
"@type": "NodeValue",
108+
"variable": "person"
109+
},
110+
"predicate": {
111+
"@type": "NodeValue",
112+
"node": "eye_color"
113+
},
114+
"object": {
115+
"@type": "Value",
116+
"variable": "eyes"
117+
}
118+
}
119+
]
120+
}
121+
},
59122
groupbyJson: {
60123
'@type': 'GroupBy',
61124
group_by: ['A', 'B'],

0 commit comments

Comments
 (0)