Commit 19f9e94b authored by Adam Hegyi's avatar Adam Hegyi

Fix insights `period_field` parameter

This change casts the `period_field` parameter to Symbol so the
validation will not detect an error when a string `period_field` is
given.
parent 0cc4fd7f
---
title: Fix insights period_field parameter
merge_request: 38404
author:
type: fixed
......@@ -45,8 +45,8 @@ module Gitlab
raise InvalidPeriodError, "Invalid value for `period`: `#{period}`. Allowed values are #{VALID_PERIOD}!"
end
unless VALID_PERIOD_FIELDS[issuable_type].include?(period_field)
raise InvalidPeriodFieldError, "Invalid value for `period_field`: `#{period_field}`. Allowed values are #{VALID_PERIOD_FIELDS[issuable_type]}!"
unless VALID_PERIOD_FIELDS[issuable_type].include?(period_field.to_sym)
raise InvalidPeriodFieldError, "Invalid value for `period_field`: `#{period_field}`. Allowed values are #{VALID_PERIOD_FIELDS[issuable_type].join(', ')}!"
end
unless period_limit > 0
......
......@@ -71,7 +71,7 @@ RSpec.describe Gitlab::Insights::Reducers::CountPerPeriodReducer do
end
it 'raises an error for an unknown :period_field option' do
expect { reduce(issuable_relation, period: 'month', period_limit: 5, period_field: :foo) }.to raise_error(described_class::InvalidPeriodFieldError, "Invalid value for `period_field`: `foo`. Allowed values are #{described_class::VALID_PERIOD_FIELDS[:issue]}!")
expect { reduce(issuable_relation, period: 'month', period_limit: 5, period_field: :foo) }.to raise_error(described_class::InvalidPeriodFieldError, "Invalid value for `period_field`: `foo`. Allowed values are #{described_class::VALID_PERIOD_FIELDS[:issue].join(', ')}!")
end
it 'raises an error for an unknown :period_limit option' do
......@@ -106,6 +106,10 @@ RSpec.describe Gitlab::Insights::Reducers::CountPerPeriodReducer do
it 'returns issuables with only the needed fields' do
expect(reduce(issuable_relation, period: query[:group_by], period_field: :closed_at)).to eq(expected)
end
it 'works when string `period_field` is passed' do
expect(reduce(issuable_relation, period: query[:group_by], period_field: 'closed_at')).to eq(expected)
end
end
context 'with opened merge requests' do
......@@ -122,7 +126,7 @@ RSpec.describe Gitlab::Insights::Reducers::CountPerPeriodReducer do
end
it 'raises an error for an unknown :period_field option' do
expect { reduce(issuable_relation, period: 'month', period_limit: 5, period_field: :foo) }.to raise_error(described_class::InvalidPeriodFieldError, "Invalid value for `period_field`: `foo`. Allowed values are #{described_class::VALID_PERIOD_FIELDS[:merge_request]}!")
expect { reduce(issuable_relation, period: 'month', period_limit: 5, period_field: :foo) }.to raise_error(described_class::InvalidPeriodFieldError, "Invalid value for `period_field`: `foo`. Allowed values are #{described_class::VALID_PERIOD_FIELDS[:merge_request].join(', ')}!")
end
it 'returns issuables with only the needed fields' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment