require 'spec_helper'

describe JamTrackMixdown do

  let(:user) {FactoryGirl.create(:user)}
  let(:jam_track) {FactoryGirl.create(:jam_track)}
  let(:settings) {  {speed:5} }

  it "can be created (factory girl)" do
    mixdown = FactoryGirl.create(:jam_track_mixdown)

    mixdown = JamTrackMixdown.find(mixdown.id)
    mixdown.settings.should eq({"speed" => 5})
  end

  it "can be created" do
    mixdown = JamTrackMixdown.create('abc', 'description', user, jam_track, settings)
    mixdown.errors.any?.should == false
  end

  it "index" do
    query, start, count = JamTrackMixdown.index({id: jam_track}, user)

    query.length.should eq(0)
    start.should be_nil
    count.should eq(0)

    mixdown = FactoryGirl.create(:jam_track_mixdown, user: user, jam_track: jam_track)

    query, start, count = JamTrackMixdown.index({id: jam_track}, user)
    query[0].should eq(mixdown)
    start.should be_nil
    count.should eq(1)
  end

  describe "settings" do
    it "validates empty settings" do
      invalid = FactoryGirl.build(:jam_track_mixdown, settings: {}.to_json)
      invalid.save
      invalid.errors.any?.should be_true
      invalid.errors["settings"].should eq(["can't be blank", "have nothing specified"])
    end

    it "validates speed numeric" do
      invalid = FactoryGirl.build(:jam_track_mixdown, settings: {"speed" => "5"}.to_json)
      invalid.save
      invalid.errors.any?.should be_true
      invalid.errors["settings"].should eq(["has non-integer speed"])
    end

    it "validates pitch numeric" do
      invalid = FactoryGirl.build(:jam_track_mixdown, settings: {"pitch" => "5"}.to_json)
      invalid.save
      invalid.errors.any?.should be_true
      invalid.errors["settings"].should eq(["has non-integer pitch"])
    end

    it "validates speed not-float" do
      invalid = FactoryGirl.build(:jam_track_mixdown, settings: {"speed" => 5.5}.to_json)
      invalid.save
      invalid.errors.any?.should be_true
      invalid.errors["settings"].should eq(["has non-integer speed"])
    end

    it "validates pitch not-float" do
      invalid = FactoryGirl.build(:jam_track_mixdown, settings: {"pitch" => 10.5}.to_json)
      invalid.save
      invalid.errors.any?.should be_true
      invalid.errors["settings"].should eq(["has non-integer pitch"])
    end
  end


end

