BVB Source Codes

parse-server Show MongoTransform.spec.js Source code

Return Download parse-server: download MongoTransform.spec.js Source code - Download parse-server Source code - Type:.js
  1. // These tests are unit tests designed to only test transform.js.
  2. "use strict";
  3.  
  4. const transform = require('../src/Adapters/Storage/Mongo/MongoTransform');
  5. const dd = require('deep-diff');
  6. const mongodb = require('mongodb');
  7.  
  8. describe('parseObjectToMongoObjectForCreate', () => {
  9.   it('a basic number', (done) => {
  10.     var input = {five: 5};
  11.     var output = transform.parseObjectToMongoObjectForCreate(null, input, {
  12.       fields: {five: {type: 'Number'}}
  13.     });
  14.     jequal(input, output);
  15.     done();
  16.   });
  17.  
  18.   it('an object with null values', (done) => {
  19.     var input = {objectWithNullValues: {isNull: null, notNull: 3}};
  20.     var output = transform.parseObjectToMongoObjectForCreate(null, input, {
  21.       fields: {objectWithNullValues: {type: 'object'}}
  22.     });
  23.     jequal(input, output);
  24.     done();
  25.   });
  26.  
  27.   it('built-in timestamps', (done) => {
  28.     var input = {
  29.       createdAt: "2015-10-06T21:24:50.332Z",
  30.       updatedAt: "2015-10-06T21:24:50.332Z"
  31.     };
  32.     var output = transform.parseObjectToMongoObjectForCreate(null, input, { fields: {} });
  33.     expect(output._created_at instanceof Date).toBe(true);
  34.     expect(output._updated_at instanceof Date).toBe(true);
  35.     done();
  36.   });
  37.  
  38.   it('array of pointers', (done) => {
  39.     var pointer = {
  40.       __type: 'Pointer',
  41.       objectId: 'myId',
  42.       className: 'Blah',
  43.     };
  44.     var out = transform.parseObjectToMongoObjectForCreate(null, {pointers: [pointer]},{
  45.       fields: {pointers: {type: 'Array'}}
  46.     });
  47.     jequal([pointer], out.pointers);
  48.     done();
  49.   });
  50.  
  51.   //TODO: object creation requests shouldn't be seeing __op delete, it makes no sense to
  52.   //have __op delete in a new object. Figure out what this should actually be testing.
  53.   xit('a delete op', (done) => {
  54.     var input = {deleteMe: {__op: 'Delete'}};
  55.     var output = transform.parseObjectToMongoObjectForCreate(null, input, { fields: {} });
  56.     jequal(output, {});
  57.     done();
  58.   });
  59.  
  60.   it('Doesnt allow ACL, as Parse Server should tranform ACL to _wperm + _rperm', done => {
  61.     var input = {ACL: {'0123': {'read': true, 'write': true}}};
  62.     expect(() => transform.parseObjectToMongoObjectForCreate(null, input, { fields: {} })).toThrow();
  63.     done();
  64.   });
  65.  
  66.   it('plain', (done) => {
  67.     var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
  68.     var out = transform.parseObjectToMongoObjectForCreate(null, {location: geoPoint},{
  69.       fields: {location: {type: 'GeoPoint'}}
  70.     });
  71.     expect(out.location).toEqual([180, -180]);
  72.     done();
  73.   });
  74.  
  75.   it('in array', (done) => {
  76.     var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
  77.     var out = transform.parseObjectToMongoObjectForCreate(null, {locations: [geoPoint, geoPoint]},{
  78.       fields: {locations: {type: 'Array'}}
  79.     });
  80.     expect(out.locations).toEqual([geoPoint, geoPoint]);
  81.     done();
  82.   });
  83.  
  84.   it('in sub-object', (done) => {
  85.     var geoPoint = {__type: 'GeoPoint', longitude: 180, latitude: -180};
  86.     var out = transform.parseObjectToMongoObjectForCreate(null, { locations: { start: geoPoint }},{
  87.       fields: {locations: {type: 'Object'}}
  88.     });
  89.     expect(out).toEqual({ locations: { start: geoPoint } });
  90.     done();
  91.   });
  92.  
  93.   it('objectId', (done) => {
  94.     var out = transform.transformWhere(null, {objectId: 'foo'});
  95.     expect(out._id).toEqual('foo');
  96.     done();
  97.   });
  98.  
  99.   it('objectId in a list', (done) => {
  100.     var input = {
  101.       objectId: {'$in': ['one', 'two', 'three']},
  102.     };
  103.     var output = transform.transformWhere(null, input);
  104.     jequal(input.objectId, output._id);
  105.     done();
  106.   });
  107.  
  108.   it('built-in timestamps', (done) => {
  109.     var input = {createdAt: new Date(), updatedAt: new Date()};
  110.     var output = transform.mongoObjectToParseObject(null, input, { fields: {} });
  111.     expect(typeof output.createdAt).toEqual('string');
  112.     expect(typeof output.updatedAt).toEqual('string');
  113.     done();
  114.   });
  115.  
  116.   it('pointer', (done) => {
  117.     var input = {_p_userPointer: '_User$123'};
  118.     var output = transform.mongoObjectToParseObject(null, input, {
  119.       fields: { userPointer: { type: 'Pointer', targetClass: '_User' } },
  120.     });
  121.     expect(typeof output.userPointer).toEqual('object');
  122.     expect(output.userPointer).toEqual(
  123.       {__type: 'Pointer', className: '_User', objectId: '123'}
  124.     );
  125.     done();
  126.   });
  127.  
  128.   it('null pointer', (done) => {
  129.     var input = {_p_userPointer: null};
  130.     var output = transform.mongoObjectToParseObject(null, input, {
  131.       fields: { userPointer: { type: 'Pointer', targetClass: '_User' } },
  132.     });
  133.     expect(output.userPointer).toBeUndefined();
  134.     done();
  135.   });
  136.  
  137.   it('file', (done) => {
  138.     var input = {picture: 'pic.jpg'};
  139.     var output = transform.mongoObjectToParseObject(null, input, {
  140.       fields: { picture: { type: 'File' }},
  141.     });
  142.     expect(typeof output.picture).toEqual('object');
  143.     expect(output.picture).toEqual({__type: 'File', name: 'pic.jpg'});
  144.     done();
  145.   });
  146.  
  147.   it('geopoint', (done) => {
  148.     var input = {location: [180, -180]};
  149.     var output = transform.mongoObjectToParseObject(null, input, {
  150.       fields: { location: { type: 'GeoPoint' }},
  151.     });
  152.     expect(typeof output.location).toEqual('object');
  153.     expect(output.location).toEqual(
  154.       {__type: 'GeoPoint', longitude: 180, latitude: -180}
  155.     );
  156.     done();
  157.   });
  158.  
  159.   it('bytes', (done) => {
  160.     var input = {binaryData: "aGVsbG8gd29ybGQ="};
  161.     var output = transform.mongoObjectToParseObject(null, input, {
  162.       fields: { binaryData: { type: 'Bytes' }},
  163.     });
  164.     expect(typeof output.binaryData).toEqual('object');
  165.     expect(output.binaryData).toEqual(
  166.       {__type: 'Bytes', base64: "aGVsbG8gd29ybGQ="}
  167.     );
  168.     done();
  169.   });
  170.  
  171.   it('nested array', (done) => {
  172.     var input = {arr: [{_testKey: 'testValue' }]};
  173.     var output = transform.mongoObjectToParseObject(null, input, {
  174.       fields: { arr: { type: 'Array' } },
  175.     });
  176.     expect(Array.isArray(output.arr)).toEqual(true);
  177.     expect(output.arr).toEqual([{ _testKey: 'testValue'}]);
  178.     done();
  179.   });
  180.  
  181.   it('untransforms objects containing nested special keys', done => {
  182.     const input = {array: [{
  183.       _id: "Test ID",
  184.       _hashed_password: "I Don't know why you would name a key this, but if you do it should work",
  185.       _tombstone: {
  186.         _updated_at: "I'm sure people will nest keys like this",
  187.         _acl: 7,
  188.         _id: { someString: "str", someNumber: 7},
  189.         regularKey: { moreContents: [1, 2, 3] },
  190.       },
  191.       regularKey: "some data",
  192.     }]}
  193.     const output = transform.mongoObjectToParseObject(null, input, {
  194.       fields: { array: { type: 'Array' }},
  195.     });
  196.     expect(dd(output, input)).toEqual(undefined);
  197.     done();
  198.   });
  199.  
  200.   it('changes new pointer key', (done) => {
  201.     var input = {
  202.       somePointer: {__type: 'Pointer', className: 'Micro', objectId: 'oft'}
  203.     };
  204.     var output = transform.parseObjectToMongoObjectForCreate(null, input, {
  205.       fields: {somePointer: {type: 'Pointer'}}
  206.     });
  207.     expect(typeof output._p_somePointer).toEqual('string');
  208.     expect(output._p_somePointer).toEqual('Micro$oft');
  209.     done();
  210.   });
  211.  
  212.   it('changes existing pointer keys', (done) => {
  213.     var input = {
  214.       userPointer: {__type: 'Pointer', className: '_User', objectId: 'qwerty'}
  215.     };
  216.     var output = transform.parseObjectToMongoObjectForCreate(null, input, {
  217.       fields: {userPointer: {type: 'Pointer'}}
  218.     });
  219.     expect(typeof output._p_userPointer).toEqual('string');
  220.     expect(output._p_userPointer).toEqual('_User$qwerty');
  221.     done();
  222.   });
  223.  
  224.   it('writes the old ACL format in addition to rperm and wperm on create', (done) => {
  225.     var input = {
  226.       _rperm: ['*'],
  227.       _wperm: ['Kevin'],
  228.     };
  229.  
  230.     var output = transform.parseObjectToMongoObjectForCreate(null, input, { fields: {} });
  231.     expect(typeof output._acl).toEqual('object');
  232.     expect(output._acl['Kevin'].w).toBeTruthy();
  233.     expect(output._acl['Kevin'].r).toBeUndefined();
  234.     expect(output._rperm).toEqual(input._rperm);
  235.     expect(output._wperm).toEqual(input._wperm);
  236.     done();
  237.   });
  238.  
  239.   it('removes Relation types', (done) => {
  240.     var input = {
  241.       aRelation: { __type: 'Relation', className: 'Stuff' },
  242.     };
  243.     var output = transform.parseObjectToMongoObjectForCreate(null, input, {
  244.       fields: {
  245.         aRelation: { __type: 'Relation', className: 'Stuff' },
  246.       },
  247.     });
  248.     expect(output).toEqual({});
  249.     done();
  250.   });
  251.  
  252.   it('writes the old ACL format in addition to rperm and wperm on update', (done) => {
  253.     var input = {
  254.       _rperm: ['*'],
  255.       _wperm: ['Kevin']
  256.     };
  257.  
  258.     var output = transform.transformUpdate(null, input, { fields: {} });
  259.     var set = output.$set;
  260.     expect(typeof set).toEqual('object');
  261.     expect(typeof set._acl).toEqual('object');
  262.     expect(set._acl['Kevin'].w).toBeTruthy();
  263.     expect(set._acl['Kevin'].r).toBeUndefined();
  264.     expect(set._rperm).toEqual(input._rperm);
  265.     expect(set._wperm).toEqual(input._wperm);
  266.     done();
  267.   });
  268.  
  269.   it('untransforms from _rperm and _wperm to ACL', (done) => {
  270.     var input = {
  271.       _rperm: ["*"],
  272.       _wperm: ["Kevin"]
  273.     };
  274.     var output = transform.mongoObjectToParseObject(null, input, { fields: {} });
  275.     expect(output._rperm).toEqual(['*']);
  276.     expect(output._wperm).toEqual(['Kevin']);
  277.     expect(output.ACL).toBeUndefined()
  278.     done();
  279.   });
  280.  
  281.   it('untransforms mongodb number types', (done) => {
  282.     var input = {
  283.       long: mongodb.Long.fromNumber(Number.MAX_SAFE_INTEGER),
  284.       double: new mongodb.Double(Number.MAX_VALUE)
  285.     }
  286.     var output = transform.mongoObjectToParseObject(null, input, {
  287.       fields: {
  288.         long: { type: 'Number' },
  289.         double: { type: 'Number' },
  290.       },
  291.     });
  292.     expect(output.long).toBe(Number.MAX_SAFE_INTEGER);
  293.     expect(output.double).toBe(Number.MAX_VALUE);
  294.     done();
  295.   });
  296.  
  297.   it('Date object where iso attribute is of type Date', (done) => {
  298.     var input = {
  299.       ts : { __type: 'Date', iso: new Date('2017-01-18T00:00:00.000Z') }
  300.     }
  301.     var output = transform.mongoObjectToParseObject(null, input, {
  302.       fields : {
  303.         ts : { type : 'Date' }
  304.       }
  305.     });
  306.     expect(output.ts.iso).toEqual('2017-01-18T00:00:00.000Z');
  307.     done();
  308.   });
  309.  
  310.   it('Date object where iso attribute is of type String', (done) => {
  311.     var input = {
  312.       ts : { __type: 'Date', iso: '2017-01-18T00:00:00.000Z' }
  313.     }
  314.     var output = transform.mongoObjectToParseObject(null, input, {
  315.       fields : {
  316.         ts : { type : 'Date' }
  317.       }
  318.     });
  319.     expect(output.ts.iso).toEqual('2017-01-18T00:00:00.000Z');
  320.     done();
  321.   });
  322. });
  323.  
  324. describe('transformUpdate', () => {
  325.   it('removes Relation types', (done) => {
  326.     var input = {
  327.       aRelation: { __type: 'Relation', className: 'Stuff' },
  328.     };
  329.     var output = transform.transformUpdate(null, input, {
  330.       fields: {
  331.         aRelation: { __type: 'Relation', className: 'Stuff' },
  332.       },
  333.     });
  334.     expect(output).toEqual({});
  335.     done();
  336.   });
  337. });
  338.  
downloadMongoTransform.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