Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Updated shared adapter spec to be more configurable #119

Merged
merged 8 commits into from

4 participants

@mbj
Owner

The intention is to use the shared adapter specs from
dm-mongo-adapter without the need to duplicate it.

Tested against sqlite-adapter mongo-adapter and in-memory adapter, on
ruby-1.9.2-p180.

Markus Schirp added some commits
Markus Schirp Updated shared adapter spec to be more configurable
The intention is to use the shared adapter specs from
dm-mongo-adapter without the need to duplicate it.

Tested against sqlite-adapter mongo-adapter and in-memory adapter, on
ruby-1.9.2-p180.
eb8cdba
Markus Schirp fixed stupid typo 6b29508
@solnic
Owner

That's what I wanted to do a long time ago although I was thinking about doing a small refactor in those shared specs so they would be using a model which is provided by the adapter spec or defaulting to Heffalump if nothing is provided. I think that would be cleaner than setting up resource module and serial property ivars. 99% of the adapters don't require you to include a different module than DM::Resource and serial property will be gone sooner or later and replaced by some generic object id property.

@mbj
Owner

I agree that the use of a Adapter specific Heffalump where needed is a better solution. (As long it has compatible properties).

The shared adapter spec is currently configured by ivars set in before hooks (@adapter,@repository) so I did configure the Heffalump with ivars too.

This pull request preserves the default behavior on adapters who do not set the @dm_resource_module and @dm_resource_property for shared adapter specs. I do not see a reason not to use this solution in the meantime?

@solnic
Owner

We're moving away from using ivars in specs and prefer to use let/let! from rspec when setting up objects required by a spec example. You could refactor the adapters shared specs if you have time. If you don't, I can take care of that :)

@mbj
Owner

I'll configure this with the let/let!, stay tuned.

@mbj
Owner
mbj commented

I got some thoughts on the changes:

The adapter specific model must have the "same" properties like the Heffalump. Else the shared adapter test cannot create/update resources etc in a reliable way.

The model has to be assigned to a constant, else DataMapper.finalize! will fail. (#name will be nil).

In case no let or let! block does configuration the "default" Heffalump (or any other option) will be used. The absence of a let/let! is only detectable with respond_to?(:name_of_block).

Any suggestions? I would appreciate your review on my changes once they are pushed to github. If the changes are okay I'll try to nuke the ivars from the sqlite and postgres shared adapter specs also. I cannot run specs against the other adapters, I do not have the other databases in house.

@dkubb
Owner

This looks good to me. @solnic is this what you were looking for?

@mbj
Owner

The Heffalump does not have to be a constant. It can be an anonymous class as long as you override #name.

Example:

let(:heffalump) do
  Class.new do
    include DataMapper::Resource
    property :id,DataMapper::Mongo::Property::ObjectId
    def self.name; 'Heffalump'; end
  end
end

Doing heffalump setup this way would remove the need to unset constants. Suggestions?

@dkubb
Owner

I actually do this in my own specs now. I don't like the idea of creating a constant unless there's no other choice (which is rarely the case). I would support this as a change to the shared specs.

@jeroenvandijk

@dkubb since I'm working on the shared specs too should I incorporate this proposal when working on the capabilities API? https://gist.github.com/1154417

@dkubb
Owner

@jeroenvandijk I think it would be ok to specify somewhere in the proposal.

However, I don't think this issue should wait for your work specifically, which is more overarching that this small change that @mbj proposes. If he wants to submit a patch to this pull request that uses an anonymous class I think he shouldn't be prevented. We can iterate on his code in your patches if needed.

@mbj
Owner

In case the Heffalump model is created using let. Is it still overridable? This was my main intend to work at this.

@solnic
Owner

@mbj sorry for late response. yes, you can override things by another #let declarations.

btw - let's get this merged in for 1.3.0

@mbj
Owner

@solnic, No worries. Once we got that merged dm-mongo-adapter might become a good dm-1.0 citizen ;)

@solnic
Owner

@mbj can we merge this in? I'm not sure if this still works with other adapters, please confirm

@mbj
Owner

@solnic, Im unsure. Will have to test for myself. Im time limited atm. Give me some days.

@mbj
Owner
mbj commented

IMHO this is mergable now.

See also:

datamapper/dm-do-adapter#12
datamapper/dm-sqlite-adapter#5
https://github.com/mbj/dm-mongo-adapter/tree/feature/general-shared-adapter-spec

@solnic the dm-mongo-adapter branch will be merged with master once dm-core supports overriding the default Heffalump. IMHO we will be able to release a beta gem along with dm-1.3

I do not have a local postrgres, mysql or other -sql database anymore and I avoid to make pull requests for untested changes.

@solnic solnic merged commit 64bbcc6 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 29, 2011
  1. Updated shared adapter spec to be more configurable

    Markus Schirp authored
    The intention is to use the shared adapter specs from
    dm-mongo-adapter without the need to duplicate it.
    
    Tested against sqlite-adapter mongo-adapter and in-memory adapter, on
    ruby-1.9.2-p180.
Commits on May 30, 2011
  1. fixed stupid typo

    Markus Schirp authored
Commits on Jun 1, 2011
  1. Using let blocks to configure shared adapter spec

    Markus Schirp authored
Commits on Jun 3, 2011
Commits on Aug 19, 2011
  1. Merge branch 'master' of git://github.com/datamapper/dm-core into gen…

    Markus Schirp authored
    …eral-shared-adapter-spec
Commits on Jan 4, 2012
  1. Merge branch 'master' into general-shared-adapter-spec

    Markus Schirp authored
This page is out of date. Refresh to see the latest.
Showing with 75 additions and 64 deletions.
  1. +75 −64 lib/dm-core/spec/shared/adapter_spec.rb
View
139 lib/dm-core/spec/shared/adapter_spec.rb
@@ -22,23 +22,34 @@ def self.adapter_supports?(*methods)
end
end
- before :all do
- raise '+@adapter+ should be defined in before block' unless instance_variable_get('@adapter')
-
- class ::Heffalump
- include DataMapper::Resource
-
- property :id, Serial
- property :color, String
- property :num_spots, Integer
- property :striped, Boolean
+ # Hack to detect cases a let(:heffalump_model) is not present
+ unless instance_methods.map(&:to_s).include?('heffalump_model')
+ # This is the default Heffalup model. You can replace it with your own
+ # (using let/let!) # but # be shure the replacement provides the required
+ # properties.
+ let(:heffalump_model) do
+ 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
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.auto_migrate!
+ if repository.respond_to?(:auto_migrate!)
+ heffalump_model.auto_migrate!
end
end
@@ -46,12 +57,12 @@ class ::Heffalump
describe '#create' do
it 'should not raise any errors' do
lambda {
- Heffalump.create(:color => 'peach')
+ heffalump_model.create(:color => 'peach')
}.should_not raise_error
end
it 'should set the identity field for the resource' do
- heffalump = Heffalump.new(:color => 'peach')
+ heffalump = heffalump_model.new(:color => 'peach')
heffalump.id.should be_nil
heffalump.save
heffalump.id.should_not be_nil
@@ -64,19 +75,19 @@ class ::Heffalump
if adapter_supports?(:read)
describe '#read' do
before :all do
- @heffalump = Heffalump.create(:color => 'brownish hue')
+ @heffalump = heffalump_model.create(:color => 'brownish hue')
#just going to borrow this, so I can check the return values
- @query = Heffalump.all.query
+ @query = heffalump_model.all.query
end
it 'should not raise any errors' do
lambda {
- Heffalump.all()
+ heffalump_model.all()
}.should_not raise_error
end
it 'should return stuff' do
- Heffalump.all.should be_include(@heffalump)
+ heffalump_model.all.should be_include(@heffalump)
end
end
else
@@ -86,7 +97,7 @@ class ::Heffalump
if adapter_supports?(:update)
describe '#update' do
before do
- @heffalump = Heffalump.create(:color => 'indigo')
+ @heffalump = heffalump_model.create(:color => 'indigo')
end
it 'should not raise any errors' do
@@ -106,14 +117,14 @@ class ::Heffalump
it 'should update altered fields' do
@heffalump.color = 'violet'
@heffalump.save
- Heffalump.get(*@heffalump.key).color.should == 'violet'
+ 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.get(*@heffalump.key).color.should == color
+ heffalump_model.get(*@heffalump.key).color.should == color
end
end
else
@@ -123,7 +134,7 @@ class ::Heffalump
if adapter_supports?(:delete)
describe '#delete' do
before do
- @heffalump = Heffalump.create(:color => 'forest green')
+ @heffalump = heffalump_model.create(:color => 'forest green')
end
it 'should not raise any errors' do
@@ -135,7 +146,7 @@ class ::Heffalump
it 'should delete the requested resource' do
id = @heffalump.id
@heffalump.destroy
- Heffalump.get(id).should be_nil
+ heffalump_model.get(id).should be_nil
end
end
else
@@ -145,178 +156,178 @@ class ::Heffalump
if adapter_supports?(:read, :create)
describe 'query matching' do
before :all do
- @red = Heffalump.create(:color => 'red')
- @two = Heffalump.create(:num_spots => 2)
- @five = Heffalump.create(:num_spots => 5)
+ @red = heffalump_model.create(:color => 'red')
+ @two = heffalump_model.create(:num_spots => 2)
+ @five = heffalump_model.create(:num_spots => 5)
end
describe 'conditions' do
describe 'eql' do
it 'should be able to search for objects included in an inclusive range of values' do
- Heffalump.all(:num_spots => 1..5).should be_include(@five)
+ heffalump_model.all(:num_spots => 1..5).should be_include(@five)
end
it 'should be able to search for objects included in an exclusive range of values' do
- Heffalump.all(:num_spots => 1...6).should be_include(@five)
+ heffalump_model.all(:num_spots => 1...6).should be_include(@five)
end
it 'should not be able to search for values not included in an inclusive range of values' do
- Heffalump.all(:num_spots => 1..4).should_not be_include(@five)
+ heffalump_model.all(:num_spots => 1..4).should_not be_include(@five)
end
it 'should not be able to search for values not included in an exclusive range of values' do
- Heffalump.all(:num_spots => 1...5).should_not be_include(@five)
+ heffalump_model.all(:num_spots => 1...5).should_not be_include(@five)
end
end
describe 'not' do
it 'should be able to search for objects with not equal value' do
- Heffalump.all(:color.not => 'red').should_not be_include(@red)
+ heffalump_model.all(:color.not => 'red').should_not be_include(@red)
end
it 'should include objects that are not like the value' do
- Heffalump.all(:color.not => 'black').should be_include(@red)
+ heffalump_model.all(:color.not => 'black').should be_include(@red)
end
it 'should be able to search for objects with not nil value' do
- Heffalump.all(:color.not => nil).should be_include(@red)
+ heffalump_model.all(:color.not => nil).should be_include(@red)
end
it 'should not include objects with a nil value' do
- Heffalump.all(:color.not => nil).should_not be_include(@two)
+ heffalump_model.all(:color.not => nil).should_not be_include(@two)
end
it 'should be able to search for object with a nil value using required properties' do
- Heffalump.all(:id.not => nil).should == [ @red, @two, @five ]
+ heffalump_model.all(:id.not => nil).should == [ @red, @two, @five ]
end
it 'should be able to search for objects not in an empty list (match all)' do
- Heffalump.all(:color.not => []).should == [ @red, @two, @five ]
+ heffalump_model.all(:color.not => []).should == [ @red, @two, @five ]
end
it 'should be able to search for objects in an empty list and another OR condition (match none on the empty list)' do
- Heffalump.all(
+ heffalump_model.all(
:conditions => DataMapper::Query::Conditions::Operation.new(
:or,
- DataMapper::Query::Conditions::Comparison.new(:in, Heffalump.properties[:color], []),
- DataMapper::Query::Conditions::Comparison.new(:in, Heffalump.properties[:num_spots], [5])
+ DataMapper::Query::Conditions::Comparison.new(:in, heffalump_model.properties[:color], []),
+ DataMapper::Query::Conditions::Comparison.new(:in, heffalump_model.properties[:num_spots], [5])
)
).should == [ @five ]
end
it 'should be able to search for objects not included in an array of values' do
- Heffalump.all(:num_spots.not => [ 1, 3, 5, 7 ]).should be_include(@two)
+ heffalump_model.all(:num_spots.not => [ 1, 3, 5, 7 ]).should be_include(@two)
end
it 'should be able to search for objects not included in an array of values' do
- Heffalump.all(:num_spots.not => [ 1, 3, 5, 7 ]).should_not be_include(@five)
+ heffalump_model.all(:num_spots.not => [ 1, 3, 5, 7 ]).should_not be_include(@five)
end
it 'should be able to search for objects not included in an inclusive range of values' do
- Heffalump.all(:num_spots.not => 1..4).should be_include(@five)
+ heffalump_model.all(:num_spots.not => 1..4).should be_include(@five)
end
it 'should be able to search for objects not included in an exclusive range of values' do
- Heffalump.all(:num_spots.not => 1...5).should be_include(@five)
+ heffalump_model.all(:num_spots.not => 1...5).should be_include(@five)
end
it 'should not be able to search for values not included in an inclusive range of values' do
- Heffalump.all(:num_spots.not => 1..5).should_not be_include(@five)
+ heffalump_model.all(:num_spots.not => 1..5).should_not be_include(@five)
end
it 'should not be able to search for values not included in an exclusive range of values' do
- Heffalump.all(:num_spots.not => 1...6).should_not be_include(@five)
+ heffalump_model.all(:num_spots.not => 1...6).should_not be_include(@five)
end
end
describe 'like' do
it 'should be able to search for objects that match value' do
- Heffalump.all(:color.like => '%ed').should be_include(@red)
+ heffalump_model.all(:color.like => '%ed').should be_include(@red)
end
it 'should not search for objects that do not match the value' do
- Heffalump.all(:color.like => '%blak%').should_not be_include(@red)
+ heffalump_model.all(:color.like => '%blak%').should_not be_include(@red)
end
end
describe 'regexp' do
before do
- if (defined?(DataMapper::Adapters::SqliteAdapter) && @adapter.kind_of?(DataMapper::Adapters::SqliteAdapter) ||
- defined?(DataMapper::Adapters::SqlserverAdapter) && @adapter.kind_of?(DataMapper::Adapters::SqlserverAdapter))
+ if (defined?(DataMapper::Adapters::SqliteAdapter) && adapter.kind_of?(DataMapper::Adapters::SqliteAdapter) ||
+ defined?(DataMapper::Adapters::SqlserverAdapter) && adapter.kind_of?(DataMapper::Adapters::SqlserverAdapter))
pending 'delegate regexp matches to same system that the InMemory and YAML adapters use'
end
end
it 'should be able to search for objects that match value' do
- Heffalump.all(:color => /ed/).should be_include(@red)
+ heffalump_model.all(:color => /ed/).should be_include(@red)
end
it 'should not be able to search for objects that do not match the value' do
- Heffalump.all(:color => /blak/).should_not be_include(@red)
+ heffalump_model.all(:color => /blak/).should_not be_include(@red)
end
it 'should be able to do a negated search for objects that match value' do
- Heffalump.all(:color.not => /blak/).should be_include(@red)
+ heffalump_model.all(:color.not => /blak/).should be_include(@red)
end
it 'should not be able to do a negated search for objects that do not match value' do
- Heffalump.all(:color.not => /ed/).should_not be_include(@red)
+ heffalump_model.all(:color.not => /ed/).should_not be_include(@red)
end
end
describe 'gt' do
it 'should be able to search for objects with value greater than' do
- Heffalump.all(:num_spots.gt => 1).should be_include(@two)
+ heffalump_model.all(:num_spots.gt => 1).should be_include(@two)
end
it 'should not find objects with a value less than' do
- Heffalump.all(:num_spots.gt => 3).should_not be_include(@two)
+ heffalump_model.all(:num_spots.gt => 3).should_not be_include(@two)
end
end
describe 'gte' do
it 'should be able to search for objects with value greater than' do
- Heffalump.all(:num_spots.gte => 1).should be_include(@two)
+ heffalump_model.all(:num_spots.gte => 1).should be_include(@two)
end
it 'should be able to search for objects with values equal to' do
- Heffalump.all(:num_spots.gte => 2).should be_include(@two)
+ heffalump_model.all(:num_spots.gte => 2).should be_include(@two)
end
it 'should not find objects with a value less than' do
- Heffalump.all(:num_spots.gte => 3).should_not be_include(@two)
+ heffalump_model.all(:num_spots.gte => 3).should_not be_include(@two)
end
end
describe 'lt' do
it 'should be able to search for objects with value less than' do
- Heffalump.all(:num_spots.lt => 3).should be_include(@two)
+ heffalump_model.all(:num_spots.lt => 3).should be_include(@two)
end
it 'should not find objects with a value less than' do
- Heffalump.all(:num_spots.gt => 2).should_not be_include(@two)
+ heffalump_model.all(:num_spots.gt => 2).should_not be_include(@two)
end
end
describe 'lte' do
it 'should be able to search for objects with value less than' do
- Heffalump.all(:num_spots.lte => 3).should be_include(@two)
+ heffalump_model.all(:num_spots.lte => 3).should be_include(@two)
end
it 'should be able to search for objects with values equal to' do
- Heffalump.all(:num_spots.lte => 2).should be_include(@two)
+ heffalump_model.all(:num_spots.lte => 2).should be_include(@two)
end
it 'should not find objects with a value less than' do
- Heffalump.all(:num_spots.lte => 1).should_not be_include(@two)
+ heffalump_model.all(:num_spots.lte => 1).should_not be_include(@two)
end
end
end
describe 'limits' do
it 'should be able to limit the objects' do
- Heffalump.all(:limit => 2).length.should == 2
+ heffalump_model.all(:limit => 2).length.should == 2
end
end
end
Something went wrong with that request. Please try again.