Skip to content

Commit

Permalink
add unit tests and fix comment per @kanitw
Browse files Browse the repository at this point in the history
  • Loading branch information
sirahd committed Mar 6, 2018
1 parent 3094685 commit 985dbfc
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
16 changes: 8 additions & 8 deletions src/compile/data/calculate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {isSortArray} from '../../sort';
import {duplicate} from '../../util';
import {VgFormulaTransform} from '../../vega.schema';
import {ModelWithField} from '../model';
import {Channel} from './../../channel';
import {CalculateTransform} from './../../transform';
import {DataFlowNode} from './dataflow';

Expand All @@ -18,13 +19,13 @@ export class CalculateNode extends DataFlowNode {
super();
}

public static makeAllFromSort(model: ModelWithField): CalculateNode[] {
public static makeAllForSortIndex(model: ModelWithField): CalculateNode[] {
// get all the encoding with sort fields from model
const nodes = model.reduceFieldDef((acc: CalculateNode[], fieldDef: ChannelDef<any>) => {
const nodes = model.reduceFieldDef((acc: CalculateNode[], fieldDef: ChannelDef<any>, channel: Channel) => {
if (isScaleFieldDef(fieldDef) && isSortArray(fieldDef.sort)) {
const transform: CalculateTransform = {
calculate: CalculateNode.calculateExpressionFromSortField(fieldDef.field, fieldDef.sort),
as: `${fieldDef.field}_sort_index`
as: `${channel}_${fieldDef.field}_sort_index`
};
acc.push(new CalculateNode(transform));
}
Expand All @@ -35,12 +36,11 @@ export class CalculateNode extends DataFlowNode {

public static calculateExpressionFromSortField(field: string, sortFields: string[]): string {
let expression = '';
let count = 0;
for (const sortField of sortFields) {
expression += `datum.${field} === '${sortField}' ? ${count} : `;
count++;
let i: number;
for (i = 0; i < sortFields.length; i++) {
expression += `datum.${field} === '${sortFields[i]}' ? ${i} : `;
}
expression += count;
expression += i;
return expression;
}

Expand Down
5 changes: 4 additions & 1 deletion src/compile/data/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ Description of the dataflow (http://asciiflow.com/):
Timeunit
|
v
Formula From Sort Array
|
v
+--+--+
| Raw |
+-----+
Expand Down Expand Up @@ -250,7 +253,7 @@ export function parseData(model: Model): DataComponent {
head = tu;
}

for (const calculate of CalculateNode.makeAllFromSort(model)) {
for (const calculate of CalculateNode.makeAllForSortIndex(model)) {
calculate.parent = head;
head = calculate;
}
Expand Down
4 changes: 2 additions & 2 deletions src/compile/scale/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,11 @@ export function domainSort(model: UnitModel, channel: ScaleChannel, scaleType: S

const sort = model.sort(channel);

// if the sort is specified with array, use the created data
// if the sort is specified with array, use the derived sort index field
if (isSortArray(sort)) {
return {
op: 'min',
field: `${model.vgField(channel)}_sort_index`,
field: `${channel}_${model.vgField(channel)}_sort_index`,
order: 'ascending'
};
}
Expand Down
38 changes: 38 additions & 0 deletions test/compile/data/calculate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* tslint:disable:quotemark */

import {assert} from 'chai';
import {CalculateNode} from '../../../src/compile/data/calculate';
import {ModelWithField} from '../../../src/compile/model';
import {parseUnitModel} from '../../util';

function assembleFromSortArray(model: ModelWithField) {
return CalculateNode.makeAllForSortIndex(model).map(n => n.assemble());
}

describe('compile/data/calculate', function () {
describe('makeAllForSortIndex', function () {
const model = parseUnitModel({
data: {
values: [
{a: 'A',b: 28}, {a: 'B',b: 55}, {a: 'C',b: 43}
]
},
mark: 'bar',
encoding: {
x: {field: 'a', type: 'ordinal', sort: ['B', 'A', 'C']},
y: {field: 'b', type: 'quantitative'}
}
});
const nodes = assembleFromSortArray(model);
assert.deepEqual(nodes, [{
type: 'formula',
expr: "datum.a === 'B' ? 0 : datum.a === 'A' ? 1 : datum.a === 'C' ? 2 : 3",
as: 'x_a_sort_index'
}]);
});

describe('calculateExpressionFromSortField', function () {
const expression = CalculateNode.calculateExpressionFromSortField('a', ["B", "A", "C"]);
assert.equal(expression, "datum.a === 'B' ? 0 : datum.a === 'A' ? 1 : datum.a === 'C' ? 2 : 3");
});
});
11 changes: 11 additions & 0 deletions test/compile/scale/domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -817,5 +817,16 @@ describe('compile/scale', () => {
});
assert.deepEqual<VgSortField>(domainSort(model, 'x', ScaleType.ORDINAL), {op: 'min', field: 'a', order: 'descending'});
});

it('should return sort spec using derived sort index', () => {
const model = parseUnitModel({
mark: 'bar',
encoding: {
x: {field: 'a', type: 'ordinal', sort: ['B', 'A', 'C']},
y: {field: 'b', type: 'quantitative'}
}
});
assert.deepEqual<VgSortField>(domainSort(model, 'x', ScaleType.ORDINAL), {op: 'min', field: 'x_a_sort_index', order: 'ascending'});
});
});
});

0 comments on commit 985dbfc

Please sign in to comment.