From d2988420fc6dd91ca751d96ed648fd1ed52ce342 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 17:53:40 -0400 Subject: [PATCH] Added PostgreSQL to build --- Rakefile | 4 +- .../sql/unit/predicates/binary_spec.rb | 18 +++++++-- .../engines/sql/unit/primitives/value_spec.rb | 9 ++++- .../engines/sql/unit/relations/insert_spec.rb | 20 +++++++++- .../engines/sql/unit/relations/update_spec.rb | 38 +++++++++++++++++-- spec/arel/unit/predicates/predicates_spec.rb | 16 +++++++- spec/connections/postgresql_connection.rb | 12 ++++++ spec/schemas/postgresql_schema.rb | 18 +++++++++ spec/spec_helper.rb | 14 ++++++- 9 files changed, 134 insertions(+), 15 deletions(-) create mode 100644 spec/connections/postgresql_connection.rb create mode 100644 spec/schemas/postgresql_schema.rb diff --git a/Rakefile b/Rakefile index cfae22260a..17a6f8d35d 100644 --- a/Rakefile +++ b/Rakefile @@ -17,7 +17,7 @@ Spec::Rake::SpecTask.new(:coverage) do |t| end namespace :spec do - for adapter in %w[mysql sqlite3] + for adapter in %w[mysql sqlite3 postgresql] desc "Run specs with the #{adapter} database adapter" Spec::Rake::SpecTask.new(adapter) do |t| t.spec_files = @@ -29,7 +29,7 @@ namespace :spec do end desc "Run specs with mysql and sqlite3 database adapters (default)" -task :spec => ["spec:sqlite3", "spec:mysql"] +task :spec => ["spec:sqlite3", "spec:mysql", "spec:postgresql"] desc "Default task is to run specs" task :default => :spec diff --git a/spec/arel/engines/sql/unit/predicates/binary_spec.rb b/spec/arel/engines/sql/unit/predicates/binary_spec.rb index befd2878d9..b1400e2588 100644 --- a/spec/arel/engines/sql/unit/predicates/binary_spec.rb +++ b/spec/arel/engines/sql/unit/predicates/binary_spec.rb @@ -28,7 +28,11 @@ module Arel sql.should be_like(%Q{(`users`.`id` <=> 1 OR `users`.`name` <=> 'name')}) end - adapter_is_not :mysql do + adapter_is :postgresql do + sql.should be_like(%Q{("users"."id" <=> 1 OR "users"."name" <=> E'name')}) + end + + adapter_is :sqlite3 do sql.should be_like(%Q{("users"."id" <=> 1 OR "users"."name" <=> 'name')}) end end @@ -44,9 +48,13 @@ module Arel sql.should be_like(%Q{(`users`.`id` <=> 1 AND `users`.`name` <=> 'name')}) end - adapter_is_not :mysql do + adapter_is :sqlite3 do sql.should be_like(%Q{("users"."id" <=> 1 AND "users"."name" <=> 'name')}) end + + adapter_is :postgresql do + sql.should be_like(%Q{("users"."id" <=> 1 AND "users"."name" <=> E'name')}) + end end end end @@ -94,9 +102,13 @@ module Arel sql.should be_like(%Q{`users`.`name` <=> '1-asdf'}) end - adapter_is_not :mysql do + adapter_is :sqlite3 do sql.should be_like(%Q{"users"."name" <=> '1-asdf'}) end + + adapter_is :postgresql do + sql.should be_like(%Q{"users"."name" <=> E'1-asdf'}) + end end end end diff --git a/spec/arel/engines/sql/unit/primitives/value_spec.rb b/spec/arel/engines/sql/unit/primitives/value_spec.rb index da5a163d3b..ff3533f6ef 100644 --- a/spec/arel/engines/sql/unit/primitives/value_spec.rb +++ b/spec/arel/engines/sql/unit/primitives/value_spec.rb @@ -9,7 +9,14 @@ module Arel describe '#to_sql' do it "appropriately quotes the value" do Value.new(1, @relation).to_sql.should be_like('1') - Value.new('asdf', @relation).to_sql.should be_like("'asdf'") + + adapter_is_not :postgresql do + Value.new('asdf', @relation).to_sql.should be_like("'asdf'") + end + + adapter_is :postgresql do + Value.new('asdf', @relation).to_sql.should be_like("E'asdf'") + end end end diff --git a/spec/arel/engines/sql/unit/relations/insert_spec.rb b/spec/arel/engines/sql/unit/relations/insert_spec.rb index dd1995cced..29a5e0bf42 100644 --- a/spec/arel/engines/sql/unit/relations/insert_spec.rb +++ b/spec/arel/engines/sql/unit/relations/insert_spec.rb @@ -30,13 +30,21 @@ module Arel }) end - adapter_is_not :mysql do + adapter_is :sqlite3 do @insertion.to_sql.should be_like(%Q{ INSERT INTO "users" ("id", "name") VALUES (1, 'nick') }) end + + adapter_is :postgresql do + @insertion.to_sql.should be_like(%Q{ + INSERT + INTO "users" + ("id", "name") VALUES (1, E'nick') + }) + end end describe 'when given values whose types correspond to the types of the attributes' do @@ -53,13 +61,21 @@ module Arel }) end - adapter_is_not :mysql do + adapter_is :sqlite3 do @insertion.to_sql.should be_like(%Q{ INSERT INTO "users" ("name") VALUES ('nick') }) end + + adapter_is :postgresql do + @insertion.to_sql.should be_like(%Q{ + INSERT + INTO "users" + ("name") VALUES (E'nick') + }) + end end end diff --git a/spec/arel/engines/sql/unit/relations/update_spec.rb b/spec/arel/engines/sql/unit/relations/update_spec.rb index f553490ef5..4d728eb241 100644 --- a/spec/arel/engines/sql/unit/relations/update_spec.rb +++ b/spec/arel/engines/sql/unit/relations/update_spec.rb @@ -17,12 +17,19 @@ module Arel }) end - adapter_is_not :mysql do + adapter_is :sqlite3 do sql.should be_like(%Q{ UPDATE "users" SET "id" = 1, "name" = 'nick' }) end + + adapter_is :postgresql do + sql.should be_like(%Q{ + UPDATE "users" + SET "id" = 1, "name" = E'nick' + }) + end end it "manufactures sql updating attributes when given a ranged relation" do @@ -36,13 +43,21 @@ module Arel }) end - adapter_is_not :mysql do + adapter_is :sqlite3 do sql.should be_like(%Q{ UPDATE "users" SET "name" = 'nick' LIMIT 1 }) end + + adapter_is :postgresql do + sql.should be_like(%Q{ + UPDATE "users" + SET "name" = E'nick' + LIMIT 1 + }) + end end describe 'when given values whose types correspond to the types of the attributes' do @@ -58,12 +73,19 @@ module Arel }) end - adapter_is_not :mysql do + adapter_is :sqlite3 do @update.to_sql.should be_like(%Q{ UPDATE "users" SET "name" = 'nick' }) end + + adapter_is :postgresql do + @update.to_sql.should be_like(%Q{ + UPDATE "users" + SET "name" = E'nick' + }) + end end end @@ -106,13 +128,21 @@ module Arel }) end - adapter_is_not :mysql do + adapter_is :sqlite3 do @update.to_sql.should be_like(%Q{ UPDATE "users" SET "name" = 'nick' WHERE "users"."id" = 1 }) end + + adapter_is :postgresql do + @update.to_sql.should be_like(%Q{ + UPDATE "users" + SET "name" = E'nick' + WHERE "users"."id" = 1 + }) + end end end end diff --git a/spec/arel/unit/predicates/predicates_spec.rb b/spec/arel/unit/predicates/predicates_spec.rb index ac842998af..8e7e0b1d9f 100644 --- a/spec/arel/unit/predicates/predicates_spec.rb +++ b/spec/arel/unit/predicates/predicates_spec.rb @@ -21,11 +21,17 @@ module Arel }) end - adapter_is_not :mysql do + adapter_is :sqlite3 do sql.should be_like(%Q{ ("users"."id" = 1 AND "users"."name" = 'name') }) end + + adapter_is :postgresql do + sql.should be_like(%Q{ + ("users"."id" = 1 AND "users"."name" = E'name') + }) + end end end end @@ -41,11 +47,17 @@ module Arel }) end - adapter_is_not :mysql do + adapter_is :sqlite3 do sql.should be_like(%Q{ ("users"."id" = 1 OR "users"."name" = 'name') }) end + + adapter_is :postgresql do + sql.should be_like(%Q{ + ("users"."id" = 1 OR "users"."name" = E'name') + }) + end end end end diff --git a/spec/connections/postgresql_connection.rb b/spec/connections/postgresql_connection.rb new file mode 100644 index 0000000000..505dcdd1ef --- /dev/null +++ b/spec/connections/postgresql_connection.rb @@ -0,0 +1,12 @@ +require "activerecord" +puts "Using native PostgreSQL" + +ActiveRecord::Base.configurations = { + 'unit' => { + :adapter => 'postgresql', + :encoding => 'utf8', + :database => 'arel_unit', + } +} + +ActiveRecord::Base.establish_connection 'unit' diff --git a/spec/schemas/postgresql_schema.rb b/spec/schemas/postgresql_schema.rb new file mode 100644 index 0000000000..30fa665902 --- /dev/null +++ b/spec/schemas/postgresql_schema.rb @@ -0,0 +1,18 @@ +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 + ); +SQL + +sql.split(/;/).select(&:present?).each do |sql_statement| + ActiveRecord::Base.connection.execute sql_statement +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6a9a2ef23c..beb634fbd3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,15 +13,27 @@ end module AdapterGuards def adapter_is(name) + verify_adapter_name(name) yield if name.to_s == adapter_name end def adapter_is_not(name) + verify_adapter_name(name) yield if name.to_s != adapter_name end def adapter_name - Arel::Table.engine.connection.class.name.underscore.split("/").last.gsub(/_adapter/, '') + name = ActiveRecord::Base.configurations["unit"][:adapter] + 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] end end