Skip to content

Commit

Permalink
uniqueAgainst handles updates correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Graul committed Jul 30, 2012
1 parent ac93d19 commit d580ae8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 55 deletions.
46 changes: 3 additions & 43 deletions src/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,12 @@ Version 0.0.1.2
var rows = [],
colNames = _.keys(data),
row,
// get against unique col
uniqName = this.uniqueAgainst,
uniqCol = this.column(uniqName),
len = data[this._columns[1].name].length,
dataLength = _.max(_.map(colNames, function(name) {
return data[name].length;
}, this)),
toAdd = [],
toUpdate = [],
toRemove = [];


console.log('data', data, uniqCol);
_.each(data[uniqName], function(key, dataIndex) {
var rowIndex = uniqCol.data.indexOf(key);

Expand All @@ -270,46 +263,13 @@ Version 0.0.1.2

if (rowIndex === -1) {
toAdd.push( row );
console.log('adding', row);
} else {
toUpdate.push( row );
var oldRow = this.rowById(this.column('_id').data[rowIndex]);
console.log('upd', row, oldRow);

var oldRow = this.rowById(this.column('_id').data[rowIndex])._id;
this.update(oldRow, row)
}
}, this);






// var posToRemove = [], i;
// for(i = 0; i < len; i++) {
// var datum = data[this.uniqueAgainst][i];
// // this is a non unique row, remove it from all the data
// // arrays
// if (uniqCol.data.indexOf(datum) !== -1) {
// posToRemove.push(i);
// }
// }

// // sort and reverse the removal ids, this way we won't
// // lose position by removing an early id that will shift
// // array and throw all other ids off.
// posToRemove.sort().reverse();

// for(i = 0; i < dataLength; i++) {
// if (posToRemove.indexOf(i) === -1) {
// row = {};
// for(var j = 0; j < colNames.length; j++) {
// row[colNames[j]] = data[colNames[j]][i];
// }
// rows.push(row);
// }
// }

// this.add(rows);
this.add(toAdd);
},

//Always blindly add new rows
Expand Down
57 changes: 45 additions & 12 deletions test/unit/importers.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,12 @@
stop();
// expect(11);

var counter, requests = 2, madereqs = 1, expectedSize = 3;
var counter,
baseCounter,
requests = 3,
madereqs = 1,
expectedSize = 3,
events = [];

var ds = new Miso.Dataset({
url : "/poller/updated.json",
Expand All @@ -615,29 +620,49 @@
sync: true
});

ds.bind('update', function() {
console.log('UPD', arguments);
//verify the update events came through correctly
function verifyEvents() {
counter = baseCounter+1; //offset for the first request
console.log(events);
equals(events.length / 3, requests-1, 'one less set of update events than reqs');
_(requests).times(function(i) {
var row = events[i][0].changed;
if (row.name === 'alpha') {
equal(row.a, counter);
equal(row.b, counter * 2);
}
if (row.name === 'beta') {
equal(row.a, counter + 1, 'beta +1 1');
equal(row.b, counter - 1, 'beta +1 1');
}
if (row.name === 'delta') {
equal(row.a, counter + 2, 'delta +- 2');
equal(row.b, counter - 2, 'delta +- 2');
}
if (i % 3 === 2) {
counter += 1;
}
});
}

ds.bind('update', function(event) {
events.push(event.deltas);
});

ds.fetch({
success : function() {

// done
if (madereqs === requests) {
ds.importer.stop();
start();
}
madereqs++;

// check dataset length
equals(ds.length, expectedSize);
ds.eachColumn(function(cn, c) {
equals(c.data.length, 3);
});

//set the counter on the first req to
//sync req count with server
if (!counter) {
counter = ds.rowByPosition(0).a;
console.log('counter', counter);
baseCounter = counter;
}

var row0 = ds.rowByPosition(0);
Expand All @@ -651,7 +676,15 @@
var row2 = ds.rowByPosition(2);
equal(row2.a, counter + 2);
equal(row2.b, counter - 2);


// done
if (madereqs === requests) {
ds.importer.stop();
verifyEvents();
start();
}
madereqs++;
counter += 1;
},
error : function(r) {
console.log('ERROR', arguments);
Expand Down

0 comments on commit d580ae8

Please sign in to comment.