diff --git a/app/models/condition.rb b/app/models/condition.rb index 62cf448a65e..72753b3c03e 100644 --- a/app/models/condition.rb +++ b/app/models/condition.rb @@ -82,11 +82,11 @@ def self.evaluate(cond, rec, _inputs = {}, attr = :expression) # similar to MiqExpression#evaluate # @return [Boolean] true if the expression matches the record def self.subst_matches?(expr, rec) - do_eval(subst(expr, rec)) + do_eval(subst(expr, rec), rec) end - def self.do_eval(expr) - !!eval(expr) + def self.do_eval(expr, rec) + !!eval(expr, binding) end private_class_method :do_eval @@ -167,7 +167,7 @@ def self._subst_find(rec, expr) value = MiqExpression.quote(obj.send(attr), opts[:type]&.to_sym) value = value.gsub("\\", '\&\&') if value.kind_of?(String) e = search.gsub(/]*>.+<\/value>/im, value.to_s) - obj if do_eval(e) + obj if do_eval(e, obj) end.compact MiqPolicy.logger.debug("MIQ(condition-_subst_find): Search Expression returned: [#{list.length}] records") @@ -207,7 +207,7 @@ def self._subst_find(rec, expr) e = check.gsub(/]*>.+<\/value>/im, value.to_s) MiqPolicy.logger.debug("MIQ(condition-_subst_find): Check Expression after substitution: [#{e}]") - result = do_eval(e) + result = do_eval(e, obj) return true if result && checkmode == "any" return false if !result && checkmode == "all" diff --git a/lib/miq_expression.rb b/lib/miq_expression.rb index 05440ec3ca8..b420801e73f 100644 --- a/lib/miq_expression.rb +++ b/lib/miq_expression.rb @@ -190,7 +190,7 @@ def to_ruby(timezone = nil, prune_sql: false) end end - def self._to_ruby(exp, context_type, tz) + def self._to_ruby(exp, context_type, tz, obj_name: :rec) return exp unless exp.kind_of?(Hash) operator = exp.keys.first @@ -200,29 +200,29 @@ def self._to_ruby(exp, context_type, tz) case operator when "equal", "=", "<", ">", ">=", "<=", "!=" - operands = operands2rubyvalue(operator, op_args, context_type) + operands = operands2rubyvalue(operator, op_args, context_type, :obj_name => obj_name) clause = operands.join(" #{normalize_ruby_operator(operator)} ") when "before" col_type = Target.parse(col_name).column_type if col_name - col_ruby, _value = operands2rubyvalue(operator, {"field" => col_name}, context_type) + col_ruby, _value = operands2rubyvalue(operator, {"field" => col_name}, context_type, :obj_name => obj_name) val = op_args["value"] clause = ruby_for_date_compare(col_ruby, col_type, tz, "<", val) when "after" col_type = Target.parse(col_name).column_type if col_name - col_ruby, _value = operands2rubyvalue(operator, {"field" => col_name}, context_type) + col_ruby, _value = operands2rubyvalue(operator, {"field" => col_name}, context_type, :obj_name => obj_name) val = op_args["value"] clause = ruby_for_date_compare(col_ruby, col_type, tz, nil, nil, ">", val) when "includes all" - operands = operands2rubyvalue(operator, op_args, context_type) + operands = operands2rubyvalue(operator, op_args, context_type, :obj_name => obj_name) clause = "(#{operands[0]} & #{operands[1]}) == #{operands[1]}" when "includes any" - operands = operands2rubyvalue(operator, op_args, context_type) + operands = operands2rubyvalue(operator, op_args, context_type, :obj_name => obj_name) clause = "(#{operands[1]} - #{operands[0]}) != #{operands[1]}" when "includes only", "limited to" - operands = operands2rubyvalue(operator, op_args, context_type) + operands = operands2rubyvalue(operator, op_args, context_type, :obj_name => obj_name) clause = "(#{operands[0]} - #{operands[1]}) == []" when "like", "not like", "starts with", "ends with", "includes" - operands = operands2rubyvalue(operator, op_args, context_type) + operands = operands2rubyvalue(operator, op_args, context_type, :obj_name => obj_name) operands[1] = case operator when "starts with" @@ -235,7 +235,7 @@ def self._to_ruby(exp, context_type, tz) clause = operands.join(" #{normalize_ruby_operator(operator)} ") clause = "!(" + clause + ")" if operator == "not like" when "regular expression matches", "regular expression does not match" - operands = operands2rubyvalue(operator, op_args, context_type) + operands = operands2rubyvalue(operator, op_args, context_type, :obj_name => obj_name) # If it looks like a regular expression, sanitize from forward # slashes and interpolation @@ -255,11 +255,11 @@ def self._to_ruby(exp, context_type, tz) end clause = operands.join(" #{normalize_ruby_operator(operator)} ") when "and", "or" - clause = "(" + op_args.collect { |operand| _to_ruby(operand, context_type, tz) }.join(" #{normalize_ruby_operator(operator)} ") + ")" + clause = "(" + op_args.collect { |operand| _to_ruby(operand, context_type, tz, :obj_name => obj_name) }.join(" #{normalize_ruby_operator(operator)} ") + ")" when "not", "!" - clause = normalize_ruby_operator(operator) + "(" + _to_ruby(op_args, context_type, tz) + ")" + clause = normalize_ruby_operator(operator) + "(" + _to_ruby(op_args, context_type, tz, :obj_name => obj_name) + ")" when "is null", "is not null", "is empty", "is not empty" - operands = operands2rubyvalue(operator, op_args, context_type) + operands = operands2rubyvalue(operator, op_args, context_type, :obj_name => obj_name) clause = operands.join(" #{normalize_ruby_operator(operator)} ") when "contains" op_args["tag"] ||= col_name @@ -294,14 +294,14 @@ def self._to_ruby(exp, context_type, tz) check =~ /^check(.*)$/ mode = $1.downcase - clause = "" + _to_ruby(op_args["search"], context_type, tz) + "" \ - "" + _to_ruby(op_args[check], context_type, tz) + "" + clause = "" + _to_ruby(op_args["search"], context_type, tz, :obj_name => nil) + "" \ + "" + _to_ruby(op_args[check], context_type, tz, :obj_name => nil) + "" when "key exists" - clause, = operands2rubyvalue(operator, op_args, context_type) + clause, = operands2rubyvalue(operator, op_args, context_type, :obj_name => obj_name) when "value exists" - clause, = operands2rubyvalue(operator, op_args, context_type) + clause, = operands2rubyvalue(operator, op_args, context_type, :obj_name => obj_name) when "is" - col_ruby, _value = operands2rubyvalue(operator, {"field" => col_name}, context_type) + col_ruby, _value = operands2rubyvalue(operator, {"field" => col_name}, context_type, :obj_name => obj_name) col_type = Target.parse(col_name).column_type value = op_args["value"] clause = if col_type == :date && !RelativeDatetime.relative?(value) @@ -310,7 +310,7 @@ def self._to_ruby(exp, context_type, tz) ruby_for_date_compare(col_ruby, col_type, tz, ">=", value, "<=", value) end when "from" - col_ruby, _value = operands2rubyvalue(operator, {"field" => col_name}, context_type) + col_ruby, _value = operands2rubyvalue(operator, {"field" => col_name}, context_type, :obj_name => obj_name) col_type = Target.parse(col_name).column_type start_val, end_val = op_args["value"] @@ -703,7 +703,7 @@ def self.quote_by(operator, value, column_type = nil) end end - def self.operands2rubyvalue(operator, ops, context_type) + def self.operands2rubyvalue(operator, ops, context_type, obj_name: nil) if ops["field"] if ops["field"] == "" ["", quote(ops["value"], :integer)] @@ -713,6 +713,9 @@ def self.operands2rubyvalue(operator, ops, context_type) [if context_type == "hash" "#{ops["field"].split(".").last.split("-").join(".")}" + elsif obj_name && !virtual_custom_attribute?(target.column) && target.associations.empty? + # TODO: handle value for fields with associations (they could be habtm, has_one, has_many, belongs_to) + "#{obj_name}.#{target.column}" else "#{target.tag_path_with}" end, quote_by(operator, ops["value"], col_type)] @@ -838,6 +841,10 @@ def self.sanitize_regular_expression(string) string.gsub(%r{\\*/}, "\\/").gsub(/\\*#/, "\\#") end + def self.virtual_custom_attribute?(attribute) + attribute.include?(CustomAttributeMixin::CUSTOM_ATTRIBUTES_PREFIX) + end + def self.escape_virtual_custom_attribute(attribute) if attribute.include?(CustomAttributeMixin::CUSTOM_ATTRIBUTES_PREFIX) uri_parser = URI::RFC2396_Parser.new diff --git a/spec/lib/miq_expression_spec.rb b/spec/lib/miq_expression_spec.rb index 94456447b70..74425f6361e 100644 --- a/spec/lib/miq_expression_spec.rb +++ b/spec/lib/miq_expression_spec.rb @@ -325,7 +325,7 @@ it "!(ruby) => keep all expressions" do exp1 = {"=" => {"field" => "Vm-platform", "value" => "foo"}} ruby = MiqExpression.new("NOT" => exp1).to_ruby(:prune_sql => true) - expect(ruby).to eq("!(/virtual/platform == \"foo\")") + expect(ruby).to eq("!(rec.platform == \"foo\")") end it "!(sql OR ruby) => (!(sql) AND !(ruby)) => !(ruby)" do @@ -1285,7 +1285,7 @@ it "generates the ruby for a LIKE expression with field" do actual = described_class.new("LIKE" => {"field" => "Vm-name", "value" => "foo"}).to_ruby - expected = "/virtual/name =~ /foo/" + expected = "rec.name =~ /foo/" expect(actual).to eq(expected) end @@ -1303,7 +1303,7 @@ it "generates the ruby for a NOT LIKE expression with field" do actual = described_class.new("NOT LIKE" => {"field" => "Vm-name", "value" => "foo"}).to_ruby - expected = "!(/virtual/name =~ /foo/)" + expected = "!(rec.name =~ /foo/)" expect(actual).to eq(expected) end @@ -1463,7 +1463,7 @@ it "generates the SQL for a != expression" do actual = described_class.new("!=" => {"field" => "Vm-name", "value" => "foo"}).to_ruby - expected = "/virtual/name != \"foo\"" + expected = "rec.name != \"foo\"" expect(actual).to eq(expected) end @@ -1475,7 +1475,7 @@ it "detects value empty array" do exp = MiqExpression.new("INCLUDES" => {"field" => "Vm-name", "value" => "[]"}) - expect(exp.to_ruby).to eq("/virtual/name =~ /\\[\\]/") + expect(exp.to_ruby).to eq("rec.name =~ /\\[\\]/") end it "raises error if expression contains ruby script" do @@ -1485,115 +1485,115 @@ it "ignores invalid values for a numeric_set in an = expression" do actual = described_class.new("=" => {"field" => "Host-enabled_inbound_ports", "value" => "22, 427, 5988, 5989, foo, `echo 1000`.to_i, abc..123, 1..4"}).to_ruby - expected = "/virtual/enabled_inbound_ports == [1,2,3,4,22,427,5988,5989]" + expected = "rec.enabled_inbound_ports == [1,2,3,4,22,427,5988,5989]" expect(actual).to eq(expected) end it "ignores invalid values for a numeric_set in an INCLUDES ALL expression" do actual = described_class.new("INCLUDES ALL" => {"field" => "Host-enabled_inbound_ports", "value" => "22, 427, 5988, 5989, foo, `echo 1000`.to_i, abc..123, 1..4"}).to_ruby - expected = "(/virtual/enabled_inbound_ports & [1,2,3,4,22,427,5988,5989]) == [1,2,3,4,22,427,5988,5989]" + expected = "(rec.enabled_inbound_ports & [1,2,3,4,22,427,5988,5989]) == [1,2,3,4,22,427,5988,5989]" expect(actual).to eq(expected) end it "ignores invalid values for a numeric_set in an INCLUDES ANY expression" do actual = described_class.new("INCLUDES ANY" => {"field" => "Host-enabled_inbound_ports", "value" => "22, 427, 5988, 5989, foo, `echo 1000`.to_i, abc..123, 1..4"}).to_ruby - expected = "([1,2,3,4,22,427,5988,5989] - /virtual/enabled_inbound_ports) != [1,2,3,4,22,427,5988,5989]" + expected = "([1,2,3,4,22,427,5988,5989] - rec.enabled_inbound_ports) != [1,2,3,4,22,427,5988,5989]" expect(actual).to eq(expected) end it "ignores invalid values for a numeric_set in an INCLUDES ONLY expression" do actual = described_class.new("INCLUDES ONLY" => {"field" => "Host-enabled_inbound_ports", "value" => "22, 427, 5988, 5989, foo, `echo 1000`.to_i, abc..123, 1..4"}).to_ruby - expected = "(/virtual/enabled_inbound_ports - [1,2,3,4,22,427,5988,5989]) == []" + expected = "(rec.enabled_inbound_ports - [1,2,3,4,22,427,5988,5989]) == []" expect(actual).to eq(expected) end it "ignores invalid values for a numeric_set in an LIMITED TO expression" do actual = described_class.new("LIMITED TO" => {"field" => "Host-enabled_inbound_ports", "value" => "22, 427, 5988, 5989, foo, `echo 1000`.to_i, abc..123, 1..4"}).to_ruby - expected = "(/virtual/enabled_inbound_ports - [1,2,3,4,22,427,5988,5989]) == []" + expected = "(rec.enabled_inbound_ports - [1,2,3,4,22,427,5988,5989]) == []" expect(actual).to eq(expected) end it "escapes forward slashes for values in REGULAR EXPRESSION MATCHES expressions" do value = "//; puts 'Hi, mom!';//" actual = described_class.new("REGULAR EXPRESSION MATCHES" => {"field" => "Vm-name", "value" => value}).to_ruby - expected = "/virtual/name =~ /\\/; puts 'Hi, mom!';\\//" + expected = "rec.name =~ /\\/; puts 'Hi, mom!';\\//" expect(actual).to eq(expected) end it "preserves the delimiters when escaping forward slashes in case-insensitive REGULAR EXPRESSION MATCHES expressions" do value = "//; puts 'Hi, mom!';//i" actual = described_class.new("REGULAR EXPRESSION MATCHES" => {"field" => "Vm-name", "value" => value}).to_ruby - expected = "/virtual/name =~ /\\/; puts 'Hi, mom!';\\//i" + expected = "rec.name =~ /\\/; puts 'Hi, mom!';\\//i" expect(actual).to eq(expected) end it "escapes forward slashes for non-Regexp literal values in REGULAR EXPRESSION MATCHES expressions" do value = ".*/; puts 'Hi, mom!';/.*" actual = described_class.new("REGULAR EXPRESSION MATCHES" => {"field" => "Vm-name", "value" => value}).to_ruby - expected = "/virtual/name =~ /.*\\/; puts 'Hi, mom!';\\/.*/" + expected = "rec.name =~ /.*\\/; puts 'Hi, mom!';\\/.*/" expect(actual).to eq(expected) end it "does not escape escaped forward slashes for values in REGULAR EXPRESSION MATCHES expressions" do value = "/foo/bar" actual = described_class.new("REGULAR EXPRESSION MATCHES" => {"field" => "Vm-name", "value" => value}).to_ruby - expected = "/virtual/name =~ /\\/foo\\/bar/" + expected = "rec.name =~ /\\/foo\\/bar/" expect(actual).to eq(expected) end it "handles arbitarily long escaping of forward " do value = "\\\\\\/foo\\\\\\/bar" actual = described_class.new("REGULAR EXPRESSION MATCHES" => {"field" => "Vm-name", "value" => value}).to_ruby - expected = "/virtual/name =~ /\\/foo\\/bar/" + expected = "rec.name =~ /\\/foo\\/bar/" expect(actual).to eq(expected) end it "escapes interpolation in REGULAR EXPRESSION MATCHES expressions" do value = "/\#{puts 'Hi, mom!'}/" actual = described_class.new("REGULAR EXPRESSION MATCHES" => {"field" => "Vm-name", "value" => value}).to_ruby - expected = "/virtual/name =~ /\\\#{puts 'Hi, mom!'}/" + expected = "rec.name =~ /\\\#{puts 'Hi, mom!'}/" expect(actual).to eq(expected) end it "handles arbitrarily long escaping of interpolation in REGULAR EXPRESSION MATCHES expressions" do value = "/\\\\\#{puts 'Hi, mom!'}/" actual = described_class.new("REGULAR EXPRESSION MATCHES" => {"field" => "Vm-name", "value" => value}).to_ruby - expected = "/virtual/name =~ /\\\#{puts 'Hi, mom!'}/" + expected = "rec.name =~ /\\\#{puts 'Hi, mom!'}/" expect(actual).to eq(expected) end it "escapes interpolation in non-Regexp literal values in REGULAR EXPRESSION MATCHES expressions" do value = "\#{puts 'Hi, mom!'}" actual = described_class.new("REGULAR EXPRESSION MATCHES" => {"field" => "Vm-name", "value" => value}).to_ruby - expected = "/virtual/name =~ /\\\#{puts 'Hi, mom!'}/" + expected = "rec.name =~ /\\\#{puts 'Hi, mom!'}/" expect(actual).to eq(expected) end it "escapes forward slashes for values in REGULAR EXPRESSION DOES NOT MATCH expressions" do value = "//; puts 'Hi, mom!';//" actual = described_class.new("REGULAR EXPRESSION DOES NOT MATCH" => {"field" => "Vm-name", "value" => value}).to_ruby - expected = "/virtual/name !~ /\\/; puts 'Hi, mom!';\\//" + expected = "rec.name !~ /\\/; puts 'Hi, mom!';\\//" expect(actual).to eq(expected) end it "preserves the delimiters when escaping forward slashes in case-insensitive REGULAR EXPRESSION DOES NOT MATCH expressions" do value = "//; puts 'Hi, mom!';//i" actual = described_class.new("REGULAR EXPRESSION DOES NOT MATCH" => {"field" => "Vm-name", "value" => value}).to_ruby - expected = "/virtual/name !~ /\\/; puts 'Hi, mom!';\\//i" + expected = "rec.name !~ /\\/; puts 'Hi, mom!';\\//i" expect(actual).to eq(expected) end it "escapes forward slashes for non-Regexp literal values in REGULAR EXPRESSION DOES NOT MATCH expressions" do value = ".*/; puts 'Hi, mom!';/.*" actual = described_class.new("REGULAR EXPRESSION DOES NOT MATCH" => {"field" => "Vm-name", "value" => value}).to_ruby - expected = "/virtual/name !~ /.*\\/; puts 'Hi, mom!';\\/.*/" + expected = "rec.name !~ /.*\\/; puts 'Hi, mom!';\\/.*/" expect(actual).to eq(expected) end it "does not escape escaped forward slashes for values in REGULAR EXPRESSION DOES NOT MATCH expressions" do value = "/foo/bar" actual = described_class.new("REGULAR EXPRESSION DOES NOT MATCH" => {"field" => "Vm-name", "value" => value}).to_ruby - expected = "/virtual/name !~ /\\/foo\\/bar/" + expected = "rec.name !~ /\\/foo\\/bar/" expect(actual).to eq(expected) end @@ -1609,7 +1609,7 @@ field: Host-enabled_inbound_ports value: 22, 427, 5988, 5989, 1..4 ' - expected = "(/virtual/enabled_inbound_ports & [1,2,3,4,22,427,5988,5989]) == [1,2,3,4,22,427,5988,5989]" + expected = "(rec.enabled_inbound_ports & [1,2,3,4,22,427,5988,5989]) == [1,2,3,4,22,427,5988,5989]" expect(filter.to_ruby).to eq(expected) end @@ -1621,7 +1621,7 @@ value: 22, 427, 5988, 5989, 1..4 ' - expected = "([1,2,3,4,22,427,5988,5989] - /virtual/enabled_inbound_ports) != [1,2,3,4,22,427,5988,5989]" + expected = "([1,2,3,4,22,427,5988,5989] - rec.enabled_inbound_ports) != [1,2,3,4,22,427,5988,5989]" expect(filter.to_ruby).to eq(expected) end @@ -1633,7 +1633,7 @@ value: 22, 427, 5988, 5989, 1..4 ' - expected = "(/virtual/enabled_inbound_ports - [1,2,3,4,22,427,5988,5989]) == []" + expected = "(rec.enabled_inbound_ports - [1,2,3,4,22,427,5988,5989]) == []" expect(filter.to_ruby).to eq(expected) end @@ -1645,7 +1645,7 @@ value: 22, 427, 5988, 5989, 1..4 ' - expected = "(/virtual/enabled_inbound_ports - [1,2,3,4,22,427,5988,5989]) == []" + expected = "(rec.enabled_inbound_ports - [1,2,3,4,22,427,5988,5989]) == []" expect(filter.to_ruby).to eq(expected) end @@ -1657,7 +1657,7 @@ value: "ntpd, sshd, vmware-vpxa, vmware-webAccess" ' - expected = "/virtual/service_names == ['ntpd','sshd','vmware-vpxa','vmware-webAccess']" + expected = "rec.service_names == ['ntpd','sshd','vmware-vpxa','vmware-webAccess']" expect(filter.to_ruby).to eq(expected) end @@ -1669,7 +1669,7 @@ value: "ntpd, sshd, vmware-vpxa, vmware-webAccess" ' - expected = "(/virtual/service_names & ['ntpd','sshd','vmware-vpxa','vmware-webAccess']) == ['ntpd','sshd','vmware-vpxa','vmware-webAccess']" + expected = "(rec.service_names & ['ntpd','sshd','vmware-vpxa','vmware-webAccess']) == ['ntpd','sshd','vmware-vpxa','vmware-webAccess']" expect(filter.to_ruby).to eq(expected) end @@ -1681,7 +1681,7 @@ value: "ntpd, sshd, vmware-vpxa, vmware-webAccess" ' - expected = "(['ntpd','sshd','vmware-vpxa','vmware-webAccess'] - /virtual/service_names) != ['ntpd','sshd','vmware-vpxa','vmware-webAccess']" + expected = "(['ntpd','sshd','vmware-vpxa','vmware-webAccess'] - rec.service_names) != ['ntpd','sshd','vmware-vpxa','vmware-webAccess']" expect(filter.to_ruby).to eq(expected) end @@ -1693,7 +1693,7 @@ value: "ntpd, sshd, vmware-vpxa" ' - expected = "(/virtual/service_names - ['ntpd','sshd','vmware-vpxa']) == []" + expected = "(rec.service_names - ['ntpd','sshd','vmware-vpxa']) == []" expect(filter.to_ruby).to eq(expected) end @@ -1705,7 +1705,7 @@ value: "ntpd, sshd, vmware-vpxa" ' - expected = "(/virtual/service_names - ['ntpd','sshd','vmware-vpxa']) == []" + expected = "(rec.service_names - ['ntpd','sshd','vmware-vpxa']) == []" expect(filter.to_ruby).to eq(expected) end @@ -1734,7 +1734,7 @@ field: Host-name value: /^[^.]*\.galaxy\..*$/ ' - expect(filter.to_ruby).to eq('/virtual/name =~ /^[^.]*\.galaxy\..*$/') + expect(filter.to_ruby).to eq('rec.name =~ /^[^.]*\.galaxy\..*$/') end it "should test regexp with string literal" do @@ -1744,7 +1744,7 @@ field: Host-name value: ^[^.]*\.galaxy\..*$ ' - expect(filter.to_ruby).to eq('/virtual/name =~ /^[^.]*\.galaxy\..*$/') + expect(filter.to_ruby).to eq('rec.name =~ /^[^.]*\.galaxy\..*$/') end it "should test regexp as part of a FIND/checkany expression" do @@ -1799,7 +1799,7 @@ field: Vm-memory_shares value: 25.kilobytes ' - expect(filter.to_ruby).to eq('/virtual/memory_shares >= 25600') + expect(filter.to_ruby).to eq('rec.memory_shares >= 25600') end it "should test numbers with commas with methods" do @@ -1810,74 +1810,74 @@ field: Vm-used_disk_storage value: 1,000.megabytes ' - expect(filter.to_ruby).to eq('/virtual/used_disk_storage >= 1048576000') + expect(filter.to_ruby).to eq('rec.used_disk_storage >= 1048576000') end it "generates the ruby for a STARTS WITH expression" do actual = described_class.new("STARTS WITH" => {"field" => "Vm-name", "value" => "foo"}).to_ruby - expected = "/virtual/name =~ /^foo/" + expected = "rec.name =~ /^foo/" expect(actual).to eq(expected) end it "generates the ruby for an ENDS WITH expression" do actual = described_class.new("ENDS WITH" => {"field" => "Vm-name", "value" => "foo"}).to_ruby - expected = "/virtual/name =~ /foo$/" + expected = "rec.name =~ /foo$/" expect(actual).to eq(expected) end it "generates the ruby for an AND expression" do actual = described_class.new("AND" => [{"=" => {"field" => "Vm-name", "value" => "foo"}}, {"=" => {"field" => "Vm-vendor", "value" => "bar"}}]).to_ruby - expected = "(/virtual/name == \"foo\" and /virtual/vendor == \"bar\")" + expected = "(rec.name == \"foo\" and rec.vendor == \"bar\")" expect(actual).to eq(expected) end it "generates the ruby for an OR with a count" do actual = described_class.new("OR" => [{"=" => {"field" => "Vm-name", "value" => "foo"}}, {"=" => {"count" => "Vm.snapshots", "value" => "1"}}]).to_ruby - expected = "(/virtual/name == \"foo\" or /virtual/snapshots == 1)" + expected = "(rec.name == \"foo\" or /virtual/snapshots == 1)" expect(actual).to eq(expected) end it "generates the ruby for an OR expression" do actual = described_class.new("OR" => [{"=" => {"field" => "Vm-name", "value" => "foo"}}, {"=" => {"field" => "Vm-vendor", "value" => "bar"}}]).to_ruby - expected = "(/virtual/name == \"foo\" or /virtual/vendor == \"bar\")" + expected = "(rec.name == \"foo\" or rec.vendor == \"bar\")" expect(actual).to eq(expected) end it "generates the ruby for a NOT expression" do actual = described_class.new("NOT" => {"=" => {"field" => "Vm-name", "value" => "foo"}}).to_ruby - expected = "!(/virtual/name == \"foo\")" + expected = "!(rec.name == \"foo\")" expect(actual).to eq(expected) end it "generates the ruby for a ! expression" do actual = described_class.new("!" => {"=" => {"field" => "Vm-name", "value" => "foo"}}).to_ruby - expected = "!(/virtual/name == \"foo\")" + expected = "!(rec.name == \"foo\")" expect(actual).to eq(expected) end it "generates the ruby for an IS NULL expression" do actual = described_class.new("IS NULL" => {"field" => "Vm-name"}).to_ruby - expected = "/virtual/name == \"\"" + expected = "rec.name == \"\"" expect(actual).to eq(expected) end it "generates the ruby for an IS NOT NULL expression" do actual = described_class.new("IS NOT NULL" => {"field" => "Vm-name"}).to_ruby - expected = "/virtual/name != \"\"" + expected = "rec.name != \"\"" expect(actual).to eq(expected) end it "generates the ruby for an IS EMPTY expression" do actual = described_class.new("IS EMPTY" => {"field" => "Vm-name"}).to_ruby - expected = "/virtual/name == \"\"" + expected = "rec.name == \"\"" expect(actual).to eq(expected) end it "generates the ruby for an IS NOT EMPTY expression" do actual = described_class.new("IS NOT EMPTY" => {"field" => "Vm-name"}).to_ruby - expected = "/virtual/name != \"\"" + expected = "rec.name != \"\"" expect(actual).to eq(expected) end @@ -1974,27 +1974,27 @@ context "static dates and times with no timezone" do it "generates the ruby for an AFTER expression with date value" do exp = MiqExpression.new("AFTER" => {"field" => "Vm-retires_on", "value" => "2011-01-10"}) - expect(exp.to_ruby).to eq("!(val=/virtual/retires_on&.to_time).nil? and val > Time.utc(2011,1,10,23,59,59)") + expect(exp.to_ruby).to eq("!(val=rec.retires_on&.to_time).nil? and val > Time.utc(2011,1,10,23,59,59)") end it "generates the ruby for a BEFORE expression with date value" do exp = MiqExpression.new("BEFORE" => {"field" => "Vm-retires_on", "value" => "2011-01-10"}) - expect(exp.to_ruby).to eq("!(val=/virtual/retires_on&.to_time).nil? and val < Time.utc(2011,1,10,0,0,0)") + expect(exp.to_ruby).to eq("!(val=rec.retires_on&.to_time).nil? and val < Time.utc(2011,1,10,0,0,0)") end it "generates the ruby for a AFTER expression with datetime value" do exp = MiqExpression.new("AFTER" => {"field" => "Vm-last_scan_on", "value" => "2011-01-10 9:00"}) - expect(exp.to_ruby).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val > Time.utc(2011,1,10,9,0,0)") + expect(exp.to_ruby).to eq("!(val=rec.last_scan_on&.to_time).nil? and val > Time.utc(2011,1,10,9,0,0)") end it "generates the ruby for a IS expression with date value" do exp = MiqExpression.new("IS" => {"field" => "Vm-retires_on", "value" => "2011-01-10"}) - expect(exp.to_ruby).to eq("!(val=/virtual/retires_on&.to_time).nil? and val >= Time.utc(2011,1,10,0,0,0) and val <= Time.utc(2011,1,10,23,59,59)") + expect(exp.to_ruby).to eq("!(val=rec.retires_on&.to_time).nil? and val >= Time.utc(2011,1,10,0,0,0) and val <= Time.utc(2011,1,10,23,59,59)") end it "generates the ruby for a IS expression with datetime value" do exp = MiqExpression.new("IS" => {"field" => "Vm-last_scan_on", "value" => "2011-01-10"}) - expect(exp.to_ruby).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,10,0,0,0) and val <= Time.utc(2011,1,10,23,59,59)") + expect(exp.to_ruby).to eq("!(val=rec.last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,10,0,0,0) and val <= Time.utc(2011,1,10,23,59,59)") end it "generates the ruby for a IS expression with hash context" do @@ -2005,22 +2005,22 @@ it "generates the ruby for a FROM expression with date values" do exp = MiqExpression.new("FROM" => {"field" => "Vm-retires_on", "value" => ["2011-01-09", "2011-01-10"]}) - expect(exp.to_ruby).to eq("!(val=/virtual/retires_on&.to_time).nil? and val >= Time.utc(2011,1,9,0,0,0) and val <= Time.utc(2011,1,10,23,59,59)") + expect(exp.to_ruby).to eq("!(val=rec.retires_on&.to_time).nil? and val >= Time.utc(2011,1,9,0,0,0) and val <= Time.utc(2011,1,10,23,59,59)") end it "generates the ruby for a FROM expression with date values" do exp = MiqExpression.new("FROM" => {"field" => "Vm-retires_on", "value" => ["01/09/2011", "01/10/2011"]}) - expect(exp.to_ruby).to eq("!(val=/virtual/retires_on&.to_time).nil? and val >= Time.utc(2011,1,9,0,0,0) and val <= Time.utc(2011,1,10,23,59,59)") + expect(exp.to_ruby).to eq("!(val=rec.retires_on&.to_time).nil? and val >= Time.utc(2011,1,9,0,0,0) and val <= Time.utc(2011,1,10,23,59,59)") end it "generates the ruby for a FROM expression with datetime values" do exp = MiqExpression.new("FROM" => {"field" => "Vm-last_scan_on", "value" => ["2011-01-10 8:00", "2011-01-10 17:00"]}) - expect(exp.to_ruby).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,10,8,0,0) and val <= Time.utc(2011,1,10,17,0,0)") + expect(exp.to_ruby).to eq("!(val=rec.last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,10,8,0,0) and val <= Time.utc(2011,1,10,17,0,0)") end it "generates the ruby for a FROM expression with identical datetime values" do exp = MiqExpression.new("FROM" => {"field" => "Vm-last_scan_on", "value" => ["2011-01-10 00:00", "2011-01-10 00:00"]}) - expect(exp.to_ruby).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,10,0,0,0) and val <= Time.utc(2011,1,10,0,0,0)") + expect(exp.to_ruby).to eq("!(val=rec.last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,10,0,0,0) and val <= Time.utc(2011,1,10,0,0,0)") end it "generates the ruby for a FROM expression with hash context" do @@ -2038,42 +2038,42 @@ it "generates the ruby for a AFTER expression with date value" do exp = MiqExpression.new("AFTER" => {"field" => "Vm-retires_on", "value" => "2011-01-10"}) - expect(exp.to_ruby(tz)).to eq("!(val=/virtual/retires_on&.to_time).nil? and val > Time.utc(2011,1,11,4,59,59)") + expect(exp.to_ruby(tz)).to eq("!(val=rec.retires_on&.to_time).nil? and val > Time.utc(2011,1,11,4,59,59)") end it "generates the ruby for a BEFORE expression with date value" do exp = MiqExpression.new("BEFORE" => {"field" => "Vm-retires_on", "value" => "2011-01-10"}) - expect(exp.to_ruby(tz)).to eq("!(val=/virtual/retires_on&.to_time).nil? and val < Time.utc(2011,1,10,5,0,0)") + expect(exp.to_ruby(tz)).to eq("!(val=rec.retires_on&.to_time).nil? and val < Time.utc(2011,1,10,5,0,0)") end it "generates the ruby for a AFTER expression with datetime value" do exp = MiqExpression.new("AFTER" => {"field" => "Vm-last_scan_on", "value" => "2011-01-10 9:00"}) - expect(exp.to_ruby(tz)).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val > Time.utc(2011,1,10,14,0,0)") + expect(exp.to_ruby(tz)).to eq("!(val=rec.last_scan_on&.to_time).nil? and val > Time.utc(2011,1,10,14,0,0)") end it "generates the ruby for a AFTER expression with datetime value" do exp = MiqExpression.new("AFTER" => {"field" => "Vm-last_scan_on", "value" => "2011-01-10 9:00"}) - expect(exp.to_ruby(tz)).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val > Time.utc(2011,1,10,14,0,0)") + expect(exp.to_ruby(tz)).to eq("!(val=rec.last_scan_on&.to_time).nil? and val > Time.utc(2011,1,10,14,0,0)") end it "generates the ruby for a IS expression wtih date value" do exp = MiqExpression.new("IS" => {"field" => "Vm-retires_on", "value" => "2011-01-10"}) - expect(exp.to_ruby(tz)).to eq("!(val=/virtual/retires_on&.to_time).nil? and val >= Time.utc(2011,1,10,5,0,0) and val <= Time.utc(2011,1,11,4,59,59)") + expect(exp.to_ruby(tz)).to eq("!(val=rec.retires_on&.to_time).nil? and val >= Time.utc(2011,1,10,5,0,0) and val <= Time.utc(2011,1,11,4,59,59)") end it "generates the ruby for a FROM expression with date values" do exp = MiqExpression.new("FROM" => {"field" => "Vm-retires_on", "value" => ["2011-01-09", "2011-01-10"]}) - expect(exp.to_ruby(tz)).to eq("!(val=/virtual/retires_on&.to_time).nil? and val >= Time.utc(2011,1,9,5,0,0) and val <= Time.utc(2011,1,11,4,59,59)") + expect(exp.to_ruby(tz)).to eq("!(val=rec.retires_on&.to_time).nil? and val >= Time.utc(2011,1,9,5,0,0) and val <= Time.utc(2011,1,11,4,59,59)") end it "generates the ruby for a FROM expression with datetime values" do exp = MiqExpression.new("FROM" => {"field" => "Vm-last_scan_on", "value" => ["2011-01-10 8:00", "2011-01-10 17:00"]}) - expect(exp.to_ruby(tz)).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,10,13,0,0) and val <= Time.utc(2011,1,10,22,0,0)") + expect(exp.to_ruby(tz)).to eq("!(val=rec.last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,10,13,0,0) and val <= Time.utc(2011,1,10,22,0,0)") end it "generates the ruby for a FROM expression with identical datetime values" do exp = MiqExpression.new("FROM" => {"field" => "Vm-last_scan_on", "value" => ["2011-01-10 00:00", "2011-01-10 00:00"]}) - expect(exp.to_ruby(tz)).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,10,5,0,0) and val <= Time.utc(2011,1,10,5,0,0)") + expect(exp.to_ruby(tz)).to eq("!(val=rec.last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,10,5,0,0) and val <= Time.utc(2011,1,10,5,0,0)") end end end @@ -2085,72 +2085,72 @@ it "generates the SQL for a AFTER expression with a value of 'Yesterday' for a date field" do exp = described_class.new("AFTER" => {"field" => "Vm-retires_on", "value" => "Yesterday"}) ruby, * = exp.to_ruby("Asia/Jakarta") - expect(ruby).to eq("!(val=/virtual/retires_on&.to_time).nil? and val > Time.utc(2011,1,11,16,59,59)") + expect(ruby).to eq("!(val=rec.retires_on&.to_time).nil? and val > Time.utc(2011,1,11,16,59,59)") end it "generates the RUBY for a BEFORE expression with a value of 'Yesterday' for a date field" do exp = described_class.new("BEFORE" => {"field" => "Vm-retires_on", "value" => "Yesterday"}) ruby, * = exp.to_ruby("Asia/Jakarta") - expect(ruby).to eq("!(val=/virtual/retires_on&.to_time).nil? and val < Time.utc(2011,1,10,17,0,0)") + expect(ruby).to eq("!(val=rec.retires_on&.to_time).nil? and val < Time.utc(2011,1,10,17,0,0)") end it "generates the RUBY for an IS expression with a value of 'Yesterday' for a date field" do exp = described_class.new("IS" => {"field" => "Vm-retires_on", "value" => "Yesterday"}) ruby, * = exp.to_ruby("Asia/Jakarta") - expect(ruby).to eq("!(val=/virtual/retires_on&.to_time).nil? and val >= Time.utc(2011,1,10,17,0,0) and val <= Time.utc(2011,1,11,16,59,59)") + expect(ruby).to eq("!(val=rec.retires_on&.to_time).nil? and val >= Time.utc(2011,1,10,17,0,0) and val <= Time.utc(2011,1,11,16,59,59)") end it "generates the RUBY for a FROM expression with a value of 'Yesterday'/'Today' for a date field" do exp = described_class.new("FROM" => {"field" => "Vm-retires_on", "value" => %w[Yesterday Today]}) ruby, * = exp.to_ruby("Asia/Jakarta") - expect(ruby).to eq("!(val=/virtual/retires_on&.to_time).nil? and val >= Time.utc(2011,1,10,17,0,0) and val <= Time.utc(2011,1,12,16,59,59)") + expect(ruby).to eq("!(val=rec.retires_on&.to_time).nil? and val >= Time.utc(2011,1,10,17,0,0) and val <= Time.utc(2011,1,12,16,59,59)") end end context "relative dates with no time zone" do it "generates the ruby for an AFTER expression with date value of n Days Ago" do exp = MiqExpression.new("AFTER" => {"field" => "Vm-retires_on", "value" => "2 Days Ago"}) - expect(exp.to_ruby).to eq("!(val=/virtual/retires_on&.to_time).nil? and val > Time.utc(2011,1,9,23,59,59)") + expect(exp.to_ruby).to eq("!(val=rec.retires_on&.to_time).nil? and val > Time.utc(2011,1,9,23,59,59)") end it "generates the ruby for an AFTER expression with datetime value of n Days ago" do exp = MiqExpression.new("AFTER" => {"field" => "Vm-last_scan_on", "value" => "2 Days Ago"}) - expect(exp.to_ruby).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val > Time.utc(2011,1,9,23,59,59)") + expect(exp.to_ruby).to eq("!(val=rec.last_scan_on&.to_time).nil? and val > Time.utc(2011,1,9,23,59,59)") end it "generates the ruby for a BEFORE expression with date value of n Days Ago" do exp = MiqExpression.new("BEFORE" => {"field" => "Vm-retires_on", "value" => "2 Days Ago"}) - expect(exp.to_ruby).to eq("!(val=/virtual/retires_on&.to_time).nil? and val < Time.utc(2011,1,9,0,0,0)") + expect(exp.to_ruby).to eq("!(val=rec.retires_on&.to_time).nil? and val < Time.utc(2011,1,9,0,0,0)") end it "generates the ruby for a BEFORE expression with datetime value of n Days Ago" do exp = MiqExpression.new("BEFORE" => {"field" => "Vm-last_scan_on", "value" => "2 Days Ago"}) - expect(exp.to_ruby).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val < Time.utc(2011,1,9,0,0,0)") + expect(exp.to_ruby).to eq("!(val=rec.last_scan_on&.to_time).nil? and val < Time.utc(2011,1,9,0,0,0)") end it "generates the ruby for a FROM expression with datetime values of Last/This Hour" do exp = MiqExpression.new("FROM" => {"field" => "Vm-last_scan_on", "value" => ["Last Hour", "This Hour"]}) - expect(exp.to_ruby).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,11,16,0,0) and val <= Time.utc(2011,1,11,17,59,59)") + expect(exp.to_ruby).to eq("!(val=rec.last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,11,16,0,0) and val <= Time.utc(2011,1,11,17,59,59)") end it "generates the ruby for a FROM expression with date values of Last Week" do exp = MiqExpression.new("FROM" => {"field" => "Vm-retires_on", "value" => ["Last Week", "Last Week"]}) - expect(exp.to_ruby).to eq("!(val=/virtual/retires_on&.to_time).nil? and val >= Time.utc(2011,1,3,0,0,0) and val <= Time.utc(2011,1,9,23,59,59)") + expect(exp.to_ruby).to eq("!(val=rec.retires_on&.to_time).nil? and val >= Time.utc(2011,1,3,0,0,0) and val <= Time.utc(2011,1,9,23,59,59)") end it "generates the ruby for a FROM expression with datetime values of Last Week" do exp = MiqExpression.new("FROM" => {"field" => "Vm-last_scan_on", "value" => ["Last Week", "Last Week"]}) - expect(exp.to_ruby).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,3,0,0,0) and val <= Time.utc(2011,1,9,23,59,59)") + expect(exp.to_ruby).to eq("!(val=rec.last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,3,0,0,0) and val <= Time.utc(2011,1,9,23,59,59)") end it "generates the ruby for a FROM expression with datetime values of n Months Ago/Last Month" do exp = MiqExpression.new("FROM" => {"field" => "Vm-last_scan_on", "value" => ["2 Months Ago", "Last Month"]}) - expect(exp.to_ruby).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val >= Time.utc(2010,11,1,0,0,0) and val <= Time.utc(2010,12,31,23,59,59)") + expect(exp.to_ruby).to eq("!(val=rec.last_scan_on&.to_time).nil? and val >= Time.utc(2010,11,1,0,0,0) and val <= Time.utc(2010,12,31,23,59,59)") end it "generates the ruby for an IS expression with datetime value of Last Week" do exp = MiqExpression.new("IS" => {"field" => "Vm-last_scan_on", "value" => "Last Week"}) - expect(exp.to_ruby).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,3,0,0,0) and val <= Time.utc(2011,1,9,23,59,59)") + expect(exp.to_ruby).to eq("!(val=rec.last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,3,0,0,0) and val <= Time.utc(2011,1,9,23,59,59)") end it "generates the ruby for an IS expression with relative date with hash context" do @@ -2161,22 +2161,22 @@ it "generates the ruby for an IS expression with date value of Last Week" do exp = MiqExpression.new("IS" => {"field" => "Vm-retires_on", "value" => "Last Week"}) - expect(exp.to_ruby).to eq("!(val=/virtual/retires_on&.to_time).nil? and val >= Time.utc(2011,1,3,0,0,0) and val <= Time.utc(2011,1,9,23,59,59)") + expect(exp.to_ruby).to eq("!(val=rec.retires_on&.to_time).nil? and val >= Time.utc(2011,1,3,0,0,0) and val <= Time.utc(2011,1,9,23,59,59)") end it "generates the ruby for a IS expression with date value of Today" do exp = MiqExpression.new("IS" => {"field" => "Vm-retires_on", "value" => "Today"}) - expect(exp.to_ruby).to eq("!(val=/virtual/retires_on&.to_time).nil? and val >= Time.utc(2011,1,11,0,0,0) and val <= Time.utc(2011,1,11,23,59,59)") + expect(exp.to_ruby).to eq("!(val=rec.retires_on&.to_time).nil? and val >= Time.utc(2011,1,11,0,0,0) and val <= Time.utc(2011,1,11,23,59,59)") end it "generates the ruby for an IS expression with date value of n Hours Ago" do exp = MiqExpression.new("IS" => {"field" => "Vm-retires_on", "value" => "3 Hours Ago"}) - expect(exp.to_ruby).to eq("!(val=/virtual/retires_on&.to_time).nil? and val >= Time.utc(2011,1,11,14,0,0) and val <= Time.utc(2011,1,11,14,59,59)") + expect(exp.to_ruby).to eq("!(val=rec.retires_on&.to_time).nil? and val >= Time.utc(2011,1,11,14,0,0) and val <= Time.utc(2011,1,11,14,59,59)") end it "generates the ruby for a IS expression with datetime value of n Hours Ago" do exp = MiqExpression.new("IS" => {"field" => "Vm-last_scan_on", "value" => "3 Hours Ago"}) - expect(exp.to_ruby).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,11,14,0,0) and val <= Time.utc(2011,1,11,14,59,59)") + expect(exp.to_ruby).to eq("!(val=rec.last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,11,14,0,0) and val <= Time.utc(2011,1,11,14,59,59)") end end @@ -2185,47 +2185,47 @@ it "generates the ruby for a FROM expression with datetime value of Last/This Hour" do exp = MiqExpression.new("FROM" => {"field" => "Vm-last_scan_on", "value" => ["Last Hour", "This Hour"]}) - expect(exp.to_ruby(tz)).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,11,16,0,0) and val <= Time.utc(2011,1,11,17,59,59)") + expect(exp.to_ruby(tz)).to eq("!(val=rec.last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,11,16,0,0) and val <= Time.utc(2011,1,11,17,59,59)") end it "generates the ruby for a FROM expression with date values of Last Week" do exp = MiqExpression.new("FROM" => {"field" => "Vm-retires_on", "value" => ["Last Week", "Last Week"]}) - expect(exp.to_ruby(tz)).to eq("!(val=/virtual/retires_on&.to_time).nil? and val >= Time.utc(2011,1,3,10,0,0) and val <= Time.utc(2011,1,10,9,59,59)") + expect(exp.to_ruby(tz)).to eq("!(val=rec.retires_on&.to_time).nil? and val >= Time.utc(2011,1,3,10,0,0) and val <= Time.utc(2011,1,10,9,59,59)") end it "generates the ruby for a FROM expression with datetime values of Last Week" do exp = MiqExpression.new("FROM" => {"field" => "Vm-last_scan_on", "value" => ["Last Week", "Last Week"]}) - expect(exp.to_ruby(tz)).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,3,10,0,0) and val <= Time.utc(2011,1,10,9,59,59)") + expect(exp.to_ruby(tz)).to eq("!(val=rec.last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,3,10,0,0) and val <= Time.utc(2011,1,10,9,59,59)") end it "generates the ruby for a FROM expression with datetime values of n Months Ago/Last Month" do exp = MiqExpression.new("FROM" => {"field" => "Vm-last_scan_on", "value" => ["2 Months Ago", "Last Month"]}) - expect(exp.to_ruby(tz)).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val >= Time.utc(2010,11,1,10,0,0) and val <= Time.utc(2011,1,1,9,59,59)") + expect(exp.to_ruby(tz)).to eq("!(val=rec.last_scan_on&.to_time).nil? and val >= Time.utc(2010,11,1,10,0,0) and val <= Time.utc(2011,1,1,9,59,59)") end it "generates the ruby for an IS expression with datetime value of Last Week" do exp = MiqExpression.new("IS" => {"field" => "Vm-last_scan_on", "value" => "Last Week"}) - expect(exp.to_ruby(tz)).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,3,10,0,0) and val <= Time.utc(2011,1,10,9,59,59)") + expect(exp.to_ruby(tz)).to eq("!(val=rec.last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,3,10,0,0) and val <= Time.utc(2011,1,10,9,59,59)") end it "generates the ruby for an IS expression with date value of Last Week" do exp = MiqExpression.new("IS" => {"field" => "Vm-retires_on", "value" => "Last Week"}) - expect(exp.to_ruby(tz)).to eq("!(val=/virtual/retires_on&.to_time).nil? and val >= Time.utc(2011,1,3,10,0,0) and val <= Time.utc(2011,1,10,9,59,59)") + expect(exp.to_ruby(tz)).to eq("!(val=rec.retires_on&.to_time).nil? and val >= Time.utc(2011,1,3,10,0,0) and val <= Time.utc(2011,1,10,9,59,59)") end it "generates the ruby for an IS expression with date value of Today" do exp = MiqExpression.new("IS" => {"field" => "Vm-retires_on", "value" => "Today"}) - expect(exp.to_ruby(tz)).to eq("!(val=/virtual/retires_on&.to_time).nil? and val >= Time.utc(2011,1,11,10,0,0) and val <= Time.utc(2011,1,12,9,59,59)") + expect(exp.to_ruby(tz)).to eq("!(val=rec.retires_on&.to_time).nil? and val >= Time.utc(2011,1,11,10,0,0) and val <= Time.utc(2011,1,12,9,59,59)") end it "generates the ruby for an IS expression with date value of n Hours Ago" do exp = MiqExpression.new("IS" => {"field" => "Vm-retires_on", "value" => "3 Hours Ago"}) - expect(exp.to_ruby(tz)).to eq("!(val=/virtual/retires_on&.to_time).nil? and val >= Time.utc(2011,1,11,14,0,0) and val <= Time.utc(2011,1,11,14,59,59)") + expect(exp.to_ruby(tz)).to eq("!(val=rec.retires_on&.to_time).nil? and val >= Time.utc(2011,1,11,14,0,0) and val <= Time.utc(2011,1,11,14,59,59)") end it "generates the ruby for an IS expression with datetime value of n Hours Ago" do exp = MiqExpression.new("IS" => {"field" => "Vm-last_scan_on", "value" => "3 Hours Ago"}) - expect(exp.to_ruby(tz)).to eq("!(val=/virtual/last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,11,14,0,0) and val <= Time.utc(2011,1,11,14,59,59)") + expect(exp.to_ruby(tz)).to eq("!(val=rec.last_scan_on&.to_time).nil? and val >= Time.utc(2011,1,11,14,0,0) and val <= Time.utc(2011,1,11,14,59,59)") end end end diff --git a/spec/models/condition_spec.rb b/spec/models/condition_spec.rb index 1b6d60f27b8..12abf2f4393 100644 --- a/spec/models/condition_spec.rb +++ b/spec/models/condition_spec.rb @@ -27,7 +27,7 @@ it "evaluates custom attribute on right side and integer column of VmOrTemplate on left side" do condition_to_evaluate = Condition.send(:subst, @filter_2.to_ruby(nil), vm) - expect(condition_to_evaluate).to eq('10 > 30') + expect(condition_to_evaluate).to eq('rec.memory_reserve > 30') end it "evaluates custom attribute on left side and integer column of VmOrTemplate on right side" do @@ -216,11 +216,19 @@ describe ".do_eval" do it "detects true" do - expect(Condition.send(:do_eval, "true")).to be_truthy + expect(Condition.send(:do_eval, "true", nil)).to be_truthy end it "detects false" do - expect(Condition.send(:do_eval, "false")).not_to be_truthy + expect(Condition.send(:do_eval, "false", nil)).not_to be_truthy + end + + it "references object passed - happy" do + expect(Condition.send(:do_eval, "rec == 1", 1)).to be_truthy + end + + it "references object passed - sad" do + expect(Condition.send(:do_eval, "rec == 1", 2)).not_to be_truthy end end