diff --git a/anno-store-file/store-file.test.js b/anno-store-file/store-file.test.js index dd24524..5b4994e 100644 --- a/anno-store-file/store-file.test.js +++ b/anno-store-file/store-file.test.js @@ -1,5 +1,5 @@ process.env.ANNO_BASE_URL = `http://localhost:3000` -process.env.ANNO_BASE_PATH = `/anno` +process.env.ANNO_BASE_PATH = `` process.env.ANNO_NEDB_DIR = `${__dirname}/../temp` const FileStore = require('./store-file') diff --git a/anno-store-mongolike/store-mongolike.js b/anno-store-mongolike/store-mongolike.js index e8ef2da..d798ea6 100644 --- a/anno-store-mongolike/store-mongolike.js +++ b/anno-store-mongolike/store-mongolike.js @@ -90,7 +90,6 @@ class MongolikeStore extends Store { _createInsert(anno, options, cb) { const validFn = schema.validate.Annotation - console.log('_createInsert', typeof anno, anno) if (!validFn(anno)) { return cb(errors.invalidAnnotation(anno, validFn.errors)) } @@ -137,6 +136,43 @@ class MongolikeStore extends Store { }) } + /* @override */ + _import(options, cb) { + var anno = options.anno + const fromJSONLD = (anno) => { + anno = this._normalizeType(anno) + const ret = {} + Object.keys(anno).forEach(prop => { + if (prop == 'hasVersion') { + ret._revisions = anno[prop].map(fromJSONLD) + delete anno[prop] + } else if (prop == 'hasReply') { + ret._replies = anno[prop].map(fromJSONLD) + delete anno[prop] + } else { + ret[prop] = anno[prop] + } + }) + if (!ret._revisions) { + const woReplies = JSON.parse(JSON.stringify(ret)) + delete woReplies._replies + ret._revisions = [woReplies] + } + ret._replies = ret._replies || [] + return ret + } + anno = fromJSONLD(anno) + if (options.slug) { + this.db.remove({_id: options.slug}, options, (err) => { + anno._id = options.slug + this._createInsert(anno, options, cb) + }) + } else { + anno._id = this._genid() + this._createInsert(anno, options, cb) + } + } + /* @override */ _revise(options, cb) { const annoId = this._idFromURL(options.annoId) @@ -179,7 +215,7 @@ class MongolikeStore extends Store { {$set: newData}, ] } - console.log(_id, ...modQueries) + // console.log(_id, ...modQueries) this.db.update({_id}, modQueries[0], {}, (err, arg) => { if (err) return cb(err) this.db.update({_id}, modQueries[1], {}, (err, arg) => { @@ -192,39 +228,6 @@ class MongolikeStore extends Store { }) } - /* @override */ - _import(options, cb) { - var anno = options.anno - const fromJSONLD = (anno) => { - anno = this._normalizeTarget(anno) - anno = this._normalizeType(anno) - const ret = {} - Object.keys(anno).forEach(prop => { - if (prop == 'hasVersion') { - ret._revisions = anno[prop].map(fromJSONLD) - } else if (prop == 'hasReply') { - ret._replies = anno[prop].map(fromJSONLD) - } else { - ret[prop] = anno[prop] - } - }) - if (!ret._revisions) { - const woReplies = JSON.stringify(JSON.parse(ret)) - delete woReplies._replies - ret._revisions = [woReplies] - } - ret._replies = ret._replies || [] - } - if (options.slug) { - this.remove(options.slug, options, (err) => { - this._createInsert(anno, options, cb) - }) - } else { - anno._id = this._genid() - this._createInsert(anno, options, cb) - } - } - /* @override */ _delete(options, cb) { const {_id, _replyids, _revid} = splitIdRepliesRev(this._idFromURL(options.annoId)) @@ -240,7 +243,7 @@ class MongolikeStore extends Store { selector += `_replies.${_replyid - 1}.` } selector += 'deleted' - console.log(selector) + // console.log(selector) this.db.update({_id}, {$set: {[selector]: new Date()}}, (err, nrModified) => { if (err) return cb(err) return cb() diff --git a/anno-store/store-test.js b/anno-store/store-test.js index d9142b0..526403f 100644 --- a/anno-store/store-test.js +++ b/anno-store/store-test.js @@ -2,6 +2,8 @@ const async = require('async') const tap = require('tap') const fixtures = require('../anno-fixtures') +const NUMBER_OF_TESTS = 25 + module.exports = function testStore(store, testStoreCallback) { const input1 = fixtures.Annotation.ok['minimal-string-target.json'] @@ -10,9 +12,38 @@ module.exports = function testStore(store, testStoreCallback) { const input2 = fixtures.Annotation.ok['minimal-object-target.json'] const input3 = fixtures.Annotation.ok['minimal-array-target.json'] const input4 = {target: 'x://y', body: {type: ['oa:Tag']}} + const toImport = { + type: ['Annotation'], + body: 'http://body', + target: 'http://target', + hasReply: [ + {type: ['Annotation'], body: {value: "Bullshit!"}, creator: "foo@bar.com"} + ], + hasVersion: [ + {type: ['Annotation'], body: 'http://bdoy', target: 'http://target'}, + {type: ['Annotation'], body: 'http://body', target: 'http://target'} + ] + } var savedId; var savedRevId; + function testImport(t, store, done) { + t.comment('import') + store.import(JSON.parse(JSON.stringify(toImport)), {slug: 'foobar3000'}, (err, got) => { + t.equals(got.id, 'http://localhost:3000/anno/foobar3000', 'id okay') + t.equals(got.hasVersion.length, 2, '2 versions') + t.equals(got.hasReply.length, 1, '1 reply') + t.equals(got.hasReply[0].hasVersion.length, 1, 'first reply has one version') + store.import(toImport, {slug: 'foobar3000'}, (err, got) => { + t.equals(got.id, 'http://localhost:3000/anno/foobar3000', 'id STILL okay') + t.equals(got.hasVersion.length, 2, 'STILL 2 versions') + t.equals(got.hasReply.length, 1, 'STILL 1 reply') + t.equals(got.hasReply[0].hasVersion.length, 1, 'first reply has STILL one version') + t.end() + }) + }) + } + function testWipe(t, store, done) { t.comment("wipe-init") async.waterfall([ @@ -130,7 +161,7 @@ module.exports = function testStore(store, testStoreCallback) { } tap.test(`store-test / ${store.constructor.name}`, t => { - t.plan(17) + t.plan(NUMBER_OF_TESTS) async.waterfall([ cb => store.init(cb), cb => testWipe(t, store, cb), @@ -138,6 +169,7 @@ module.exports = function testStore(store, testStoreCallback) { cb => testRevise(t, store, cb), cb => testSearch(t, store, cb), cb => testDelete(t, store, cb), + cb => testImport(t, store, cb), cb => store.disconnect(cb), ], (err) => { if (err) t.fail(err); diff --git a/anno-store/store.js b/anno-store/store.js index 1c8f16d..36c4cdb 100644 --- a/anno-store/store.js +++ b/anno-store/store.js @@ -346,7 +346,7 @@ class Store { import(anno, options, cb) { if (typeof options === 'function') [cb, options] = [options, {}] this._callMethod(Object.assign(options, { - method: 'create', + method: 'import', anno, }), cb) }