Permalink
Browse files

adding tests

  • Loading branch information...
1 parent 91a2347 commit b35faf76b422c98ded21a2b5b48f25f1176b0c38 @onomojo onomojo committed Mar 25, 2012
@@ -30,7 +30,7 @@ def self.is_unique?(klass, attribute, value, params)
(params[:scope] || {}).each do |attribute, value|
value = type_cast_value(klass, attribute, value)
- if relation.is_a?(String)
+ if relation.is_a?(Arel::Nodes::SqlLiteral)
relation = Arel::Nodes::SqlLiteral.new("#{relation} AND #{t[attribute].eq(value).to_sql}")
else
relation = relation.and(t[attribute].eq(value))
@@ -43,6 +43,7 @@ def self.is_unique?(klass, attribute, value, params)
private
def self.type_cast_value(klass, attribute, value)
+# raise attribute.inspect
klass.columns_hash[attribute].type_cast(value)
end
@@ -85,6 +85,24 @@ def test_mysql_adapter_uniqueness_when_id_is_given
assert_equal 'true', last_response.body
end
+ def test_mysql_adapter_uniqueness_when_id_is_given_with_scope
+ user = User.create(email: 'user@test.com', name: 'Brian')
+ ActiveRecord::ConnectionAdapters::SQLite3Adapter.
+ any_instance.expects(:instance_variable_get).
+ with("@config").
+ returns({:adapter => "mysql"})
+
+ sql_without_binary = "#{User.arel_table["email"].eq(user.email).to_sql} AND #{User.arel_table.primary_key.not_eq(user.id).to_sql} AND #{User.arel_table["name"].eq(user.name).to_sql}"
+ relation = Arel::Nodes::SqlLiteral.new("BINARY #{sql_without_binary}")
+
+ #NOTE: Stubs User#where because SQLite3 don't know BINARY
+ result = User.where(sql_without_binary)
+ User.expects(:where).with(relation).returns(result)
+
+ get '/validators/uniqueness', { 'user[email]' => user.email, 'scope' => {'name' => user.name}, 'case_sensitive' => true, 'id' => user.id}
+ assert_equal 'true', last_response.body
+ end
+
def test_uniqueness_when_scope_is_given
User.create(:email => 'user@test.com', :age => 25)
get '/validators/uniqueness', { 'user[email]' => 'user@test.com', 'scope' => { 'age' => 30 }, 'case_sensitive' => true }

0 comments on commit b35faf7

Please sign in to comment.