Skip to content

Commit 776f23d

Browse files
fix(operator): replace the hard-coded duration by the duration variable (#621)
1 parent ed2a5d7 commit 776f23d

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ gemspec
1414
group :development, :test do
1515
gem 'byebug'
1616
gem 'rspec-rails'
17+
gem "timecop"
1718
end
1819

1920
group :test do

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ GEM
231231
attr_required (>= 0.0.5)
232232
httpclient (>= 2.4)
233233
thor (1.2.1)
234+
timecop (0.9.6)
234235
timeout (0.3.1)
235236
tzinfo (2.0.5)
236237
concurrent-ruby (~> 1.0)
@@ -272,6 +273,7 @@ DEPENDENCIES
272273
rspec-rails
273274
simplecov (~> 0.17.0)
274275
sqlite3 (~> 1.4)
276+
timecop
275277
useragent
276278

277279
BUNDLED WITH

app/services/forest_liana/operator_date_interval_parser.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ def get_date_filter(operator, value)
114114
else
115115
from = to_client_timezone(duration.send(period).ago
116116
.send("beginning_of_#{period_of_time}"))
117-
to = to_client_timezone(1.send(period).ago
117+
to = to_client_timezone(duration.send(period).ago
118118
.send("end_of_#{period_of_time}"))
119119
end
120+
120121
"BETWEEN '#{from}' AND '#{to}'"
121122
end
122123

@@ -151,9 +152,10 @@ def get_date_filter_for_previous_interval(operator, value)
151152
else
152153
from = to_client_timezone((duration * 2).send(period).ago
153154
.send("beginning_of_#{period_of_time}"))
154-
to = to_client_timezone((1 + duration).send(period).ago
155+
to = to_client_timezone((duration * 2).send(period).ago
155156
.send("end_of_#{period_of_time}"))
156157
end
158+
157159
"BETWEEN '#{from}' AND '#{to}'"
158160
end
159161

spec/services/forest_liana/filters_parser_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
module ForestLiana
2+
include ActiveSupport::Testing::TimeHelpers
3+
24
describe FiltersParser do
35
let(:timezone) { 'Europe/Paris' }
46
let(:resource) { Tree }
@@ -488,5 +490,22 @@ module ForestLiana
488490

489491
it { expect(filter_parser.apply_filters_on_previous_interval(date_condition_3).count).to eq 1 }
490492
end
493+
494+
describe 'parse_condition with time operator' do
495+
let(:freeze_time) { Time.utc(2022, 5, 22, 0, 0, 0) }
496+
before do
497+
Timecop.freeze freeze_time
498+
end
499+
500+
after do
501+
Timecop.return
502+
end
503+
504+
it 'parse_condition should return the correct query interval' do
505+
res = filter_parser.parse_condition({ 'field' => 'created_at', 'operator' => 'previous_quarter', 'value' => nil })
506+
507+
expect(res[res.index('BETWEEN')..-1]).to eq "BETWEEN '2021-12-31 22:00:00 UTC' AND '2022-03-31 21:59:59 UTC'"
508+
end
509+
end
491510
end
492511
end

0 commit comments

Comments
 (0)