Permalink
Browse files

Merge branch 'master' of github.com:brynary/arel

  • Loading branch information...
2 parents 5e76aa7 + cbb5241 commit 602722922c8365afcb3e9bed3721d61756322353 @miloops miloops committed Mar 12, 2010
View
@@ -1,6 +1,6 @@
coverage/*
config/database.yml
-spec/fixtures/*database*
+spec/support/fixtures/*database*
*.DS_Store
debug.log
pkg
View
@@ -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
View
@@ -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'
@@ -9,6 +9,15 @@ def initialize(relation, *predicates, &block)
@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
View
@@ -11,13 +11,13 @@ def start
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
@@ -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
View
@@ -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
Binary file not shown.
View
@@ -7,44 +7,8 @@
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 @@ def check(*args)
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
View
@@ -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
@@ -4,7 +4,7 @@
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" => {
View
@@ -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
View
@@ -0,0 +1,3 @@
+require "support/matchers/be_like"
+require "support/matchers/disambiguate_attributes"
+require "support/matchers/hash_the_same_as"
File renamed without changes.
File renamed without changes.

0 comments on commit 6027229

Please sign in to comment.