2014-01-12 08:49:57 +00:00
require 'spec_helper'
describe Band do
2014-02-19 22:56:13 +00:00
let ( :user ) { FactoryGirl . create ( :user ) }
let ( :user2 ) { FactoryGirl . create ( :user ) }
let ( :fan ) { FactoryGirl . create ( :fan ) }
let ( :band ) { FactoryGirl . create ( :band ) }
2014-07-20 02:11:16 +00:00
let ( :band_in_austin ) { FactoryGirl . create ( :band , country : 'US' , state : 'TX' , city : 'Austin' ) }
2014-02-19 22:56:13 +00:00
let ( :new_band ) { FactoryGirl . build ( :band ) }
let ( :band_params ) {
{
name : " The Band " ,
biography : " Biography " ,
city : 'Austin' ,
state : 'TX' ,
country : 'US' ,
genres : [ 'country' ]
}
}
2014-01-12 08:49:57 +00:00
2015-07-16 00:33:10 +00:00
let ( :band_params_no_genre ) {
{
name : " The Band " ,
biography : " Biography " ,
city : 'Austin' ,
state : 'TX' ,
country : 'US' ,
validate_genres : true
}
}
VRFS-3242 : Schema and model changes required for band profile functionality.
* Additional attributes for band_type, band_status, concert_count,
add_new_members, play_commitment, touring_option, paid_gigs,
hourly_rate, gig_minimum
* For joined table musician_instruments, remove the hard requirement
that they be joined to a user, rather a “player” that is polymorphic.
* For joined table performance_stamples, remove the hard requirement
that they be joined to a user, rather a “player” that is polymorphic.
* For joined table online_presences, remove the hard requirement that
they be joined to a user, rather a “player” that is polymorphic.
* Change models as appropriate with new attributes and modify
belongs_to / has_many directives as necessary.
* Fix existing usages of user_id to work with polymorphic player_id.
* Fix tests that use user_id
* Add new tests that exercise online_presence, performance_samples, and
instruments that target a band, rather than a user.
2015-05-14 02:06:14 +00:00
describe 'with instruments' do
it 'builds with instruments' do
band . musician_instruments << FactoryGirl . build ( :musician_instrument , player : band )
band . musician_instruments . should have ( 1 ) . items
band . instruments . should have ( 1 ) . items
end
it 'creates with instruments' do
FactoryGirl . create ( :musician_instrument , player : band )
band . reload
band . musician_instruments . should have ( 1 ) . items
band . instruments . should have ( 1 ) . items
end
end
2014-01-12 08:49:57 +00:00
describe 'website update' do
it 'should have http prefix on website url' do
band . website = 'example.com'
band . save!
expect ( band . website ) . to match ( / ^http: \/ \/ example.com$ / )
end
end
2014-02-19 22:56:13 +00:00
describe 'band validations' do
it " minimum genres " do
new_band . save . should be_false
new_band . errors [ :genres ] . should == [ ValidationMessages :: BAND_GENRE_MINIMUM_NOT_MET ]
2015-07-16 00:33:10 +00:00
2015-07-16 02:00:35 +00:00
new_band . genres = [ Genre . first ]
2015-07-16 00:33:10 +00:00
new_band . save . should be_true
2014-02-19 22:56:13 +00:00
end
it " maximum genres " do
new_band . genres = Genre . limit ( 4 )
new_band . save . should be_false
new_band . errors [ :genres ] . should == [ ValidationMessages :: BAND_GENRE_LIMIT_EXCEEDED ]
end
end
describe " save " do
2015-07-16 00:33:10 +00:00
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
2014-02-19 22:56:13 +00:00
it " can succeed " do
band = Band . save ( user , band_params )
band . errors . any? . should be_false
band . name . should == band_params [ :name ]
band . biography . should == band_params [ :biography ]
band . genres . should == [ Genre . find ( band_params [ :genres ] [ 0 ] ) ]
band . city . should == band_params [ :city ]
band . state . should == band_params [ :state ]
band . country . should == band_params [ :country ]
end
2015-07-15 15:04:45 +00:00
it " saves current interests " do
parms = band_params
parms [ :paid_gigs ] = true
parms [ :free_gigs ] = false
parms [ :hourly_rate ] = 5000
parms [ :gig_minimum ] = 30000
parms [ :add_new_members ] = true
parms [ :touring_option ] = false
parms [ :band_type ] = " virtual "
parms [ :band_status ] = " amateur "
parms [ :concert_count ] = 3
band = Band . save ( user , parms )
band . errors . any? . should be_false
band . paid_gigs . should == true
band . free_gigs . should == false
band . hourly_rate . should == 5000
parms [ :gig_minimum ] = 30000
band . add_new_members . should == true
band . touring_option . should == false
band . band_type . should == " virtual "
band . band_status . should == " amateur "
band . concert_count . should == 3
end
2014-02-19 22:56:13 +00:00
it " ensures user is a musician " do
expect { Band . save ( fan , band_params ) } . to raise_error ( " must be a musician " )
end
it " can update " do
band = Band . save ( user , band_params )
band . errors . any? . should be_false
band_params [ :id ] = band . id
band_params [ :name ] = " changed name "
band = Band . save ( user , band_params )
band . errors . any? . should be_false
Band . find ( band . id ) . name . should == band_params [ :name ]
end
it " stops non-members from updating " do
band = Band . save ( user , band_params )
band . errors . any? . should be_false
band_params [ :id ] = band . id
band_params [ :name ] = " changed name "
expect { Band . save ( user2 , band_params ) } . to raise_error ( ValidationMessages :: USER_NOT_BAND_MEMBER_VALIDATION_ERROR )
end
end
describe " validate " do
it " can pass " do
band = Band . build_band ( user , band_params )
band . valid? . should be_true
end
it " can fail " do
band_params [ :name ] = nil
band = Band . build_band ( user , band_params )
band . valid? . should be_false
band . errors [ :name ] . should == [ " can't be blank " ]
end
end
2014-07-20 02:11:16 +00:00
describe " after_maxmind_import " do
before ( :each ) do
create_phony_database
end
after ( :all ) do
create_phony_database
end
it " updates to non-null after import if matching location is available " do
band_in_austin . lat . should be_nil
band_in_austin . lng . should be_nil
Band . after_maxmind_import ( false )
band_in_austin . reload
band_in_austin . lat . should == 30 . 2076
band_in_austin . lng . should == - 97 . 8587
end
it " updates to non-null after import if matching location is available, with two matching geoip locations " do
band_in_austin . lat . should be_nil
band_in_austin . lng . should be_nil
# change the dallas entry to austin, to make two austin entries
GeoIpLocations . connection . execute ( " UPDATE geoiplocations SET city = 'Austin', region = 'TX', countrycode ='US' WHERE city = 'Dallas' AND region = 'TX' and countrycode = 'US' " ) . check
Band . after_maxmind_import ( false )
band_in_austin . reload
# you don't know which GeoIpLocation it'll be. So we need to check both
[ 30 . 2076 , 32 . 7825 ] . include? ( band_in_austin . lat ) . should be_true
[ - 97 . 8587 , - 96 . 8207 ] . include? ( band_in_austin . lng ) . should be_true
end
it " updates to null if no matching location available " do
band_in_austin . city = 'Blibbity'
band_in_austin . save!
# change the dallas entry to austin, to make two austin entries
GeoIpLocations . connection . execute ( " UPDATE geoiplocations SET city = 'Austin', region = 'TX', countrycode ='US' WHERE city = 'Dallas' AND region = 'TX' and countrycode = 'US' " ) . check
Band . after_maxmind_import ( false )
band_in_austin . reload
band_in_austin . lat . should be_nil
band_in_austin . lng . should be_nil
end
end
2014-09-13 02:34:59 +00:00
describe " recent history " do
it " should only retrieve recordings with a claimed recording " do
user = FactoryGirl . create ( :user )
band = FactoryGirl . create ( :band )
band . users << user
band . save!
claimed_recording = FactoryGirl . create ( :claimed_recording , :user = > user )
claimed_recording . recording . owner = user
claimed_recording . recording . band = band
claimed_recording . recording . save!
recording = FactoryGirl . create ( :recording , :owner = > user , :band = > band )
Recording . where ( :owner_id = > user . id ) . size . should == 2
Recording . where ( :band_id = > band . id ) . size . should == 2
2014-12-08 02:35:30 +00:00
history = band . recent_history ( nil , nil )
2014-09-13 02:34:59 +00:00
history . size . should == 1
history . first . id . should == claimed_recording . recording . id
2014-12-08 02:35:30 +00:00
# exclude the claimed recording
history = band . recent_history ( nil , claimed_recording . id )
history . size . should == 0
2014-09-13 02:34:59 +00:00
end
2015-07-15 15:04:45 +00:00
end
2014-01-12 08:49:57 +00:00
end