|
| 1 | +import test from 'tape'; |
| 2 | + |
| 3 | +import database from '../../db'; |
| 4 | +import {archiveDocument} from './index' |
| 5 | +import {generateId} from '../../utils' |
| 6 | + |
| 7 | +const dbconf = { |
| 8 | + mongodb: { |
| 9 | + uri: process.env.MONGO_URI || 'mongodb://localhost:27017/dev', |
| 10 | + }, |
| 11 | + |
| 12 | + neo4j: { |
| 13 | + uri: process.env.NEO_URI || 'bolt://localhost', |
| 14 | + }, |
| 15 | +}; |
| 16 | + |
| 17 | +test( 'archiveDocument', t => { |
| 18 | + const db = database( dbconf ); |
| 19 | + const worldA = {_id: generateId(),title: "Test World A"}; |
| 20 | + const worldB = {_id: generateId(),title: "Test World B"}; |
| 21 | + const invalidWorld = {_id: generateId(),title: "Invalid world"}; |
| 22 | + const userId = "testingId"; |
| 23 | + const collection = 'world'; |
| 24 | + let neo, mongo, actual, expected; |
| 25 | + |
| 26 | + db |
| 27 | + .map( db => { |
| 28 | + neo = db.neo; |
| 29 | + mongo = db.mongo; |
| 30 | + return mongo; |
| 31 | + }) |
| 32 | + .flatMap( mongo => { |
| 33 | + return mongo.collection(collection).insertMany( [worldA,worldB] ) |
| 34 | + }) |
| 35 | + .flatMap( () => { |
| 36 | + return db::archiveDocument(collection,worldA._id,userId) |
| 37 | + }) |
| 38 | + .flatMap( document => { |
| 39 | + |
| 40 | + expected = worldA._id; |
| 41 | + actual = document._id; |
| 42 | + t.equal(actual, expected,'should have the same _id that has been passed to archiveDocument'); |
| 43 | + |
| 44 | + expected = userId; |
| 45 | + actual = document.archiver; |
| 46 | + t.equal(actual, expected,'should match the userId that has been passed to archiveDocument'); |
| 47 | + |
| 48 | + expected = true; |
| 49 | + actual = document.archived; |
| 50 | + t.equal(actual, expected,'should be archived') |
| 51 | + |
| 52 | + return mongo.collection(collection).findOne({_id : worldB._id}) |
| 53 | + }) |
| 54 | + .flatMap(document => { |
| 55 | + |
| 56 | + expected = worldB; |
| 57 | + actual = document; |
| 58 | + t.deepEqual(actual, expected,'should not change when the _id does not match the document _id'); |
| 59 | + |
| 60 | + return mongo.collection(collection).deleteMany( |
| 61 | + { |
| 62 | + _id : { $in: [worldA._id,worldB._id]} |
| 63 | + } |
| 64 | + ) |
| 65 | + }) |
| 66 | + .flatMap(r => |
| 67 | + db::archiveDocument(collection,invalidWorld._id,userId) |
| 68 | + ) |
| 69 | + .subscribe((document) => { |
| 70 | + |
| 71 | + expected = null; |
| 72 | + actual = document; |
| 73 | + t.equal(actual, expected,'should not exist when the document is invalid'); |
| 74 | + |
| 75 | + t.end(); |
| 76 | + |
| 77 | + mongo.close().then( () => neo.close( () => neo.disconnect() )); |
| 78 | + }) |
| 79 | +}); |
| 80 | + |
0 commit comments