summaryrefslogtreecommitdiff
path: root/test/models/tag_test.rb
blob: 57e30f6bc04e6bf6d2b17bc4e447f455a322db1b (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
# frozen_string_literal: true

# == Schema Information
#
# Table name: tags
#
#  id                :integer          not null, primary key
#  category          :string
#  description       :string           default("")
#  image             :string
#  images_count      :integer          default(0), not null
#  mod_notes         :string
#  name              :string           not null
#  name_in_namespace :string
#  namespace         :string
#  short_description :string           default("")
#  slug              :string           not null
#  created_at        :datetime         not null
#  updated_at        :datetime         not null
#  aliased_tag_id    :integer
#
# Indexes
#
#  index_tags_on_aliased_tag_id  (aliased_tag_id)
#  index_tags_on_name            (name)
#  index_tags_on_slug            (slug)
#
# Foreign Keys
#
#  fk_rails_...  (aliased_tag_id => tags.id) ON DELETE => nullify ON UPDATE => cascade
#
require 'test_helper'

class TagTest < ActiveSupport::TestCase
  include ActiveJob::TestHelper

  setup do
    @tag = create(:tag)
  end

  test 'should normalize tag names on creation' do
    Tag.create(name: 'EenAhp')
    assert_not Tag.exists?(name: 'EenAhp')
    assert Tag.exists?(name: 'eenahp')
  end

  test 'should normalize tag names before uniqueness validation' do
    Tag.create(name: 'EenAhp')
    assert_raises(ActiveRecord::RecordInvalid) { Tag.create!(name: 'eenahp') }
  end

  def make_image_and_tag
    @image = create(:dedupe_image_1)
    @image.add_tags [@tag]
    @image.save
  end

  test 'images_count should change when tagged' do
    assert_difference('@tag.images_count') do
      make_image_and_tag
      [@image, @tag].each(&:reload)
    end

    # Prevent tag suiciding.
    @image2 = create(:dedupe_image_2)
    @image2.add_tags [@tag]
    @image2.save
    [@image2, @tag].each(&:reload)
    assert_difference('@tag.images_count', -1) do
      @image.remove_tags [@tag]
      @image.save
      [@image, @tag].each(&:reload)
    end
  end

  test 'aliasing should be revertible' do
    @tag.target_tag_name = create(:tag).name
    @tag.save

    @tag.target_tag_name = ''
    assert @tag.save
    assert_nil @tag.aliased_tag
    assert_nil @tag.aliased_tag_name
  end
end