aboutsummaryrefslogtreecommitdiff
path: root/node_modules/mongo-factory/test/test.js
blob: 9ac91c303357a7585482f0afcd8b13d75264b859 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var chai = require('chai');
var expect = chai.expect;
var mongo = require('mongodb');

describe('mongoFactory', function() {
  var mongoFactory;

  beforeEach(function() {
    mongoFactory = require('../');
  });

  it('can be required without blowing up', function(done) {
    var mongoFactory = require('../');
    expect(mongoFactory).to.exist;
    done();
  });

  describe('getConnection()', function() {
    describe('with no parameters', function() {
      it('should fulfill the promise', function(done) {
        var conn = mongoFactory.getConnection();
        expect(conn).to.be.fulfilled;
        done();
      });

      it('should reject the promise', function(done) {
        var conn = mongoFactory.getConnection();
        expect(conn).to.be.rejected;
        done();
      });
     });

    describe('with a null parameter', function() {
      it('should reject the promise', function(done) {
        var conn = mongoFactory.getConnection(null);
        expect(conn).to.be.rejected;
        done();
      });
    });

    describe('with a valid mongo string parameter', function() {
      describe('needing to create the pool the first time', function() {
        it('should return a fulfilled promise', function(done) {
          var conn = mongoFactory.getConnection('mongodb://localhost:27017').then(function(db) {
            expect(conn).to.be.fulfilled;
            expect(conn).to.not.be.rejected;
            done();
          });
        });
      });

      describe('after a pool is already instantiated', function() {
        it('should return a fulfilled promise', function(done) {
          var conn1 = mongoFactory.getConnection('mongodb://localhost:27017').then(function() {
            var conn = mongoFactory.getConnection('mongodb://localhost:27017').then(function() {
              expect(conn).to.be.fulfilled;
              expect(conn).to.not.be.rejected;
              done();
            });
          });
        });
      });
    });
  });

  describe('ObjectID', function() {
    it('should return mongo\'s ObjectID method', function() {
      var oid = mongoFactory.ObjectID;
      expect(oid).to.be.a.function;
    });

    it('should allow you to create a new ObjectID', function() {
      var oid = new mongoFactory.ObjectID;
      expect(oid.toHexString().length).to.equal(24);
    });

    it('should allow you to compare ObjectIDs', function() {
      var oid1 = new mongoFactory.ObjectID;
      var oid2 = new mongoFactory.ObjectID(oid1.id);
      var oid3 = new mongoFactory.ObjectID;
      expect(oid1.equals(oid2));
      expect(!oid1.equals(oid3));
    });

    it('should allow you to create a new ObjectID with a specific timestamp', function() {
      // Get a timestamp in seconds
      var timestamp = Math.floor(new Date().getTime()/1000);
      // Create a date with the timestamp
      var timestampDate = new Date(timestamp*1000);
      var oid = new mongoFactory.ObjectID(timestamp);
      expect(oid.getTimestamp().toString()).to.equal(timestampDate.toString());
    });
  });
});