| 
 | 1 | +/*global assert:true */  | 
 | 2 | +'use strict';  | 
 | 3 | + | 
 | 4 | +var assert = require('chai').assert;  | 
 | 5 | +var mocha = require('mocha');  | 
 | 6 | +var sinon = require('sinon');  | 
 | 7 | +var DSMongoDBAdapter = require('./');  | 
 | 8 | +var JSData = require('js-data');  | 
 | 9 | + | 
 | 10 | +var adapter, store, DSUtils, DSErrors, User, Post, Comment;  | 
 | 11 | + | 
 | 12 | +var globals = module.exports = {  | 
 | 13 | +  fail: function (msg) {  | 
 | 14 | +    assert.equal('should not reach this!: ' + msg, 'failure');  | 
 | 15 | +  },  | 
 | 16 | +  TYPES_EXCEPT_STRING: [123, 123.123, null, undefined, {}, [], true, false, function () {  | 
 | 17 | +  }],  | 
 | 18 | +  TYPES_EXCEPT_STRING_OR_ARRAY: [123, 123.123, null, undefined, {}, true, false, function () {  | 
 | 19 | +  }],  | 
 | 20 | +  TYPES_EXCEPT_STRING_OR_NUMBER: [null, undefined, {}, [], true, false, function () {  | 
 | 21 | +  }],  | 
 | 22 | +  TYPES_EXCEPT_STRING_OR_OBJECT: [123, 123.123, null, undefined, [], true, false, function () {  | 
 | 23 | +  }],  | 
 | 24 | +  TYPES_EXCEPT_STRING_OR_NUMBER_OBJECT: [null, undefined, [], true, false, function () {  | 
 | 25 | +  }],  | 
 | 26 | +  TYPES_EXCEPT_STRING_OR_ARRAY_OR_NUMBER: [null, undefined, {}, true, false, function () {  | 
 | 27 | +  }],  | 
 | 28 | +  TYPES_EXCEPT_NUMBER: ['string', null, undefined, {}, [], true, false, function () {  | 
 | 29 | +  }],  | 
 | 30 | +  TYPES_EXCEPT_OBJECT: ['string', 123, 123.123, null, undefined, true, false, function () {  | 
 | 31 | +  }],  | 
 | 32 | +  TYPES_EXCEPT_BOOLEAN: ['string', 123, 123.123, null, undefined, {}, [], function () {  | 
 | 33 | +  }],  | 
 | 34 | +  TYPES_EXCEPT_FUNCTION: ['string', 123, 123.123, null, undefined, {}, [], true, false],  | 
 | 35 | +  assert: assert,  | 
 | 36 | +  sinon: sinon,  | 
 | 37 | +  adapter: undefined  | 
 | 38 | +};  | 
 | 39 | + | 
 | 40 | +var test = new mocha();  | 
 | 41 | + | 
 | 42 | +var testGlobals = [];  | 
 | 43 | + | 
 | 44 | +for (var key in globals) {  | 
 | 45 | +  global[key] = globals[key];  | 
 | 46 | +  testGlobals.push(globals[key]);  | 
 | 47 | +}  | 
 | 48 | +test.globals(testGlobals);  | 
 | 49 | + | 
 | 50 | +beforeEach(function () {  | 
 | 51 | +  store = new JSData.DS();  | 
 | 52 | +  adapter = new DSMongoDBAdapter('mongodb://localhost:27017');  | 
 | 53 | +  DSUtils = JSData.DSUtils;  | 
 | 54 | +  DSErrors = JSData.DSErrors;  | 
 | 55 | +  globals.User = global.User = User = store.defineResource({  | 
 | 56 | +    name: 'user',  | 
 | 57 | +    idAttribute: '_id',  | 
 | 58 | +    relations: {  | 
 | 59 | +      hasMany: {  | 
 | 60 | +        post: {  | 
 | 61 | +          localField: 'posts',  | 
 | 62 | +          foreignKey: 'post'  | 
 | 63 | +        }  | 
 | 64 | +      }  | 
 | 65 | +    }  | 
 | 66 | +  });  | 
 | 67 | +  globals.Post = global.Post = Post = store.defineResource({  | 
 | 68 | +    name: 'post',  | 
 | 69 | +    idAttribute: '_id',  | 
 | 70 | +    relations: {  | 
 | 71 | +      belongsTo: {  | 
 | 72 | +        user: {  | 
 | 73 | +          localField: 'user',  | 
 | 74 | +          localKey: 'userId'  | 
 | 75 | +        }  | 
 | 76 | +      },  | 
 | 77 | +      hasMany: {  | 
 | 78 | +        comment: {  | 
 | 79 | +          localField: 'comments',  | 
 | 80 | +          foreignKey: 'postId'  | 
 | 81 | +        }  | 
 | 82 | +      }  | 
 | 83 | +    }  | 
 | 84 | +  });  | 
 | 85 | +  globals.Comment = global.Comment = Comment = store.defineResource({  | 
 | 86 | +    name: 'comment',  | 
 | 87 | +    idAttribute: '_id',  | 
 | 88 | +    relations: {  | 
 | 89 | +      belongsTo: {  | 
 | 90 | +        post: {  | 
 | 91 | +          localField: 'post',  | 
 | 92 | +          localKey: 'postId'  | 
 | 93 | +        },  | 
 | 94 | +        user: {  | 
 | 95 | +          localField: 'user',  | 
 | 96 | +          localKey: 'userId'  | 
 | 97 | +        }  | 
 | 98 | +      }  | 
 | 99 | +    }  | 
 | 100 | +  });  | 
 | 101 | + | 
 | 102 | +  globals.adapter = adapter;  | 
 | 103 | +  global.adapter = globals.adapter;  | 
 | 104 | + | 
 | 105 | +  globals.DSUtils = DSUtils;  | 
 | 106 | +  global.DSUtils = globals.DSUtils;  | 
 | 107 | + | 
 | 108 | +  globals.DSErrors = DSErrors;  | 
 | 109 | +  global.DSErrors = globals.DSErrors;  | 
 | 110 | +});  | 
 | 111 | + | 
 | 112 | +afterEach(function (done) {  | 
 | 113 | +  globals.adapter = null;  | 
 | 114 | +  global.adapter = null;  | 
 | 115 | + | 
 | 116 | +  adapter.destroyAll(User, {}).then(function () {  | 
 | 117 | +    done();  | 
 | 118 | +  }, done);  | 
 | 119 | +});  | 
0 commit comments