<commit_before>require 'time'

module Vanity::Adapters::SharedTests

  DummyAlternative = Struct.new(:id)

  def identity
    'test-identity'
  end

  def experiment
    :experiment
  end

  def alternative
    1
  end

  def metric
    :purchases
  end

  def run_experiment
    @subject.ab_add_participant(experiment, alternative, identity)
    @subject.ab_add_participant(experiment, alternative, 'other-identity')
    @subject.ab_add_conversion(experiment, alternative, identity, 2)
  end

  def self.included(base)
    base.instance_eval do
      before do
        @subject = adapter
        @subject.flushdb
      end

      describe 'metrics methods' do
        describe '#get_metric_last_update_at' do
          it 'is nil if the metric has never been tracked' do
            refute(@subject.get_metric_last_update_at(metric))
          end

          it 'returns the time of the last tracked event if present' do
            time = Time.now
            @subject.metric_track(metric, time, identity, [1])

            assert_in_delta(
              time,
              @subject.get_metric_last_update_at(metric),
              0.1
            )
          end
        end

        describe '#metric_values' do
          it 'returns the tracked metrics from the given range, binned by date' do
            time_1 = Time.iso8601("2016-01-01T00:00:00+00:00")
            time_2 = Time.iso8601("2016-01-01T12:00:00+00:00")
            time_3 = Time.iso8601("2016-01-02T00:00:00+00:00")
            time_4 = Time.iso8601("2016-01-02T12:00:00+00:00")
            time_5 = Time.iso8601("2016-01-03T12:00:00+00:00")

            @subject.metric_track(metric, time_1, identity, [1, 1])
            @subject.metric_track(metric, time_2, identity, [2, 1])
            @subject.metric_track(metric, time_3, identity, [3])
            @subject.metric_track(metric, time_4, identity, [4, 10])
            @subject.metric_track(metric, time_5, identity, [5])

            assert_equal(
              [[3, 2], [7, 10]],
              @subject.metric_values(metric, time_1, time_4)
            )
          end
        end

        describe '#destroy_metric' do
          it 'removes all data related to the metric' do
            @subject.metric_track(metric, Time.now, identity, [1])

            @subject.destroy_metric(metric)

            refute(@subject.get_metric_last_update_at(metric))
          end
        end
      end

      describe 'generic experiment methods' do
        describe '#experiment_persisted?' do
          it 'returns false if the experiment is unknown' do
            refute(@subject.experiment_persisted?(experiment))
          end

          it 'returns true if the experiment has data' do
            run_experiment

            assert(@subject.experiment_persisted?(experiment))
          end
        end

        describe '#set_experiment_created_at' do
          it 'sets the experiment creation date' do
            time = Time.now
            @subject.set_experiment_created_at(experiment, time)

            assert_equal(time, @subject.get_experiment_created_at(experiment))
          end
        end

        describe '#destroy_experiment' do
          it 'removes all information about the experiment' do
            run_experiment
            @subject.destroy_experiment(experiment)

            refute(@subject.experiment_persisted?(experiment))
          end
        end

        describe '#is_experiment_enabled?' do
          def with_experiments_start_enabled(enabled)<commit_msg>Relax resolution on adapter timestamp tests<commit_after>require 'time'

module Vanity::Adapters::SharedTests

  DummyAlternative = Struct.new(:id)

  def identity
    'test-identity'
  end

  def experiment
    :experiment
  end

  def alternative
    1
  end

  def metric
    :purchases
  end

  def run_experiment
    @subject.ab_add_participant(experiment, alternative, identity)
    @subject.ab_add_participant(experiment, alternative, 'other-identity')
    @subject.ab_add_conversion(experiment, alternative, identity, 2)
  end

  def self.included(base)
    base.instance_eval do
      before do
        @subject = adapter
        @subject.flushdb
      end

      describe 'metrics methods' do
        describe '#get_metric_last_update_at' do
          it 'is nil if the metric has never been tracked' do
            refute(@subject.get_metric_last_update_at(metric))
          end

          it 'returns the time of the last tracked event if present' do
            time = Time.now
            @subject.metric_track(metric, time, identity, [1])

            assert_in_delta(
              time,
              @subject.get_metric_last_update_at(metric),
              1.0
            )
          end
        end

        describe '#metric_values' do
          it 'returns the tracked metrics from the given range, binned by date' do
            time_1 = Time.iso8601("2016-01-01T00:00:00+00:00")
            time_2 = Time.iso8601("2016-01-01T12:00:00+00:00")
            time_3 = Time.iso8601("2016-01-02T00:00:00+00:00")
            time_4 = Time.iso8601("2016-01-02T12:00:00+00:00")
            time_5 = Time.iso8601("2016-01-03T12:00:00+00:00")

            @subject.metric_track(metric, time_1, identity, [1, 1])
            @subject.metric_track(metric, time_2, identity, [2, 1])
            @subject.metric_track(metric, time_3, identity, [3])
            @subject.metric_track(metric, time_4, identity, [4, 10])
            @subject.metric_track(metric, time_5, identity, [5])

            assert_equal(
              [[3, 2], [7, 10]],
              @subject.metric_values(metric, time_1, time_4)
            )
          end
        end

        describe '#destroy_metric' do
          it 'removes all data related to the metric' do
            @subject.metric_track(metric, Time.now, identity, [1])

            @subject.destroy_metric(metric)

            refute(@subject.get_metric_last_update_at(metric))
          end
        end
      end

      describe 'generic experiment methods' do
        describe '#experiment_persisted?' do
          it 'returns false if the experiment is unknown' do
            refute(@subject.experiment_persisted?(experiment))
          end

          it 'returns true if the experiment has data' do
            run_experiment

            assert(@subject.experiment_persisted?(experiment))
          end
        end

        describe '#set_experiment_created_at' do
          it 'sets the experiment creation date' do
            time = Time.now
            @subject.set_experiment_created_at(experiment, time)

            assert_in_delta(
              time,
              @subject.get_experiment_created_at(experiment),
              1.0
            )
          end
        end

        describe '#destroy_experiment' do
          it 'removes all information about the experiment' do
            run_experiment
            @subject.destroy_experiment(experiment)

            refute(@subject.experiment_persisted?(experiment))
          end
        end

        describe '#is_experiment_enabled?' do
          def with_experiments_start_enabled(enabled)
{'fraction_non_alphanumeric': 0.08618753509264458, 'fraction_numerical': 0.040145985401459854, 'mean_word_length': 2.1059285091543156, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4486703', 'n_tokens_mistral': 2225, 'n_tokens_neox': 2027, 'n_words': 486}