From f389f07b52d331f5c96ed47d1ccd76f7fa4507d6 Mon Sep 17 00:00:00 2001 From: Steven Miers Date: Wed, 15 Jul 2015 19:33:10 -0500 Subject: [PATCH] VRFS-3338 : Tests for genre validation behavior. --- ruby/lib/jam_ruby/models/band.rb | 2 +- ruby/spec/jam_ruby/models/band_spec.rb | 30 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/ruby/lib/jam_ruby/models/band.rb b/ruby/lib/jam_ruby/models/band.rb index 500b7eab3..251a88e5e 100644 --- a/ruby/lib/jam_ruby/models/band.rb +++ b/ruby/lib/jam_ruby/models/band.rb @@ -205,7 +205,7 @@ module JamRuby band.concert_count = params[:concert_count] if params.has_key?(:concert_count) band.play_commitment = params[:play_commitment] if params.has_key?(:play_commitment) - if params[:validate_genres] + if params[:validate_genres] || params[:genres].present? # loop through each genre in the array and save to the db genres = [] params[:genres].each { |genre_id| genres << Genre.find(genre_id) } if params[:genres].present? diff --git a/ruby/spec/jam_ruby/models/band_spec.rb b/ruby/spec/jam_ruby/models/band_spec.rb index 9bbd30dd4..947602223 100644 --- a/ruby/spec/jam_ruby/models/band_spec.rb +++ b/ruby/spec/jam_ruby/models/band_spec.rb @@ -19,6 +19,17 @@ describe Band do } } + let(:band_params_no_genre) { + { + name: "The Band", + biography: "Biography", + city: 'Austin', + state: 'TX', + country: 'US', + validate_genres:true + } + } + describe 'with instruments' do it 'builds with instruments' do band.musician_instruments << FactoryGirl.build(:musician_instrument, player: band) @@ -46,6 +57,9 @@ describe Band do it "minimum genres" do new_band.save.should be_false new_band.errors[:genres].should == [ValidationMessages::BAND_GENRE_MINIMUM_NOT_MET] + + new_band.genres = Genre.first + new_band.save.should be_true end it "maximum genres" do @@ -56,6 +70,22 @@ describe Band do end describe "save" do + it "genres validate" do + band=Band.save(user, band_params_no_genre) + band.errors.any?.should be_true + band.errors[:genres].should == [ValidationMessages::BAND_GENRE_MINIMUM_NOT_MET] + + band = Band.save(user, band_params) + band.errors.any?.should be_false + + # Save again without a genre and make sure we get an error: + p = band_params_no_genre.clone + p[:id] = band.id + band = Band.save(user, p) + band.errors.any?.should be_true + band.errors[:genres].should == [ValidationMessages::BAND_GENRE_MINIMUM_NOT_MET] + end + it "can succeed" do band = Band.save(user, band_params) band.errors.any?.should be_false