Permalink
Browse files

Get shared tests running in Memory adapter

  • Loading branch information...
1 parent c249c43 commit b389a56dd80c80d6c16788a3234e64faf981b2eb mde committed Sep 16, 2012
Showing with 96 additions and 68 deletions.
  1. +31 −1 lib/adapters/memory/index.js
  2. +65 −67 lib/adapters/transformers/mr.js
@@ -33,11 +33,15 @@ utils.mixin(Adapter.prototype, new (function () {
this.load = function (query, callback) {
var key = query.model.modelName
+ , props = model.descriptionRegistry[key].properties
+ , datatype
, id = query.byId
, conditions
, sort
, items = _data[key]
, item
+ , data
+ , val
, filter
, res = [];
if (id) {
@@ -51,14 +55,40 @@ utils.mixin(Adapter.prototype, new (function () {
filter = new Function('data', 'return (' + conditions + ')');
for (var p in items) {
item = items[p];
- if (filter(item)) {
+
+ // Use data-only for filtering
+ data = item.toData({whitelist: ['id', 'createdAt']});
+
+ // Do annoying stringification for dates
+ for (var p in data) {
+ val = data[p];
+ datatype = null;
+
+ // Flag all date/datetime props
+ if (p == 'createdAt' || p == 'updatedAt') {
+ datatype = 'datetime';
+ }
+ else if (props[p]) {
+ datatype = props[p].datatype;
+ }
+
+ if (val && (datatype == 'date' || datatype == 'datetime')) {
+ data[p] = JSON.stringify(val).replace(/"/g, "'");
+ }
+ }
+
+ if (filter(data)) {
res.push(item);
}
}
if (sort) {
sort(res);
}
+ if (query.opts.limit == 1) {
+ res = res[0];
+ }
+
callback(null, res);
}
};
@@ -1,74 +1,72 @@
var operation = require('../../query/operation')
, comparison = require('../../query/comparison')
- , datatypes = require('../../datatypes')
- , _serializeForDataType
- , _sortFunction;
-
-_serializeForDataType = function (datatype, val) {
- var ret;
- switch (true) {
- case val === null:
- ret = 'null';
- break;
- case val === '':
- ret = '\'\'';
- break;
- case datatype == 'date' || datatype == 'datetime':
- ret = JSON.stringify(val).replace(/"/g, "'");
- break;
- default:
- ret = datatypes[datatype].serialize(val, {
- useQuotes: true
- , escape: true
- });
- }
- return ret;
-};
-
-// This function is special -- its source is transformed by
-// replacing the __sort__ variable. It's also converted into
-// a JSON-safe string in the Riak adapter and posted as the
-// reduce-sort
-_sortFunction = function (values) {
- // Dummy value to replace with real sort data -- will look
- // like {'foo': 'asc', 'bar': 'desc'}
- var sort = '__sort__'
- // Directional sort, returns explicit zero if equal
- , baseSort = function (a, b, dir) {
- if (a == b) {
- return 0;
- }
- if (dir == 'asc') {
- return a > b ? 1 : -1;
- }
- else {
- return a > b ? -1 : 1;
- }
- }
- // Iterates each of the sort columns until it finds a
- // pair of values that are not the same
- , columnSort = function (a, b) {
- var ret;
- for (var p in sort) {
- // Call the directional sort for the two values
- // in this property
- ret = baseSort(a[p], b[p], sort[p]);
- // -1 and 1 are truthy
- if (ret) {
- return ret;
+ , datatypes = require('../../datatypes');
+
+var mr = new (function () {
+
+ var _transformForDataType = function (datatype, val) {
+ var ret;
+ switch (true) {
+ case val === null:
+ ret = 'null';
+ break;
+ case val === '':
+ ret = '\'\'';
+ break;
+ case datatype == 'date' || datatype == 'datetime':
+ ret = JSON.stringify(val).replace(/"/g, "'");
+ break;
+ default:
+ ret = datatypes[datatype].serialize(val, {
+ useQuotes: true
+ , escape: true
+ });
}
+ return ret;
}
- return 1;
- };
- return values.sort(columnSort);
-};
-var mr = new (function () {
+ // This function is special -- its source is transformed by
+ // replacing the __sort__ variable. It's also converted into
+ // a JSON-safe string in the Riak adapter and posted as the
+ // reduce-sort
+ , _sortFunction = function (values) {
+ // Dummy value to replace with real sort data -- will look
+ // like {'foo': 'asc', 'bar': 'desc'}
+ var sort = '__sort__'
+ // Directional sort, returns explicit zero if equal
+ , baseSort = function (a, b, dir) {
+ if (a == b) {
+ return 0;
+ }
+ if (dir == 'asc') {
+ return a > b ? 1 : -1;
+ }
+ else {
+ return a > b ? -1 : 1;
+ }
+ }
+ // Iterates each of the sort columns until it finds a
+ // pair of values that are not the same
+ , columnSort = function (a, b) {
+ var ret;
+ for (var p in sort) {
+ // Call the directional sort for the two values
+ // in this property
+ ret = baseSort(a[p], b[p], sort[p]);
+ // -1 and 1 are truthy
+ if (ret) {
+ return ret;
+ }
+ }
+ return 1;
+ };
+ return values.sort(columnSort);
+ }
- var _operationSymbols = {
- 'and': '&&'
- , 'or': '||'
- };
+ , _operationSymbols = {
+ 'and': '&&'
+ , 'or': '||'
+ };
this.transformSortOrder = function (sort) {
var sortString
@@ -138,7 +136,7 @@ var mr = new (function () {
case comp instanceof comparison.InclusionComparison:
comp.value.forEach(function (item) {
arr.push(name + ' == ' +
- _serializeForDataType(comp.datatype, item));
+ _transformForDataType(comp.datatype, item));
});
ret = arr.join(' || ');
break;
@@ -166,7 +164,7 @@ var mr = new (function () {
};
this.transformComparisonValue = function (comp) {
- return _serializeForDataType(comp.datatype, comp.value);
+ return _transformForDataType(comp.datatype, comp.value);
};
})();

0 comments on commit b389a56

Please sign in to comment.