Skip to content

Commit 28ccdb8

Browse files
committed
Remove sharedb-mingo-memory circular dependency
sharedb had depended on sharedb-mingo-memory for 4 tests mostly relating to query subscriptions. This removes that dependency by faking out enough of MemoryDB in the tests to handle simple queries.
1 parent 5f4bd90 commit 28ccdb8

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
"expect.js": "^0.3.1",
1717
"istanbul": "^0.4.2",
1818
"jshint": "^2.9.2",
19-
"mocha": "^5.2.0",
20-
"sharedb-mingo-memory": "^1.0.0-beta"
19+
"mocha": "^5.2.0"
2120
},
2221
"scripts": {
2322
"test": "./node_modules/.bin/mocha && npm run jshint",

test/client/query-subscribe.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,14 +413,13 @@ describe('client query subscribe', function() {
413413

414414
it('changing a sorted property moves in a subscribed query', function(done) {
415415
var connection = this.backend.connect();
416-
var matchAllDbQuery = this.matchAllDbQuery;
417416

418417
async.parallel([
419418
function(cb) { connection.get('dogs', 'fido').create({age: 3}, cb); },
420419
function(cb) { connection.get('dogs', 'spot').create({age: 5}, cb); }
421420
], function(err) {
422421
if (err) return done(err);
423-
var dbQuery = getQuery({query: matchAllDbQuery, sort: [['age', 1]]});
422+
var dbQuery = getQuery({query: {}, sort: [['age', 1]]});
424423
var query = connection.createSubscribeQuery(
425424
'dogs',
426425
dbQuery,

test/db-memory.js

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
var expect = require('expect.js');
2+
var orderBy = require('lodash.orderby');
23
var DB = require('../lib/db');
34
var MemoryDB = require('../lib/db/memory');
45

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-
106
describe('DB base class', function() {
117
it('can call db.close() without callback', function() {
128
var db = new DB();
@@ -59,10 +55,75 @@ describe('DB base class', function() {
5955
});
6056
});
6157

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.
62121
require('./db')({
63122
create: function(callback) {
64-
var db = new ShareDbMingo();
123+
var db = new BasicQueryableMemoryDB();
65124
callback(null, db);
66125
},
67-
getQuery: getQuery
126+
getQuery: function(options) {
127+
return {filter: options.query, sort: options.sort};
128+
}
68129
});

0 commit comments

Comments
 (0)