1+ var _ = require ( 'lodash' ) ;
12var should = require ( 'should' ) ;
23var helper = require ( '../support/spec_helper' ) ;
34var ORM = require ( '../../' ) ;
@@ -291,61 +292,59 @@ describe("hasMany", function () {
291292 describe ( "addAccessor" , function ( ) {
292293 before ( setup ( ) ) ;
293294
294- if ( common . protocol ( ) == "mongodb" ) return ;
295-
296- it ( "might add duplicates" , function ( done ) {
297- Pet . find ( { name : "Mutt" } , function ( err , pets ) {
298- Person . find ( { name : "Jane" } , function ( err , people ) {
299- should . equal ( err , null ) ;
300-
301- people [ 0 ] . addPets ( pets [ 0 ] , function ( err ) {
295+ if ( common . protocol ( ) != "mongodb" ) {
296+ it ( "might add duplicates" , function ( done ) {
297+ Pet . find ( { name : "Mutt" } , function ( err , pets ) {
298+ Person . find ( { name : "Jane" } , function ( err , people ) {
302299 should . equal ( err , null ) ;
303300
304- people [ 0 ] . getPets ( "name" , function ( err , pets ) {
301+ people [ 0 ] . addPets ( pets [ 0 ] , function ( err ) {
305302 should . equal ( err , null ) ;
306303
307- should ( Array . isArray ( pets ) ) ;
308- pets . length . should . equal ( 2 ) ;
309- pets [ 0 ] . name . should . equal ( "Mutt" ) ;
310- pets [ 1 ] . name . should . equal ( "Mutt" ) ;
304+ people [ 0 ] . getPets ( "name" , function ( err , pets ) {
305+ should . equal ( err , null ) ;
311306
312- return done ( ) ;
307+ should ( Array . isArray ( pets ) ) ;
308+ pets . length . should . equal ( 2 ) ;
309+ pets [ 0 ] . name . should . equal ( "Mutt" ) ;
310+ pets [ 1 ] . name . should . equal ( "Mutt" ) ;
311+
312+ return done ( ) ;
313+ } ) ;
313314 } ) ;
314315 } ) ;
315316 } ) ;
316317 } ) ;
317- } ) ;
318- } ) ;
319-
320- describe ( "addAccessor" , function ( ) {
321- before ( setup ( ) ) ;
318+ }
322319
323320 it ( "should keep associations and add new ones" , function ( done ) {
324321 Pet . find ( { name : "Deco" } ) . first ( function ( err , Deco ) {
325322 Person . find ( { name : "Jane" } ) . first ( function ( err , Jane ) {
326323 should . equal ( err , null ) ;
327324
328- Jane . addPets ( Deco , function ( err ) {
329- should . equal ( err , null ) ;
325+ Jane . getPets ( function ( err , janesPets ) {
326+ should . not . exist ( err ) ;
327+
328+ var petsAtStart = janesPets . length ;
330329
331- Jane . getPets ( "name" , function ( err , pets ) {
330+ Jane . addPets ( Deco , function ( err ) {
332331 should . equal ( err , null ) ;
333332
334- should ( Array . isArray ( pets ) ) ;
335- pets . length . should . equal ( 2 ) ;
336- pets [ 0 ] . name . should . equal ( "Deco" ) ;
337- pets [ 1 ] . name . should . equal ( "Mutt" ) ;
333+ Jane . getPets ( "name" , function ( err , pets ) {
334+ should . equal ( err , null ) ;
338335
339- return done ( ) ;
336+ should ( Array . isArray ( pets ) ) ;
337+ pets . length . should . equal ( petsAtStart + 1 ) ;
338+ pets [ 0 ] . name . should . equal ( "Deco" ) ;
339+ pets [ 1 ] . name . should . equal ( "Mutt" ) ;
340+
341+ return done ( ) ;
342+ } ) ;
340343 } ) ;
341344 } ) ;
342345 } ) ;
343346 } ) ;
344347 } ) ;
345- } ) ;
346-
347- describe ( "addAccessor" , function ( ) {
348- before ( setup ( ) ) ;
349348
350349 it ( "should accept several arguments as associations" , function ( done ) {
351350 Pet . find ( function ( err , pets ) {
@@ -369,16 +368,14 @@ describe("hasMany", function () {
369368 } ) ;
370369
371370 it ( "should accept array as list of associations" , function ( done ) {
372- Pet . find ( function ( err , pets ) {
373- var petCount = pets . length ;
374-
371+ Pet . create ( [ { name : 'Ruff' } , { name : 'Spotty' } ] , function ( err , pets ) {
375372 Person . find ( { name : "Justin" } ) . first ( function ( err , Justin ) {
376373 should . equal ( err , null ) ;
377374
378375 Justin . getPets ( function ( err , justinsPets ) {
379376 should . equal ( err , null ) ;
380377
381- should . equal ( justinsPets . length , 2 ) ;
378+ var petCount = justinsPets . length ;
382379
383380 Justin . addPets ( pets , function ( err ) {
384381 should . equal ( err , null ) ;
@@ -387,7 +384,7 @@ describe("hasMany", function () {
387384 should . equal ( err , null ) ;
388385
389386 should ( Array . isArray ( justinsPets ) ) ;
390- // We're not checking uniqueness .
387+ // Mongo doesn't like adding duplicates here, so we add new ones .
391388 should . equal ( justinsPets . length , petCount + 2 ) ;
392389
393390 return done ( ) ;
0 commit comments