<commit_before>share_examples_for 'An Adapter' do

  def self.adapter_supports?(*methods)

    # FIXME obviously this needs a real fix!
    # --------------------------------------
    # Probably, delaying adapter_supports?
    # to be executed after DataMapper.setup
    # has been called will solve our current
    # problem with described_type() being nil
    # for as long as DataMapper.setup wasn't
    # called
    return true if ENV['ADAPTER_SUPPORTS'] == 'all'

    methods.all? do |method|
      # TODO: figure out a way to see if the instance method is only inherited
      # from the Abstract Adapter, and not defined in it's class.  If that is
      # the case return false

      # CRUD methods can be inherited from parent class
      described_type.instance_methods.any? { |instance_method| method.to_s == instance_method.to_s }
    end
  end

  # Hack to detect cases a let(:heffalump_model) is not present
  unless instance_methods.map(&:to_s).include?('heffalump_model')
    # This is the default Heffalump model. You can replace it with your own
    # (using let/let!) # but # be shure the replacement provides the required
    # properties.
    let(:heffalump_model) do
      model = Class.new do
        include DataMapper::Resource

        property :id,        DataMapper::Property::Serial
        property :color,     DataMapper::Property::String
        property :num_spots, DataMapper::Property::Integer
        property :striped,   DataMapper::Property::Boolean

        # This is needed for DataMapper.finalize
        def self.name; 'Heffalump'; end
      end

      DataMapper.finalize

      model
    end
  end

  before :all do
    raise '+#adapter+ should be defined in a let(:adapter) block' unless respond_to?(:adapter)
    raise '+#repository+ should be defined in a let(:repository) block' unless respond_to?(:repository)

    DataMapper.finalize

    # create all tables and constraints before each spec
    if repository.respond_to?(:auto_migrate!)
      heffalump_model.auto_migrate!
    end
  end

  if adapter_supports?(:create)
    describe '#create' do
      after do
        heffalump_model.destroy
      end

      it 'should not raise any errors' do
        lambda {
          heffalump_model.create(:color => 'peach')
        }.should_not raise_error
      end

      it 'should set the identity field for the resource' do
        heffalump = heffalump_model.new(:color => 'peach')
        heffalump.id.should be_nil
        heffalump.save.should be(true)
        heffalump.id.should_not be_nil
      end
    end
  else
    it 'needs to support #create'
  end

  if adapter_supports?(:read)
    describe '#read' do
      before :all do
        @heffalump = heffalump_model.create(:color => 'brownish hue')
        #just going to borrow this, so I can check the return values
        @query = heffalump_model.all.query
      end

      after :all do
        heffalump_model.destroy
      end

      it 'should not raise any errors' do
        lambda {
          heffalump_model.all()
        }.should_not raise_error
      end

      it 'should return stuff' do
        heffalump_model.all.should include(@heffalump)
      end
    end
  else
    it 'needs to support #read'
  end

  if adapter_supports?(:update)
    describe '#update' do
      before do
        @heffalump = heffalump_model.create(:color => 'indigo')
      end

      after do
        heffalump_model.destroy
      end

      it 'should not raise any errors' do
        lambda {
          @heffalump.color = 'violet'
          @heffalump.save
        }.should_not raise_error
      end

      it 'should not alter the identity field' do
        id = @heffalump.id
        @heffalump.color = 'violet'
        @heffalump.save
        @heffalump.id.should == id
      end

      it 'should update altered fields' do
        @heffalump.color = 'violet'
        @heffalump.save
        heffalump_model.get(*@heffalump.key).color.should == 'violet'
      end

      it 'should not alter other fields' do
        color = @heffalump.color
        @heffalump.num_spots = 3
        @heffalump.save
        heffalump_model.get(*@heffalump.key).color.should == color
      end
    end
  else
    it 'needs to support #update'
  end

  if adapter_supports?(:delete)
    describe '#delete' do
      before do
        @heffalump = heffalump_model.create(:color => 'forest green')
      end

      after do
        heffalump_model.destroy
      end

      it 'should not raise any errors' do
        lambda {
          @heffalump.destroy
        }.should_not raise_error
      end

      it 'should delete the requested resource' do
        key = @heffalump.key
        @heffalump.destroy
        heffalump_model.get(*key).should be_nil
      end
    end
  else
    it 'needs to support #delete'
  end

  if adapter_supports?(:read, :create)
    describe 'query matching' do
      before :all do
        @red  = heffalump_model.create(:color => 'red')
        @two  = heffalump_model.create(:num_spots => 2)
        @five = heffalump_model.create(:num_spots => 5)
      end

      after :all do
        heffalump_model.destroy
      end

      describe 'conditions' do
        describe 'eql' do
          it 'should be able to search for objects included in an inclusive range of values' do
            heffalump_model.all(:num_spots => 1..5).should include(@five)
          end

          it 'should be able to search for objects included in an exclusive range of values' do
            heffalump_model.all(:num_spots => 1...6).should include(@five)
          end

          it 'should not be able to search for values not included in an inclusive range of values' do
            heffalump_model.all(:num_spots => 1..4).should_not include(@five)
          end

          it 'should not be able to search for values not included in an exclusive range of values' do
            heffalump_model.all(:num_spots => 1...5).should_not include(@five)
          end
        end<commit_msg>Update shared adapter specs to ensure the resource are saved<commit_after>share_examples_for 'An Adapter' do

  def self.adapter_supports?(*methods)

    # FIXME obviously this needs a real fix!
    # --------------------------------------
    # Probably, delaying adapter_supports?
    # to be executed after DataMapper.setup
    # has been called will solve our current
    # problem with described_type() being nil
    # for as long as DataMapper.setup wasn't
    # called
    return true if ENV['ADAPTER_SUPPORTS'] == 'all'

    methods.all? do |method|
      # TODO: figure out a way to see if the instance method is only inherited
      # from the Abstract Adapter, and not defined in it's class.  If that is
      # the case return false

      # CRUD methods can be inherited from parent class
      described_type.instance_methods.any? { |instance_method| method.to_s == instance_method.to_s }
    end
  end

  # Hack to detect cases a let(:heffalump_model) is not present
  unless instance_methods.map(&:to_s).include?('heffalump_model')
    # This is the default Heffalump model. You can replace it with your own
    # (using let/let!) # but # be shure the replacement provides the required
    # properties.
    let(:heffalump_model) do
      model = Class.new do
        include DataMapper::Resource

        property :id,        DataMapper::Property::Serial
        property :color,     DataMapper::Property::String
        property :num_spots, DataMapper::Property::Integer
        property :striped,   DataMapper::Property::Boolean

        # This is needed for DataMapper.finalize
        def self.name; 'Heffalump'; end
      end

      DataMapper.finalize

      model
    end
  end

  before :all do
    raise '+#adapter+ should be defined in a let(:adapter) block' unless respond_to?(:adapter)
    raise '+#repository+ should be defined in a let(:repository) block' unless respond_to?(:repository)

    DataMapper.finalize

    # create all tables and constraints before each spec
    if repository.respond_to?(:auto_migrate!)
      heffalump_model.auto_migrate!
    end
  end

  if adapter_supports?(:create)
    describe '#create' do
      after do
        heffalump_model.destroy
      end

      it 'should not raise any errors' do
        lambda {
          heffalump_model.new(:color => 'peach').save.should be(true)
        }.should_not raise_error
      end

      it 'should set the identity field for the resource' do
        heffalump = heffalump_model.new(:color => 'peach')
        heffalump.id.should be_nil
        heffalump.save.should be(true)
        heffalump.id.should_not be_nil
      end
    end
  else
    it 'needs to support #create'
  end

  if adapter_supports?(:read)
    describe '#read' do
      before :all do
        @heffalump = heffalump_model.create(:color => 'brownish hue')
        @heffalump.should be_saved
        @query = heffalump_model.all.query
      end

      after :all do
        heffalump_model.destroy
      end

      it 'should not raise any errors' do
        lambda {
          heffalump_model.all()
        }.should_not raise_error
      end

      it 'should return expected results' do
        heffalump_model.all.should == [ @heffalump ]
      end
    end
  else
    it 'needs to support #read'
  end

  if adapter_supports?(:update)
    describe '#update' do
      before do
        @heffalump = heffalump_model.create(:color => 'peach', :num_spots => 1, :striped => false)
        @heffalump.should be_saved
      end

      after do
        heffalump_model.destroy
      end

      it 'should not raise any errors' do
        lambda {
          @heffalump.num_spots = 0
          @heffalump.save.should be(true)
        }.should_not raise_error
      end

      it 'should not alter the identity field' do
        key = @heffalump.key
        @heffalump.num_spots = 0
        @heffalump.save.should be(true)
        @heffalump.key.should == key
      end

      it 'should update altered fields' do
        @heffalump.num_spots = 0
        @heffalump.save.should be(true)
        heffalump_model.get!(*@heffalump.key).num_spots.should be(0)
      end

      it 'should not alter other fields' do
        num_spots = @heffalump.num_spots
        @heffalump.striped = true
        @heffalump.save.should be(true)
        heffalump_model.get!(*@heffalump.key).num_spots.should be(num_spots)
      end
    end
  else
    it 'needs to support #update'
  end

  if adapter_supports?(:delete)
    describe '#delete' do
      before do
        @heffalump = heffalump_model.create(:color => 'forest green')
        @heffalump.should be_saved
      end

      after do
        heffalump_model.destroy
      end

      it 'should not raise any errors' do
        lambda {
          @heffalump.destroy
        }.should_not raise_error
      end

      it 'should delete the requested resource' do
        key = @heffalump.key
        @heffalump.destroy
        heffalump_model.get(*key).should be_nil
      end
    end
  else
    it 'needs to support #delete'
  end

  if adapter_supports?(:read, :create)
    describe 'query matching' do
      before :all do
        @red  = heffalump_model.create(:color => 'red')
        @two  = heffalump_model.create(:num_spots => 2)
        @five = heffalump_model.create(:num_spots => 5)
        [ @red, @two, @five ].each { |resource| resource.should be_saved }
      end

      after :all do
        heffalump_model.destroy
      end

      describe 'conditions' do
        describe 'eql' do
          it 'should be able to search for objects included in an inclusive range of values' do
            heffalump_model.all(:num_spots => 1..5).should include(@five)
          end

          it 'should be able to search for objects included in an exclusive range of values' do
            heffalump_model.all(:num_spots => 1...6).should include(@five)
          end

          it 'should not be able to search for values not included in an inclusive range of values' do
            heffalump_model.all(:num_spots => 1..4).should_not include(@five)
          end

          it 'should not be able to search for values not included in an exclusive range of values' do
            heffalump_model.all(:num_spots => 1...5).should_not include(@five)
          end
        end
{'fraction_non_alphanumeric': 0.08654865812872176, 'fraction_numerical': 0.0021208907741251328, 'mean_word_length': 2.27807486631016, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '3002377', 'n_tokens_mistral': 3760, 'n_tokens_neox': 3621, 'n_words': 1129}