<commit_before>require 'spec_helper'

describe "post /compare.json" do

  def compare(obj)
    post(
      '/compare.json', Oj.dump(obj), { "CONTENT_TYPE" => "application/json" }
    )
  end

  before do
    @key      = "test"
    @response = {
      pass:         true,
      additions:    [],
      subtractions: [],
      difference:   0
    }
  end

  before { redis.del(@key) }

  describe "baseline not stored" do

    before do
      @value = 1
      @response.merge!(baseline: @value)
      compare(key: @key, value: @value)
    end

    describe "response" do
      subject { last_response }
      it      { should be_ok }

      describe "body" do
        subject { Oj.load(last_response.body) }
        it      { should eq(@response) }
      end
    end

    describe "redis" do

      subject { Oj.load(redis.get(@key)) }
      it      { should eq(@value) }
    end
  end

  describe "baseline stored" do
    describe "array value" do

      before do
        @value = [ 'a', 'b', 'c' ]
        @response.merge!(baseline: @value)
        redis.set(@key, Oj.dump(@value))
      end

      describe "item added to array value" do

        before do
          compare(
            key:   @key,
            tests: { additions: true },
            value: [ 'a', 'b', 'c', 'd' ]
          )
        end

        describe "response" do
          subject { last_response }
          it      { should be_ok }

          describe "body" do
            subject { Oj.load(last_response.body) }
            it do
              should eq(@response.merge(additions: [ "d" ], pass: false))
            end
          end
        end
      end

      describe "item removed from array value" do

        before do
          compare(
            key:   @key,
            tests: { subtractions: true },
            value: [ 'a', 'c' ]
          )
        end

        describe "response" do
          subject { last_response }
          it      { should be_ok }

          describe "body" do
            subject { Oj.load(last_response.body) }
            it do
              should eq(@response.merge(subtractions: [ "b" ], pass: false))
            end
          end
        end
      end
    end

    describe "number value" do
      describe "greater than multiplier" do

        before do
          @value = 1
          redis.set(@key, Oj.dump(@value))
        end

        describe "fail" do

          before do
            compare(
              key:   @key,
              tests: { greater_than: 2 },
              value: 2.1
            )
          end

          describe "response" do
            subject { last_response }
            it      { should be_ok }

            describe "body" do
              subject { Oj.load(last_response.body) }
              it do
                should eq(@response.merge(baseline: @value, difference: 1.1, pass: false))
              end
            end
          end
        end

        describe "pass" do
          before do
            compare(
              key:   @key,
              tests: { greater_than: 2 },
              value: 2
            )
          end

          describe "response" do
            subject { last_response }
            it      { should be_ok }

            describe "body" do
              subject { Oj.load(last_response.body) }
              it do
                should eq(@response.merge(baseline: @value, difference: 1))
              end
            end
          end
        end
      end

      describe "less than multiplier" do

        before do
          @value = 1
          redis.set(@key, Oj.dump(@value))
        end

        describe "fail" do

          before do
            compare(
              key:   @key,
              tests: { less_than: 0.9 },
              value: 0.8
            )
          end

          describe "response" do
            subject { last_response }
            it      { should be_ok }

            describe "body" do
              subject { Oj.load(last_response.body) }
              it do
                should eq(@response.merge(baseline: @value, difference: -0.2, pass: false))
              end
            end
          end
        end

        describe "pass" do
          before do
            compare(
              key:   @key,
              tests: { less_than: 0.9 },
              value: 0.9
            )
          end

          describe "response" do
            subject { last_response }
            it      { should be_ok }

            describe "body" do
              subject { Oj.load(last_response.body) }
              it do
                should eq(@response.merge(baseline: @value, difference: -0.1))
              end
            end
          end
        end
      end
    end
  end
end<commit_msg>Cleanup<commit_after>require 'spec_helper'

describe "post /compare.json" do

  def compare(obj)
    post(
      '/compare.json',
      Oj.dump(obj),
      "CONTENT_TYPE" => "application/json"
    )
  end

  before do
    @key      = "test"
    @response = {
      pass:         true,
      additions:    [],
      subtractions: [],
      difference:   0
    }
  end

  before { redis.del(@key) }

  describe "baseline not stored" do

    before do
      @value = 1
      @response.merge!(baseline: @value)
      compare(key: @key, value: @value)
    end

    describe "response" do
      subject { last_response }
      it      { should be_ok }

      describe "body" do
        subject { Oj.load(last_response.body) }
        it      { should eq(@response) }
      end
    end

    describe "redis" do

      subject { Oj.load(redis.get(@key)) }
      it      { should eq(@value) }
    end
  end

  describe "baseline stored" do
    describe "array value" do

      before do
        @value = [ 'a', 'b', 'c' ]
        @response.merge!(baseline: @value)
        redis.set(@key, Oj.dump(@value))
      end

      describe "item added to array value" do

        before do
          compare(
            key:   @key,
            tests: { additions: true },
            value: [ 'a', 'b', 'c', 'd' ]
          )
        end

        describe "response" do
          subject { last_response }
          it      { should be_ok }

          describe "body" do
            subject { Oj.load(last_response.body) }
            it do
              should eq(@response.merge(
                additions: [ "d" ], pass: false
              ))
            end
          end
        end
      end

      describe "item removed from array value" do

        before do
          compare(
            key:   @key,
            tests: { subtractions: true },
            value: [ 'a', 'c' ]
          )
        end

        describe "response" do
          subject { last_response }
          it      { should be_ok }

          describe "body" do
            subject { Oj.load(last_response.body) }
            it do
              should eq(@response.merge(
                subtractions: [ "b" ], pass: false
              ))
            end
          end
        end
      end
    end

    describe "number value" do
      describe "greater than multiplier" do

        before do
          @value = 1
          redis.set(@key, Oj.dump(@value))
        end

        describe "fail" do

          before do
            compare(
              key:   @key,
              tests: { greater_than: 2 },
              value: 2.1
            )
          end

          describe "response" do
            subject { last_response }
            it      { should be_ok }

            describe "body" do
              subject { Oj.load(last_response.body) }
              it do
                should eq(@response.merge(
                  baseline: @value, difference: 1.1, pass: false
                ))
              end
            end
          end
        end

        describe "pass" do
          before do
            compare(
              key:   @key,
              tests: { greater_than: 2 },
              value: 2
            )
          end

          describe "response" do
            subject { last_response }
            it      { should be_ok }

            describe "body" do
              subject { Oj.load(last_response.body) }
              it do
                should eq(@response.merge(
                  baseline: @value, difference: 1
                ))
              end
            end
          end
        end
      end

      describe "less than multiplier" do

        before do
          @value = 1
          redis.set(@key, Oj.dump(@value))
        end

        describe "fail" do

          before do
            compare(
              key:   @key,
              tests: { less_than: 0.9 },
              value: 0.8
            )
          end

          describe "response" do
            subject { last_response }
            it      { should be_ok }

            describe "body" do
              subject { Oj.load(last_response.body) }
              it do
                should eq(@response.merge(
                  baseline: @value, difference: -0.2, pass: false
                ))
              end
            end
          end
        end

        describe "pass" do
          before do
            compare(
              key:   @key,
              tests: { less_than: 0.9 },
              value: 0.9
            )
          end

          describe "response" do
            subject { last_response }
            it      { should be_ok }

            describe "body" do
              subject { Oj.load(last_response.body) }
              it do
                should eq(@response.merge(
                  baseline: @value, difference: -0.1
                ))
              end
            end
          end
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08860498660622296, 'fraction_numerical': 0.00494539460127756, 'mean_word_length': 1.0817070555436414, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6331192', 'n_tokens_mistral': 2724, 'n_tokens_neox': 2585, 'n_words': 743}