diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/arel/algebra/integration/basic_spec.rb | 120 | ||||
-rw-r--r-- | spec/doubles/hash.rb | 27 | ||||
-rw-r--r-- | spec/spec/fixtures/fixture_database.sqlite3 | bin | 0 -> 4096 bytes | |||
-rw-r--r-- | spec/spec_helper.rb | 46 | ||||
-rw-r--r-- | spec/support/check.rb | 6 | ||||
-rw-r--r-- | spec/support/connections/mysql_connection.rb (renamed from spec/connections/mysql_connection.rb) | 0 | ||||
-rw-r--r-- | spec/support/connections/oracle_connection.rb (renamed from spec/connections/oracle_connection.rb) | 0 | ||||
-rw-r--r-- | spec/support/connections/postgresql_connection.rb (renamed from spec/connections/postgresql_connection.rb) | 0 | ||||
-rw-r--r-- | spec/support/connections/sqlite3_connection.rb (renamed from spec/connections/sqlite3_connection.rb) | 2 | ||||
-rw-r--r-- | spec/support/guards.rb | 28 | ||||
-rw-r--r-- | spec/support/matchers.rb | 3 | ||||
-rw-r--r-- | spec/support/matchers/be_like.rb (renamed from spec/matchers/be_like.rb) | 0 | ||||
-rw-r--r-- | spec/support/matchers/disambiguate_attributes.rb (renamed from spec/matchers/disambiguate_attributes.rb) | 0 | ||||
-rw-r--r-- | spec/support/matchers/hash_the_same_as.rb (renamed from spec/matchers/hash_the_same_as.rb) | 0 | ||||
-rw-r--r-- | spec/support/schemas/mysql_schema.rb (renamed from spec/schemas/mysql_schema.rb) | 0 | ||||
-rw-r--r-- | spec/support/schemas/oracle_schema.rb (renamed from spec/schemas/oracle_schema.rb) | 0 | ||||
-rw-r--r-- | spec/support/schemas/postgresql_schema.rb (renamed from spec/schemas/postgresql_schema.rb) | 0 | ||||
-rw-r--r-- | spec/support/schemas/sqlite3_schema.rb (renamed from spec/schemas/sqlite3_schema.rb) | 0 |
18 files changed, 163 insertions, 69 deletions
diff --git a/spec/arel/algebra/integration/basic_spec.rb b/spec/arel/algebra/integration/basic_spec.rb new file mode 100644 index 0000000000..84b8105f66 --- /dev/null +++ b/spec/arel/algebra/integration/basic_spec.rb @@ -0,0 +1,120 @@ +require 'spec_helper' + +module Arel + module Testing + class Engine + attr_reader :rows + + def initialize + @rows = [] + end + + def supports(operation) + false + end + + def read(relation) + @rows.dup.map { |r| Row.new(relation, r) } + end + + def create(insert) + @rows << insert.record.tuple + insert + end + end + end +end + +class Thing < Arel::Relation + attr_reader :engine, :attributes + + def initialize(engine, attributes) + @engine = engine + @attributes = attributes.map { |a| a.to_attribute(self) } + end + + def format(attribute, value) + value + end + + def insert(row) + insert = super Arel::Row.new(self, row) + insert.record + end +end + +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 + end +end + +describe "Arel::Relation" do + + before :all do + @engine = Arel::Testing::Engine.new + @relation = Thing.new(@engine, [:id, :name, :age]) + end + + describe "..." do + before :all do + @expected = (1..20).map { |i| @relation.insert([i, nil, 2 * i]) } + end + + it_should_behave_like 'A Relation' + end + + describe "#insert" do + it "inserts the row into the engine" do + @relation.insert([1, 'Foo', 10]) + @engine.rows.should == [[1, 'Foo', 10]] + end + end + +end
\ No newline at end of file diff --git a/spec/doubles/hash.rb b/spec/doubles/hash.rb deleted file mode 100644 index fd9edd34ad..0000000000 --- a/spec/doubles/hash.rb +++ /dev/null @@ -1,27 +0,0 @@ -class Hash - def ordered_array - to_a.sort { |(key1, value1), (key2, value2)| key1.hash <=> key2.hash } - end - - undef :keys - def keys - ordered_array.collect(&:first) - end - - undef :values - def values - ordered_array.collect { |_, v| v } - end - - undef :each - def each(&block) - ordered_array.each(&block) - end - - undef :shift - def shift - returning to_a.first do |k, v| - delete(k) - end - end -end diff --git a/spec/spec/fixtures/fixture_database.sqlite3 b/spec/spec/fixtures/fixture_database.sqlite3 Binary files differnew file mode 100644 index 0000000000..cb52aacf7e --- /dev/null +++ b/spec/spec/fixtures/fixture_database.sqlite3 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1c45f34dfe..894e70c29d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,44 +7,8 @@ require 'pp' require 'fileutils' require 'arel' -[:matchers, :doubles].each do |helper| - Dir["#{dir}/#{helper}/*"].each { |m| require "#{dir}/#{helper}/#{File.basename(m)}" } -end - -module AdapterGuards - def adapter_is(*names) - names = names.map(&:to_s) - names.each{|name| verify_adapter_name(name)} - yield if names.include? adapter_name - end - - def adapter_is_not(*names) - names = names.map(&:to_s) - names.each{|name| verify_adapter_name(name)} - yield unless names.include? adapter_name - end - - def adapter_name - name = ActiveRecord::Base.configurations["unit"][:adapter] - name = 'oracle' if name == 'oracle_enhanced' - verify_adapter_name(name) - name - end - - def verify_adapter_name(name) - raise "Invalid adapter name: #{name}" unless valid_adapters.include?(name.to_s) - end - - def valid_adapters - %w[mysql postgresql sqlite3 oracle] - end -end - -module Check - # This is used to eliminate Ruby warnings on some RSpec assertion lines - # See: https://rspec.lighthouseapp.com/projects/5645/tickets/504 - def check(*args) - end +Dir["#{dir}/support/*.rb"].each do |file| + require file end Spec::Runner.configure do |config| @@ -53,12 +17,12 @@ Spec::Runner.configure do |config| config.include Check config.before do - Arel::Table.engine = Arel::Sql::Engine.new(ActiveRecord::Base) + Arel::Table.engine = Arel::Sql::Engine.new(ActiveRecord::Base) if defined?(ActiveRecord::Base) end end # load corresponding adapter using ADAPTER environment variable when running single *_spec.rb file if adapter = ENV['ADAPTER'] - require "#{dir}/connections/#{adapter}_connection.rb" - require "#{dir}/schemas/#{adapter}_schema.rb" + require "#{dir}/support/connections/#{adapter}_connection.rb" + require "#{dir}/support/schemas/#{adapter}_schema.rb" end diff --git a/spec/support/check.rb b/spec/support/check.rb new file mode 100644 index 0000000000..0a89349629 --- /dev/null +++ b/spec/support/check.rb @@ -0,0 +1,6 @@ +module Check + # This is used to eliminate Ruby warnings on some RSpec assertion lines + # See: https://rspec.lighthouseapp.com/projects/5645/tickets/504 + def check(*args) + end +end
\ No newline at end of file diff --git a/spec/connections/mysql_connection.rb b/spec/support/connections/mysql_connection.rb index 66a53b5037..66a53b5037 100644 --- a/spec/connections/mysql_connection.rb +++ b/spec/support/connections/mysql_connection.rb diff --git a/spec/connections/oracle_connection.rb b/spec/support/connections/oracle_connection.rb index 05be04e410..05be04e410 100644 --- a/spec/connections/oracle_connection.rb +++ b/spec/support/connections/oracle_connection.rb diff --git a/spec/connections/postgresql_connection.rb b/spec/support/connections/postgresql_connection.rb index 0fb6dfe065..0fb6dfe065 100644 --- a/spec/connections/postgresql_connection.rb +++ b/spec/support/connections/postgresql_connection.rb diff --git a/spec/connections/sqlite3_connection.rb b/spec/support/connections/sqlite3_connection.rb index e8eeee17d0..abdbd24fe4 100644 --- a/spec/connections/sqlite3_connection.rb +++ b/spec/support/connections/sqlite3_connection.rb @@ -4,7 +4,7 @@ require 'logger' ActiveRecord::Base.logger = Logger.new("debug.log") -db_file = "spec/fixtures/fixture_database.sqlite3" +db_file = "spec/support/fixtures/fixture_database.sqlite3" ActiveRecord::Base.configurations = { "unit" => { diff --git a/spec/support/guards.rb b/spec/support/guards.rb new file mode 100644 index 0000000000..cfa4b7b79a --- /dev/null +++ b/spec/support/guards.rb @@ -0,0 +1,28 @@ +module AdapterGuards + def adapter_is(*names) + names = names.map(&:to_s) + names.each{|name| verify_adapter_name(name)} + yield if names.include? adapter_name + end + + def adapter_is_not(*names) + names = names.map(&:to_s) + names.each{|name| verify_adapter_name(name)} + yield unless names.include? adapter_name + end + + def adapter_name + name = ActiveRecord::Base.configurations["unit"][:adapter] + name = 'oracle' if name == 'oracle_enhanced' + verify_adapter_name(name) + name + end + + def verify_adapter_name(name) + raise "Invalid adapter name: #{name}" unless valid_adapters.include?(name.to_s) + end + + def valid_adapters + %w[mysql postgresql sqlite3 oracle] + end +end
\ No newline at end of file diff --git a/spec/support/matchers.rb b/spec/support/matchers.rb new file mode 100644 index 0000000000..6f50d6cbc7 --- /dev/null +++ b/spec/support/matchers.rb @@ -0,0 +1,3 @@ +require "support/matchers/be_like" +require "support/matchers/disambiguate_attributes" +require "support/matchers/hash_the_same_as"
\ No newline at end of file diff --git a/spec/matchers/be_like.rb b/spec/support/matchers/be_like.rb index c9d4d4b979..c9d4d4b979 100644 --- a/spec/matchers/be_like.rb +++ b/spec/support/matchers/be_like.rb diff --git a/spec/matchers/disambiguate_attributes.rb b/spec/support/matchers/disambiguate_attributes.rb index bc4a5215d4..bc4a5215d4 100644 --- a/spec/matchers/disambiguate_attributes.rb +++ b/spec/support/matchers/disambiguate_attributes.rb diff --git a/spec/matchers/hash_the_same_as.rb b/spec/support/matchers/hash_the_same_as.rb index 03e955a0cb..03e955a0cb 100644 --- a/spec/matchers/hash_the_same_as.rb +++ b/spec/support/matchers/hash_the_same_as.rb diff --git a/spec/schemas/mysql_schema.rb b/spec/support/schemas/mysql_schema.rb index a2e913d756..a2e913d756 100644 --- a/spec/schemas/mysql_schema.rb +++ b/spec/support/schemas/mysql_schema.rb diff --git a/spec/schemas/oracle_schema.rb b/spec/support/schemas/oracle_schema.rb index c8207c8d98..c8207c8d98 100644 --- a/spec/schemas/oracle_schema.rb +++ b/spec/support/schemas/oracle_schema.rb diff --git a/spec/schemas/postgresql_schema.rb b/spec/support/schemas/postgresql_schema.rb index 23a48f5ad8..23a48f5ad8 100644 --- a/spec/schemas/postgresql_schema.rb +++ b/spec/support/schemas/postgresql_schema.rb diff --git a/spec/schemas/sqlite3_schema.rb b/spec/support/schemas/sqlite3_schema.rb index c1fed70859..c1fed70859 100644 --- a/spec/schemas/sqlite3_schema.rb +++ b/spec/support/schemas/sqlite3_schema.rb |