|
1 | 1 | var expect = require('expect.js');
|
| 2 | +var orderBy = require('lodash.orderby'); |
2 | 3 | var DB = require('../lib/db');
|
3 | 4 | var MemoryDB = require('../lib/db/memory');
|
4 | 5 |
|
5 |
| -// Extend from MemoryDB as defined in this package, not the one that |
6 |
| -// sharedb-mingo-memory depends on. |
7 |
| -var ShareDbMingo = require('sharedb-mingo-memory').extendMemoryDB(MemoryDB); |
8 |
| -var getQuery = require('sharedb-mingo-memory/get-query'); |
9 |
| - |
10 | 6 | describe('DB base class', function() {
|
11 | 7 | it('can call db.close() without callback', function() {
|
12 | 8 | var db = new DB();
|
@@ -59,10 +55,75 @@ describe('DB base class', function() {
|
59 | 55 | });
|
60 | 56 | });
|
61 | 57 |
|
| 58 | + |
| 59 | +// Extension of MemoryDB that supports query filters and sorts on simple |
| 60 | +// top-level properties, which is enough for the core ShareDB tests on |
| 61 | +// query subscription updating. |
| 62 | +function BasicQueryableMemoryDB() { |
| 63 | + MemoryDB.apply(this, arguments); |
| 64 | +} |
| 65 | +BasicQueryableMemoryDB.prototype = Object.create(MemoryDB.prototype); |
| 66 | +BasicQueryableMemoryDB.prototype.constructor = BasicQueryableMemoryDB; |
| 67 | + |
| 68 | +BasicQueryableMemoryDB.prototype._querySync = function(snapshots, query, options) { |
| 69 | + if (query.filter) { |
| 70 | + snapshots = snapshots.filter(function(snapshot) { |
| 71 | + for (var queryKey in query.filter) { |
| 72 | + // This fake only supports simple property equality filters, so |
| 73 | + // throw an error on Mongo-like filter properties with dots. |
| 74 | + if (queryKey.includes('.')) { |
| 75 | + throw new Error('Only simple property filters are supported, got:', queryKey); |
| 76 | + } |
| 77 | + if (snapshot.data[queryKey] !== query.filter[queryKey]) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + } |
| 81 | + return true; |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + if (query.sort) { |
| 86 | + if (!Array.isArray(query.sort)) { |
| 87 | + throw new Error('query.sort must be an array'); |
| 88 | + } |
| 89 | + if (query.sort.length) { |
| 90 | + snapshots.sort(snapshotComparator(query.sort)); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return {snapshots: snapshots} |
| 95 | +}; |
| 96 | + |
| 97 | +// sortProperties is an array whose items are each [propertyName, direction]. |
| 98 | +function snapshotComparator(sortProperties) { |
| 99 | + return function(snapshotA, snapshotB) { |
| 100 | + for (var sortProperty of sortProperties) { |
| 101 | + var sortKey = sortProperty[0]; |
| 102 | + var sortDirection = sortProperty[1]; |
| 103 | + |
| 104 | + var aPropVal = snapshotA.data[sortKey]; |
| 105 | + var bPropVal = snapshotB.data[sortKey]; |
| 106 | + if (aPropVal < bPropVal) { |
| 107 | + return -1 * sortDirection; |
| 108 | + } else if (aPropVal > bPropVal) { |
| 109 | + return sortDirection; |
| 110 | + } else if (aPropVal === bPropVal) { |
| 111 | + continue; |
| 112 | + } else { |
| 113 | + throw new Error('Could not compare ' + aPropVal + ' and ' + bPropVal); |
| 114 | + } |
| 115 | + } |
| 116 | + return 0; |
| 117 | + }; |
| 118 | +} |
| 119 | + |
| 120 | +// Run all the DB-based tests against the BasicQueryableMemoryDB. |
62 | 121 | require('./db')({
|
63 | 122 | create: function(callback) {
|
64 |
| - var db = new ShareDbMingo(); |
| 123 | + var db = new BasicQueryableMemoryDB(); |
65 | 124 | callback(null, db);
|
66 | 125 | },
|
67 |
| - getQuery: getQuery |
| 126 | + getQuery: function(options) { |
| 127 | + return {filter: options.query, sort: options.sort}; |
| 128 | + } |
68 | 129 | });
|
0 commit comments