Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict relative datetime handling to operators that support them in MiqExpression#to_ruby #10295

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions app/models/miq_expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -439,31 +439,30 @@ def self._to_ruby(exp, context_type, tz)

operator = exp.keys.first
case operator.downcase
when "equal", "=", "<", ">", ">=", "<=", "!=", "before", "after"
when "equal", "=", "<", ">", ">=", "<=", "!="
col_type = get_col_type(exp[operator]["field"]) if exp[operator]["field"]
if col_type == :date || col_type == :datetime
col_name = exp[operator]["field"]
col_ruby, _ = operands2rubyvalue(operator, {"field" => col_name}, context_type)

normalized_operator = normalize_ruby_operator(operator)
mode = case normalized_operator
when ">", "<=" then "end" # (> <date> 23::59:59), (<= <date> 23::59:59)
when "<", ">=" then "beginning" # (< <date> 00::00:00), (>= <date> 00::00:00)
operands = operands2rubyvalue(operator, exp[operator], context_type)
clause = operands.join(" #{normalize_ruby_operator(operator)} ")
when "before"
col_type = get_col_type(exp[operator]["field"]) if exp[operator]["field"]
col_name = exp[operator]["field"]
col_ruby, = operands2rubyvalue(operator, {"field" => col_name}, context_type)
val = RelativeDatetime.normalize(exp[operator]["value"], tz, "beginning")
clause = if col_type == :date
"val=#{col_ruby}; !val.nil? && val.to_date < #{quote(val.to_date, :date)}"
else
"val=#{col_ruby}; !val.nil? && val.to_time < #{quote(val.utc, :datetime)}"
end
when "after"
col_type = get_col_type(exp[operator]["field"]) if exp[operator]["field"]
col_name = exp[operator]["field"]
col_ruby, = operands2rubyvalue(operator, {"field" => col_name}, context_type)
val = RelativeDatetime.normalize(exp[operator]["value"], tz, "end")
clause = if col_type == :date
"val=#{col_ruby}; !val.nil? && val.to_date > #{quote(val.to_date, :date)}"
else
"val=#{col_ruby}; !val.nil? && val.to_time > #{quote(val.utc, :datetime)}"
end

if col_type == :date
val = RelativeDatetime.normalize(exp[operator]["value"], tz, mode)

clause = "val=#{col_ruby}; !val.nil? && val.to_date #{normalized_operator} #{quote(val.to_date, :date)}"
else
val = RelativeDatetime.normalize(exp[operator]["value"], tz, mode)

clause = "val=#{col_ruby}; !val.nil? && val.to_time #{normalized_operator} #{quote(val.utc, :datetime)}"
end
else
operands = operands2rubyvalue(operator, exp[operator], context_type)
clause = operands.join(" #{normalize_ruby_operator(operator)} ")
end
when "includes all"
operands = operands2rubyvalue(operator, exp[operator], context_type)
clause = "(#{operands[0]} & #{operands[1]}) == #{operands[1]}"
Expand Down
69 changes: 6 additions & 63 deletions spec/models/miq_expression_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -957,41 +957,16 @@
expect(exp.to_ruby).to eq("val=<value ref=vm, type=date>/virtual/retires_on</value>; !val.nil? && val.to_date > '2011-01-10'.to_date")
end

it "generates the ruby for a > expression date value" do
exp = MiqExpression.new(">" => {"field" => "Vm-retires_on", "value" => "2011-01-10"})
expect(exp.to_ruby).to eq("val=<value ref=vm, type=date>/virtual/retires_on</value>; !val.nil? && val.to_date > '2011-01-10'.to_date")
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=<value ref=vm, type=date>/virtual/retires_on</value>; !val.nil? && val.to_date < '2011-01-10'.to_date")
end

it "generates the ruby for a < expression with date value" do
exp = MiqExpression.new("<" => {"field" => "Vm-retires_on", "value" => "2011-01-10"})
expect(exp.to_ruby).to eq("val=<value ref=vm, type=date>/virtual/retires_on</value>; !val.nil? && val.to_date < '2011-01-10'.to_date")
end

it "generates the ruby for a >= expression with date value" do
exp = MiqExpression.new(">=" => {"field" => "Vm-retires_on", "value" => "2011-01-10"})
expect(exp.to_ruby).to eq("val=<value ref=vm, type=date>/virtual/retires_on</value>; !val.nil? && val.to_date >= '2011-01-10'.to_date")
end

it "generates the ruby for a <= expression with date value" do
exp = MiqExpression.new("<=" => {"field" => "Vm-retires_on", "value" => "2011-01-10"})
expect(exp.to_ruby).to eq("val=<value ref=vm, type=date>/virtual/retires_on</value>; !val.nil? && val.to_date <= '2011-01-10'.to_date")
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=<value ref=vm, type=datetime>/virtual/last_scan_on</value>; !val.nil? && val.to_time > '2011-01-10T09:00:00Z'.to_time(:utc)")
end

it "generates the ruby for a > expression with datetime value" do
exp = MiqExpression.new(">" => {"field" => "Vm-last_scan_on", "value" => "2011-01-10 9:00"})
expect(exp.to_ruby).to eq("val=<value ref=vm, type=datetime>/virtual/last_scan_on</value>; !val.nil? && val.to_time > '2011-01-10T09:00:00Z'.to_time(:utc)")
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=<value ref=vm, type=date>/virtual/retires_on</value>; !val.nil? && val.to_date == '2011-01-10'.to_date")
Expand Down Expand Up @@ -1031,38 +1006,18 @@
expect(exp.to_ruby(tz)).to eq("val=<value ref=vm, type=date>/virtual/retires_on</value>; !val.nil? && val.to_date > '2011-01-10'.to_date")
end

it "generates the ruby for a > expression with date value" do
exp = MiqExpression.new(">" => {"field" => "Vm-retires_on", "value" => "2011-01-10"})
expect(exp.to_ruby(tz)).to eq("val=<value ref=vm, type=date>/virtual/retires_on</value>; !val.nil? && val.to_date > '2011-01-10'.to_date")
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=<value ref=vm, type=date>/virtual/retires_on</value>; !val.nil? && val.to_date < '2011-01-10'.to_date")
end

it "generates the ruby for a < expression with date value" do
exp = MiqExpression.new("<" => {"field" => "Vm-retires_on", "value" => "2011-01-10"})
expect(exp.to_ruby(tz)).to eq("val=<value ref=vm, type=date>/virtual/retires_on</value>; !val.nil? && val.to_date < '2011-01-10'.to_date")
end

it "generates the ruby for a >= expression with date value" do
exp = MiqExpression.new(">=" => {"field" => "Vm-retires_on", "value" => "2011-01-10"})
expect(exp.to_ruby(tz)).to eq("val=<value ref=vm, type=date>/virtual/retires_on</value>; !val.nil? && val.to_date >= '2011-01-10'.to_date")
end

it "generates the ruby for a <= expression with date value" do
exp = MiqExpression.new("<=" => {"field" => "Vm-retires_on", "value" => "2011-01-10"})
expect(exp.to_ruby(tz)).to eq("val=<value ref=vm, type=date>/virtual/retires_on</value>; !val.nil? && val.to_date <= '2011-01-10'.to_date")
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=<value ref=vm, type=datetime>/virtual/last_scan_on</value>; !val.nil? && val.to_time > '2011-01-10T14:00:00Z'.to_time(:utc)")
end

it "generates the ruby for a > expression with datetime value" do
exp = MiqExpression.new(">" => {"field" => "Vm-last_scan_on", "value" => "2011-01-10 9:00"})
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=<value ref=vm, type=datetime>/virtual/last_scan_on</value>; !val.nil? && val.to_time > '2011-01-10T14:00:00Z'.to_time(:utc)")
end

Expand Down Expand Up @@ -1092,30 +1047,18 @@
around { |example| Timecop.freeze("2011-01-11 17:30 UTC") { example.run } }

context "given a non-UTC timezone" do
it "generates the SQL for a > expression with a value of 'Yesterday' for a date field" do
exp = described_class.new(">" => {"field" => "Vm-retires_on", "value" => "Yesterday"})
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=<value ref=vm, type=date>/virtual/retires_on</value>; !val.nil? && val.to_date > '2011-01-11'.to_date")
end

it "generates the RUBY for a >= expression with a value of 'Yesterday' for a date field" do
exp = described_class.new(">=" => {"field" => "Vm-retires_on", "value" => "Yesterday"})
ruby, * = exp.to_ruby("Asia/Jakarta")
expect(ruby).to eq("val=<value ref=vm, type=date>/virtual/retires_on</value>; !val.nil? && val.to_date >= '2011-01-11'.to_date")
end

it "generates the RUBY for a < expression with a value of 'Yesterday' for a date field" do
exp = described_class.new("<" => {"field" => "Vm-retires_on", "value" => "Yesterday"})
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=<value ref=vm, type=date>/virtual/retires_on</value>; !val.nil? && val.to_date < '2011-01-11'.to_date")
end

it "generates the RUBY for a <= expression with a value of 'Yesterday' for a date field" do
exp = described_class.new("<=" => {"field" => "Vm-retires_on", "value" => "Yesterday"})
ruby, * = exp.to_ruby("Asia/Jakarta")
expect(ruby).to eq("val=<value ref=vm, type=date>/virtual/retires_on</value>; !val.nil? && val.to_date <= '2011-01-11'.to_date")
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")
Expand Down