BVB Source Codes

parse-server Show MongoStorageAdapter.spec.js Source code

Return Download parse-server: download MongoStorageAdapter.spec.js Source code - Download parse-server Source code - Type:.js
  1. 'use strict';
  2.  
  3. const MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAdapter');
  4. const MongoClient = require('mongodb').MongoClient;
  5. const databaseURI = 'mongodb://localhost:27017/parseServerMongoAdapterTestDatabase';
  6.  
  7. // These tests are specific to the mongo storage adapter + mongo storage format
  8. // and will eventually be moved into their own repo
  9. describe_only_db('mongo')('MongoStorageAdapter', () => {
  10.   beforeEach(done => {
  11.     new MongoStorageAdapter({ uri: databaseURI })
  12.     .deleteAllClasses()
  13.     .then(done, fail);
  14.   });
  15.  
  16.   it('auto-escapes symbols in auth information', () => {
  17.     spyOn(MongoClient, 'connect').and.returnValue(Promise.resolve(null));
  18.     new MongoStorageAdapter({
  19.       uri: 'mongodb://user!with@+ symbols:password!with@+ symbols@localhost:1234/parse'
  20.     }).connect();
  21.     expect(MongoClient.connect).toHaveBeenCalledWith(
  22.       'mongodb://user!with%40%2B%20symbols:password!with%40%2B%20symbols@localhost:1234/parse',
  23.       jasmine.any(Object)
  24.     );
  25.   });
  26.  
  27.   it("doesn't double escape already URI-encoded information", () => {
  28.     spyOn(MongoClient, 'connect').and.returnValue(Promise.resolve(null));
  29.     new MongoStorageAdapter({
  30.       uri: 'mongodb://user!with%40%2B%20symbols:password!with%40%2B%20symbols@localhost:1234/parse'
  31.     }).connect();
  32.     expect(MongoClient.connect).toHaveBeenCalledWith(
  33.       'mongodb://user!with%40%2B%20symbols:password!with%40%2B%20symbols@localhost:1234/parse',
  34.       jasmine.any(Object)
  35.     );
  36.   });
  37.  
  38.   // https://github.com/ParsePlatform/parse-server/pull/148#issuecomment-180407057
  39.   it('preserves replica sets', () => {
  40.     spyOn(MongoClient, 'connect').and.returnValue(Promise.resolve(null));
  41.     new MongoStorageAdapter({
  42.       uri: 'mongodb://test:testpass@ds056315-a0.mongolab.com:59325,ds059315-a1.mongolab.com:59315/testDBname?replicaSet=rs-ds059415'
  43.     }).connect();
  44.     expect(MongoClient.connect).toHaveBeenCalledWith(
  45.       'mongodb://test:testpass@ds056315-a0.mongolab.com:59325,ds059315-a1.mongolab.com:59315/testDBname?replicaSet=rs-ds059415',
  46.       jasmine.any(Object)
  47.     );
  48.   });
  49.  
  50.   it('stores objectId in _id', done => {
  51.     const adapter = new MongoStorageAdapter({ uri: databaseURI });
  52.     adapter.createObject('Foo', { fields: {} }, { objectId: 'abcde' })
  53.     .then(() => adapter._rawFind('Foo', {}))
  54.     .then(results => {
  55.       expect(results.length).toEqual(1);
  56.       var obj = results[0];
  57.       expect(obj._id).toEqual('abcde');
  58.       expect(obj.objectId).toBeUndefined();
  59.       done();
  60.     });
  61.   });
  62.  
  63.   it('find succeeds when query is within maxTimeMS', (done) => {
  64.     const maxTimeMS = 250;
  65.     const adapter = new MongoStorageAdapter({
  66.       uri: databaseURI,
  67.       mongoOptions: { maxTimeMS },
  68.     });
  69.     adapter.createObject('Foo', { fields: {} }, { objectId: 'abcde' })
  70.     .then(() => adapter._rawFind('Foo', { '$where': `sleep(${maxTimeMS / 2})` }))
  71.     .then(
  72.       () => done(),
  73.       (err) => {
  74.         done.fail(`maxTimeMS should not affect fast queries ${err}`);
  75.       }
  76.     );
  77.   })
  78.  
  79.   it('find fails when query exceeds maxTimeMS', (done) => {
  80.     const maxTimeMS = 250;
  81.     const adapter = new MongoStorageAdapter({
  82.       uri: databaseURI,
  83.       mongoOptions: { maxTimeMS },
  84.     });
  85.     adapter.createObject('Foo', { fields: {} }, { objectId: 'abcde' })
  86.     .then(() => adapter._rawFind('Foo', { '$where': `sleep(${maxTimeMS * 2})` }))
  87.     .then(
  88.       () => {
  89.         done.fail('Find succeeded despite taking too long!');
  90.       },
  91.       (err) => {
  92.         expect(err.name).toEqual('MongoError');
  93.         expect(err.code).toEqual(50);
  94.         expect(err.message).toEqual('operation exceeded time limit');
  95.         done();
  96.       }
  97.     );
  98.   });
  99.  
  100.   it('stores pointers with a _p_ prefix', (done) => {
  101.     const obj = {
  102.       objectId: 'bar',
  103.       aPointer: {
  104.         __type: 'Pointer',
  105.         className: 'JustThePointer',
  106.         objectId: 'qwerty'
  107.       }
  108.     };
  109.     const adapter = new MongoStorageAdapter({ uri: databaseURI });
  110.     adapter.createObject('APointerDarkly', { fields: {
  111.       objectId: { type: 'String' },
  112.       aPointer: { type: 'Pointer', targetClass: 'JustThePointer' },
  113.     }}, obj)
  114.     .then(() => adapter._rawFind('APointerDarkly', {}))
  115.     .then(results => {
  116.       expect(results.length).toEqual(1);
  117.       const output = results[0];
  118.       expect(typeof output._id).toEqual('string');
  119.       expect(typeof output._p_aPointer).toEqual('string');
  120.       expect(output._p_aPointer).toEqual('JustThePointer$qwerty');
  121.       expect(output.aPointer).toBeUndefined();
  122.       done();
  123.     });
  124.   });
  125.  
  126.   it('handles object and subdocument', done => {
  127.     const adapter = new MongoStorageAdapter({ uri: databaseURI });
  128.     const schema = { fields : { subdoc: { type: 'Object' } } };
  129.     const obj = { subdoc: {foo: 'bar', wu: 'tan'} };
  130.     adapter.createObject('MyClass', schema, obj)
  131.     .then(() => adapter._rawFind('MyClass', {}))
  132.     .then(results => {
  133.       expect(results.length).toEqual(1);
  134.       const mob = results[0];
  135.       expect(typeof mob.subdoc).toBe('object');
  136.       expect(mob.subdoc.foo).toBe('bar');
  137.       expect(mob.subdoc.wu).toBe('tan');
  138.       const obj = { 'subdoc.wu': 'clan' };
  139.       return adapter.findOneAndUpdate('MyClass', schema, {}, obj);
  140.     })
  141.     .then(() => adapter._rawFind('MyClass', {}))
  142.     .then(results => {
  143.       expect(results.length).toEqual(1);
  144.       const mob = results[0];
  145.       expect(typeof mob.subdoc).toBe('object');
  146.       expect(mob.subdoc.foo).toBe('bar');
  147.       expect(mob.subdoc.wu).toBe('clan');
  148.       done();
  149.     });
  150.   });
  151.  
  152.   it('handles creating an array, object, date', (done) => {
  153.     const adapter = new MongoStorageAdapter({ uri: databaseURI });
  154.     const obj = {
  155.       array: [1, 2, 3],
  156.       object: {foo: 'bar'},
  157.       date: {
  158.         __type: 'Date',
  159.         iso: '2016-05-26T20:55:01.154Z',
  160.       },
  161.     };
  162.     const schema = { fields: {
  163.       array: { type: 'Array' },
  164.       object: { type: 'Object' },
  165.       date: { type: 'Date' },
  166.     } };
  167.     adapter.createObject('MyClass', schema, obj)
  168.     .then(() => adapter._rawFind('MyClass', {}))
  169.     .then(results => {
  170.       expect(results.length).toEqual(1);
  171.       const mob = results[0];
  172.       expect(mob.array instanceof Array).toBe(true);
  173.       expect(typeof mob.object).toBe('object');
  174.       expect(mob.date instanceof Date).toBe(true);
  175.       return adapter.find('MyClass', schema, {}, {});
  176.     })
  177.     .then(results => {
  178.       expect(results.length).toEqual(1);
  179.       const mob = results[0];
  180.       expect(mob.array instanceof Array).toBe(true);
  181.       expect(typeof mob.object).toBe('object');
  182.       expect(mob.date.__type).toBe('Date');
  183.       expect(mob.date.iso).toBe('2016-05-26T20:55:01.154Z');
  184.       done();
  185.     })
  186.     .catch(error => {
  187.       console.log(error);
  188.       fail();
  189.       done();
  190.     });
  191.   });
  192.  
  193.   it("handles updating a single object with array, object date", (done) => {
  194.     const adapter = new MongoStorageAdapter({ uri: databaseURI });
  195.  
  196.     const schema = { fields: {
  197.       array: { type: 'Array' },
  198.       object: { type: 'Object' },
  199.       date: { type: 'Date' },
  200.     } };
  201.  
  202.  
  203.     adapter.createObject('MyClass', schema, {})
  204.     .then(() => adapter._rawFind('MyClass', {}))
  205.     .then(results => {
  206.       expect(results.length).toEqual(1);
  207.       const update = {
  208.         array: [1, 2, 3],
  209.         object: {foo: 'bar'},
  210.         date: {
  211.           __type: 'Date',
  212.           iso: '2016-05-26T20:55:01.154Z',
  213.         },
  214.       };
  215.       const query = {};
  216.       return adapter.findOneAndUpdate('MyClass', schema, query, update)
  217.     })
  218.     .then(results => {
  219.       const mob = results;
  220.       expect(mob.array instanceof Array).toBe(true);
  221.       expect(typeof mob.object).toBe('object');
  222.       expect(mob.date.__type).toBe('Date');
  223.       expect(mob.date.iso).toBe('2016-05-26T20:55:01.154Z');
  224.       return adapter._rawFind('MyClass', {});
  225.     })
  226.     .then(results => {
  227.       expect(results.length).toEqual(1);
  228.       const mob = results[0];
  229.       expect(mob.array instanceof Array).toBe(true);
  230.       expect(typeof mob.object).toBe('object');
  231.       expect(mob.date instanceof Date).toBe(true);
  232.       done();
  233.     })
  234.     .catch(error => {
  235.       console.log(error);
  236.       fail();
  237.       done();
  238.     });
  239.   });
  240. });
  241.  
downloadMongoStorageAdapter.spec.js Source code - Download parse-server Source code
Related Source Codes/Software:
react-boilerplate - 2017-06-07
webtorrent - Streaming torrent client for the web ... 2017-06-06
machine-learning-for-software-engineers - A complete daily plan for studying to become a mac... 2017-06-06
upterm - A terminal emulator for the 21st century. 2017-06-06
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
storybook - 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top