require 'spec_helper'

describe Snippet do  
  let(:site) { FactoryGirl.create(:site) } 
  let(:user) { FactoryGirl.create(:user, :site_id => site.id) }
  before(:each) do
    @snippet = FactoryGirl.create(:snippet, :site => site, :created_by_id => user.id, :updated_by_id => user.id)
  end 
  
  # -- Associations ---
  context "Associations" do          
    it "should belong to a site" do
      @snippet.should belong_to(:site)
    end                               
    
    it "should reference a user with created_by" do
      @snippet.should belong_to(:created_by).of_type(User)
    end
    
    it "should reference a user with updated_by" do
      @snippet.should belong_to(:updated_by).of_type(User)
    end
  end                      
  
  # -- Validations ---
  context "Validations" do
    it "should be a valid snippet" do
      @snippet.should be_valid
    end                       
    
    it "should not be valid without a name" do
      @snippet.name = nil
      @snippet.should_not be_valid
    end        
    
    it "should not be valid without a unique name" do 
      FactoryGirl.build(:snippet, :name => @snippet.name, :site_id => site.id).should_not be_valid
    end
    
    it "should not be valid without a filter" do
      @snippet.filter_name = nil
      @snippet.should_not be_valid
    end
    
    it "should not be valid without a created_by" do
      @snippet.created_by_id = nil
      @snippet.should_not be_valid
    end
    
    it "should not be valid without a updated_by" do
      @snippet.updated_by_id = nil
      @snippet.should_not be_valid
    end
  end  
  
  # --  Scopes -----------------------------------------------------
  describe "Scopes" do
    describe "Snippet#find_by_site_and_name(site, name)" do
      it "should return the snippet a mongoid criteria" do
        Snippet.find_by_site_and_name(site, @snippet.name).should be_an_instance_of(Mongoid::Criteria)
      end
      
      it "should return the snippet with the name" do
        Snippet.find_by_site_and_name(site, @snippet.name).first.should == @snippet
      end
    end
  end

  # -- Class Methods ----
  describe "Class Methods" do
    describe "snippet_by_name" do
      it "should return the snippet by name" do
        Snippet.snippet_by_name(@snippet.name).should == @snippet  
      end
    end
  end

end
