Skip to content

Commit

Permalink
fix(operator): replace the hard-coded duration by the duration variab…
Browse files Browse the repository at this point in the history
…le (#621)
  • Loading branch information
nicolasalexandre9 authored May 23, 2023
1 parent ed2a5d7 commit 776f23d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ gemspec
group :development, :test do
gem 'byebug'
gem 'rspec-rails'
gem "timecop"
end

group :test do
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ GEM
attr_required (>= 0.0.5)
httpclient (>= 2.4)
thor (1.2.1)
timecop (0.9.6)
timeout (0.3.1)
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
Expand Down Expand Up @@ -272,6 +273,7 @@ DEPENDENCIES
rspec-rails
simplecov (~> 0.17.0)
sqlite3 (~> 1.4)
timecop
useragent

BUNDLED WITH
Expand Down
6 changes: 4 additions & 2 deletions app/services/forest_liana/operator_date_interval_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ def get_date_filter(operator, value)
else
from = to_client_timezone(duration.send(period).ago
.send("beginning_of_#{period_of_time}"))
to = to_client_timezone(1.send(period).ago
to = to_client_timezone(duration.send(period).ago
.send("end_of_#{period_of_time}"))
end

"BETWEEN '#{from}' AND '#{to}'"
end

Expand Down Expand Up @@ -151,9 +152,10 @@ def get_date_filter_for_previous_interval(operator, value)
else
from = to_client_timezone((duration * 2).send(period).ago
.send("beginning_of_#{period_of_time}"))
to = to_client_timezone((1 + duration).send(period).ago
to = to_client_timezone((duration * 2).send(period).ago
.send("end_of_#{period_of_time}"))
end

"BETWEEN '#{from}' AND '#{to}'"
end

Expand Down
19 changes: 19 additions & 0 deletions spec/services/forest_liana/filters_parser_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module ForestLiana
include ActiveSupport::Testing::TimeHelpers

describe FiltersParser do
let(:timezone) { 'Europe/Paris' }
let(:resource) { Tree }
Expand Down Expand Up @@ -488,5 +490,22 @@ module ForestLiana

it { expect(filter_parser.apply_filters_on_previous_interval(date_condition_3).count).to eq 1 }
end

describe 'parse_condition with time operator' do
let(:freeze_time) { Time.utc(2022, 5, 22, 0, 0, 0) }
before do
Timecop.freeze freeze_time
end

after do
Timecop.return
end

it 'parse_condition should return the correct query interval' do
res = filter_parser.parse_condition({ 'field' => 'created_at', 'operator' => 'previous_quarter', 'value' => nil })

expect(res[res.index('BETWEEN')..-1]).to eq "BETWEEN '2021-12-31 22:00:00 UTC' AND '2022-03-31 21:59:59 UTC'"
end
end
end
end

0 comments on commit 776f23d

Please sign in to comment.