<commit_before>require 'test_helper'

describe 'Redis::Breadcrumb' do
  before do
    Redis::Breadcrumb.redis = MockRedis.new
  end

  it 'will track tracked keys in tracked_in' do
    class TrackedInBreadcrumb < Redis::Breadcrumb
      tracked_in 'tracking_key'

      owns :a_owned_key

      member_of_set :id => :a_set_of_things
    end

    TrackedInBreadcrumb .track

    assert_equal [
      ["srem", "a_set_of_things", "id"],
      ["del", "a_owned_key"]
    ].sort, TrackedInBreadcrumb.tracked_keys.sort
  end

  it 'can track owned keys for a specific object' do
    class OwnedBreadcrumb < Redis::Breadcrumb
      tracked_in 'widget:<id>:tracking'

      owns 'widget:<id>'    
    end

    obj = Object.new
    class << obj
      def id; "foo"; end
    end

    breadcrumb = OwnedBreadcrumb.track(obj)

    assert_equal [["del", "widget:foo"]], breadcrumb.tracked_keys
  end

  it 'can track member of set keys for a specific object' do
    class MemberOfSetBreadcrumb < Redis::Breadcrumb
      tracked_in 'widget:<id>:tracking'

      member_of_set "<id>" => :a_set_of_things
    end

    obj = Object.new
    class << obj
      def id; "foo"; end
    end

    breadcrumb = MemberOfSetBreadcrumb.track(obj)

    assert_equal [["srem", "a_set_of_things", "foo"]], breadcrumb.tracked_keys
  end
end
<commit_msg>Add test 'will raise if no object given for specialized template'<commit_after>require 'test_helper'

describe 'Redis::Breadcrumb' do
  before do
    Redis::Breadcrumb.redis = MockRedis.new
  end

  it 'will track tracked keys in tracked_in' do
    class TrackedInBreadcrumb < Redis::Breadcrumb
      tracked_in 'tracking_key'

      owns :a_owned_key

      member_of_set :id => :a_set_of_things
    end

    TrackedInBreadcrumb.track

    assert_equal [
      ["srem", "a_set_of_things", "id"],
      ["del", "a_owned_key"]
    ].sort, TrackedInBreadcrumb.tracked_keys.sort
  end

  it 'will raise if no object given for specialized template' do
    class UnspecializedBreadcrumb < Redis::Breadcrumb
      tracked_in 'tracking_key'

      owns 'a_special_<id>_key'
    end

    assert_raises BreadcrumbSpecializationError do
      UnspecializedBreadcrumb.track
    end
  end

  it 'can track owned keys for a specific object' do
    class OwnedBreadcrumb < Redis::Breadcrumb
      tracked_in 'widget:<id>:tracking'

      owns 'widget:<id>'    
    end

    obj = Object.new
    class << obj
      def id; "foo"; end
    end

    breadcrumb = OwnedBreadcrumb.track(obj)

    assert_equal [["del", "widget:foo"]], breadcrumb.tracked_keys
  end

  it 'can track member of set keys for a specific object' do
    class MemberOfSetBreadcrumb < Redis::Breadcrumb
      tracked_in 'widget:<id>:tracking'

      member_of_set "<id>" => :a_set_of_things
    end

    obj = Object.new
    class << obj
      def id; "foo"; end
    end

    breadcrumb = MemberOfSetBreadcrumb.track(obj)

    assert_equal [["srem", "a_set_of_things", "foo"]], breadcrumb.tracked_keys
  end
end

{'fraction_non_alphanumeric': 0.09498327759197324, 'fraction_numerical': 0.0, 'mean_word_length': 3.1657381615598887, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 16, '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': '1267894', 'n_tokens_mistral': 1095, 'n_tokens_neox': 1045, 'n_words': 260}