From 915606d10923e3c5de6d904b58eb13b9b5658ea4 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 11 Mar 2010 11:15:52 -0800 Subject: Latest ActiveSupport replaces metaclass with singleton_class --- lib/arel/session.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 -- cgit v1.2.3 From 9e3450dd830caa8d1c12a0ce33b3837dc29d779c Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 11 Mar 2010 15:52:01 -0800 Subject: Get the specs to start when an AR connection is not present. --- lib/arel.rb | 3 ++- spec/spec_helper.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) 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/spec/spec_helper.rb b/spec/spec_helper.rb index 1c45f34dfe..b210af5ab4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -53,7 +53,7 @@ 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 -- cgit v1.2.3 From e3461239adfa972de8f25a2bc6b48b4a8aa62c9c Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 11 Mar 2010 16:13:47 -0800 Subject: Start writing integration specs for arel. --- lib/arel/algebra/relations/operations/where.rb | 9 ++ spec/arel/algebra/integration/basic_spec.rb | 120 +++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 spec/arel/algebra/integration/basic_spec.rb 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/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 -- cgit v1.2.3 From 8db90ba95d4a240fde68127e806162b3e600c383 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 11 Mar 2010 17:08:55 -0800 Subject: Move all spec support files into spec/support --- .gitignore | 2 +- Rakefile | 4 +- spec/connections/mysql_connection.rb | 16 -------- spec/connections/oracle_connection.rb | 19 ---------- spec/connections/postgresql_connection.rb | 15 -------- spec/connections/sqlite3_connection.rb | 26 ------------- spec/doubles/hash.rb | 27 ------------- spec/matchers/be_like.rb | 24 ------------ spec/matchers/disambiguate_attributes.rb | 28 -------------- spec/matchers/hash_the_same_as.rb | 26 ------------- spec/schemas/mysql_schema.rb | 26 ------------- spec/schemas/oracle_schema.rb | 20 ---------- spec/schemas/postgresql_schema.rb | 26 ------------- spec/schemas/sqlite3_schema.rb | 26 ------------- spec/spec/fixtures/fixture_database.sqlite3 | Bin 0 -> 4096 bytes spec/spec_helper.rb | 44 ++-------------------- spec/support/check.rb | 6 +++ spec/support/connections/mysql_connection.rb | 16 ++++++++ spec/support/connections/oracle_connection.rb | 19 ++++++++++ spec/support/connections/postgresql_connection.rb | 15 ++++++++ spec/support/connections/sqlite3_connection.rb | 26 +++++++++++++ spec/support/doubles.rb | 1 + spec/support/doubles/hash.rb | 27 +++++++++++++ spec/support/guards.rb | 28 ++++++++++++++ spec/support/matchers.rb | 3 ++ spec/support/matchers/be_like.rb | 24 ++++++++++++ spec/support/matchers/disambiguate_attributes.rb | 28 ++++++++++++++ spec/support/matchers/hash_the_same_as.rb | 26 +++++++++++++ spec/support/schemas/mysql_schema.rb | 26 +++++++++++++ spec/support/schemas/oracle_schema.rb | 20 ++++++++++ spec/support/schemas/postgresql_schema.rb | 26 +++++++++++++ spec/support/schemas/sqlite3_schema.rb | 26 +++++++++++++ 32 files changed, 324 insertions(+), 322 deletions(-) delete mode 100644 spec/connections/mysql_connection.rb delete mode 100644 spec/connections/oracle_connection.rb delete mode 100644 spec/connections/postgresql_connection.rb delete mode 100644 spec/connections/sqlite3_connection.rb delete mode 100644 spec/doubles/hash.rb delete mode 100644 spec/matchers/be_like.rb delete mode 100644 spec/matchers/disambiguate_attributes.rb delete mode 100644 spec/matchers/hash_the_same_as.rb delete mode 100644 spec/schemas/mysql_schema.rb delete mode 100644 spec/schemas/oracle_schema.rb delete mode 100644 spec/schemas/postgresql_schema.rb delete mode 100644 spec/schemas/sqlite3_schema.rb create mode 100644 spec/spec/fixtures/fixture_database.sqlite3 create mode 100644 spec/support/check.rb create mode 100644 spec/support/connections/mysql_connection.rb create mode 100644 spec/support/connections/oracle_connection.rb create mode 100644 spec/support/connections/postgresql_connection.rb create mode 100644 spec/support/connections/sqlite3_connection.rb create mode 100644 spec/support/doubles.rb create mode 100644 spec/support/doubles/hash.rb create mode 100644 spec/support/guards.rb create mode 100644 spec/support/matchers.rb create mode 100644 spec/support/matchers/be_like.rb create mode 100644 spec/support/matchers/disambiguate_attributes.rb create mode 100644 spec/support/matchers/hash_the_same_as.rb create mode 100644 spec/support/schemas/mysql_schema.rb create mode 100644 spec/support/schemas/oracle_schema.rb create mode 100644 spec/support/schemas/postgresql_schema.rb create mode 100644 spec/support/schemas/sqlite3_schema.rb 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/spec/connections/mysql_connection.rb b/spec/connections/mysql_connection.rb deleted file mode 100644 index 66a53b5037..0000000000 --- a/spec/connections/mysql_connection.rb +++ /dev/null @@ -1,16 +0,0 @@ -puts "Using native MySQL" -require "active_record" -require 'logger' - -ActiveRecord::Base.logger = Logger.new("debug.log") - -ActiveRecord::Base.configurations = { - 'unit' => { - :adapter => 'mysql', - :username => 'root', - :encoding => 'utf8', - :database => 'arel_unit', - } -} - -ActiveRecord::Base.establish_connection 'unit' diff --git a/spec/connections/oracle_connection.rb b/spec/connections/oracle_connection.rb deleted file mode 100644 index 05be04e410..0000000000 --- a/spec/connections/oracle_connection.rb +++ /dev/null @@ -1,19 +0,0 @@ -puts "Using native Oracle" -require "active_record" -require 'logger' - -# Prepend oracle_enhanced local development directory in front of load path -$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../../oracle-enhanced/lib" - -ActiveRecord::Base.logger = Logger.new("debug.log") - -ActiveRecord::Base.configurations = { - 'unit' => { - :adapter => 'oracle_enhanced', - :username => 'arel_unit', - :password => 'arel_unit', - :database => 'orcl', - } -} - -ActiveRecord::Base.establish_connection 'unit' diff --git a/spec/connections/postgresql_connection.rb b/spec/connections/postgresql_connection.rb deleted file mode 100644 index 0fb6dfe065..0000000000 --- a/spec/connections/postgresql_connection.rb +++ /dev/null @@ -1,15 +0,0 @@ -puts "Using native PostgreSQL" -require "active_record" -require 'logger' - -ActiveRecord::Base.logger = Logger.new("debug.log") - -ActiveRecord::Base.configurations = { - 'unit' => { - :adapter => 'postgresql', - :encoding => 'utf8', - :database => 'arel_unit', - } -} - -ActiveRecord::Base.establish_connection 'unit' diff --git a/spec/connections/sqlite3_connection.rb b/spec/connections/sqlite3_connection.rb deleted file mode 100644 index e8eeee17d0..0000000000 --- a/spec/connections/sqlite3_connection.rb +++ /dev/null @@ -1,26 +0,0 @@ -puts "Using native SQLite3" -require "active_record" -require 'logger' - -ActiveRecord::Base.logger = Logger.new("debug.log") - -db_file = "spec/fixtures/fixture_database.sqlite3" - -ActiveRecord::Base.configurations = { - "unit" => { - :adapter => 'sqlite3', - :database => db_file, - :timeout => 5000 - } -} - -unless File.exist?(db_file) - puts "SQLite3 database not found at #{db_file}. Rebuilding it." - require 'fileutils' - FileUtils.mkdir_p(File.dirname(db_file)) - sqlite_command = %Q{sqlite3 "#{db_file}" "create table a (a integer); drop table a;"} - puts "Executing '#{sqlite_command}'" - raise "Seems that there is no sqlite3 executable available" unless system(sqlite_command) -end - -ActiveRecord::Base.establish_connection("unit") 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/matchers/be_like.rb b/spec/matchers/be_like.rb deleted file mode 100644 index c9d4d4b979..0000000000 --- a/spec/matchers/be_like.rb +++ /dev/null @@ -1,24 +0,0 @@ -module BeLikeMatcher - class BeLike - def initialize(expected) - @expected = expected - end - - def matches?(actual) - @actual = actual - @expected.gsub(/\s+/, ' ').strip == @actual.gsub(/\s+/, ' ').strip - end - - def failure_message - "expected\n#{@actual}\nto be like\n#{@expected}" - end - - def negative_failure_message - "expected\n#{@actual}\nto be unlike\n#{@expected}" - end - end - - def be_like(expected) - BeLike.new(expected) - end -end diff --git a/spec/matchers/disambiguate_attributes.rb b/spec/matchers/disambiguate_attributes.rb deleted file mode 100644 index bc4a5215d4..0000000000 --- a/spec/matchers/disambiguate_attributes.rb +++ /dev/null @@ -1,28 +0,0 @@ -module DisambiguateAttributesMatcher - class DisambiguateAttributes - def initialize(attributes) - @attributes = attributes - end - - def matches?(actual) - @actual = actual - attribute1, attribute2 = @attributes - @actual[attribute1].descends_from?(attribute1) && - !@actual[attribute1].descends_from?(attribute2) && - @actual[attribute2].descends_from?(attribute2) - end - - def failure_message - "" - # "expected #{@actual} to disambiguate its attributes" - end - - def negative_failure_message - "expected #{@actual} to not disambiguate its attributes" - end - end - - def disambiguate_attributes(*attributes) - DisambiguateAttributes.new(attributes) - end -end diff --git a/spec/matchers/hash_the_same_as.rb b/spec/matchers/hash_the_same_as.rb deleted file mode 100644 index 03e955a0cb..0000000000 --- a/spec/matchers/hash_the_same_as.rb +++ /dev/null @@ -1,26 +0,0 @@ -module HashTheSameAsMatcher - class HashTheSameAs - def initialize(expected) - @expected = expected - end - - def matches?(actual) - @actual = actual - hash = {} - hash[@expected] = :some_arbitrary_value - hash[@actual] == :some_arbitrary_value - end - - def failure_message - "expected #{@actual} to hash the same as #{@expected}; they must be `eql?` and have the same `#hash` value" - end - - def negative_failure_message - "expected #{@actual} to hash differently than #{@expected}; they must not be `eql?` or have a differing `#hash` values" - end - end - - def hash_the_same_as(expected) - HashTheSameAs.new(expected) - end -end diff --git a/spec/schemas/mysql_schema.rb b/spec/schemas/mysql_schema.rb deleted file mode 100644 index a2e913d756..0000000000 --- a/spec/schemas/mysql_schema.rb +++ /dev/null @@ -1,26 +0,0 @@ -sql = <<-SQL - DROP TABLE IF EXISTS users; - CREATE TABLE users ( - id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, - name VARCHAR(255) NOT NULL - ); - - DROP TABLE IF EXISTS photos; - CREATE TABLE photos ( - id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, - user_id INTEGER NOT NULL, - camera_id INTEGER NOT NULL - ); - DROP TABLE IF EXISTS developers; - CREATE TABLE developers ( - id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, - name VARCHAR(255) NOT NULL, - salary INTEGER NOT NULL, - department VARCHAR(255) NOT NULL, - created_at TIMESTAMP NOT NULL - ); -SQL - -sql.split(/;/).select(&:present?).each do |sql_statement| - ActiveRecord::Base.connection.execute sql_statement -end diff --git a/spec/schemas/oracle_schema.rb b/spec/schemas/oracle_schema.rb deleted file mode 100644 index c8207c8d98..0000000000 --- a/spec/schemas/oracle_schema.rb +++ /dev/null @@ -1,20 +0,0 @@ -ActiveRecord::Schema.define do - suppress_messages do - create_table :users, :primary_key_trigger => true, :force => true do |t| - t.string :name, :limit => 255, :null => false - end - - create_table :photos, :primary_key_trigger => true, :force => true do |t| - t.integer :user_id - t.integer :camera_id - end - - create_table :developers, :primary_key_trigger => true, :force => true do |t| - t.string :name, :limit => 255, :null => false - t.integer :salary - t.string :department, :limit => 255, :null => false - t.timestamp :created_at, :null => false - end - - end -end diff --git a/spec/schemas/postgresql_schema.rb b/spec/schemas/postgresql_schema.rb deleted file mode 100644 index 23a48f5ad8..0000000000 --- a/spec/schemas/postgresql_schema.rb +++ /dev/null @@ -1,26 +0,0 @@ -sql = <<-SQL - DROP TABLE IF EXISTS users; - CREATE TABLE users ( - id SERIAL PRIMARY KEY NOT NULL, - name VARCHAR(255) NOT NULL - ); - - DROP TABLE IF EXISTS photos; - CREATE TABLE photos ( - id SERIAL PRIMARY KEY NOT NULL, - user_id INTEGER NOT NULL, - camera_id INTEGER NOT NULL - ); - DROP TABLE IF EXISTS developers; - CREATE TABLE developers ( - id SERIAL PRIMARY KEY NOT NULL, - name VARCHAR(255) NOT NULL, - salary INTEGER NOT NULL, - department VARCHAR(255) NOT NULL, - created_at TIMESTAMP NOT NULL - ); -SQL - -sql.split(/;/).select(&:present?).each do |sql_statement| - ActiveRecord::Base.connection.execute sql_statement -end diff --git a/spec/schemas/sqlite3_schema.rb b/spec/schemas/sqlite3_schema.rb deleted file mode 100644 index c1fed70859..0000000000 --- a/spec/schemas/sqlite3_schema.rb +++ /dev/null @@ -1,26 +0,0 @@ -sql = <<-SQL - DROP TABLE IF EXISTS users; - CREATE TABLE users ( - id INTEGER NOT NULL PRIMARY KEY, - name VARCHAR(255) NOT NULL - ); - - DROP TABLE IF EXISTS photos; - CREATE TABLE photos ( - id INTEGER NOT NULL PRIMARY KEY, - user_id INTEGER NOT NULL, - camera_id INTEGER NOT NULL - ); - DROP TABLE IF EXISTS developers; - CREATE TABLE developers ( - id INTEGER NOT NULL PRIMARY KEY, - name VARCHAR(255) NOT NULL, - salary INTEGER NOT NULL, - department VARCHAR(255) NOT NULL, - created_at TIMESTAMP NOT NULL - ); -SQL - -sql.split(/;/).select(&:present?).each do |sql_statement| - ActiveRecord::Base.connection.execute sql_statement -end diff --git a/spec/spec/fixtures/fixture_database.sqlite3 b/spec/spec/fixtures/fixture_database.sqlite3 new file mode 100644 index 0000000000..cb52aacf7e Binary files /dev/null and b/spec/spec/fixtures/fixture_database.sqlite3 differ diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b210af5ab4..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| @@ -59,6 +23,6 @@ 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/support/connections/mysql_connection.rb b/spec/support/connections/mysql_connection.rb new file mode 100644 index 0000000000..66a53b5037 --- /dev/null +++ b/spec/support/connections/mysql_connection.rb @@ -0,0 +1,16 @@ +puts "Using native MySQL" +require "active_record" +require 'logger' + +ActiveRecord::Base.logger = Logger.new("debug.log") + +ActiveRecord::Base.configurations = { + 'unit' => { + :adapter => 'mysql', + :username => 'root', + :encoding => 'utf8', + :database => 'arel_unit', + } +} + +ActiveRecord::Base.establish_connection 'unit' diff --git a/spec/support/connections/oracle_connection.rb b/spec/support/connections/oracle_connection.rb new file mode 100644 index 0000000000..05be04e410 --- /dev/null +++ b/spec/support/connections/oracle_connection.rb @@ -0,0 +1,19 @@ +puts "Using native Oracle" +require "active_record" +require 'logger' + +# Prepend oracle_enhanced local development directory in front of load path +$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../../oracle-enhanced/lib" + +ActiveRecord::Base.logger = Logger.new("debug.log") + +ActiveRecord::Base.configurations = { + 'unit' => { + :adapter => 'oracle_enhanced', + :username => 'arel_unit', + :password => 'arel_unit', + :database => 'orcl', + } +} + +ActiveRecord::Base.establish_connection 'unit' diff --git a/spec/support/connections/postgresql_connection.rb b/spec/support/connections/postgresql_connection.rb new file mode 100644 index 0000000000..0fb6dfe065 --- /dev/null +++ b/spec/support/connections/postgresql_connection.rb @@ -0,0 +1,15 @@ +puts "Using native PostgreSQL" +require "active_record" +require 'logger' + +ActiveRecord::Base.logger = Logger.new("debug.log") + +ActiveRecord::Base.configurations = { + 'unit' => { + :adapter => 'postgresql', + :encoding => 'utf8', + :database => 'arel_unit', + } +} + +ActiveRecord::Base.establish_connection 'unit' diff --git a/spec/support/connections/sqlite3_connection.rb b/spec/support/connections/sqlite3_connection.rb new file mode 100644 index 0000000000..abdbd24fe4 --- /dev/null +++ b/spec/support/connections/sqlite3_connection.rb @@ -0,0 +1,26 @@ +puts "Using native SQLite3" +require "active_record" +require 'logger' + +ActiveRecord::Base.logger = Logger.new("debug.log") + +db_file = "spec/support/fixtures/fixture_database.sqlite3" + +ActiveRecord::Base.configurations = { + "unit" => { + :adapter => 'sqlite3', + :database => db_file, + :timeout => 5000 + } +} + +unless File.exist?(db_file) + puts "SQLite3 database not found at #{db_file}. Rebuilding it." + require 'fileutils' + FileUtils.mkdir_p(File.dirname(db_file)) + sqlite_command = %Q{sqlite3 "#{db_file}" "create table a (a integer); drop table a;"} + puts "Executing '#{sqlite_command}'" + raise "Seems that there is no sqlite3 executable available" unless system(sqlite_command) +end + +ActiveRecord::Base.establish_connection("unit") diff --git a/spec/support/doubles.rb b/spec/support/doubles.rb new file mode 100644 index 0000000000..212859cbd6 --- /dev/null +++ b/spec/support/doubles.rb @@ -0,0 +1 @@ +require "support/doubles/hash" \ No newline at end of file diff --git a/spec/support/doubles/hash.rb b/spec/support/doubles/hash.rb new file mode 100644 index 0000000000..fd9edd34ad --- /dev/null +++ b/spec/support/doubles/hash.rb @@ -0,0 +1,27 @@ +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/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/support/matchers/be_like.rb b/spec/support/matchers/be_like.rb new file mode 100644 index 0000000000..c9d4d4b979 --- /dev/null +++ b/spec/support/matchers/be_like.rb @@ -0,0 +1,24 @@ +module BeLikeMatcher + class BeLike + def initialize(expected) + @expected = expected + end + + def matches?(actual) + @actual = actual + @expected.gsub(/\s+/, ' ').strip == @actual.gsub(/\s+/, ' ').strip + end + + def failure_message + "expected\n#{@actual}\nto be like\n#{@expected}" + end + + def negative_failure_message + "expected\n#{@actual}\nto be unlike\n#{@expected}" + end + end + + def be_like(expected) + BeLike.new(expected) + end +end diff --git a/spec/support/matchers/disambiguate_attributes.rb b/spec/support/matchers/disambiguate_attributes.rb new file mode 100644 index 0000000000..bc4a5215d4 --- /dev/null +++ b/spec/support/matchers/disambiguate_attributes.rb @@ -0,0 +1,28 @@ +module DisambiguateAttributesMatcher + class DisambiguateAttributes + def initialize(attributes) + @attributes = attributes + end + + def matches?(actual) + @actual = actual + attribute1, attribute2 = @attributes + @actual[attribute1].descends_from?(attribute1) && + !@actual[attribute1].descends_from?(attribute2) && + @actual[attribute2].descends_from?(attribute2) + end + + def failure_message + "" + # "expected #{@actual} to disambiguate its attributes" + end + + def negative_failure_message + "expected #{@actual} to not disambiguate its attributes" + end + end + + def disambiguate_attributes(*attributes) + DisambiguateAttributes.new(attributes) + end +end diff --git a/spec/support/matchers/hash_the_same_as.rb b/spec/support/matchers/hash_the_same_as.rb new file mode 100644 index 0000000000..03e955a0cb --- /dev/null +++ b/spec/support/matchers/hash_the_same_as.rb @@ -0,0 +1,26 @@ +module HashTheSameAsMatcher + class HashTheSameAs + def initialize(expected) + @expected = expected + end + + def matches?(actual) + @actual = actual + hash = {} + hash[@expected] = :some_arbitrary_value + hash[@actual] == :some_arbitrary_value + end + + def failure_message + "expected #{@actual} to hash the same as #{@expected}; they must be `eql?` and have the same `#hash` value" + end + + def negative_failure_message + "expected #{@actual} to hash differently than #{@expected}; they must not be `eql?` or have a differing `#hash` values" + end + end + + def hash_the_same_as(expected) + HashTheSameAs.new(expected) + end +end diff --git a/spec/support/schemas/mysql_schema.rb b/spec/support/schemas/mysql_schema.rb new file mode 100644 index 0000000000..a2e913d756 --- /dev/null +++ b/spec/support/schemas/mysql_schema.rb @@ -0,0 +1,26 @@ +sql = <<-SQL + DROP TABLE IF EXISTS users; + CREATE TABLE users ( + id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(255) NOT NULL + ); + + DROP TABLE IF EXISTS photos; + CREATE TABLE photos ( + id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, + user_id INTEGER NOT NULL, + camera_id INTEGER NOT NULL + ); + DROP TABLE IF EXISTS developers; + CREATE TABLE developers ( + id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(255) NOT NULL, + salary INTEGER NOT NULL, + department VARCHAR(255) NOT NULL, + created_at TIMESTAMP NOT NULL + ); +SQL + +sql.split(/;/).select(&:present?).each do |sql_statement| + ActiveRecord::Base.connection.execute sql_statement +end diff --git a/spec/support/schemas/oracle_schema.rb b/spec/support/schemas/oracle_schema.rb new file mode 100644 index 0000000000..c8207c8d98 --- /dev/null +++ b/spec/support/schemas/oracle_schema.rb @@ -0,0 +1,20 @@ +ActiveRecord::Schema.define do + suppress_messages do + create_table :users, :primary_key_trigger => true, :force => true do |t| + t.string :name, :limit => 255, :null => false + end + + create_table :photos, :primary_key_trigger => true, :force => true do |t| + t.integer :user_id + t.integer :camera_id + end + + create_table :developers, :primary_key_trigger => true, :force => true do |t| + t.string :name, :limit => 255, :null => false + t.integer :salary + t.string :department, :limit => 255, :null => false + t.timestamp :created_at, :null => false + end + + end +end diff --git a/spec/support/schemas/postgresql_schema.rb b/spec/support/schemas/postgresql_schema.rb new file mode 100644 index 0000000000..23a48f5ad8 --- /dev/null +++ b/spec/support/schemas/postgresql_schema.rb @@ -0,0 +1,26 @@ +sql = <<-SQL + DROP TABLE IF EXISTS users; + CREATE TABLE users ( + id SERIAL PRIMARY KEY NOT NULL, + name VARCHAR(255) NOT NULL + ); + + DROP TABLE IF EXISTS photos; + CREATE TABLE photos ( + id SERIAL PRIMARY KEY NOT NULL, + user_id INTEGER NOT NULL, + camera_id INTEGER NOT NULL + ); + DROP TABLE IF EXISTS developers; + CREATE TABLE developers ( + id SERIAL PRIMARY KEY NOT NULL, + name VARCHAR(255) NOT NULL, + salary INTEGER NOT NULL, + department VARCHAR(255) NOT NULL, + created_at TIMESTAMP NOT NULL + ); +SQL + +sql.split(/;/).select(&:present?).each do |sql_statement| + ActiveRecord::Base.connection.execute sql_statement +end diff --git a/spec/support/schemas/sqlite3_schema.rb b/spec/support/schemas/sqlite3_schema.rb new file mode 100644 index 0000000000..c1fed70859 --- /dev/null +++ b/spec/support/schemas/sqlite3_schema.rb @@ -0,0 +1,26 @@ +sql = <<-SQL + DROP TABLE IF EXISTS users; + CREATE TABLE users ( + id INTEGER NOT NULL PRIMARY KEY, + name VARCHAR(255) NOT NULL + ); + + DROP TABLE IF EXISTS photos; + CREATE TABLE photos ( + id INTEGER NOT NULL PRIMARY KEY, + user_id INTEGER NOT NULL, + camera_id INTEGER NOT NULL + ); + DROP TABLE IF EXISTS developers; + CREATE TABLE developers ( + id INTEGER NOT NULL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + salary INTEGER NOT NULL, + department VARCHAR(255) NOT NULL, + created_at TIMESTAMP NOT NULL + ); +SQL + +sql.split(/;/).select(&:present?).each do |sql_statement| + ActiveRecord::Base.connection.execute sql_statement +end -- cgit v1.2.3 From cbb524122c6132ba37661934c09b540a68b1c64d Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 11 Mar 2010 17:12:52 -0800 Subject: Remove crazy --- spec/support/doubles.rb | 1 - spec/support/doubles/hash.rb | 27 --------------------------- 2 files changed, 28 deletions(-) delete mode 100644 spec/support/doubles.rb delete mode 100644 spec/support/doubles/hash.rb diff --git a/spec/support/doubles.rb b/spec/support/doubles.rb deleted file mode 100644 index 212859cbd6..0000000000 --- a/spec/support/doubles.rb +++ /dev/null @@ -1 +0,0 @@ -require "support/doubles/hash" \ No newline at end of file diff --git a/spec/support/doubles/hash.rb b/spec/support/doubles/hash.rb deleted file mode 100644 index fd9edd34ad..0000000000 --- a/spec/support/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 -- cgit v1.2.3