require 'spec_helper'

describe RestColores::Models::Position do
  describe :initialization do
    it "should successfully create a blank position" do
      position = RestColores::Models::Position.new
      
      expect(position).to_not be_nil
      expect(position.id).to be_nil
      expect(position.name).to be_nil
      expect(position.description).to be_nil
      expect(position).to_not exist_in_database
    end
    
    it "should successfully create a position with the given data" do
      position = RestColores::Models::Position.new(:id => 1, :name => 'test', :description => 'test position')
      
      expect(position).to_not be_nil
      expect(position.id).to eq(1)
      expect(position.name).to eq('test')
      expect(position.description).to eq('test position')
      expect(position).to_not exist_in_database
    end
  end
  
  describe :validation do
    it "should have invalid data" do
      position = RestColores::Models::Position.new
      expect(position.valid?).to be_falsey
      expect(position.errors[:name]).to include("can't be blank")
      expect(position.errors[:description]).to include("can't be blank")
      expect(position).to_not exist_in_database
    end
  end
  
  describe :create do
    before :each do
      @created = RestColores::Models::Position.new(:name => 'create test', :description => 'test position')
    end
    
    it "should create new position" do
      expect(@created.save).to be_truthy
      expect(@created.id).to_not be_nil
      expect(@created.name).to eq('create test')
      expect(@created.description).to eq('test position')
      expect(@created).to exist_in_database
    end
    
    it "should fail to create a position with the same name" do
      @created.save
      same_position = RestColores::Models::Position.new(:name => 'create test', :description => 'test position')
      expect(same_position.valid?).to_not be_truthy
      expect(same_position.errors[:name]).to include('has already been taken')
      expect(@created).to exist_in_database
    end
    
    after :each do
      RestColores::Models::Position.delete @created.id
    end
  end
end
