<commit_before>require File.join(File.dirname(__FILE__), '..', '/spec_helper.rb')

describe "with respect to resources" do
  before :each do
    class TestResource
      include Mordor::Resource

      attribute :first,  :index => true
      attribute :second, :index => true, :index_type => Mongo::ASCENDING
      attribute :third,  :finder_method => :find_by_third_attribute
      attribute :at
      attribute :created_at,     :timestamp => true

      # Put this in here again to ensure the original method is still here
      class_eval do
        def self.ensure_indices
          collection.ensure_index( indices.map{|index| [index.to_s, Mongo::DESCENDING]} ) if indices.any?
        end
      end
    end
  end

  after :each do
    drop_db_collections
  end

  it "should create accessor methods for all attributes" do
    ["first", "first=", "second", "second="].each{ |v| TestResource.public_instance_methods.map{|m| m.to_s}.should include(v) }
  end

  it "should create class level finder methods for all attributes" do
    ["find_by_first", "find_by_second"].each do |finder_method|
      TestResource.methods.map{|m| m.to_s}.should include(finder_method)
    end
  end

  it "should create finder methods with the supplied finder method name" do
    TestResource.methods.map{|m| m.to_s}.should include "find_by_third_attribute"
  end

  it "should ensure indices when the option :index => true is given" do
    TestResource.send(:indices).should include :first
  end

  it "should default to descending indices" do
    TestResource.send(:index_types).keys.should include :first
    TestResource.send(:index_types)[:first].should == Mongo::DESCENDING
  end

  it "should be possible to set index type using the 'index_type' option" do
    TestResource.send(:index_types).keys.should include :second
    TestResource.send(:index_types)[:second].should == Mongo::ASCENDING
  end

  it "should be possible to designate an attribute as a timestamp" do
    TestResource.timestamped_attribute.should_not be_nil
    TestResource.timestamped_attribute.should == :created_at
  end

  it "should only be possible to have one attribute as a timestamp" do
    lambda {
      TestResource2.class_eval do
        attribute :some_timestamp, :timestamp => true
        attribute :another_timestamp, :timestamp => true
      end
    }.should raise_error
  end

  it "should provide timestamped attribute as first attribute when creating a Resource" do
    tr = TestResource.create({:first => 'first'})
    tr.reload
    tr.at.should_not be_nil
    TestResource.get(tr._id).at.should_not == BSON::Timestamp.new(0,0)
  end

  context "with respect to replacing params" do
    it "should correctly substitute non-alphanumeric characters in keys with underscores" do
      options = {
        "o*p#t>i_o@n)s" => "test"
      }
      result = TestResource.new.replace_params(options)
      result.keys.first.should eql "o_p_t_i_o_n_s"
    end

    it "should correctly replace Date and DateTimes" do
      options = {
        "option" => Date.today,
        "another" => DateTime.now
      }
      result = TestResource.new.replace_params(options)
      result.each do |k, v|
        v.should be_a Time
      end
    end

    it "should correctly replace BigDecimals" do
      options = {
        "option" => BigDecimal.new("1.00")
      }
      result = TestResource.new.replace_params(options)
      result.each do |k,v|
        v.should be_a Float
      end
    end

    it "should correctly replace BSON::Timestamps" do
      options = {
        "option" => BSON::Timestamp.new(324244, 12)
      }
      result = TestResource.new.replace_params(options)
      result.each do |k, v|
        v["seconds"].should == 324244
        v["increment"].should == 12
      end
    end
<commit_msg>Get rid of deprecation warning<commit_after>require File.join(File.dirname(__FILE__), '..', '/spec_helper.rb')

describe "with respect to resources" do
  before :each do
    class TestResource
      include Mordor::Resource

      attribute :first,  :index => true
      attribute :second, :index => true, :index_type => Mongo::ASCENDING
      attribute :third,  :finder_method => :find_by_third_attribute
      attribute :at
      attribute :created_at,     :timestamp => true

      # Put this in here again to ensure the original method is still here
      class_eval do
        def self.ensure_indices
          collection.ensure_index( indices.map{|index| [index.to_s, Mongo::DESCENDING]} ) if indices.any?
        end
      end
    end
  end

  after :each do
    drop_db_collections
  end

  it "should create accessor methods for all attributes" do
    ["first", "first=", "second", "second="].each{ |v| TestResource.public_instance_methods.map{|m| m.to_s}.should include(v) }
  end

  it "should create class level finder methods for all attributes" do
    ["find_by_first", "find_by_second"].each do |finder_method|
      TestResource.methods.map{|m| m.to_s}.should include(finder_method)
    end
  end

  it "should create finder methods with the supplied finder method name" do
    TestResource.methods.map{|m| m.to_s}.should include "find_by_third_attribute"
  end

  it "should ensure indices when the option :index => true is given" do
    TestResource.send(:indices).should include :first
  end

  it "should default to descending indices" do
    TestResource.send(:index_types).keys.should include :first
    TestResource.send(:index_types)[:first].should == Mongo::DESCENDING
  end

  it "should be possible to set index type using the 'index_type' option" do
    TestResource.send(:index_types).keys.should include :second
    TestResource.send(:index_types)[:second].should == Mongo::ASCENDING
  end

  it "should be possible to designate an attribute as a timestamp" do
    TestResource.timestamped_attribute.should_not be_nil
    TestResource.timestamped_attribute.should == :created_at
  end

  it "should only be possible to have one attribute as a timestamp" do
    lambda {
      TestResource2.class_eval do
        attribute :some_timestamp, :timestamp => true
        attribute :another_timestamp, :timestamp => true
      end
    }.should raise_error
  end

  it "should provide timestamped attribute as first attribute when creating a Resource" do
    tr = TestResource.create({:first => 'first'})
    tr.reload
    tr.at.should_not be_nil
    TestResource.get(tr._id).at.should_not == BSON::Timestamp.new(0,0)
  end

  context "with respect to replacing params" do
    it "should correctly substitute non-alphanumeric characters in keys with underscores" do
      options = {
        "o*p#t>i_o@n)s" => "test"
      }
      result = TestResource.new.replace_params(options)
      result.keys.first.should eql "o_p_t_i_o_n_s"
    end

    it "should correctly replace Date and DateTimes" do
      options = {
        "option" => Date.today,
        "another" => DateTime.now
      }
      result = TestResource.new.replace_params(options)
      result.each do |k, v|
        v.should be_a Time
      end
    end

    it "should correctly replace BigDecimals" do
      options = {
        "option" => BigDecimal("1.00")
      }
      result = TestResource.new.replace_params(options)
      result.each do |k,v|
        v.should be_a Float
      end
    end

    it "should correctly replace BSON::Timestamps" do
      options = {
        "option" => BSON::Timestamp.new(324244, 12)
      }
      result = TestResource.new.replace_params(options)
      result.each do |k, v|
        v["seconds"].should == 324244
        v["increment"].should == 12
      end
    end

{'fraction_non_alphanumeric': 0.09674013461792266, 'fraction_numerical': 0.005807047644186354, 'mean_word_length': 3.0698174006444683, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, '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': '333661', 'n_tokens_mistral': 2311, 'n_tokens_neox': 2211, 'n_words': 664}