aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/test/cases/validations_test.rb
blob: c6c6e1d78607a37955d8a01d5b0ee71f2c46a6ab (plain) (blame)
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
62
63
64
65
66
67
require 'abstract_unit'
require 'fixtures/project'
require 'active_support/core_ext/hash/conversions'

# The validations are tested thoroughly under ActiveModel::Validations
# This test case simply makes sure that they are all accessible by
# Active Resource objects.
class ValidationsTest < ActiveModel::TestCase
  VALID_PROJECT_HASH = { :name => "My Project", :description => "A project" }
  def setup
    @my_proj = { "person" => VALID_PROJECT_HASH }.to_json
    ActiveResource::HttpMock.respond_to do |mock|
      mock.post "/projects.json", {}, @my_proj, 201, 'Location' => '/projects/5.json'
    end
  end

  def test_validates_presence_of
    p = new_project(:name => nil)
    assert !p.valid?, "should not be a valid record without name"
    assert !p.save, "should not have saved an invalid record"
    assert_equal ["can't be blank"], p.errors[:name], "should have an error on name"

    p.name = "something"

    assert p.save, "should have saved after fixing the validation, but had: #{p.errors.inspect}"
  end

  def test_fails_save!
    p = new_project(:name => nil)
    assert_raise(ActiveResource::ResourceInvalid) { p.save! }
  end

  def test_save_without_validation
    p = new_project(:name => nil)
    assert !p.save
    assert p.save(:validate => false)
  end

  def test_validate_callback
    # we have a callback ensuring the description is longer than three letters
    p = new_project(:description => 'a')
    assert !p.valid?, "should not be a valid record when it fails a validation callback"
    assert !p.save, "should not have saved an invalid record"
    assert_equal ["must be greater than three letters long"], p.errors[:description], "should be an error on description"

    # should now allow this description
    p.description = 'abcd'
    assert p.save, "should have saved after fixing the validation, but had: #{p.errors.inspect}"
  end

  def test_client_side_validation_maximum
    project = Project.new(:description => '123456789012345')
    assert ! project.valid?
    assert_equal ['is too long (maximum is 10 characters)'], project.errors[:description]
  end

  protected

  # quickie helper to create a new project with all the required
  # attributes.
  # Pass in any params you specifically want to override
  def new_project(opts = {})
    Project.new(VALID_PROJECT_HASH.merge(opts))
  end

end