Skip to content

Commit

Permalink
Fix ranking to make spec return [ ] if answerSet is empty (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixCodes authored and kanitw committed Aug 5, 2016
1 parent c64e853 commit f91dc68
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/ranking/ranking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ export function rank(group: SpecQueryModelGroup, query: Query, schema: Schema, l
if (query.orderBy || query.chooseBy) {
group.items.sort(comparator(query.orderBy || query.chooseBy, schema, query.config));
if (query.chooseBy) {
// for chooseBy -- only keep the top-item
group.items = [group.items[0]];
if (group.items.length > 0) {
// for chooseBy -- only keep the top-item
group.items = [group.items[0]];
} else { // except when the group is empty
group.items = [];
}
}
}
} else {
Expand Down
34 changes: 34 additions & 0 deletions test/ranking/ranking.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {Channel} from 'vega-lite/src/channel';
import {Mark} from 'vega-lite/src/mark';
import {Type} from 'vega-lite/src/type';


import {schema} from '../fixture';
import {rank} from '../../src/ranking/ranking';

import {SpecQueryModelGroup} from '../../src/modelgroup';

import {assert} from 'chai';


describe('ranking', () => {
describe('rank', () => {
it('should return an empty group if the input group is empty', () => {
let group: SpecQueryModelGroup = rank(
{name: '', path: '', items: []},
{
spec: {
mark: Mark.BAR,
encodings: [
{channel: Channel.SHAPE, field: 'N', type: Type.NOMINAL},
]
},
chooseBy: 'effectiveness'
},
schema,
0
);
assert.deepEqual(group.items, []);
});
});
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"test/ranking/effectiveness/effectiveness.test.ts",
"test/ranking/effectiveness/channel.test.ts",
"test/ranking/effectiveness/mark.test.ts",
"test/ranking/ranking.test.ts",
"test/enumerator.test.ts",
"test/enumspec.test.ts",
"test/enumspecindex.test.ts",
Expand Down

0 comments on commit f91dc68

Please sign in to comment.