diff options
Diffstat (limited to 'spec/attributes')
-rw-r--r-- | spec/attributes/attribute_spec.rb | 168 | ||||
-rw-r--r-- | spec/attributes/boolean_spec.rb | 57 | ||||
-rw-r--r-- | spec/attributes/float_spec.rb | 119 | ||||
-rw-r--r-- | spec/attributes/header_spec.rb | 42 | ||||
-rw-r--r-- | spec/attributes/integer_spec.rb | 119 | ||||
-rw-r--r-- | spec/attributes/string_spec.rb | 43 | ||||
-rw-r--r-- | spec/attributes/time_spec.rb | 24 |
7 files changed, 168 insertions, 404 deletions
diff --git a/spec/attributes/attribute_spec.rb b/spec/attributes/attribute_spec.rb new file mode 100644 index 0000000000..faef096792 --- /dev/null +++ b/spec/attributes/attribute_spec.rb @@ -0,0 +1,168 @@ +require 'spec_helper' + +module Arel + module Attributes + describe 'attribute' do + describe '#not_eq' do + it 'should create a NotEqual node' do + relation = Table.new(:users) + relation[:id].not_eq(10).should be_kind_of Nodes::NotEqual + end + + it 'should generate != in sql' do + relation = Table.new(:users) + mgr = relation.project relation[:id] + mgr.where relation[:id].not_eq(10) + mgr.to_sql.should be_like %{ + SELECT "users"."id" FROM "users" WHERE "users"."id" != 10 + } + end + + it 'should handle nil' do + relation = Table.new(:users) + mgr = relation.project relation[:id] + mgr.where relation[:id].not_eq(nil) + mgr.to_sql.should be_like %{ + SELECT "users"."id" FROM "users" WHERE "users"."id" IS NOT NULL + } + end + end + + describe '#gt' do + it 'should create a GreaterThan node' do + relation = Table.new(:users) + relation[:id].gt(10).should be_kind_of Nodes::GreaterThan + end + + it 'should generate >= in sql' do + relation = Table.new(:users) + mgr = relation.project relation[:id] + mgr.where relation[:id].gt(10) + mgr.to_sql.should be_like %{ + SELECT "users"."id" FROM "users" WHERE "users"."id" > 10 + } + end + end + + describe '#gteq' do + it 'should create a GreaterThanOrEqual node' do + relation = Table.new(:users) + relation[:id].gteq(10).should be_kind_of Nodes::GreaterThanOrEqual + end + + it 'should generate >= in sql' do + relation = Table.new(:users) + mgr = relation.project relation[:id] + mgr.where relation[:id].gteq(10) + mgr.to_sql.should be_like %{ + SELECT "users"."id" FROM "users" WHERE "users"."id" >= 10 + } + end + end + + describe '#average' do + it 'should create a AVG node' do + relation = Table.new(:users) + relation[:id].average.should be_kind_of Nodes::Avg + end + + # FIXME: backwards compat. Is this really necessary? + it 'should set the alias to "avg_id"' do + relation = Table.new(:users) + mgr = relation.project relation[:id].average + mgr.to_sql.should be_like %{ + SELECT AVG("users"."id") AS avg_id + FROM "users" + } + end + end + + describe '#maximum' do + it 'should create a MAX node' do + relation = Table.new(:users) + relation[:id].maximum.should be_kind_of Nodes::Max + end + + # FIXME: backwards compat. Is this really necessary? + it 'should set the alias to "max_id"' do + relation = Table.new(:users) + mgr = relation.project relation[:id].maximum + mgr.to_sql.should be_like %{ + SELECT MAX("users"."id") AS max_id + FROM "users" + } + end + end + + describe '#minimum' do + it 'should create a Min node' do + relation = Table.new(:users) + relation[:id].minimum.should be_kind_of Nodes::Min + end + end + + describe '#sum' do + it 'should create a SUM node' do + relation = Table.new(:users) + relation[:id].sum.should be_kind_of Nodes::Sum + end + + # FIXME: backwards compat. Is this really necessary? + it 'should set the alias to "sum_id"' do + relation = Table.new(:users) + mgr = relation.project relation[:id].sum + mgr.to_sql.should be_like %{ + SELECT SUM("users"."id") AS sum_id + FROM "users" + } + end + end + + describe '#count' do + it 'should return a count node' do + relation = Table.new(:users) + relation[:id].count.should be_kind_of Nodes::Count + end + + it 'should take a distinct param' do + relation = Table.new(:users) + count = relation[:id].count(nil) + count.should be_kind_of Nodes::Count + count.distinct.should be_nil + end + end + + describe '#eq' do + it 'should return an equality node' do + attribute = Attribute.new nil, nil, nil + equality = attribute.eq 1 + check equality.left.should == attribute + check equality.right.should == 1 + equality.should be_kind_of Nodes::Equality + end + end + + describe '#in' do + it 'can be constructed with a list' do + end + + it 'should return an in node' do + attribute = Attribute.new nil, nil, nil + node = Nodes::In.new attribute, [1,2,3] + check node.left.should == attribute + check node.right.should == [1, 2, 3] + end + end + end + + describe 'equality' do + describe '#to_sql' do + it 'should produce sql' do + table = Table.new :users + condition = table['id'].eq 1 + condition.to_sql.should == '"users"."id" = 1' + end + end + end + end +end diff --git a/spec/attributes/boolean_spec.rb b/spec/attributes/boolean_spec.rb deleted file mode 100644 index 12b243e828..0000000000 --- a/spec/attributes/boolean_spec.rb +++ /dev/null @@ -1,57 +0,0 @@ -require 'spec_helper' - -module Arel - describe "Attributes::Boolean" do - - before :all do - @relation = Model.build do |r| - r.engine Testing::Engine.new - r.attribute :awesome, Attributes::Boolean - end - end - - def type_cast(val) - @relation[:awesome].type_cast(val) - end - - describe "#type_cast" do - it "returns same value if passed a boolean" do - val = true - type_cast(val).should eql(val) - end - - it "returns boolean representation (false) of nil" do - type_cast(nil).should eql(false) - end - - it "returns boolean representation of 'true', 'false'" do - type_cast('true').should eql(true) - type_cast('false').should eql(false) - end - - it "returns boolean representation of :true, :false" do - type_cast(:true).should eql(true) - type_cast(:false).should eql(false) - end - - it "returns boolean representation of 0, 1" do - type_cast(1).should == true - type_cast(0).should == false - end - - it "calls #to_s on arbitrary objects" do - obj = Object.new - obj.extend Module.new { def to_s ; 'true' ; end } - type_cast(obj).should == true - end - - [ Object.new, 'string', '00.0', 5 ].each do |value| - it "raises exception when attempting type_cast of non-boolean value #{value.inspect}" do - lambda do - type_cast(value) - end.should raise_error(TypecastError, /could not typecast/) - end - end - end - end -end diff --git a/spec/attributes/float_spec.rb b/spec/attributes/float_spec.rb deleted file mode 100644 index 3a9a8debbc..0000000000 --- a/spec/attributes/float_spec.rb +++ /dev/null @@ -1,119 +0,0 @@ -require 'spec_helper' -require 'bigdecimal' - -module Arel - describe "Attributes::Float" do - - before :all do - @relation = Model.build do |r| - r.engine Testing::Engine.new - r.attribute :percentage, Attributes::Float - end - end - - def type_cast(val) - @relation[:percentage].type_cast(val) - end - - describe "#type_cast" do - it "returns same value if an float" do - type_cast(24.01).should eql(24.01) - end - - it "returns nil if passed nil" do - type_cast(nil).should be_nil - end - - it "returns nil if passed empty string" do - type_cast('').should be_nil - end - - it "returns float representation of a zero string float" do - type_cast('0').should eql(0.0) - end - - it "returns float representation of a positive string integer" do - type_cast('24').should eql(24.0) - end - - it "returns float representation of a positive string integer with spaces" do - type_cast(' 24').should eql(24.0) - type_cast('24 ').should eql(24.0) - end - - it "returns float representation of a negative string float" do - type_cast('-24.23').should eql(-24.23) - end - - it "returns float representation of a negative string integer with spaces" do - type_cast('-24 ').should eql(-24.0) - type_cast(' -24').should eql(-24.0) - end - - it "returns integer representation of a zero string float" do - type_cast('0.0').should eql(0.0) - end - - it "returns integer representation of a positive string float" do - type_cast('24.35').should eql(24.35) - end - - it "returns integer representation of a positive string float with spaces" do - type_cast(' 24.35').should eql(24.35) - type_cast('24.35 ').should eql(24.35) - end - - it "returns integer representation of a negative string float" do - type_cast('-24.35').should eql(-24.35) - end - - it "returns integer representation of a negative string float with spaces" do - type_cast(' -24.35 ').should eql(-24.35) - end - - it "returns integer representation of a zero string float, with no leading digits" do - type_cast('.0').should eql(0.0) - end - - it "returns integer representation of a zero string float, with no leading digits with spaces" do - type_cast(' .0').should eql(0.0) - end - - it "returns integer representation of a positive string float, with no leading digits" do - type_cast('.41').should eql(0.41) - end - - it "returns integer representation of a zero float" do - type_cast(0.0).should eql(0.0) - end - - it "returns integer representation of a positive float" do - type_cast(24.35).should eql(24.35) - end - - it "returns integer representation of a negative float" do - type_cast(-24.35).should eql(-24.35) - end - - it "returns integer representation of a zero decimal" do - type_cast(BigDecimal('0.0')).should eql(0.0) - end - - it "returns integer representation of a positive decimal" do - type_cast(BigDecimal('24.35')).should eql(24.35) - end - - it "returns integer representation of a negative decimal" do - type_cast(BigDecimal('-24.35')).should eql(-24.35) - end - - [ Object.new, true, '00.0', '0.', 'string' ].each do |value| - it "raises exception when attempting type_cast of non-numeric value #{value.inspect}" do - lambda do - type_cast(value) - end.should raise_error(TypecastError, /could not typecast/) - end - end - end - end -end diff --git a/spec/attributes/header_spec.rb b/spec/attributes/header_spec.rb deleted file mode 100644 index 51cba93df3..0000000000 --- a/spec/attributes/header_spec.rb +++ /dev/null @@ -1,42 +0,0 @@ -require 'spec_helper' - -module Arel - describe "Header" do - before :all do - @relation = Model.build do |r| - r.attribute :id, Attributes::Integer - r.attribute :name, Attributes::String - r.attribute :age, Attributes::Integer - end - - @other = Model.build do |r| - r.attribute :foo, Attributes::String - end - - @subset = Model.build do |r| - r.attribute :id, Attributes::Integer - end - end - - describe "attribute lookup" do - it "finds attributes by name" do - @relation.attributes[:name].should == Attributes::String.new(@relation, :name) - end - - it "returns nil if no attribute is found" do - @relation.attributes[:does_not_exist].should be_nil - @relation[:does_not_exist].should be_nil - end - end - - describe "#union" do - it "keeps all attributes from disjoint headers" do - (@relation.attributes.union @other.attributes).to_ary.should have(4).items - end - - it "keeps all attributes from both relations even if they seem like subsets" do - (@relation.attributes.union @subset.attributes).to_ary.should have(4).items - end - end - end -end diff --git a/spec/attributes/integer_spec.rb b/spec/attributes/integer_spec.rb deleted file mode 100644 index 59b7afb2b4..0000000000 --- a/spec/attributes/integer_spec.rb +++ /dev/null @@ -1,119 +0,0 @@ -require 'spec_helper' -require 'bigdecimal' - -module Arel - describe "Attributes::Integer" do - - before :all do - @relation = Model.build do |r| - r.engine Testing::Engine.new - r.attribute :age, Attributes::Integer - end - end - - def type_cast(val) - @relation[:age].type_cast(val) - end - - describe "#type_cast" do - it "returns same value if an integer" do - type_cast(24).should eql(24) - end - - it "returns nil if passed nil" do - type_cast(nil).should be_nil - end - - it "returns nil if passed empty string" do - type_cast('').should be_nil - end - - it "returns integer representation of a zero string integer" do - type_cast('0').should eql(0) - end - - it "returns integer representation of a positive string integer" do - type_cast('24').should eql(24) - end - - it "returns integer representation of a positive string integer with spaces" do - type_cast(' 24').should eql(24) - type_cast('24 ').should eql(24) - end - - it "returns integer representation of a negative string integer" do - type_cast('-24').should eql(-24) - end - - it "returns integer representation of a negative string integer with spaces" do - type_cast('-24 ').should eql(-24) - type_cast(' -24').should eql(-24) - end - - it "returns integer representation of a zero string float" do - type_cast('0.0').should eql(0) - end - - it "returns integer representation of a positive string float" do - type_cast('24.35').should eql(24) - end - - it "returns integer representation of a positive string float with spaces" do - type_cast(' 24.35').should eql(24) - type_cast('24.35 ').should eql(24) - end - - it "returns integer representation of a negative string float" do - type_cast('-24.35').should eql(-24) - end - - it "returns integer representation of a negative string float with spaces" do - type_cast(' -24.35 ').should eql(-24) - end - - it "returns integer representation of a zero string float, with no leading digits" do - type_cast('.0').should eql(0) - end - - it "returns integer representation of a zero string float, with no leading digits with spaces" do - type_cast(' .0').should eql(0) - end - - it "returns integer representation of a positive string float, with no leading digits" do - type_cast('.41').should eql(0) - end - - it "returns integer representation of a zero float" do - type_cast(0.0).should eql(0) - end - - it "returns integer representation of a positive float" do - type_cast(24.35).should eql(24) - end - - it "returns integer representation of a negative float" do - type_cast(-24.35).should eql(-24) - end - - it "returns integer representation of a zero decimal" do - type_cast(BigDecimal('0.0')).should eql(0) - end - - it "returns integer representation of a positive decimal" do - type_cast(BigDecimal('24.35')).should eql(24) - end - - it "returns integer representation of a negative decimal" do - type_cast(BigDecimal('-24.35')).should eql(-24) - end - - [ Object.new, true, '00.0', '0.', 'string' ].each do |value| - it "raises exception when attempting type_cast of non-numeric value #{value.inspect}" do - lambda do - type_cast(value) - end.should raise_error(TypecastError, /could not typecast/) - end - end - end - end -end diff --git a/spec/attributes/string_spec.rb b/spec/attributes/string_spec.rb deleted file mode 100644 index 487d82249d..0000000000 --- a/spec/attributes/string_spec.rb +++ /dev/null @@ -1,43 +0,0 @@ -require 'spec_helper' -require 'bigdecimal' - -module Arel - describe "Attributes::String" do - - before :all do - @relation = Model.build do |r| - r.engine Testing::Engine.new - r.attribute :name, Attributes::String - end - end - - def type_cast(val) - @relation[:name].type_cast(val) - end - - describe "#type_cast" do - it "returns same value if passed a String" do - val = "hell" - type_cast(val).should eql(val) - end - - it "returns nil if passed nil" do - type_cast(nil).should be_nil - end - - it "returns String representation of Symbol" do - type_cast(:hello).should == "hello" - end - - it "returns string representation of Integer" do - type_cast(1).should == '1' - end - - it "calls #to_s on arbitrary objects" do - obj = Object.new - obj.extend Module.new { def to_s ; 'hello' ; end } - type_cast(obj).should == 'hello' - end - end - end -end diff --git a/spec/attributes/time_spec.rb b/spec/attributes/time_spec.rb deleted file mode 100644 index 15384e2073..0000000000 --- a/spec/attributes/time_spec.rb +++ /dev/null @@ -1,24 +0,0 @@ -require 'spec_helper' -require 'bigdecimal' - -module Arel - describe "Attributes::Time" do - - before :all do - @relation = Model.build do |r| - r.engine Testing::Engine.new - r.attribute :created_at, Attributes::Time - end - end - - def type_cast(val) - @relation[:created_at].type_cast(val) - end - - describe "#type_cast" do - it "works" do - pending - end - end - end -end |