File: test_uniqueness_validator.rb

package info (click to toggle)
ruby-client-side-validations 3.2.6%2Bgh-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 672 kB
  • ctags: 510
  • sloc: ruby: 2,959; makefile: 2
file content (61 lines) | stat: -rw-r--r-- 3,202 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'active_record/cases/test_base'

class ActiveRecord::UniquenessValidatorTest < ClientSideValidations::ActiveRecordTestBase

  def test_uniqueness_client_side_hash
    expected_hash = { :message => "has already been taken", :case_sensitive => true }
    assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name]).client_side_hash(@user, :name)
  end

  def test_uniqueness_client_side_hash_allowing_blank
    expected_hash = { :message => "has already been taken", :case_sensitive => true, :allow_blank => true }
    assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name], :allow_blank => true).client_side_hash(@user, :name)
  end

  def test_uniqueness_client_side_hash_with_custom_message
    expected_hash = { :message => "is not available", :case_sensitive => true }
    assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name], :message => "is not available").client_side_hash(@user, :name)
  end

  def test_uniqueness_client_side_hash
    @user.stubs(:new_record?).returns(false)
    @user.stubs(:id).returns(1)
    expected_hash = { :message => "has already been taken", :case_sensitive => true, :id => 1 }
    assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name]).client_side_hash(@user, :name)
  end

  def test_uniqueness_client_side_hash_with_single_scope_item
    @user.stubs(:age).returns(30)
    @user.stubs(:title).returns("test title")
    expected_hash = { :message => "has already been taken", :case_sensitive => true, :scope => {:title => "test title"} }
    result_hash = UniquenessValidator.new(:attributes => [:name], :scope => :title).client_side_hash(@user, :name)
    assert_equal expected_hash, result_hash
  end

  def test_uniqueness_client_side_hash_with_multiple_scope_items
    @user.stubs(:age).returns(30)
    @user.stubs(:title).returns("test title")
    expected_hash = { :message => "has already been taken", :case_sensitive => true, :scope => {:age => 30, :title => "test title"} }
    result_hash = UniquenessValidator.new(:attributes => [:name], :scope => [:age, :title]).client_side_hash(@user, :name)
    assert_equal expected_hash, result_hash
  end

  def test_uniqueness_client_side_hash_with_empty_scope_array
    expected_hash = { :message => "has already been taken", :case_sensitive => true }
    result_hash = UniquenessValidator.new(:attributes => [:name], :scope => []).client_side_hash(@user, :name)
    assert_equal expected_hash, result_hash
  end

  def test_uniqueness_client_side_hash_when_nested_module
    @user = ActiveRecordTestModule::User2.new
    expected_hash = { :message => "has already been taken", :case_sensitive => true, :class => 'active_record_test_module/user2' }
    assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name]).client_side_hash(@user, :name)
  end

  def test_uniqueness_client_side_hash_with_class_from_options
    @user = UserForm.new
    expected_hash = { :message => "has already been taken", :case_sensitive => true, :class => 'user' }
    assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name], :client_validations => { :class => 'User' } ).client_side_hash(@user, :name)
  end
end