sql_spec.rb 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Ticket::Selector::Sql do
  4. context 'when relative time range is selected in ticket selector' do
  5. def get_condition(operator, range)
  6. {
  7. 'ticket.created_at' => {
  8. operator: operator,
  9. range: range, # minute|hour|day|month|
  10. value: '10',
  11. },
  12. }
  13. end
  14. before do
  15. freeze_time
  16. end
  17. it 'calculates proper time interval, when operator is within last relative' do
  18. condition = get_condition('within last (relative)', 'minute')
  19. _, bind_params = Ticket.selector2sql(condition)
  20. expect(bind_params).to eq([10.minutes.ago, Time.zone.now])
  21. end
  22. it 'calculates proper time interval, when operator is within next relative' do
  23. condition = get_condition('within next (relative)', 'hour')
  24. _, bind_params = Ticket.selector2sql(condition)
  25. expect(bind_params).to eq([Time.zone.now, 10.hours.from_now])
  26. end
  27. it 'calculates proper time interval, when operator is before (relative)' do
  28. condition = get_condition('before (relative)', 'day')
  29. _, bind_params = Ticket.selector2sql(condition)
  30. expect(bind_params).to eq([10.days.ago])
  31. end
  32. it 'calculates proper time interval, when operator is after (relative)' do
  33. condition = get_condition('after (relative)', 'week')
  34. _, bind_params = Ticket.selector2sql(condition)
  35. expect(bind_params).to eq([10.weeks.from_now])
  36. end
  37. it 'calculates proper time interval, when operator is till (relative)' do
  38. condition = get_condition('till (relative)', 'month')
  39. _, bind_params = Ticket.selector2sql(condition)
  40. expect(bind_params).to eq([10.months.from_now])
  41. end
  42. it 'calculates proper time interval, when operator is from (relative)' do
  43. condition = get_condition('from (relative)', 'year')
  44. _, bind_params = Ticket.selector2sql(condition)
  45. expect(bind_params).to eq([10.years.ago])
  46. end
  47. context 'when today operator is used' do
  48. before do
  49. travel_to '2022-10-11 14:40:00'
  50. Setting.set('timezone_default', 'Europe/Berlin')
  51. end
  52. it 'calculates proper time interval when today operator is used', :aggregate_failures do
  53. _, bind_params = Ticket.selector2sql({ 'ticket.created_at' => { 'operator' => 'today' } })
  54. Time.use_zone(Setting.get('timezone_default_sanitized').presence) do
  55. expect(bind_params[0].to_s).to eq('2022-10-10 22:00:00 UTC')
  56. expect(bind_params[1].to_s).to eq('2022-10-11 21:59:59 UTC')
  57. end
  58. end
  59. end
  60. end
  61. end