Skip to content

Commit

Permalink
Fix bugs in import, integration test for import, #2
Browse files Browse the repository at this point in the history
  • Loading branch information
kba committed May 8, 2017
1 parent 8e67742 commit c2e2612
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 39 deletions.
2 changes: 1 addition & 1 deletion anno-store-file/store-file.test.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
75 changes: 39 additions & 36 deletions anno-store-mongolike/store-mongolike.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) => {
Expand All @@ -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))
Expand All @@ -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()
Expand Down
34 changes: 33 additions & 1 deletion anno-store/store-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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([
Expand Down Expand Up @@ -130,14 +161,15 @@ 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),
cb => testCreateGet(t, store, cb),
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);
Expand Down
2 changes: 1 addition & 1 deletion anno-store/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit c2e2612

Please sign in to comment.