@@ -658,21 +658,67 @@ describe('manipulation', function() {
658658 } ) ;
659659
660660 describe ( 'updateOrCreate' , function ( ) {
661- var ds = getSchema ( ) ;
662- var Post ;
661+ var Post , Todo ;
663662
664- before ( 'prepare "Post" model ' , function ( done ) {
665- Post = ds . define ( 'Post' , {
663+ before ( 'prepare "Post" and "Todo" models ' , function ( done ) {
664+ Post = db . define ( 'Post' , {
666665 title : { type : String , id : true } ,
667666 content : { type : String } ,
668667 } ) ;
669- ds . automigrate ( 'Post' , done ) ;
668+ Todo = db . define ( 'Todo' , {
669+ content : String ,
670+ } ) ;
671+ db . automigrate ( [ 'Post' , 'Todo' ] , done ) ;
672+ } ) ;
673+
674+ beforeEach ( function deleteModelsInstances ( done ) {
675+ Todo . deleteAll ( done ) ;
670676 } ) ;
671677
672678 it ( 'has an alias "patchOrCreate"' , function ( ) {
673679 StubUser . updateOrCreate . should . equal ( StubUser . patchOrCreate ) ;
674680 } ) ;
675681
682+ it ( 'creates a model when one does not exist' , function ( done ) {
683+ Todo . updateOrCreate ( { content : 'a' } , function ( err , data ) {
684+ if ( err ) return done ( err ) ;
685+
686+ Todo . findById ( data . id , function ( err , todo ) {
687+ should . exist ( todo ) ;
688+ should . exist ( todo . content ) ;
689+ todo . content . should . equal ( 'a' ) ;
690+
691+ done ( ) ;
692+ } ) ;
693+ } ) ;
694+ } ) ;
695+
696+ it ( 'updates a model if it exists' , function ( done ) {
697+ Todo . create ( { content : 'a' } , function ( err , todo ) {
698+ Todo . updateOrCreate ( { id : todo . id , content : 'b' } , function ( err , data ) {
699+ if ( err ) return done ( err ) ;
700+
701+ should . exist ( data ) ;
702+ should . exist ( data . id ) ;
703+ data . id . should . equal ( todo . id ) ;
704+ should . exist ( data . content ) ;
705+ data . content . should . equal ( 'b' ) ;
706+
707+ done ( ) ;
708+ } ) ;
709+ } ) ;
710+ } ) ;
711+
712+ it ( 'throws error for queries with array input' , function ( done ) {
713+ Todo . updateOrCreate ( [ { content : 'a' } ] , function ( err , data ) {
714+ should . exist ( err ) ;
715+ err . message . should . containEql ( 'bulk' ) ;
716+ should . not . exist ( data ) ;
717+
718+ done ( ) ;
719+ } ) ;
720+ } ) ;
721+
676722 it ( 'should preserve properties with dynamic setters on create' , function ( done ) {
677723 StubUser . updateOrCreate ( { password : 'foo' } , function ( err , created ) {
678724 if ( err ) return done ( err ) ;
0 commit comments