require 'spec_helper' describe Track do let (:user) {FactoryGirl.create(:user) } let (:music_session) { FactoryGirl.create(:music_session, :creator => user)} let (:connection) { FactoryGirl.create(:connection, :user => user, :music_session => music_session) } let (:track) { FactoryGirl.create(:track, :connection => connection)} let (:track2) { FactoryGirl.create(:track, :connection => connection)} let (:msuh) {FactoryGirl.create(:music_session_user_history, :history => music_session.music_session_history, :user => user, :client_id => connection.client_id) } let (:track_hash) { {:client_track_id => 'client_guid', :sound => 'stereo', :instrument_id => 'drums'} } before(:each) do msuh.touch end describe "sync" do it "create one track" do tracks = Track.sync(connection.client_id, [track_hash]) tracks.length.should == 1 track = tracks[0] track.client_track_id.should == track_hash[:client_track_id] track.sound = track_hash[:sound] track.instrument.should == Instrument.find('drums') end it "create two tracks" do tracks = Track.sync(connection.client_id, [track_hash, track_hash]) tracks.length.should == 2 track = tracks[0] track.client_track_id.should == track_hash[:client_track_id] track.sound = track_hash[:sound] track.instrument.should == Instrument.find('drums') track = tracks[1] track.client_track_id.should == track_hash[:client_track_id] track.sound = track_hash[:sound] track.instrument.should == Instrument.find('drums') end it "delete only track" do track.id.should_not be_nil connection.tracks.length.should == 1 tracks = Track.sync(connection.client_id, []) tracks.length.should == 0 end it "delete one of two tracks using .id to correlate" do track.id.should_not be_nil track2.id.should_not be_nil connection.tracks.length.should == 2 tracks = Track.sync(connection.client_id, [{:id => track.id, :client_track_id => 'client_guid_new', :sound => 'mono', :instrument_id => 'drums'}]) tracks.length.should == 1 found = tracks[0] found.id.should == track.id found.sound.should == 'mono' found.client_track_id.should == 'client_guid_new' end it "delete one of two tracks using .client_track_id to correlate" do track.id.should_not be_nil track2.id.should_not be_nil connection.tracks.length.should == 2 tracks = Track.sync(connection.client_id, [{:client_track_id => track.client_track_id, :sound => 'mono', :instrument_id => 'drums'}]) tracks.length.should == 1 found = tracks[0] found.id.should == track.id found.sound.should == 'mono' found.client_track_id.should == track.client_track_id end it "updates a single track using .id to correlate" do track.id.should_not be_nil connection.tracks.length.should == 1 begin ActiveRecord::Base.record_timestamps = false track.updated_at = 1.days.ago track.save! ensure # very important to turn it back; it'll break all tests otherwise ActiveRecord::Base.record_timestamps = true end tracks = Track.sync(connection.client_id, [{:id => track.id, :client_track_id => 'client_guid_new', :sound => 'mono', :instrument_id => 'drums'}]) tracks.length.should == 1 found = tracks[0] found.id.should == track.id found.sound.should == 'mono' found.client_track_id.should == 'client_guid_new' found.updated_at.should_not == track.updated_at end it "updates a single track using .client_track_id to correlate" do track.id.should_not be_nil connection.tracks.length.should == 1 tracks = Track.sync(connection.client_id, [{:client_track_id => track.client_track_id, :sound => 'mono', :instrument_id => 'drums'}]) tracks.length.should == 1 found = tracks[0] found.id.should == track.id found.sound.should == 'mono' found.client_track_id.should == track.client_track_id end it "does not touch updated_at when nothing changes" do track.id.should_not be_nil connection.tracks.length.should == 1 begin ActiveRecord::Base.record_timestamps = false track.updated_at = 1.days.ago track.save! ensure # very important to turn it back; it'll break all tests otherwise ActiveRecord::Base.record_timestamps = true end tracks = Track.sync(connection.client_id, [{:id => track.id, :client_track_id => track.client_track_id, :sound => track.sound, :instrument_id => track.instrument_id}]) tracks.length.should == 1 found = tracks[0] expect(found.id).to eq track.id expect(found.updated_at.to_i).to eq track.updated_at.to_i end end end