Permalink
Browse files

fix tests when running with postgres

  • Loading branch information...
1 parent 196b919 commit f23b80d7b86518f432892e3ac5cd43e24dd70803 Dmitriy Kalinin and Mark Rushakoff committed Mar 26, 2013
Showing with 49 additions and 21 deletions.
  1. +1 −1 lib/cloud_controller.rb
  2. +14 −0 lib/vcap/rest_api/query.rb
  3. +34 −20 spec/spec_helper.rb
@@ -50,7 +50,7 @@ def initialize(config, token_decoder)
end
if uaa_id
- user = Models::User.find(:guid => uaa_id)
+ user = Models::User.find(:guid => uaa_id.to_s)
user ||= create_admin_if_in_config(token_information)
user ||= create_admin_if_in_token(token_information)
end
@@ -73,6 +73,7 @@ def filter_args_from_query
q_key, q_val = parse
q_key, q_val = clean_up_foreign_key(q_key, q_val)
q_key, q_val = clean_up_boolean(q_key, q_val)
+ q_key, q_val = clean_up_integer(q_key, q_val)
{q_key => q_val}
end
@@ -132,6 +133,19 @@ def clean_up_boolean(q_key, q_val)
[q_key, q_val]
end
+ def clean_up_integer(q_key, q_val)
+ column = model.db_schema[q_key.to_sym]
+ unless column
+ return [q_key, q_val]
+ end
+
+ if column[:type] == :integer
+ q_val = q_val.to_i if q_val
+ end
+
+ [q_key, q_val]
+ end
+
attr_accessor :model, :access_filter, :queryable_attributes, :query
end
end
View
@@ -49,28 +49,14 @@ def log_filename
artifact_filename("spec.log")
end
- def without_foreign_key_checks
- case db.database_type
- when :sqlite
- db.execute("PRAGMA foreign_keys = OFF")
- yield
- db.execute("PRAGMA foreign_keys = ON")
- when :mysql
- db.execute("SET foreign_key_checks = 0")
- yield
- db.execute("SET foreign_key_checks = 1")
- else
- raise "Unknown db"
- end
- end
-
def reset_database
- without_foreign_key_checks do
- db.tables.each do |table|
- db.drop_table(table)
- end
- VCAP::CloudController::DB.apply_migrations(db)
+ prepare_database
+
+ db.tables.each do |table|
+ drop_table_unsafely(table)
end
+
+ VCAP::CloudController::DB.apply_migrations(db)
end
def db
@@ -98,6 +84,34 @@ def db_logger
end
@db_logger
end
+
+ private
+
+ def prepare_database
+ if db.database_type == :postgres
+ db.execute("CREATE EXTENSION IF NOT EXISTS citext")
+ end
+ end
+
+ def drop_table_unsafely(table)
+ case db.database_type
+ when :sqlite
+ db.execute("PRAGMA foreign_keys = OFF")
+ db.drop_table(table)
+ db.execute("PRAGMA foreign_keys = ON")
+
+ when :mysql
+ db.execute("SET foreign_key_checks = 0")
+ db.drop_table(table)
+ db.execute("SET foreign_key_checks = 1")
+
+ # Postgres uses CASCADE directive in DROP TABLE
+ # to remove foreign key contstraints.
+ # http://www.postgresql.org/docs/9.2/static/sql-droptable.html
+ else
+ db.drop_table(table, :cascade => true)
+ end
+ end
end
end

0 comments on commit f23b80d

Please sign in to comment.