# 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