diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/algebra/integration/basic_spec.rb | 87 | ||||
-rw-r--r-- | spec/shared/relation_spec.rb | 92 | ||||
-rw-r--r-- | spec/spec_helper.rb | 4 | ||||
-rw-r--r-- | spec/support/matchers.rb | 3 | ||||
-rw-r--r-- | spec/support/matchers/be_like.rb | 2 | ||||
-rw-r--r-- | spec/support/matchers/disambiguate_attributes.rb | 2 | ||||
-rw-r--r-- | spec/support/matchers/hash_the_same_as.rb | 2 | ||||
-rw-r--r-- | spec/support/matchers/have_rows.rb | 18 |
8 files changed, 117 insertions, 93 deletions
diff --git a/spec/algebra/integration/basic_spec.rb b/spec/algebra/integration/basic_spec.rb index e48f94625d..ae9da3a037 100644 --- a/spec/algebra/integration/basic_spec.rb +++ b/spec/algebra/integration/basic_spec.rb @@ -1,93 +1,7 @@ require 'spec_helper' -def have_rows(expected) - simple_matcher "have rows" do |given, matcher| - found, got, expected = [], [], expected.map { |r| r.tuple } - given.each do |row| - got << row.tuple - found << expected.find { |r| row.tuple == r } - end - - matcher.failure_message = "Expected to get:\n" \ - "#{expected.map {|r| " #{r.inspect}" }.join("\n")}\n" \ - "instead, got:\n" \ - "#{got.map {|r| " #{r.inspect}" }.join("\n")}" - - found.compact.length == expected.length && got.compact.length == expected.length - end -end - -share_examples_for 'A Relation' do - - before :all do - # The two needed instance variables need to be set in a - # before :all callback. - # @relation is the relation being tested here. - # @expected is an array of the elements that are expected to be in - # the relation. - %w[ @relation @expected ].each do |ivar| - raise "#{ivar} needs to be defined" unless instance_variable_get(ivar) - end - - # There needs to be enough items to be able to run all the tests - raise "@expected needs to have at least 6 items" unless @expected.length >= 6 - end - - describe "#each" do - it "iterates over the rows in any order" do - @relation.should have_rows(@expected) - end - end - - describe "#where" do - before :all do - @expected = @expected.sort_by { |r| r[@relation[:age]] } - @pivot = @expected[@expected.length / 2] - end - - it "finds rows with an equal to predicate" do - expected = @expected.select { |r| r[@relation[:age]] == @pivot[@relation[:age]] } - @relation.where(@relation[:age].eq(@pivot[@relation[:age]])).should have_rows(expected) - end - - it "finds rows with a not predicate" do - expected = @expected.select { |r| r[@relation[:age]] != @pivot[@relation[:age]] } - @relation.where(@relation[:age].not(@pivot[@relation[:age]])).should have_rows(expected) - end - - it "finds rows with a less than predicate" do - expected = @expected.select { |r| r[@relation[:age]] < @pivot[@relation[:age]] } - @relation.where(@relation[:age].lt(@pivot[@relation[:age]])).should have_rows(expected) - end - - it "finds rows with a less than or equal to predicate" do - expected = @expected.select { |r| r[@relation[:age]] <= @pivot[@relation[:age]] } - @relation.where(@relation[:age].lteq(@pivot[@relation[:age]])).should have_rows(expected) - end - - it "finds rows with a greater than predicate" do - expected = @expected.select { |r| r[@relation[:age]] > @pivot[@relation[:age]] } - @relation.where(@relation[:age].gt(@pivot[@relation[:age]])).should have_rows(expected) - end - - it "finds rows with a greater than or equal to predicate" do - expected = @expected.select { |r| r[@relation[:age]] >= @pivot[@relation[:age]] } - @relation.where(@relation[:age].gteq(@pivot[@relation[:age]])).should have_rows(expected) - end - - it "finds rows with a matches predicate" - - it "finds rows with an in predicate" do - pending - set = @expected[1..(@expected.length/2+1)] - @relation.all(:id.in => set.map { |r| r.id }).should have_resources(set) - end - end -end - module Arel describe "Relation" do - before :all do @engine = Testing::Engine.new @relation = Model.build do |r| @@ -113,6 +27,5 @@ module Arel @engine.rows.should == [[1, 'Foo', 10]] end end - end end
\ No newline at end of file diff --git a/spec/shared/relation_spec.rb b/spec/shared/relation_spec.rb new file mode 100644 index 0000000000..c492ca847e --- /dev/null +++ b/spec/shared/relation_spec.rb @@ -0,0 +1,92 @@ +share_examples_for 'A Relation' do + + before :all do + # The two needed instance variables need to be set in a + # before :all callback. + # @relation is the relation being tested here. + # @expected is an array of the elements that are expected to be in + # the relation. + %w[ @relation @expected ].each do |ivar| + raise "#{ivar} needs to be defined" unless instance_variable_get(ivar) + end + + # There needs to be enough items to be able to run all the tests + raise "@expected needs to have at least 6 items" unless @expected.length >= 6 + end + + describe "#each" do + it "iterates over the rows in any order" do + @relation.should have_rows(@expected) + end + end + + describe "#where" do + before :all do + @expected = @expected.sort_by { |r| r[@relation[:age]] } + @pivot = @expected[@expected.length / 2] + end + + it "finds rows with an equal to predicate" do + expected = @expected.select { |r| r[@relation[:age]] == @pivot[@relation[:age]] } + @relation.where(@relation[:age].eq(@pivot[@relation[:age]])).should have_rows(expected) + end + + it "finds rows with a not predicate" do + expected = @expected.select { |r| r[@relation[:age]] != @pivot[@relation[:age]] } + @relation.where(@relation[:age].not(@pivot[@relation[:age]])).should have_rows(expected) + end + + it "finds rows with a less than predicate" do + expected = @expected.select { |r| r[@relation[:age]] < @pivot[@relation[:age]] } + @relation.where(@relation[:age].lt(@pivot[@relation[:age]])).should have_rows(expected) + end + + it "finds rows with a less than or equal to predicate" do + expected = @expected.select { |r| r[@relation[:age]] <= @pivot[@relation[:age]] } + @relation.where(@relation[:age].lteq(@pivot[@relation[:age]])).should have_rows(expected) + end + + it "finds rows with a greater than predicate" do + expected = @expected.select { |r| r[@relation[:age]] > @pivot[@relation[:age]] } + @relation.where(@relation[:age].gt(@pivot[@relation[:age]])).should have_rows(expected) + end + + it "finds rows with a greater than or equal to predicate" do + expected = @expected.select { |r| r[@relation[:age]] >= @pivot[@relation[:age]] } + @relation.where(@relation[:age].gteq(@pivot[@relation[:age]])).should have_rows(expected) + end + + it "finds rows with a matches predicate" + + it "finds rows with an in predicate" do + pending + set = @expected[1..(@expected.length/2+1)] + @relation.all(:id.in => set.map { |r| r.id }).should have_resources(set) + end + end + + describe "#order" do + describe "by one attribute" do + before :all do + @expected.map! { |r| r[@relation[:age]] } + @expected.sort! + end + + it "can be specified as ascending order" do + actual = [] + @relation.order(@relation[:age].asc).each { |r| actual << r[@relation[:age]] } + actual.should == @expected + end + + it "can be specified as descending order" do + actual = [] + @relation.order(@relation[:age].desc).each { |r| actual << r[@relation[:age]] } + actual.should == @expected.reverse + end + end + + describe "by two attributes" do + it "works" + end + end +end
\ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 894e70c29d..5941840437 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,12 +7,12 @@ require 'pp' require 'fileutils' require 'arel' -Dir["#{dir}/support/*.rb"].each do |file| +Dir["#{dir}/{support,shared}/*.rb"].each do |file| require file end Spec::Runner.configure do |config| - config.include BeLikeMatcher, HashTheSameAsMatcher, DisambiguateAttributesMatcher + config.include Matchers config.include AdapterGuards config.include Check diff --git a/spec/support/matchers.rb b/spec/support/matchers.rb index 6f50d6cbc7..8cef5d947e 100644 --- a/spec/support/matchers.rb +++ b/spec/support/matchers.rb @@ -1,3 +1,4 @@ require "support/matchers/be_like" require "support/matchers/disambiguate_attributes" -require "support/matchers/hash_the_same_as"
\ No newline at end of file +require "support/matchers/hash_the_same_as" +require "support/matchers/have_rows"
\ No newline at end of file diff --git a/spec/support/matchers/be_like.rb b/spec/support/matchers/be_like.rb index c9d4d4b979..0608abbbb4 100644 --- a/spec/support/matchers/be_like.rb +++ b/spec/support/matchers/be_like.rb @@ -1,4 +1,4 @@ -module BeLikeMatcher +module Matchers class BeLike def initialize(expected) @expected = expected diff --git a/spec/support/matchers/disambiguate_attributes.rb b/spec/support/matchers/disambiguate_attributes.rb index bc4a5215d4..cec5924ca1 100644 --- a/spec/support/matchers/disambiguate_attributes.rb +++ b/spec/support/matchers/disambiguate_attributes.rb @@ -1,4 +1,4 @@ -module DisambiguateAttributesMatcher +module Matchers class DisambiguateAttributes def initialize(attributes) @attributes = attributes diff --git a/spec/support/matchers/hash_the_same_as.rb b/spec/support/matchers/hash_the_same_as.rb index 03e955a0cb..ed00d37f28 100644 --- a/spec/support/matchers/hash_the_same_as.rb +++ b/spec/support/matchers/hash_the_same_as.rb @@ -1,4 +1,4 @@ -module HashTheSameAsMatcher +module Matchers class HashTheSameAs def initialize(expected) @expected = expected diff --git a/spec/support/matchers/have_rows.rb b/spec/support/matchers/have_rows.rb new file mode 100644 index 0000000000..7d9c6a20c9 --- /dev/null +++ b/spec/support/matchers/have_rows.rb @@ -0,0 +1,18 @@ +module Matchers + def have_rows(expected) + simple_matcher "have rows" do |given, matcher| + found, got, expected = [], [], expected.map { |r| r.tuple } + given.each do |row| + got << row.tuple + found << expected.find { |r| row.tuple == r } + end + + matcher.failure_message = "Expected to get:\n" \ + "#{expected.map {|r| " #{r.inspect}" }.join("\n")}\n" \ + "instead, got:\n" \ + "#{got.map {|r| " #{r.inspect}" }.join("\n")}" + + found.compact.length == expected.length && got.compact.length == expected.length + end + end +end
\ No newline at end of file |