aboutsummaryrefslogtreecommitdiff
path: root/test/models/thing_test.rb
blob: 0b3961eb812502350b5449030d705e7c2fd3a55b (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# frozen_string_literal: true

require 'test_helper'

class ThingTest < ActiveSupport::TestCase
  test 'validates presence' do
    assert_raise(ActiveRecord::RecordInvalid) { Thing.new.save! }

    # Missing 'title'
    assert_raise(ActiveRecord::RecordInvalid) do
      Thing.new(target: 'target', authors: 'Author',
                user_id: users(:user).id, rate: 5,
                status: Thing.statuses[:read], kind: Thing.kinds[:novel]).save!
    end

    # Missing 'target'
    assert_raise(ActiveRecord::RecordInvalid) do
      Thing.new(title: 'title', authors: 'Author',
                user_id: users(:user).id, rate: 5,
                status: Thing.statuses[:read], kind: Thing.kinds[:novel]).save!
    end

    # Missing 'authors'
    assert_raise(ActiveRecord::RecordInvalid) do
      Thing.new(title: 'title', target: 'target',
                user_id: users(:user).id, rate: 5,
                status: Thing.statuses[:read], kind: Thing.kinds[:novel]).save!
    end

    # Valid!
    assert_difference('Thing.count') do
      Thing.new(title: 'title', target: 'target', authors: 'Author',
                user_id: users(:user).id, rate: 5,
                status: Thing.statuses[:read], kind: Thing.kinds[:novel]).save!
    end
  end

  test "'rate' has to be between 0 and 10" do
    thing = things(:thing1)

    thing.rate = -1
    assert_raise(ActiveRecord::RecordInvalid) { thing.save! }

    thing.rate = 11
    assert_raise(ActiveRecord::RecordInvalid) { thing.save! }

    thing.rate = 5
    thing.save!
  end

  test "'status' has to have a value as defined by its enum" do
    thing = things(:thing1)

    thing.status = 'whatever'
    assert_raise(ActiveRecord::RecordInvalid) { thing.save! }

    thing.status = Thing.statuses[:tobepublished]
    thing.save!
  end

  test "'kind' has to have a value as defined by its enum" do
    thing = things(:thing1)

    thing.kind = 'whatever'
    assert_raise(ActiveRecord::RecordInvalid) { thing.save! }

    thing.kind = Thing.kinds[:other]
    thing.save!
  end

  test 'title and target must be unique' do
    thing = things(:thing1).dup

    thing.title = 'another'
    assert_raise(ActiveRecord::RecordInvalid) { thing.save! }

    thing.target = 'also another'
    thing.title = things(:thing1).title
    assert_raise(ActiveRecord::RecordInvalid) { thing.save! }

    thing.target = 'also another'
    thing.title = 'now for real'
    assert_difference('Thing.count') { thing.save! }
  end

  test 'has many comments' do
    thing = things(:thing1)
    assert thing.comments.size == 1
  end

  test 'has many tags through tag_references' do
    thing = things(:thing1)
    assert thing.tags.size == 2
  end
end