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 --- 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 +++++++++++++++++++++ 16 files changed, 317 insertions(+) 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 (limited to 'spec/support') 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