Home Manual Reference Source Repository

src/test/supergroup_vows.js

'use strict';

import assert from 'assert';
import vows from 'vows';
import _, {Supergroup, Value} from '../supergroup';

var suite = vows.describe("supergroup");

var gradeBook = [
  {lastName: "Gold",    firstName: "Sigfried", class: "Remedial Programming",           grade: "C", num: 2},
  {lastName: "Gold",    firstName: "Sigfried", class: "Literary Posturing",             grade: "B", num: 3},
  {lastName: "Gold",    firstName: "Sigfried", class: "Documenting with Pretty Colors", grade: "B", num: 3},
  {lastName: "Sassoon", firstName: "Sigfried", class: "Remedial Programming",           grade: "A", num: 3},
  {lastName: "Androy",  firstName: "Sigfried", class: "Remedial Programming",           grade: "B", num: 3}
];
//var gradesByLastName = _.supergroup(gradeBook, 'lastName');

var gradesByLastName = _.supergroup(gradeBook, 'lastName');

var gradesByName = _.supergroup(gradeBook, function(d) { 
  return d.firstName + ' ' + d.lastName; }, {dimName: 'fullName'});

var gradesByGradeLastName = _.supergroup(gradeBook, ['grade','lastName']);


suite.addBatch({
 "supergroup state": {
  topic: gradesByGradeLastName.state(),
  /*
  "should be a Supergroup State": function(selector) {
    assert.instanceOf(selector, _.stateClass);
  },
  "should allow selection by value": function(selector) {
    selector.selectByVal(gradesByGradeLastName.lookup("A"));
    assert.deepEqual(selector.selectedRecs(), [gradeBook[3]]);
  },
  "should allow selection by filter": function(selector) {
    selector.selectByVal(gradesByGradeLastName.lookup("A"));
    assert.deepEqual(selector.selectedRecs(), [gradeBook[3]]);
  },
  */
 }
});

suite.addBatch({
 "supergroup general": {
  topic: function(){ return null; }, 
  "rawValues are group names": function() {
    assert.deepEqual(gradesByLastName.rawValues(), ["Gold","Sassoon","Androy"]);
  },
  "dimensions can be functions": function() {
    assert.deepEqual(gradesByName.rawValues(), ["Sigfried Gold","Sigfried Sassoon","Sigfried Androy"]);
  },
  "multi-level supergroups have top-level rawValues": function() {
    assert.deepEqual(gradesByGradeLastName.rawValues().sort(), ["A","B","C"]);
  },
  "multi-level supergroups have children": function() {
    assert.equal(gradesByGradeLastName[1]._hasChildren, true);
  },
  "multi-level supergroups have second-level rawValues": function() {
    assert.deepEqual(gradesByGradeLastName[1].children.rawValues().sort(), ["Androy","Gold"]);
  },
  "multi-level supergroups have Value at second-level": function() {
    assert.equal(gradesByGradeLastName[1].children[0] instanceof Value, true);
  },
  "first group contains three raw records": function() {
    assert.deepEqual(gradesByLastName[0].records.slice(0), [
      {"lastName":"Gold","firstName":"Sigfried","class":"Remedial Programming","grade":"C","num":2},
      {"lastName":"Gold","firstName":"Sigfried","class":"Literary Posturing","grade":"B","num":3},
      {"lastName":"Gold","firstName":"Sigfried","class":"Documenting with Pretty Colors","grade":"B","num":3}
    ]); 
  },
  "lookup finds the right thing": function() {
    assert.equal(gradesByLastName.lookup("Sassoon").records[0], gradeBook[3])
  },
  "two groups for 'B'": function() {
    //console.log('lookup:', gradesByGradeLastName.lookup("B"));
    assert.deepEqual(gradesByGradeLastName.lookup("B").children.rawValues(), ["Gold","Androy"]);
  },
  "leafnodes on leaf returns Value": function() {
    let leafNode = gradesByGradeLastName[1].children[0].leafNodes()[0];
    //console.log(`leafNode is Value: ${leafNode instanceof Value}, leafNode: ${leafNode}`);
    assert.equal(leafNode instanceof Value, true);
  },
  "leafnodes on supergroup returns Values": function() {
    let leafNode = gradesByGradeLastName.leafNodes()[0];
    console.log(`leafNode is Value: ${leafNode instanceof Value}, its a ${leafNode.constructor}, leafNode: ${leafNode}`);
    assert.equal(gradesByGradeLastName.leafNodes()[0] instanceof Value, true);
  },
  "leafnodes": function() {
    assert.deepEqual(gradesByGradeLastName.leafNodes().namePaths(), 
        ["C/Gold","B/Gold","B/Androy","A/Sassoon"]);
  },
  "sort": function() {
    assert.deepEqual(gradesByGradeLastName.leafNodes().sort(function(a,b){
          return a.namePath() < b.namePath() ? -1 : 
              b.namePath() < a.namePath() ? 1 : 0
        }).namePaths(),
      [ 'A/Sassoon', 'B/Androy', 'B/Gold', 'C/Gold' ]);
  },
  /*
  "sortBy": function() {
    assert.deepEqual(gradesByGradeLastName.leafNodes().sortBy(
          function(d){ return d.namePath(); }).namePaths(),
      [ 'A/Sassoon', 'B/Androy', 'B/Gold', 'C/Gold' ]);
  },
  "previous": function() {
    assert.deepEqual(gradesByGradeLastName.sort()[2].previous().namePath(),
          "B");
  },
  "vals should have rootList": function() {
    assert.equal(gradesByGradeLastName.lookup(['A','Sassoon']).rootList(),
           gradesByGradeLastName);
  },
  */
 },
 /*
 "asRootVal": {
  topic: function(){ 
    // make new version of gradesByGradeLastName so asRootVal doesn't mess up other one
    var gradesByGradeLastName = _.supergroup(gradeBook, ['grade','lastName']);
    var root = gradesByGradeLastName.asRootVal();
    return {gradesByGradeLastName:gradesByGradeLastName, root:root};
  }, 
  'should set its dimension as "root"': function(topic) {
    assert.equal(topic.root.dim, 'root');
  },
  'should contain all the records': function(topic) {
    assert.equal(topic.root.aggregate(_.sum, 'num'), 14);
  },
  'should namePath to root': function(topic) {
    assert.deepEqual(topic.gradesByGradeLastName.leafNodes().namePaths(),
     [ 'Root/C/Gold','Root/B/Gold','Root/B/Androy','Root/A/Sassoon' ]);
  }
  */
  /* haven't translated these yet

  describe('hierarchicalTableToTree', function() {
    var treePairs = [{"p":"animal","c":"mammal"},{"p":"animal","c":"reptile"},{"p":"animal","c":"fish"},{"p":"animal","c":"bird"},{"p":"bird","c":"kiwi"},{"p":"kiwi","c":"orange tailed kiwi"},{"p":"plant","c":"tree"},{"p":"plant","c":"bush"},{"p":"plant","c":"grass"},{"p":"plant","c":"fruit"},{"p":"fruit","c":"kiwi"},{"p":"kiwi","c":"purple kiwi"},{"p":"tree","c":"oak"},{"p":"tree","c":"maple"},{"p":"oak","c":"pin oak"},{"p":"mammal","c":"primate"},{"p":"mammal","c":"bovine"},{"p":"bovine","c":"cow"},{"p":"bovine","c":"ox"},{"p":"primate","c":"monkey"},{"p":"primate","c":"ape"},{"p":"ape","c":"chimpanzee"},{"p":"ape","c":"gorilla"},{"p":"ape","c":"me"}];
    var tree;
    it('should work with (data, parentProp, childProp) params', function() {
      tree = _.hierarchicalTableToTree(treePairs, 'p', 'c');
      expect(tree).toBeDefined();
    });
    it('should make this tree', function() {
      var paths = _.invoke(tree.flattenTree(), 'namePath');
      expect(paths).toEqual(["animal", "animal/mammal", "animal/mammal/primate", "animal/mammal/primate/monkey", "animal/mammal/primate/ape", "animal/mammal/primate/ape/chimpanzee", "animal/mammal/primate/ape/gorilla", "animal/mammal/primate/ape/me", "animal/mammal/bovine", "animal/mammal/bovine/cow", "animal/mammal/bovine/ox", "animal/reptile", "animal/fish", "animal/bird", "animal/bird/kiwi", "plant", "plant/tree", "plant/tree/oak", "plant/tree/oak/pin oak", "plant/tree/maple", "plant/bush", "plant/grass", "plant/fruit", "plant/fruit/kiwi", "plant/fruit/kiwi/orange tailed kiwi", "plant/fruit/kiwi/purple kiwi"]);
    });
  });
 }
  */
});

suite.run();