BVB Source Codes

mongo-express Show bsonSpec.js Source code

Return Download mongo-express: download bsonSpec.js Source code - Download mongo-express Source code - Type:.js
  1. 'use strict';
  2.  
  3. var expect = require('chai').expect;
  4. var mongodb = require('mongodb');
  5.  
  6. var bson = require('../../lib/bson');
  7.  
  8. describe('BSON', function () {
  9.   describe('toBSON', function () {
  10.     it('should convert documents with native JS types', function () {
  11.       var test = '{test: true, "s": "hello", i: -99.44}';
  12.       var result = bson.toBSON(test);
  13.  
  14.       expect(result).to.eql({
  15.         test: true,
  16.         s: 'hello',
  17.         i: -99.44,
  18.       });
  19.     });
  20.  
  21.     it('should convert ObjectID to BSON', function () {
  22.       var test = '{_id: ObjectID(), id2: ObjectId()}';
  23.       var result = bson.toBSON(test);
  24.  
  25.       expect(result).to.have.property('_id').to.be.an.instanceof(mongodb.ObjectID);
  26.       expect(result).to.have.property('id2').to.be.an.instanceof(mongodb.ObjectID);
  27.     });
  28.  
  29.     it('should convert ISODate to BSON', function () {
  30.       var test = '{date: ISODate(), date2: new Date()}';
  31.       var result = bson.toBSON(test);
  32.  
  33.       expect(result).to.have.property('date').to.be.an.instanceof(Date);
  34.       expect(result).to.have.property('date2').to.be.an.instanceof(Date);
  35.     });
  36.  
  37.     it('should convert Timestamp to BSON', function () {
  38.       var test = '{ts: Timestamp()}';
  39.       var result = bson.toBSON(test);
  40.  
  41.       expect(result).to.have.property('ts').to.be.an.instanceof(mongodb.Timestamp);
  42.     });
  43.  
  44.     it('should convert DBRef to BSON', function () {
  45.       // eslint-disable-next-line max-len
  46.       var test = '{ref: DBRef("coll", "579e18580bddc20700502419"), ref2: DBRef("coll", "579e18580bddc20700502419", "db"), ref3: Dbref("coll", "579e18580bddc20700502419", "")}';
  47.       var result = bson.toBSON(test);
  48.  
  49.       expect(result).to.have.property('ref').to.be.an.instanceof(mongodb.DBRef);
  50.       expect(result).to.have.property('ref').to.have.property('namespace', 'coll');
  51.       expect(result).to.have.property('ref').to.have.property('oid').to.be.an.instanceof(mongodb.ObjectID);
  52.       expect(result).to.have.property('ref').to.have.property('oid').eql(mongodb.ObjectID('579e18580bddc20700502419'));
  53.       expect(result).to.have.property('ref').to.have.property('db', '');
  54.  
  55.       expect(result).to.have.property('ref2').to.be.an.instanceof(mongodb.DBRef);
  56.       expect(result).to.have.property('ref2').to.have.property('db', 'db');
  57.  
  58.       expect(result).to.have.property('ref3').to.be.an.instanceof(mongodb.DBRef);
  59.     });
  60.  
  61.     it('should convert Symbol to BSON', function () {
  62.       var test = '{symbol: Symbol("test")}';
  63.       var result = bson.toBSON(test);
  64.  
  65.       expect(result).to.have.property('symbol').to.be.an.instanceof(mongodb.Symbol);
  66.     });
  67.  
  68.     it('should convert MinKey to BSON', function () {
  69.       var test = '{key: MinKey()}';
  70.       var result = bson.toBSON(test);
  71.  
  72.       expect(result).to.have.property('key').to.be.an.instanceof(mongodb.MinKey);
  73.     });
  74.  
  75.     it('should convert MaxKey to BSON', function () {
  76.       var test = '{key: MaxKey()}';
  77.       var result = bson.toBSON(test);
  78.  
  79.       expect(result).to.have.property('key').to.be.an.instanceof(mongodb.MaxKey);
  80.     });
  81.  
  82.     it('should convert Code to BSON', function () {
  83.       var test = '{code: Code(function() { x; }), code2: Code("function() { x; }")}';
  84.       var result = bson.toBSON(test);
  85.  
  86.       expect(result).to.have.property('code').to.be.an.instanceof(mongodb.Code);
  87.       expect(result).to.have.property('code2').to.be.an.instanceof(mongodb.Code);
  88.     });
  89.   });
  90.  
  91.   describe('toString', function () {
  92.     it('should convert simple documents', function () {
  93.       var test = {
  94.         bool: true,
  95.         str: 'string',
  96.         number: -678.53,
  97.         list: [
  98.           1,
  99.           2,
  100.           3,
  101.         ],
  102.       };
  103.       var result = bson.toString(test);
  104.       var test2 = bson.toBSON(result);
  105.  
  106.       expect(test).to.eql(test2);
  107.     });
  108.  
  109.     it('should convert ObjectID to string', function () {
  110.       var test = {
  111.         id: mongodb.ObjectID(),
  112.         id2: mongodb.ObjectID('4fb1299686a989240b000001'),
  113.       };
  114.       var result = bson.toString(test);
  115.       var test2 = bson.toBSON(result);
  116.  
  117.       expect(test).to.eql(test2);
  118.     });
  119.  
  120.     it('should convert ISODate to string', function () {
  121.       var test = {
  122.         date: new Date(),
  123.       };
  124.       var result = bson.toString(test);
  125.       var test2 = bson.toBSON(result);
  126.  
  127.       expect(test).to.eql(test2);
  128.     });
  129.  
  130.     it('should convert Timestamp to string', function () {
  131.       var test = {
  132.         ts: mongodb.Timestamp(),
  133.         ts2: mongodb.Timestamp(100, 100),
  134.       };
  135.       var result = bson.toString(test);
  136.       var test2 = bson.toBSON(result);
  137.  
  138.       expect(test).to.eql(test2);
  139.     });
  140.  
  141.     it('should convert DBRef to string', function () {
  142.       var test = {
  143.         ref: mongodb.DBRef('coll', mongodb.ObjectID('57b80f922128ccef64333288'), ''),
  144.         ref2: mongodb.DBRef('coll', mongodb.ObjectID('57b80f922128ccef64333288'), 'db'),
  145.       };
  146.       var result = bson.toString(test);
  147.       var test2 = bson.toBSON(result);
  148.  
  149.       expect(test).to.eql(test2);
  150.     });
  151.  
  152.     it('should convert Symbol to string', function () {
  153.       var test = { symbol: mongodb.Symbol('test') };
  154.       var result = bson.toString(test);
  155.       var test2 = bson.toBSON(result);
  156.  
  157.       expect(test).to.eql(test2);
  158.     });
  159.  
  160.     it('should convert MinKey to string', function () {
  161.       var test = { key: mongodb.MinKey() };
  162.       var result = bson.toString(test);
  163.       var test2 = bson.toBSON(result);
  164.  
  165.       expect(test).to.eql(test2);
  166.     });
  167.  
  168.     it('should convert MaxKey to string', function () {
  169.       var test = { key: mongodb.MaxKey() };
  170.       var result = bson.toString(test);
  171.       var test2 = bson.toBSON(result);
  172.  
  173.       expect(test).to.eql(test2);
  174.     });
  175.  
  176.     it('should convert Code to string', function () {
  177.       var test = {
  178.         code: mongodb.Code('function() { x; }'),
  179.       };
  180.       var result = bson.toString(test);
  181.       var test2 = bson.toBSON(result);
  182.  
  183.       expect(test).to.eql(test2);
  184.     });
  185.   });
  186.  
  187.   describe('toJsonString', function () {
  188.     it('should convert to a valid JSON string', function () {
  189.       const doc = {
  190.         dateObject: new Date(Date.UTC(2017, 1, 11)),
  191.         objectID: new mongodb.ObjectID('589f79826ea20d18e06b1c36'),
  192.         someValue: 'someValue',
  193.         nestedObject: {
  194.           level1: {
  195.             level2: 2,
  196.           },
  197.         },
  198.       };
  199.       const result = bson.toJsonString(doc);
  200.       const expected = '{"dateObject":"2017-02-11T00:00:00.000Z","objectID":{ "$oid": "589f79826ea20d18e06b1c36" },"someValue":"someValue","nestedObject":{"level1":{"level2":2}}}'; // eslint-disable-line max-len
  201.       expect(result).to.equal(expected);
  202.       const parsed = JSON.parse(result);
  203.       expect(parsed.someValue).to.equal(doc.someValue);
  204.     });
  205.  
  206.     it('shouldn\'t convert lone parenthesis to }', function () {
  207.       const doc = {
  208.         someString: '))))',
  209.       };
  210.       const result = bson.toJsonString(doc);
  211.       const expected = '{"someString":"))))"}';
  212.       expect(result).to.equal(expected);
  213.       const parsed = JSON.parse(result);
  214.       expect(parsed.someString).to.equal(doc.someString);
  215.     });
  216.   });
  217. });
  218.  
downloadbsonSpec.js Source code - Download mongo-express Source code
Related Source Codes/Software:
XCL-Charts - Android charting libraries (XCL-Charts is a free c... 2017-05-17
scrollMonitor - A simple and fast API to monitor elements as you s... 2017-05-16
XCDYouTubeKit - YouTube video player for iOS, tvOS and macOS 2017-05-16
rails-erd - Generate Entity-Relationship Diagrams for Rails ap... 2017-05-16
np - A better `npm publish` 2017-05-16
kaminari - A Scope & Engine based, clean, powerful, custo... 2017-06-04
python-fire - Python Fire is a library for automatically generat... 2017-06-04
Dapper - Dapper - a simple object mapper for .Net 2017-06-05
pkg - Package your Node.js project into an executable ... 2017-06-05
packer - Packer is a tool for creating identical machine im... 2017-06-05
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