<commit_before>require 'test_helper'

class ActsAsTaggableTest < ActiveSupport::TestCase
  setup do
    3.times do |i|
      User.create(name: "user#{i+1}")
    end
    3.times do |i|
      Blog.create(title: "blog title #{i+1}")
    end
    3.times do |i|
      Post.create(title: "post title #{i+1}")
    end
    ['vector', 'list', 'deque'].each do |tag|
      ActsAsTaggable::Tag.create(name: tag, user: User.all[0])
    end
  end

  test "module 'ActsAsTaggable' is existed" do
    assert_kind_of Module, ActsAsTaggable
  end

  test "acts_as_tagger method is available" do
    assert User.respond_to? :acts_as_tagger
    assert User.respond_to? :acts_as_tagger_on
  end

  test "acts_as_taggable method is available" do
    assert Blog.respond_to? :acts_as_taggable
    assert Blog.respond_to? :acts_as_taggable_by
    assert Post.respond_to? :acts_as_taggable
    assert Post.respond_to? :acts_as_taggable_by
  end

  test "tag?/tag/untag methods are available" do
    user = User.first
    assert user.respond_to? :tag?
    assert user.respond_to? :tag
    assert user.respond_to? :untag
  end

  test "method 'acts_as_tagger' is available" do
    user1, user2 = User.all[0], User.all[1];
    post1, post2 = Post.all[0], Post.all[1];
    tag1,  tag2  = ActsAsTaggable::Tag.all[0], ActsAsTaggable::Tag.all[1];

    assert_equal(false, user1.tag?(tag1, post1, 'favorite'))
    user1.tag(tag1, post1, 'favorite')
    assert_equal(true, user1.tag?(tag1, post1, 'favorite'))
    assert_equal 1, ActsAsTaggable::Tagging.all.count
    assert_equal 1, user1.taggings.count

    user1.tag(tag2, post2, 'favorite')
    assert_equal(true, user1.tag?(tag2, post2, 'favorite'))
    assert_equal 2, user1.taggings.count

    user1.tag(tag1, post2, 'read')
    assert(user1.tag?(tag1, post2, 'read'))
    assert_equal 3, user1.taggings.count
    assert_equal 2, user1.taggings.where(context: 'favorite').count
    assert_equal 1, user1.taggings.where(context: 'read').count

    user1.untag(tag1, post2, 'read')
    assert(!user1.tag?(tag1, post2, 'read'))
    assert_equal 2, user1.taggings.count

    user1.untag(tag2, post2, 'favorite')
    assert_equal(false, user1.tag?(tag2, post2, 'favorite'))
    assert_equal 1, user1.taggings.count

    user1.untag(tag1, post1, 'favorite')
    assert_equal(false, user1.tag?(tag1, post1, 'favorite'))
    assert_equal 0, user1.taggings.count
  end

=begin
  test "method 'acts_as_tagger_on' is available" do
    user1, user2 = User.all[0], User.all[1];
    post1, post2 = Post.all[0], Post.all[1];

    assert_equal 0, user1.favorite_posts_taggings.count
    assert_equal 0, user1.favorite_posts.count
    user1.tag(post1, 'favorite')
    assert_equal 1, user1.taggings.where(context: 'favorite').size
    assert_equal 1, user1.favorite_posts_taggings.count
    assert_equal 1, user1.favorite_posts.count

    assert_equal 0, user1.read_posts_taggings.count
    assert_equal 0, user1.read_posts.count
    user1.tag(post1, 'read')
    assert_equal 1, user1.taggings.where(context: 'read').size
    assert_equal 1, user1.read_posts_taggings.count
    assert_equal 1, user1.read_posts.count

    assert_equal 2, user1.taggings.size
  end
=end

  test "method 'acts_as_taggable_by' is available" do
    user1, user2 = User.all[0], User.all[1];
    post1, post2 = Post.all[0], Post.all[1];
    tag1, tag2, tag3  = ActsAsTaggable::Tag.all[0], ActsAsTaggable::Tag.all[1], ActsAsTaggable::Tag.all[2];

    assert_equal 0, post1.favorite_taggings.count
    assert_equal 0, post1.favorite_tags.count
  # user1.tag(tag1, post1, 'favorite')
    post1.favorite_by tag1, user1
    assert_equal 1, post1.taggings.where(context: 'favorite').size
    assert_equal 1, post1.favorite_taggings.count
    assert_equal 1, post1.favorite_tags.count

    p post1.favorite_taggings.size
    p post1.favorite_tags.count
    p post1.favorite_tag_list

  # user1.tag(tag2, post1, 'favorite')
    post1.favorite_by tag2, user1
    p post1.favorite_taggings.size
    p post1.favorite_tags.count
    p post1.favorite_tag_list

    post1.favorite_tag_list = 'ruby, rails, gems'
    p post1.favorite_taggings.size
    p post1.favorite_tags.count
    p post1.favorite_tag_list

    tagging = post1.tag_by tag3, user1, 'favorite'
    p post1.favorite_taggings.size
  # post1.favorite_taggings << tagging
    p post1.favorite_tags.count
    p post1.favorite_tag_list
  # p post1.favorite_tags << tag3

  # assert_equal 0, post1.read_users_taggings.count
  # assert_equal 0, post1.read_users.count
  # user1.tag(post1, 'read')
  # assert_equal 1, post1.taggings.where(context: 'read').size
  # assert_equal 1, post1.read_users_taggings.count
  # assert_equal 1, post1.read_users.count

  # assert_equal 2, post1.taggings.size
  end

=begin
  test "generated methods by 'acts_as_tagger_on' are available" do
    user1, user2 = User.all[0], User.all[1];
    post1, post2 = Post.all[0], Post.all[1];
<commit_msg>add more asserts for test cases.<commit_after>require 'test_helper'

class ActsAsTaggableTest < ActiveSupport::TestCase
  setup do
    3.times do |i|
      User.create(name: "user#{i+1}")
    end
    3.times do |i|
      Blog.create(title: "blog title #{i+1}")
    end
    3.times do |i|
      Post.create(title: "post title #{i+1}")
    end
    ['vector', 'list', 'deque'].each do |tag|
      ActsAsTaggable::Tag.create(name: tag, user: User.all[0])
    end
  end

  test "module 'ActsAsTaggable' is existed" do
    assert_kind_of Module, ActsAsTaggable
  end

  test "acts_as_tagger method is available" do
    assert User.respond_to? :acts_as_tagger
    assert User.respond_to? :acts_as_tagger_on
  end

  test "acts_as_taggable method is available" do
    assert Blog.respond_to? :acts_as_taggable
    assert Blog.respond_to? :acts_as_taggable_by
    assert Post.respond_to? :acts_as_taggable
    assert Post.respond_to? :acts_as_taggable_by
  end

  test "tag?/tag/untag methods are available" do
    user = User.first
    assert user.respond_to? :tag?
    assert user.respond_to? :tag
    assert user.respond_to? :untag
  end

  test "method 'acts_as_tagger' is available" do
    user1, user2 = User.all[0], User.all[1];
    post1, post2 = Post.all[0], Post.all[1];
    tag1,  tag2  = ActsAsTaggable::Tag.all[0], ActsAsTaggable::Tag.all[1];

    assert_equal(false, user1.tag?(tag1, post1, 'favorite'))
    user1.tag(tag1, post1, 'favorite')
    assert_equal(true, user1.tag?(tag1, post1, 'favorite'))
    assert_equal 1, ActsAsTaggable::Tagging.all.count
    assert_equal 1, user1.taggings.count

    user1.tag(tag2, post2, 'favorite')
    assert_equal(true, user1.tag?(tag2, post2, 'favorite'))
    assert_equal 2, user1.taggings.count

    user1.tag(tag1, post2, 'read')
    assert(user1.tag?(tag1, post2, 'read'))
    assert_equal 3, user1.taggings.count
    assert_equal 2, user1.taggings.where(context: 'favorite').count
    assert_equal 1, user1.taggings.where(context: 'read').count

    user1.untag(tag1, post2, 'read')
    assert(!user1.tag?(tag1, post2, 'read'))
    assert_equal 2, user1.taggings.count

    user1.untag(tag2, post2, 'favorite')
    assert_equal(false, user1.tag?(tag2, post2, 'favorite'))
    assert_equal 1, user1.taggings.count

    user1.untag(tag1, post1, 'favorite')
    assert_equal(false, user1.tag?(tag1, post1, 'favorite'))
    assert_equal 0, user1.taggings.count
  end

=begin
  test "method 'acts_as_tagger_on' is available" do
    user1, user2 = User.all[0], User.all[1];
    post1, post2 = Post.all[0], Post.all[1];

    assert_equal 0, user1.favorite_posts_taggings.count
    assert_equal 0, user1.favorite_posts.count
    user1.tag(post1, 'favorite')
    assert_equal 1, user1.taggings.where(context: 'favorite').size
    assert_equal 1, user1.favorite_posts_taggings.count
    assert_equal 1, user1.favorite_posts.count

    assert_equal 0, user1.read_posts_taggings.count
    assert_equal 0, user1.read_posts.count
    user1.tag(post1, 'read')
    assert_equal 1, user1.taggings.where(context: 'read').size
    assert_equal 1, user1.read_posts_taggings.count
    assert_equal 1, user1.read_posts.count

    assert_equal 2, user1.taggings.size
  end
=end

  test "method 'acts_as_taggable_by' is available" do
    user1, user2 = User.all[0], User.all[1];
    post1, post2 = Post.all[0], Post.all[1];
    tag1, tag2, tag3  = ActsAsTaggable::Tag.all[0], ActsAsTaggable::Tag.all[1], ActsAsTaggable::Tag.all[2];

    assert_equal 0, post1.favorite_taggings.count
    assert_equal 0, post1.favorite_tags.count

    post1.favorite_by tag1, user1
    assert_equal 1, post1.taggings.where(context: 'favorite').size
    assert_equal 1, post1.favorite_taggings.count
    assert_equal 1, post1.favorite_tags.count
    assert_equal 1, post1.favorite_taggings.size
    assert_equal 1, post1.favorite_tags.count
    assert_equal 'vector', post1.favorite_tag_list

    post1.favorite_by tag2, user1
    assert_equal 2, post1.favorite_taggings.size
    assert_equal 2, post1.favorite_tags.count
    assert_equal 'vector, list', post1.favorite_tag_list

    post1.favorite_tag_list = 'ruby, rails, gems'
    assert_equal 3, post1.favorite_taggings.size
    assert_equal 3, post1.favorite_tags.count
    assert_equal 'ruby, rails, gems', post1.favorite_tag_list

    tagging = post1.tag_by tag3, user1, 'favorite'
    assert_equal 4, post1.favorite_taggings.size
    assert_equal 4, post1.favorite_tags.count
    assert_equal 'ruby, rails, gems, deque', post1.favorite_tag_list

    assert_equal 0, post1.read_taggings.count
    assert_equal 0, post1.read_tags.count

    post1.read_by tag1, user1
    assert_equal 1, post1.taggings.where(context: 'read').size
    assert_equal 1, post1.read_taggings.count
    assert_equal 1, post1.read_tags.count
    assert_equal 'vector', post1.read_tag_list
  end

=begin
  test "generated methods by 'acts_as_tagger_on' are available" do
    user1, user2 = User.all[0], User.all[1];
    post1, post2 = Post.all[0], Post.all[1];

{'fraction_non_alphanumeric': 0.11696437581996165, 'fraction_numerical': 0.037440710465233625, 'mean_word_length': 3.937717987045341, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6587393', 'n_tokens_mistral': 3892, 'n_tokens_neox': 3770, 'n_words': 759}