UNPKG

4.43 kBJavaScriptView Raw
1var assert = require('assert')
2var LongModule = require('../')
3var mongoose = require('mongoose')
4var Schema = mongoose.Schema;
5var Long;
6
7describe('Long', function(){
8 before(function(){
9 Long = LongModule(mongoose);
10 })
11
12 it('is a function', function(){
13 assert.equal('function', typeof Long);
14 })
15
16 it('extends mongoose.Schema.Types', function(){
17 assert.ok(Schema.Types.Long);
18 assert.equal(Long, Schema.Types.Long);
19 })
20
21 it('extends mongoose.Types', function(){
22 assert.ok(mongoose.Types.Long);
23 assert.equal(mongoose.mongo.Long, mongoose.Types.Long);
24 })
25
26 it('can be used in schemas', function(){
27 var s = new Schema({ long: Long });
28 var long = s.path('long')
29 assert.ok(long instanceof mongoose.SchemaType);
30 assert.equal('function', typeof long.get);
31
32 var s = new Schema({ long: 'long' });
33 var long = s.path('long')
34 assert.ok(long instanceof mongoose.SchemaType);
35 assert.equal('function', typeof long.get);
36 })
37
38 describe('integration', function(){
39 var db, S, schema, id;
40
41 before(function(done){
42 db = mongoose.createConnection('mongodb://localhost:27017/mlong');
43 db.once('open', function () {
44 schema = new Schema({ long: Long, name: 'string' });
45 S = db.model('Long', schema);
46 done();
47 });
48 })
49
50 describe('casts', function(){
51 it('numbers', function(){
52 var v = 200000000;
53 var s = new S({ long: v });
54 assert.ok(s.long instanceof mongoose.Types.Long);
55 assert.equal(v, s.long.toNumber());
56
57 v = new Number(200000000);
58 s = new S({ long: v });
59 assert.ok(s.long instanceof mongoose.Types.Long);
60 assert.equal(+v, s.long.toNumber());
61 })
62
63 it('strings', function(){
64 var v = '200000000';
65 var s = new S({ long: v});
66 assert.ok(s.long instanceof mongoose.Types.Long);
67 assert.equal(v, s.long.toString());
68 })
69
70 it('null', function(){
71 var s = new S({ long: null });
72 assert.equal(null, s.long);
73 })
74
75 it('mongo.Long', function(){
76 var s = new S({ long: new mongoose.Types.Long("90") });
77 assert.ok(s.long instanceof mongoose.Types.Long);
78 assert.equal(90, s.long.toNumber());
79 })
80
81 it('non-castables produce _saveErrors', function(done){
82 var schema = new Schema({ long: Long }, { strict: 'throw' });
83 var M = db.model('throws', schema);
84 var m = new M({ long: [] });
85 m.save(function (err) {
86 assert.ok(err);
87 assert.equal('ValidationError', err.name);
88 assert.equal(err.errors['long'].name, 'CastError');
89 done();
90 });
91 })
92 })
93
94 it('can be saved', function(done){
95 var s = new S({ long: 20 });
96 id = s.id;
97 s.save(function (err) {
98 assert.ifError(err);
99 done();
100 })
101 })
102
103 it('is queryable', function(done){
104 S.findById(id, function (err, doc) {
105 assert.ifError(err);
106 assert.ok(doc.long instanceof mongoose.Types.Long);
107 assert.equal(20, doc.long.toNumber());
108 done();
109 });
110 })
111
112 it('can be updated', function(done){
113 S.findById(id, function (err, doc) {
114 assert.ifError(err);
115 doc.long = doc.long.add(mongoose.Types.Long.fromString("10"));
116 doc.save(function (err) {
117 assert.ifError(err);
118 S.findById(id, function (err, doc) {
119 assert.ifError(err);
120 assert.equal(30, doc.long.toNumber());
121 done();
122 });
123 })
124 })
125 })
126
127 it('can be required', function(done){
128 var s = new Schema({ long: { type: Long, required: true }});
129 var M = db.model('required', s);
130 var m = new M;
131 m.save(function (err) {
132 assert.ok(err);
133 m.long = 10;
134 m.validate(function (err) {
135 assert.ifError(err);
136 done();
137 })
138 })
139 })
140
141 it('works with update', function(done){
142 S.create({ long: 99999 }, function (err, s) {
143 assert.ifError(err);
144 S.update({ long: s.long, _id: s._id }, { name: 'changed' }, { upsert: true }, function (err) {
145 assert.ifError(err);
146
147 S.findById(s._id, function (err, doc) {
148 assert.ifError(err);
149 assert.equal(99999, doc.long);
150 assert.equal('changed', doc.name);
151 done();
152 })
153 });
154 });
155
156 })
157 })
158})