33const assert = require ( 'assert' ) ;
44const start = require ( './common' ) ;
55const BSON = require ( 'bson' ) ;
6+ const sinon = require ( 'sinon' ) ;
67
78const mongoose = start . mongoose ;
89const Schema = mongoose . Schema ;
910
1011const INT32_MAX = 0x7FFFFFFF ;
1112const INT32_MIN = - 0x80000000 ;
1213
13- describe ( 'Int32' , function ( ) {
14+ describe . only ( 'Int32' , function ( ) {
1415 beforeEach ( ( ) => mongoose . deleteModel ( / T e s t / ) ) ;
1516
1617 it ( 'is a valid schema type' , function ( ) {
@@ -27,7 +28,7 @@ describe('Int32', function() {
2728 } ) ;
2829
2930 describe ( 'supports the required property' , function ( ) {
30- it ( 'when vaglue is null' , async function ( ) {
31+ it ( 'when value is null' , async function ( ) {
3132 const schema = new Schema ( {
3233 int32 : {
3334 type : Schema . Types . Int32 ,
@@ -141,7 +142,7 @@ describe('Int32', function() {
141142 const Test = mongoose . model ( 'Test' , schema ) ;
142143
143144 const doc = new Test ( {
144- myInt : - 42
145+ myInt : ' -42'
145146 } ) ;
146147 assert . strictEqual ( doc . myInt , - 42 ) ;
147148 } ) ;
@@ -182,16 +183,40 @@ describe('Int32', function() {
182183 assert . strictEqual ( doc . myInt , - 997 ) ;
183184 } ) ;
184185
185- it ( 'casts from BSON.Long provided its value is within bounds of Int32 ', function ( ) {
186- const schema = new Schema ( {
187- myInt : Schema . Types . Int32
186+ describe ( 'long ', function ( ) {
187+ after ( function ( ) {
188+ sinon . restore ( ) ;
188189 } ) ;
189- const Test = mongoose . model ( 'Test' , schema ) ;
190190
191- const doc = new Test ( {
192- myInt : BSON . Long . fromNumber ( - 997 )
191+ it ( 'casts from BSON.Long provided its value is within bounds of Int32' , function ( ) {
192+ const schema = new Schema ( {
193+ myInt : Schema . Types . Int32
194+ } ) ;
195+ const Test = mongoose . model ( 'Test' , schema ) ;
196+
197+ const doc = new Test ( {
198+ myInt : BSON . Long . fromNumber ( - 997 )
199+ } ) ;
200+ assert . strictEqual ( doc . myInt , - 997 ) ;
201+ } ) ;
202+
203+ it ( 'calls Long.toNumber when casting long' , function ( ) {
204+ // this is a perf optimization, since long.toNumber() is faster than Number(long)
205+ const schema = new Schema ( {
206+ myInt : Schema . Types . Int32
207+ } ) ;
208+ const Test = mongoose . model ( 'Test' , schema ) ;
209+
210+ sinon . stub ( BSON . Long . prototype , 'toNumber' ) . callsFake ( function ( ) {
211+ return 2 ;
212+ } ) ;
213+
214+ const doc = new Test ( {
215+ myInt : BSON . Long . fromNumber ( - 997 )
216+ } ) ;
217+
218+ assert . strictEqual ( doc . myInt , 2 ) ;
193219 } ) ;
194- assert . strictEqual ( doc . myInt , - 997 ) ;
195220 } ) ;
196221
197222 it ( 'casts from BSON.Double provided its value is an integer' , function ( ) {
0 commit comments