Skip to content

Commit

Permalink
Add specs for median serializing
Browse files Browse the repository at this point in the history
  • Loading branch information
gpks committed May 23, 2019
1 parent ddb2949 commit ee92acc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/serializers/analytics_stage_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class AnalyticsStageEntity < Grape::Entity

expose :median, as: :value do |stage|
# median returns a BatchLoader instance which we first have to unwrap by using to_f
# we use to_f to make sure results below 1 are presented to the end-user
!stage.median.to_f.zero? ? distance_of_time_in_words(stage.median) : nil
end
end
30 changes: 30 additions & 0 deletions spec/serializers/analytics_stage_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,34 @@
it 'contains important elements of AnalyticsStage' do
expect(subject).to include(:title, :description, :value)
end

context 'when median is equal 0' do
before do
allow_any_instance_of(Gitlab::CycleAnalytics::BaseStage).to receive(:median).and_return(0)
end

it 'value is nil' do
expect(subject.fetch(:value)).to be_nil
end
end

context 'when median is below 1' do
before do
allow_any_instance_of(Gitlab::CycleAnalytics::BaseStage).to receive(:median).and_return(0.12)
end

it 'value is equal to median' do
expect(subject.fetch(:value)).to eq('less than a minute')
end
end

context 'when median is above 1' do
before do
allow_any_instance_of(Gitlab::CycleAnalytics::BaseStage).to receive(:median).and_return(1.12)
end

it 'value is equal to median' do
expect(subject.fetch(:value)).to eq('less than a minute')
end
end
end

0 comments on commit ee92acc

Please sign in to comment.