|
2 | 2 |
|
3 | 3 | const MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAdapter');
|
4 | 4 | const MongoClient = require('mongodb').MongoClient;
|
| 5 | +const databaseURI = 'mongodb://localhost:27017/parseServerMongoAdapterTestDatabase'; |
5 | 6 |
|
| 7 | +// These tests are specific to the mongo storage adapter + mongo storage format |
| 8 | +// and will eventually be moved into their own repo |
6 | 9 | describe('MongoStorageAdapter', () => {
|
| 10 | + beforeEach(done => { |
| 11 | + new MongoStorageAdapter({ uri: databaseURI }) |
| 12 | + .deleteAllSchemas() |
| 13 | + .then(done, fail); |
| 14 | + }); |
| 15 | + |
7 | 16 | it('auto-escapes symbols in auth information', () => {
|
8 | 17 | spyOn(MongoClient, 'connect').and.returnValue(Promise.resolve(null));
|
9 | 18 | new MongoStorageAdapter({
|
@@ -37,4 +46,110 @@ describe('MongoStorageAdapter', () => {
|
37 | 46 | jasmine.any(Object)
|
38 | 47 | );
|
39 | 48 | });
|
| 49 | + |
| 50 | + it('stores objectId in _id', done => { |
| 51 | + let adapter = new MongoStorageAdapter({ uri: databaseURI }); |
| 52 | + adapter.createObject('Foo', { objectId: 'abcde' }, { fields: { objectId: 'String' } }) |
| 53 | + .then(() => adapter._rawFind('Foo', {})) |
| 54 | + .then(results => { |
| 55 | + expect(results.length).toEqual(1); |
| 56 | + var obj = results[0]; |
| 57 | + expect(typeof obj._id).toEqual('string'); |
| 58 | + expect(obj.objectId).toBeUndefined(); |
| 59 | + done(); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('stores pointers with a _p_ prefix', (done) => { |
| 64 | + let obj = { |
| 65 | + objectId: 'bar', |
| 66 | + aPointer: { |
| 67 | + __type: 'Pointer', |
| 68 | + className: 'JustThePointer', |
| 69 | + objectId: 'qwerty' |
| 70 | + } |
| 71 | + }; |
| 72 | + let adapter = new MongoStorageAdapter({ uri: databaseURI }); |
| 73 | + adapter.createObject('APointerDarkly', obj, { fields: { |
| 74 | + objectId: { type: 'String' }, |
| 75 | + aPointer: { type: 'Pointer', targetClass: 'JustThePointer' }, |
| 76 | + }}) |
| 77 | + .then(() => adapter._rawFind('APointerDarkly', {})) |
| 78 | + .then(results => { |
| 79 | + expect(results.length).toEqual(1); |
| 80 | + let output = results[0]; |
| 81 | + expect(typeof output._id).toEqual('string'); |
| 82 | + expect(typeof output._p_aPointer).toEqual('string'); |
| 83 | + expect(output._p_aPointer).toEqual('JustThePointer$qwerty'); |
| 84 | + expect(output.aPointer).toBeUndefined(); |
| 85 | + done(); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('handles object and subdocument', done => { |
| 90 | + let adapter = new MongoStorageAdapter({ uri: databaseURI }); |
| 91 | + let schema = { fields : { subdoc: { type: 'Object' } } }; |
| 92 | + let obj = { subdoc: {foo: 'bar', wu: 'tan'} }; |
| 93 | + adapter.createObject('MyClass', obj, schema) |
| 94 | + .then(() => adapter._rawFind('MyClass', {})) |
| 95 | + .then(results => { |
| 96 | + expect(results.length).toEqual(1); |
| 97 | + let mob = results[0]; |
| 98 | + expect(typeof mob.subdoc).toBe('object'); |
| 99 | + expect(mob.subdoc.foo).toBe('bar'); |
| 100 | + expect(mob.subdoc.wu).toBe('tan'); |
| 101 | + let obj = { 'subdoc.wu': 'clan' }; |
| 102 | + return adapter.findOneAndUpdate('MyClass', {}, schema, obj); |
| 103 | + }) |
| 104 | + .then(() => adapter._rawFind('MyClass', {})) |
| 105 | + .then(results => { |
| 106 | + expect(results.length).toEqual(1); |
| 107 | + let mob = results[0]; |
| 108 | + expect(typeof mob.subdoc).toBe('object'); |
| 109 | + expect(mob.subdoc.foo).toBe('bar'); |
| 110 | + expect(mob.subdoc.wu).toBe('clan'); |
| 111 | + done(); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('handles array, object, date', (done) => { |
| 116 | + let adapter = new MongoStorageAdapter({ uri: databaseURI }); |
| 117 | + let obj = { |
| 118 | + array: [1, 2, 3], |
| 119 | + object: {foo: 'bar'}, |
| 120 | + date: { |
| 121 | + __type: 'Date', |
| 122 | + iso: '2016-05-26T20:55:01.154Z', |
| 123 | + }, |
| 124 | + }; |
| 125 | + let schema = { fields: { |
| 126 | + array: { type: 'Array' }, |
| 127 | + object: { type: 'Object' }, |
| 128 | + date: { type: 'Date' }, |
| 129 | + } }; |
| 130 | + adapter.createObject('MyClass', obj, schema) |
| 131 | + .then(() => adapter._rawFind('MyClass', {})) |
| 132 | + .then(results => { |
| 133 | + expect(results.length).toEqual(1); |
| 134 | + let mob = results[0]; |
| 135 | + expect(mob.array instanceof Array).toBe(true); |
| 136 | + expect(typeof mob.object).toBe('object'); |
| 137 | + expect(mob.date instanceof Date).toBe(true); |
| 138 | + return adapter.find('MyClass', {}, schema, {}); |
| 139 | + }) |
| 140 | + .then(results => { |
| 141 | + expect(results.length).toEqual(1); |
| 142 | + let mob = results[0]; |
| 143 | + expect(mob.array instanceof Array).toBe(true); |
| 144 | + expect(typeof mob.object).toBe('object'); |
| 145 | + expect(mob.date.__type).toBe('Date'); |
| 146 | + expect(mob.date.iso).toBe('2016-05-26T20:55:01.154Z'); |
| 147 | + done(); |
| 148 | + }) |
| 149 | + .catch(error => { |
| 150 | + console.log(error); |
| 151 | + fail(); |
| 152 | + done(); |
| 153 | + }); |
| 154 | + }); |
40 | 155 | });
|
0 commit comments