aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2010-03-12 11:34:01 -0300
committerEmilio Tagua <miloops@gmail.com>2010-03-12 11:34:01 -0300
commit602722922c8365afcb3e9bed3721d61756322353 (patch)
tree8e07cc7fdf6662b77f4c7b0fb9aa6ccf55c7530a
parent5e76aa7962c4e02c67c1e1515e6ae1b5ca6190a7 (diff)
parentcbb524122c6132ba37661934c09b540a68b1c64d (diff)
downloadrails-602722922c8365afcb3e9bed3721d61756322353.tar.gz
rails-602722922c8365afcb3e9bed3721d61756322353.tar.bz2
rails-602722922c8365afcb3e9bed3721d61756322353.zip
Merge branch 'master' of github.com:brynary/arel
-rw-r--r--.gitignore2
-rw-r--r--Rakefile4
-rw-r--r--lib/arel.rb3
-rw-r--r--lib/arel/algebra/relations/operations/where.rb9
-rw-r--r--lib/arel/session.rb4
-rw-r--r--spec/arel/algebra/integration/basic_spec.rb120
-rw-r--r--spec/doubles/hash.rb27
-rw-r--r--spec/spec/fixtures/fixture_database.sqlite3bin0 -> 4096 bytes
-rw-r--r--spec/spec_helper.rb46
-rw-r--r--spec/support/check.rb6
-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.rb28
-rw-r--r--spec/support/matchers.rb3
-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
23 files changed, 179 insertions, 75 deletions
diff --git a/.gitignore b/.gitignore
index 39cbb3948b..3fec2d912b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,6 @@
coverage/*
config/database.yml
-spec/fixtures/*database*
+spec/support/fixtures/*database*
*.DS_Store
debug.log
pkg
diff --git a/Rakefile b/Rakefile
index a1384196a2..ef633c0042 100644
--- a/Rakefile
+++ b/Rakefile
@@ -28,8 +28,8 @@ else
t.libs << "#{File.dirname(__FILE__)}/spec"
# t.warning = true
t.spec_files =
- ["spec/connections/#{adapter}_connection.rb"] +
- ["spec/schemas/#{adapter}_schema.rb"] +
+ ["spec/support/connections/#{adapter}_connection.rb"] +
+ ["spec/support/schemas/#{adapter}_schema.rb"] +
FileList['spec/**/*_spec.rb']
end
end
diff --git a/lib/arel.rb b/lib/arel.rb
index 14511d5b38..ef2308ca53 100644
--- a/lib/arel.rb
+++ b/lib/arel.rb
@@ -1,6 +1,7 @@
require 'active_support/inflector'
-require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/class/attribute_accessors'
+require 'active_support/core_ext/module/delegation'
+require 'active_support/core_ext/object/blank'
module Arel
require 'arel/algebra'
diff --git a/lib/arel/algebra/relations/operations/where.rb b/lib/arel/algebra/relations/operations/where.rb
index 608aaeb4b7..2fc51c7f24 100644
--- a/lib/arel/algebra/relations/operations/where.rb
+++ b/lib/arel/algebra/relations/operations/where.rb
@@ -9,6 +9,15 @@ module Arel
@predicate = predicate.bind(@relation)
end
+ def engine
+ # Temporary check of whether or not the engine supports where.
+ if relation.engine.respond_to?(:supports) && !relation.engine.supports(:where)
+ Memory::Engine.new
+ else
+ relation.engine
+ end
+ end
+
def wheres
@wheres ||= (relation.wheres + [predicate]).collect { |p| p.bind(self) }
end
diff --git a/lib/arel/session.rb b/lib/arel/session.rb
index d844cd7423..c7fcc53a3b 100644
--- a/lib/arel/session.rb
+++ b/lib/arel/session.rb
@@ -11,13 +11,13 @@ module Arel
begin
@started = true
@instance = manufacture
- metaclass.class_eval do
+ singleton_class.class_eval do
undef :new
alias_method :new, :instance
end
yield
ensure
- metaclass.class_eval do
+ singleton_class.class_eval do
undef :new
alias_method :new, :manufacture
end
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
new file mode 100644
index 0000000000..cb52aacf7e
--- /dev/null
+++ b/spec/spec/fixtures/fixture_database.sqlite3
Binary files differ
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