<commit_before>require "test_utils"
require "logstash/filters/mutate"

describe LogStash::Filters::Mutate do
  extend LogStash::RSpec

  describe "basics" do
    # The logstash config goes here.
    # At this time, only filters are supported.
    config <<-CONFIG
      filter {
        mutate {
          lowercase => "lowerme"
          uppercase => "upperme"
          convert => [ "intme", "integer", "floatme", "float" ]
          rename => [ "rename1", "rename2" ]
          replace => [ "replaceme", "hello world" ]
          remove => [ "removeme" ] 
        }
      }
    CONFIG

    event = { "@fields" => { 
      "lowerme" => [ "ExAmPlE" ], "upperme" => [ "ExAmPlE" ],
      "intme" => [ "1234" ], "floatme" => [ "1234.455" ],
      "rename1" => [ "hello world" ],
      "replaceme" => [ "who cares" ],
      "removeme" => [ "something" ] 
      } 
    } 
    
    sample event do
      insist { subject["lowerme"] } == subject["lowerme"].collect(&:downcase)
      insist { subject["upperme"] } == subject["lowerme"].collect(&:upcase)
      insist { subject["intme"] } == subject["intme"].collect(&:to_i)
      insist { subject["floatme"] } == subject["floatme"].collect(&:to_f)
      reject { subject }.include?("rename1")
      insist { subject["rename2"] } == [ "hello world" ]
      reject { subject }.include?("removeme")
    end
  end

  describe "regression - check grok+mutate" do
    config <<-CONFIG
      filter {
        grok {
          pattern => "%{WORD:foo}"
        }
        mutate {
          lowercase => "foo"
        }
      }
    CONFIG

    sample "HELLO WORLD" do
      insist { subject["foo"] } == subject["foo"].collect(&:downcase)
    end
  end
end<commit_msg>Spec mutate filter: using explicit result expectations<commit_after>require "test_utils"
require "logstash/filters/mutate"

describe LogStash::Filters::Mutate do
  extend LogStash::RSpec

  describe "basics" do
    config <<-CONFIG
      filter {
        mutate {
          lowercase => "lowerme"
          uppercase => "upperme"
          convert => [ "intme", "integer", "floatme", "float" ]
          rename => [ "rename1", "rename2" ]
          replace => [ "replaceme", "hello world" ]
          remove => [ "removeme" ]
        }
      }
    CONFIG

    event = { "@fields" => {
      "lowerme" => [ "ExAmPlE" ],
      "upperme" => [ "ExAmPlE" ],
      "intme" => [ "1234", "7890.4", "7.9" ],
      "floatme" => [ "1234.455" ],
      "rename1" => [ "hello world" ],
      "replaceme" => [ "who cares" ],
      "removeme" => [ "something" ]
      }
    }

    sample event do
      insist { subject["lowerme"] } == ['example']
      insist { subject["upperme"] } == ['EXAMPLE']
      insist { subject["intme"] }   == [1234, 7890, 7]
      insist { subject["floatme"] } == [1234.455]
      reject { subject }.include?("rename1")
      insist { subject["rename2"] } == [ "hello world" ]
      reject { subject }.include?("removeme")
    end
  end

  describe "regression - check grok+mutate" do
    config <<-CONFIG
      filter {
        grok {
          pattern => "%{WORD:foo}"
        }
        mutate {
          lowercase => "foo"
        }
      }
    CONFIG

    sample "HELLO WORLD" do
      insist { subject["foo"] } == ['hello']
    end
  end
end
{'fraction_non_alphanumeric': 0.1705521472392638, 'fraction_numerical': 0.01687116564417178, 'mean_word_length': 2.1783625730994154, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 35, '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': '5794675', 'n_tokens_mistral': 1090, 'n_tokens_neox': 1078, 'n_words': 231}