[#49,query][s]: introduce Query model object and use it in Dataset.

* tests: refactor Memory backend tests so that tests are independent (to avoid issues of carry over of query state)
* TODO: use query stuff in views - NB sorting seems broken in DataTable.
This commit is contained in:
Rufus Pollock
2012-02-17 23:05:34 +00:00
parent 7fa2517450
commit 866ad12a15
2 changed files with 95 additions and 71 deletions

View File

@@ -13,37 +13,31 @@ this.recline.Model = this.recline.Model || {};
my.Dataset = Backbone.Model.extend({ my.Dataset = Backbone.Model.extend({
__type__: 'Dataset', __type__: 'Dataset',
initialize: function(model, backend) { initialize: function(model, backend) {
_.bindAll(this, 'query');
this.backend = backend; this.backend = backend;
if (backend && backend.constructor == String) { if (backend && backend.constructor == String) {
this.backend = my.backends[backend]; this.backend = my.backends[backend];
} }
this.currentDocuments = new my.DocumentList(); this.currentDocuments = new my.DocumentList();
this.docCount = null; this.docCount = null;
this.defaultQuery = { this.queryState = new my.Query();
size: 100 this.queryState.bind('change', this.query);
, offset: 0
};
// this.queryState = {};
}, },
// ### getDocuments // ### query
// //
// AJAX method with promise API to get rows (documents) from the backend. // AJAX method with promise API to get documents from the backend.
//
// It will query based on current query state (given by this.queryState)
// updated by queryObj (if provided).
// //
// Resulting DocumentList are used to reset this.currentDocuments and are // Resulting DocumentList are used to reset this.currentDocuments and are
// also returned. // also returned.
//
// :param numRows: passed onto backend getDocuments.
// :param start: passed onto backend getDocuments.
//
// this does not fit very well with Backbone setup. Backbone really expects you to know the ids of objects your are fetching (which you do in classic RESTful ajax-y world). But this paradigm does not fill well with data set up we have here.
// This also illustrates the limitations of separating the Dataset and the Backend
query: function(queryObj) { query: function(queryObj) {
var self = this; var self = this;
this.queryState = queryObj || this.defaultQuery; this.queryState.set(queryObj, {silent: true});
this.queryState = _.extend({size: 100, offset: 0}, this.queryState);
var dfd = $.Deferred(); var dfd = $.Deferred();
this.backend.query(this, this.queryState).done(function(rows) { this.backend.query(this, this.queryState.toJSON()).done(function(rows) {
var docs = _.map(rows, function(row) { var docs = _.map(rows, function(row) {
var _doc = new my.Document(row); var _doc = new my.Document(row);
_doc.backend = self.backend; _doc.backend = self.backend;
@@ -79,6 +73,13 @@ my.DocumentList = Backbone.Collection.extend({
model: my.Document model: my.Document
}); });
my.Query = Backbone.Model.extend({
defaults: {
size: 100
, offset: 0
}
});
// ## Backend registry // ## Backend registry
// //
// Backends will register themselves by id into this registry // Backends will register themselves by id into this registry

View File

@@ -1,68 +1,91 @@
(function ($) { (function ($) {
module("Dataset"); module("Dataset");
test('Memory Backend', function () { var memoryData = {
var datasetId = 'test-dataset'; metadata: {
var inData = { title: 'My Test Dataset'
metadata: { , name: '1-my-test-dataset'
title: 'My Test Dataset' , id: 'test-dataset'
, name: '1-my-test-dataset' , headers: ['x', 'y', 'z']
, id: datasetId },
, headers: ['x', 'y', 'z'] documents: [
}, {id: 0, x: 1, y: 2, z: 3}
documents: [ , {id: 1, x: 2, y: 4, z: 6}
{id: 0, x: 1, y: 2, z: 3} , {id: 2, x: 3, y: 6, z: 9}
, {id: 1, x: 2, y: 4, z: 6} , {id: 3, x: 4, y: 8, z: 12}
, {id: 2, x: 3, y: 6, z: 9} , {id: 4, x: 5, y: 10, z: 15}
, {id: 3, x: 4, y: 8, z: 12} , {id: 5, x: 6, y: 12, z: 18}
, {id: 4, x: 5, y: 10, z: 15} ]
, {id: 5, x: 6, y: 12, z: 18} };
]
}; function makeBackendDataset() {
var backend = new recline.Model.BackendMemory(); var backend = new recline.Model.BackendMemory();
backend.addDataset(inData); backend.addDataset(memoryData);
var dataset = new recline.Model.Dataset({id: datasetId}, backend); var dataset = new recline.Model.Dataset({id: memoryData.metadata.id}, backend);
// ### Start testing return dataset;
expect(10); }
// convenience for tests
var data = backend.datasets[datasetId]; test('Memory Backend: basics', function () {
var dataset = makeBackendDataset();
expect(3);
// convenience for tests - get the data that should get changed
var data = dataset.backend.datasets[memoryData.metadata.id];
dataset.fetch().then(function(datasetAgain) { dataset.fetch().then(function(datasetAgain) {
equal(dataset.get('name'), data.metadata.name); equal(dataset.get('name'), data.metadata.name);
deepEqual(dataset.get('headers'), data.metadata.headers); deepEqual(dataset.get('headers'), data.metadata.headers);
equal(dataset.docCount, 6); equal(dataset.docCount, 6);
var queryObj = { });
size: 4 });
, offset: 2
};
dataset.query(queryObj).then(function(documentList) {
deepEqual(data.documents[2], documentList.models[0].toJSON());
});
var queryObj = {
sort: [
['y', 'desc']
]
};
dataset.query(queryObj).then(function(docs) {
var doc0 = dataset.currentDocuments.models[0].toJSON();
equal(doc0.x, 6);
});
dataset.query().then(function(docList) {
equal(docList.length, Math.min(100, data.documents.length));
var doc1 = docList.models[0];
deepEqual(doc1.toJSON(), data.documents[0]);
// Test UPDATE test('Memory Backend: query', function () {
var newVal = 10; var dataset = makeBackendDataset();
doc1.set({x: newVal}); // convenience for tests - get the data that should get changed
doc1.save().then(function() { var data = dataset.backend.datasets[memoryData.metadata.id];
equal(data.documents[0].x, newVal); var dataset = makeBackendDataset();
}) var queryObj = {
size: 4
, offset: 2
};
dataset.query(queryObj).then(function(documentList) {
deepEqual(data.documents[2], documentList.models[0].toJSON());
});
});
// Test Delete test('Memory Backend: query sort', function () {
doc1.destroy().then(function() { var dataset = makeBackendDataset();
equal(data.documents.length, 5); // convenience for tests - get the data that should get changed
equal(data.documents[0].x, inData.documents[1].x); var data = dataset.backend.datasets[memoryData.metadata.id];
}); var queryObj = {
sort: [
['y', 'desc']
]
};
dataset.query(queryObj).then(function(docs) {
var doc0 = dataset.currentDocuments.models[0].toJSON();
equal(doc0.x, 6);
});
});
test('Memory Backend: update and delete', function () {
var dataset = makeBackendDataset();
// convenience for tests - get the data that should get changed
var data = dataset.backend.datasets[memoryData.metadata.id];
dataset.query().then(function(docList) {
equal(docList.length, Math.min(100, data.documents.length));
var doc1 = docList.models[0];
deepEqual(doc1.toJSON(), data.documents[0]);
// Test UPDATE
var newVal = 10;
doc1.set({x: newVal});
doc1.save().then(function() {
equal(data.documents[0].x, newVal);
})
// Test Delete
doc1.destroy().then(function() {
equal(data.documents.length, 5);
equal(data.documents[0].x, memoryData.documents[1].x);
}); });
}); });
}); });