<commit_before>require File.expand_path('../../spec_helper', __FILE__)

describe ResourcePage do
  
  describe "being created" do
    before do
      Paperclip.options[:log] = false
      @resource_page = ResourcePage.new
    end
    
    it "should not be valid by default" do
      @resource_page.should_not be_valid
    end
    
    it "should require a user id and name" do
      %w( user_id name ).each do |attribute|      
        r = build_resource_page(attribute.to_sym => nil)
        r.should_not be_valid
        r.errors[attribute.to_sym].should_not be_nil
      end

      r = build_resource_page
      r.should be_valid
    end
    
    it "should be set to 'draft' status by default" do
      r = build_resource_page(:publication_status => nil)
      r.publication_status.should be_nil
      r.should be_valid
      r.publication_status.should == 'draft'
    end
  end
  
  
  describe "after creation" do
    before do 
      @resource_page = create_resource_page
      @attachment = File.new(::Rails.root.to_s + '/spec/fixtures/images/rails.png')
    end
    
    it "should allow a new file to be attached" do
      @resource_page.attached_files.size.should == 0
      @resource_page.has_attached_files?.should be_false
      @resource_page.new_attached_files = { 'name' => 'attachment 1', 'attachment' => @attachment }
      @resource_page.has_attached_files?.should be_true
      @resource_page.attached_files.size.should == 1
    end
    
    it "should allow multiple new files to be attached" do
      @resource_page.attached_files.size.should == 0
      @resource_page.has_attached_files?.should be_false
      @resource_page.new_attached_files = [
        { 'name' => 'attachment 1', 'attachment' => @attachment },
        { 'name' => 'attachment 2', 'attachment' => @attachment },
        { 'name' => 'attachment 3', 'attachment' => @attachment }
      ]
      @resource_page.has_attached_files?.should be_true
      @resource_page.attached_files.size.should == 3
    end
    
    it "should include draft flag when in draft or private mode" do
      %w(draft private).each do |status|
        @resource_page = create_resource_page(:publication_status => status)
        @resource_page.display_name.should == "[#{status.upcase}] #{@resource_page.name}"
      end
    end
  end

protected

  def build_resource_page(options = {})
    ResourcePage.new({<commit_msg>expose match_any scope failure in resource page model<commit_after>require File.expand_path('../../spec_helper', __FILE__)

describe ResourcePage do
  
  before(:each) do
    generate_default_users_with_factories
      # @anon_user
      # @admin_user
  end
  
  describe "being created" do
    before do
      Paperclip.options[:log] = false
      @resource_page = ResourcePage.new
    end
    
    it "should not be valid by default" do
      @resource_page.should_not be_valid
    end
    
    it "should require a user id and name" do
      %w( user_id name ).each do |attribute|      
        r = build_resource_page(attribute.to_sym => nil)
        r.should_not be_valid
        r.errors[attribute.to_sym].should_not be_nil
      end

      r = build_resource_page
      r.should be_valid
    end
    
    it "should be set to 'draft' status by default" do
      r = build_resource_page(:publication_status => nil)
      r.publication_status.should be_nil
      r.should be_valid
      r.publication_status.should == 'draft'
    end
  end
  
  
  describe "after creation" do
    before do 
      @resource_page = create_resource_page
      @attachment = File.new(::Rails.root.to_s + '/spec/fixtures/images/rails.png')
    end
    
    it "should allow a new file to be attached" do
      @resource_page.attached_files.size.should == 0
      @resource_page.has_attached_files?.should be_false
      @resource_page.new_attached_files = { 'name' => 'attachment 1', 'attachment' => @attachment }
      @resource_page.has_attached_files?.should be_true
      @resource_page.attached_files.size.should == 1
    end
    
    it "should allow multiple new files to be attached" do
      @resource_page.attached_files.size.should == 0
      @resource_page.has_attached_files?.should be_false
      @resource_page.new_attached_files = [
        { 'name' => 'attachment 1', 'attachment' => @attachment },
        { 'name' => 'attachment 2', 'attachment' => @attachment },
        { 'name' => 'attachment 3', 'attachment' => @attachment }
      ]
      @resource_page.has_attached_files?.should be_true
      @resource_page.attached_files.size.should == 3
    end
    
    it "should include draft flag when in draft or private mode" do
      %w(draft private).each do |status|
        @resource_page = create_resource_page(:publication_status => status)
        @resource_page.display_name.should == "[#{status.upcase}] #{@resource_page.name}"
      end
    end
  end
  
  describe "searching" do
    
    it "should be searchable" do
      params = {
        :name => "abc",
        :user => @admin_user
      }
      ResourcePage.search_list(params)
    end
    
  end

  # @resource_pages = ResourcePage.search_list({
  #   :name => @name,
  #   :user => current_user,
  #   :portal_clazz_id => @portal_clazz_id,
  #   :include_drafts => @include_drafts,
  #   :sort_order => @sort_order,
  #   :paginate => true,
  #   :page => params[:page]
  # })
  
protected

  def build_resource_page(options = {})
    ResourcePage.new({
{'fraction_non_alphanumeric': 0.10362024381233838, 'fraction_numerical': 0.0029553010712966383, 'mean_word_length': 2.770891364902507, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5916638', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1684, 'n_words': 400}