Permalink
Browse files

Add spec_model_no_assoc tasks for running model specs without the ass…

…ociations code

This should be used to check that the main model code has no
dependencies on the associations code.  There were a few places
in record_spec.rb that assumed the associations code was loaded,
move those specs to the associations spec.
  • Loading branch information...
1 parent a3b50ea commit 2d542f9d83a00c93418a1cd0183974b4efabc04b @jeremyevans committed Mar 26, 2012
Showing with 93 additions and 57 deletions.
  1. +7 −0 Rakefile
  2. +86 −0 spec/model/associations_spec.rb
  3. +0 −57 spec/model/record_spec.rb
View
@@ -139,12 +139,19 @@ begin
spec_with_cov.call("spec", Dir["spec/{core,model}/*_spec.rb"], "Run core and model specs"){|t| t.rcov_opts.concat(%w'--exclude "lib/sequel/adapters/([a-ln-z]|m[a-np-z])"')}
spec.call("spec_core", Dir["spec/core/*_spec.rb"], "Run core specs")
spec.call("spec_model", Dir["spec/model/*_spec.rb"], "Run model specs")
+ spec.call("_spec_model_no_assoc", Dir["spec/model/*_spec.rb"].delete_if{|f| f =~ /association|eager_loading/}, '')
spec_with_cov.call("spec_plugin", Dir["spec/extensions/*_spec.rb"], "Run extension/plugin specs")
spec_with_cov.call("spec_integration", Dir["spec/integration/*_test.rb"], "Run integration tests")
%w'postgres sqlite mysql informix oracle firebird mssql db2'.each do |adapter|
spec_with_cov.call("spec_#{adapter}", ["spec/adapters/#{adapter}_spec.rb"] + Dir["spec/integration/*_test.rb"], "Run #{adapter} specs")
end
+
+ desc "Run model specs without the associations code"
+ task :spec_model_no_assoc do
+ ENV['SEQUEL_NO_ASSOCIATIONS'] = '1'
+ Rake::Task['_spec_model_no_assoc'].invoke
+ end
rescue LoadError
task :default do
puts "Must install rspec to run the default task (which runs specs)"
@@ -3017,3 +3017,89 @@ def al(v)
@bar.mtmfoos.should == []
end
end
+
+describe "Model#pk_or_nil" do
+ before do
+ @m = Class.new(Sequel::Model)
+ @m.columns :id, :x, :y
+ end
+
+ it "should be default return the value of the :id column" do
+ m = @m.load(:id => 111, :x => 2, :y => 3)
+ m.pk_or_nil.should == 111
+ end
+
+ it "should be return the primary key value for custom primary key" do
+ @m.set_primary_key :x
+ m = @m.load(:id => 111, :x => 2, :y => 3)
+ m.pk_or_nil.should == 2
+ end
+
+ it "should be return the primary key value for composite primary key" do
+ @m.set_primary_key [:y, :x]
+ m = @m.load(:id => 111, :x => 2, :y => 3)
+ m.pk_or_nil.should == [3, 2]
+ end
+
+ it "should raise if no primary key" do
+ @m.set_primary_key nil
+ m = @m.new(:id => 111, :x => 2, :y => 3)
+ m.pk_or_nil.should be_nil
+
+ @m.no_primary_key
+ m = @m.new(:id => 111, :x => 2, :y => 3)
+ m.pk_or_nil.should be_nil
+ end
+end
+
+describe Sequel::Model, "#refresh" do
+ before do
+ @c = Class.new(Sequel::Model(:items)) do
+ unrestrict_primary_key
+ columns :id, :x
+ end
+ MODEL_DB.reset
+ end
+
+ specify "should remove cached associations" do
+ @c.many_to_one :node, :class=>@c
+ @m = @c.new(:id => 555)
+ @m.associations[:node] = 15
+ @m.reload
+ @m.associations.should == {}
+ end
+end
+
+describe "Model#freeze" do
+ before do
+ class ::Album < Sequel::Model
+ columns :id
+ class B < Sequel::Model
+ columns :id, :album_id
+ many_to_one :album, :class=>Album
+ end
+ one_to_one :b, :key=>:album_id, :class=>B
+ end
+ @o = Album.load(:id=>1).freeze
+ MODEL_DB.sqls
+ end
+ after do
+ Object.send(:remove_const, :Album)
+ end
+
+ it "should freeze the object's associations" do
+ @o.associations.frozen?.should be_true
+ end
+
+ it "should not break associations getters" do
+ Album::B.dataset._fetch = {:album_id=>1, :id=>2}
+ @o.b.should == Album::B.load(:id=>2, :album_id=>1)
+ @o.associations[:b].should be_nil
+ end
+
+ it "should not break reciprocal associations" do
+ b = Album::B.load(:id=>2, :album_id=>nil)
+ b.album = @o
+ @o.associations[:b].should be_nil
+ end
+end
View
@@ -338,9 +338,7 @@ class ::Album < Sequel::Model
columns :id
class B < Sequel::Model
columns :id, :album_id
- many_to_one :album, :class=>Album
end
- one_to_one :b, :key=>:album_id, :class=>B
end
@o = Album.load(:id=>1).freeze
MODEL_DB.sqls
@@ -355,7 +353,6 @@ class B < Sequel::Model
it "should freeze the object's values, associations, changed_columns, errors, and this" do
@o.values.frozen?.should be_true
- @o.associations.frozen?.should be_true
@o.changed_columns.frozen?.should be_true
@o.errors.frozen?.should be_true
@o.this.frozen?.should be_true
@@ -365,18 +362,6 @@ class B < Sequel::Model
Sequel::Model::BOOLEAN_SETTINGS.each{|m| @o.send(m) == Album.send(m)}
end
- it "should not break associations getters" do
- Album::B.dataset._fetch = {:album_id=>1, :id=>2}
- @o.b.should == Album::B.load(:id=>2, :album_id=>1)
- @o.associations[:b].should be_nil
- end
-
- it "should not break reciprocal associations" do
- b = Album::B.load(:id=>2, :album_id=>nil)
- b.album = @o
- @o.associations[:b].should be_nil
- end
-
it "should have working new? method" do
@o.new?.should be_false
Album.new.freeze.new?.should be_true
@@ -709,40 +694,6 @@ def o.modified?; false; end
end
end
-describe "Model#pk_or_nil" do
- before do
- @m = Class.new(Sequel::Model)
- @m.columns :id, :x, :y
- end
-
- it "should be default return the value of the :id column" do
- m = @m.load(:id => 111, :x => 2, :y => 3)
- m.pk_or_nil.should == 111
- end
-
- it "should be return the primary key value for custom primary key" do
- @m.set_primary_key :x
- m = @m.load(:id => 111, :x => 2, :y => 3)
- m.pk_or_nil.should == 2
- end
-
- it "should be return the primary key value for composite primary key" do
- @m.set_primary_key [:y, :x]
- m = @m.load(:id => 111, :x => 2, :y => 3)
- m.pk_or_nil.should == [3, 2]
- end
-
- it "should raise if no primary key" do
- @m.set_primary_key nil
- m = @m.new(:id => 111, :x => 2, :y => 3)
- m.pk_or_nil.should be_nil
-
- @m.no_primary_key
- m = @m.new(:id => 111, :x => 2, :y => 3)
- m.pk_or_nil.should be_nil
- end
-end
-
describe "Model#pk_hash" do
before do
@m = Class.new(Sequel::Model)
@@ -1494,14 +1445,6 @@ def z=(v)
@m[:x].should == 'kaboom'
MODEL_DB.sqls.should == ["SELECT * FROM items WHERE (id = 555) LIMIT 1"]
end
-
- specify "should remove cached associations" do
- @c.many_to_one :node, :class=>@c
- @m = @c.new(:id => 555)
- @m.associations[:node] = 15
- @m.reload
- @m.associations.should == {}
- end
end
describe Sequel::Model, "typecasting" do

0 comments on commit 2d542f9

Please sign in to comment.