<commit_before>require "spec_helper"

if defined?(::Sidekiq)
  require 'sidekiq/testing'

  describe Tantot::Strategy::Sidekiq do
    class SidekiqWatcher
      include Tantot::Watcher

      def perform(changes)
      end
    end

    before do
      stub_model(:city) do
        watch SidekiqWatcher, :name
      end
    end

    it "should call a sidekiq worker" do
      ::Sidekiq::Testing.inline! do
        Tantot.strategy(:sidekiq) do
          city = City.create name: 'foo'
          expect_any_instance_of(SidekiqWatcher).to receive(:perform).with({City => {city.id => {"name" => [nil, 'foo']}}})
        end
      end
    end

    it "should skip sidekiq and process atomically when `join`ing, the resume using sidekiq" do
      Sidekiq::Testing.fake! do
        Tantot.strategy(:sidekiq) do

          # Create a model, then join. It should have called perform wihtout triggering a sidekiq worker
          city = City.create name: 'foo'
          expect_any_instance_of(SidekiqWatcher).to receive(:perform).with({City => {city.id => {"name" => [nil, 'foo']}}})
          Tantot.strategy.join(SidekiqWatcher)
          expect(Tantot::Strategy::Sidekiq::Worker.jobs.size).to eq(0)

          # Further modifications should trigger through sidekiq when exiting the strategy block
          city.name = 'bar'
          city.save
        end
        expect(Tantot::Strategy::Sidekiq::Worker.jobs.size).to eq(1)
        expect(Tantot::Strategy::Sidekiq::Worker.jobs.first["args"]).to eq(["SidekiqWatcher", {"City" => {"1" => {"name" => ['foo', 'bar']}}}])
      end
    end<commit_msg>Add sidekiq specs<commit_after>require "spec_helper"

if defined?(::Sidekiq)
  require 'sidekiq/testing'

  describe Tantot::Strategy::Sidekiq do
    class SidekiqWatcher
      include Tantot::Watcher

      def perform(changes)
      end
    end

    before do
      Sidekiq::Worker.clear_all
      stub_model(:city) do
        watch SidekiqWatcher, :name
      end
    end

    it "should call a sidekiq worker" do
      Tantot.strategy(:sidekiq) do
        City.create name: 'foo'
      end
      expect(Tantot::Strategy::Sidekiq::Worker.jobs.size).to eq(1)
      expect(Tantot::Strategy::Sidekiq::Worker.jobs.first["args"]).to eq(["SidekiqWatcher", {"City" => {"1" => {"name" => [nil, 'foo']}}}])
    end

    it "should call the watcher" do
      ::Sidekiq::Testing.inline! do
        Tantot.strategy(:sidekiq) do
          city = City.create name: 'foo'
          expect_any_instance_of(SidekiqWatcher).to receive(:perform).with({City => {city.id => {"name" => [nil, 'foo']}}})
        end
      end
    end

    it "should skip sidekiq and process atomically when `join`ing, then resume using sidekiq" do
      Sidekiq::Testing.fake! do
        Tantot.strategy(:sidekiq) do

          # Create a model, then join. It should have called perform wihtout triggering a sidekiq worker
          city = City.create name: 'foo'
          expect_any_instance_of(SidekiqWatcher).to receive(:perform).with({City => {city.id => {"name" => [nil, 'foo']}}})
          Tantot.strategy.join(SidekiqWatcher)
          expect(Tantot::Strategy::Sidekiq::Worker.jobs.size).to eq(0)

          # Further modifications should trigger through sidekiq when exiting the strategy block
          city.name = 'bar'
          city.save
        end
        expect(Tantot::Strategy::Sidekiq::Worker.jobs.size).to eq(1)
        expect(Tantot::Strategy::Sidekiq::Worker.jobs.first["args"]).to eq(["SidekiqWatcher", {"City" => {"1" => {"name" => ['foo', 'bar']}}}])
      end
    end
{'fraction_non_alphanumeric': 0.13549295774647888, 'fraction_numerical': 0.0022535211267605635, 'mean_word_length': 2.872410032715376, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, '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': '4513155', 'n_tokens_mistral': 1161, 'n_tokens_neox': 1130, 'n_words': 268}