<commit_before>require "spec_helper"

describe MiqExpression do
  let(:exp) { {"=" => {"field" => "Vm-name", "value" => "test"}} }
  let(:qs_exp) { {"=" => {"field" => "Vm-name", "value" => :user_input}} }
  let(:complex_qs_exp) do
    {"AND" => [
      {"=" => {"field" => "Vm-name", "value" => "test"}}, {"=" => {"field" => "Vm-name", "value" => :user_input}}
    ]}
  end

  describe ".to_ruby" do
    # Based on FogBugz 6181: something INCLUDES []
    it "detects value empty array" do
      exp = MiqExpression.new("INCLUDES" => {"field" => "Vm-name", "value" => "/[]/"})
      expect(exp.to_ruby).to eq("<value ref=vm, type=string>/virtual/name</value> =~ /\\/\\[\\]\\//")
    end

    it "raises error if expression contains ruby script" do
      exp = MiqExpression.new("RUBY" => {"field" => "Host-name", "value" => "puts 'Hello world!'"})
      expect { exp.to_ruby }.to raise_error(RuntimeError, "Ruby scripts in expressions are no longer supported. Please use the regular expression feature of conditions instead.")
    end
  end

  it "supports yaml" do
    exp = YAML.load '--- !ruby/object:MiqExpression
    exp:
      "=":
        field: Host-enabled_inbound_ports
        value: "22,427,5988,5989"
    '
    expect(exp.to_ruby).to eq('<value ref=host, type=numeric_set>/virtual/enabled_inbound_ports</value> == [22,427,5988,5989]')
  end

  it "tests numeric set expressions" do
    exp = MiqExpression.new("=" => {"field" => "Host-enabled_inbound_ports", "value" => "22,427,5988,5989"})
    expect(exp.to_ruby).to eq('<value ref=host, type=numeric_set>/virtual/enabled_inbound_ports</value> == [22,427,5988,5989]')
  end

  it "expands ranges" do
    filter = YAML.load '--- !ruby/object:MiqExpression
    exp:
      INCLUDES ALL:
        field: Host-enabled_inbound_ports
        value: 22, 427, 5988, 5989, 1..4
    '
    expect(filter.to_ruby).to eq('(<value ref=host, type=numeric_set>/virtual/enabled_inbound_ports</value> & [1,2,3,4,22,427,5988,5989]) == [1,2,3,4,22,427,5988,5989]')

    filter = YAML.load '--- !ruby/object:MiqExpression
    exp:
      INCLUDES ANY:
        field: Host-enabled_inbound_ports<commit_msg>Add test cases for value2tag<commit_after>require "spec_helper"

describe MiqExpression do
  let(:exp) { {"=" => {"field" => "Vm-name", "value" => "test"}} }
  let(:qs_exp) { {"=" => {"field" => "Vm-name", "value" => :user_input}} }
  let(:complex_qs_exp) do
    {"AND" => [
      {"=" => {"field" => "Vm-name", "value" => "test"}}, {"=" => {"field" => "Vm-name", "value" => :user_input}}
    ]}
  end

  describe ".to_ruby" do
    # Based on FogBugz 6181: something INCLUDES []
    it "detects value empty array" do
      exp = MiqExpression.new("INCLUDES" => {"field" => "Vm-name", "value" => "/[]/"})
      expect(exp.to_ruby).to eq("<value ref=vm, type=string>/virtual/name</value> =~ /\\/\\[\\]\\//")
    end

    it "raises error if expression contains ruby script" do
      exp = MiqExpression.new("RUBY" => {"field" => "Host-name", "value" => "puts 'Hello world!'"})
      expect { exp.to_ruby }.to raise_error(RuntimeError, "Ruby scripts in expressions are no longer supported. Please use the regular expression feature of conditions instead.")
    end
  end

  context "value2tag" do
    it "virtual default" do
      expect(described_class.value2tag("env")).to eq ["env", "/virtual"]
    end

    it "dotted notation" do
      expect(described_class.value2tag("env.prod.ny")).to eq ["env", "/virtual/prod/ny"]
    end

    it "with value" do
      expect(described_class.value2tag("env", "thing1")).to eq ["env", "/virtual/thing1"]
    end

    it "dotted notation with value" do
      expect(described_class.value2tag("env.prod.ny", "thing1")).to eq ["env", "/virtual/prod/ny/thing1"]
    end

    it "value with escaped slash" do
      expect(described_class.value2tag("env.prod.ny", "thing1/thing2")).to eq ["env", "/virtual/prod/ny/thing1%2fthing2"]
    end

    it "user_tag" do
      expect(described_class.value2tag("env.user_tag.ny", "thing1")).to eq ["env", "/user/ny/thing1"]
    end

    it "model and column" do
      expect(described_class.value2tag("env.vm-name", "thing1")).to eq ["env", "/virtual/vm/name/thing1"]
    end

    it "managed" do
      expect(described_class.value2tag("env.managed.host", "thing1")).to eq ["env", "/managed/host/thing1"]
    end

    it "managed" do
      expect(described_class.value2tag("env.managed.host")).to eq ["env", "/managed/host"]
    end

    it "false value" do
      expect(described_class.value2tag("MiqGroup.vms-disconnected", false)).to eq ["miqgroup", "/virtual/vms/disconnected/false"]
    end
  end

  it "supports yaml" do
    exp = YAML.load '--- !ruby/object:MiqExpression
    exp:
      "=":
        field: Host-enabled_inbound_ports
        value: "22,427,5988,5989"
    '
    expect(exp.to_ruby).to eq('<value ref=host, type=numeric_set>/virtual/enabled_inbound_ports</value> == [22,427,5988,5989]')
  end

  it "tests numeric set expressions" do
    exp = MiqExpression.new("=" => {"field" => "Host-enabled_inbound_ports", "value" => "22,427,5988,5989"})
    expect(exp.to_ruby).to eq('<value ref=host, type=numeric_set>/virtual/enabled_inbound_ports</value> == [22,427,5988,5989]')
  end

  it "expands ranges" do
    filter = YAML.load '--- !ruby/object:MiqExpression
    exp:
      INCLUDES ALL:
        field: Host-enabled_inbound_ports
        value: 22, 427, 5988, 5989, 1..4
    '
    expect(filter.to_ruby).to eq('(<value ref=host, type=numeric_set>/virtual/enabled_inbound_ports</value> & [1,2,3,4,22,427,5988,5989]) == [1,2,3,4,22,427,5988,5989]')

    filter = YAML.load '--- !ruby/object:MiqExpression
    exp:
      INCLUDES ANY:
        field: Host-enabled_inbound_ports
{'fraction_non_alphanumeric': 0.18539915966386555, 'fraction_numerical': 0.04149159663865546, 'mean_word_length': 3.4667709147771695, 'pattern_counts': {'":': 2, '<': 19, '<?xml version=': 0, '>': 63, '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': '4620369', 'n_tokens_mistral': 2219, 'n_tokens_neox': 2011, 'n_words': 426}