aboutsummaryrefslogtreecommitdiff
path: root/test/models/thing_test.rb
blob: ef2a1e2ba14b25db2f7c42ecae72f1fd48350da6 (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
# frozen_string_literal: true

require 'test_helper'

class ThingTest < ActiveSupport::TestCase
  test 'presence: title' do
    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
  end

  test 'presence: target' do
    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
  end

  test 'presence: authors' do
    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
  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 'target must be unique' do
    thing = things(:thing1).dup

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

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

  test 'has many comments' do
    thing = things(:thing1)

    assert_equal 1, thing.comments.size
  end

  test 'has many tags through tag_references' do
    thing = things(:thing1)

    assert_equal 2, thing.tags.size
  end
end