From 28891e3a7c15a2e10e6341814ba4dbbae24b3a4b Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Fri, 18 Oct 2024 12:05:42 -0500 Subject: [PATCH] adding specs --- ...er_classification_count_controller_spec.rb | 18 +++++++++++++ ...r_classification_counts_serializer_spec.rb | 26 +++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/spec/controllers/user_classification_count_controller_spec.rb b/spec/controllers/user_classification_count_controller_spec.rb index 0f3aa54..aed046f 100644 --- a/spec/controllers/user_classification_count_controller_spec.rb +++ b/spec/controllers/user_classification_count_controller_spec.rb @@ -126,6 +126,24 @@ get :query, params: { id: 1, time_spent: 'true' } expect(controller.params[:time_spent]).to eq(true) end + + context 'order_project_contributions validations' do + it 'errors if not a valid order_project_contributions_by option' do + get :query, params: { id: 1, order_project_contributions_by: 'blank' } + expect(response.status).to eq(400) + expect(response.body).to include('Can only order project contributions by recents or count') + end + + it 'allows allowable order_project_contributions_by option' do + get :query, params: { id: 1, order_project_contributions_by: 'recents' } + expect(response.status).to eq(200) + end + + it 'allows order_project_contributions_by to be case-insensitive' do + get :query, params: { id: 1, order_project_contributions_by: 'COUNT' } + expect(response.status).to eq(200) + end + end end end end diff --git a/spec/serializers/user_classification_counts_serializer_spec.rb b/spec/serializers/user_classification_counts_serializer_spec.rb index a3fce9a..ac24d30 100644 --- a/spec/serializers/user_classification_counts_serializer_spec.rb +++ b/spec/serializers/user_classification_counts_serializer_spec.rb @@ -59,7 +59,7 @@ let(:user_diff_period_classification_count) { build(:user_diff_period_classification_count) } let(:serializer) { described_class.new([user_diff_period_classification_count, user_classification_count, user_diff_proj_count]) } - it 'shows project_contributions ordered desc by count' do + it 'shows project_contributions ordered desc by count when order_proj_contribution_by by not given' do serialized = serializer.as_json(serializer_options: { project_contributions: true }) expect(serialized[:project_contributions].length).to eq(2) expect(serialized[:project_contributions][0][:project_id]).to eq(user_classification_count.project_id) @@ -68,7 +68,29 @@ expect(serialized[:project_contributions][1][:count]).to eq(user_diff_proj_count.count) end - it 'shows response data bucketed by period when querying top_projects' do + context 'when order_project_contributions_by param is given' do + it 'shows project_contributions ordered desc by count when order_proj_contribution_by is count' do + serialized = serializer.as_json(serializer_options: { project_contributions: true, order_project_contributions_by: 'count' }) + expect(serialized[:project_contributions].length).to eq(2) + expect(serialized[:project_contributions][0][:project_id]).to eq(user_classification_count.project_id) + expect(serialized[:project_contributions][0][:count]).to eq(user_classification_count.count + user_diff_period_classification_count.count) + expect(serialized[:project_contributions][1][:project_id]).to eq(user_diff_proj_count.project_id) + expect(serialized[:project_contributions][1][:count]).to eq(user_diff_proj_count.count) + end + + it 'shows project_contributions ordered by recents when order_proj_contribution_by is recents' do + classification_count_diff_project_created_yesterday = build(:user_diff_proj_classification_count, period: Date.today - 1) + serializer = described_class.new([classification_count_diff_project_created_yesterday,user_classification_count]) + serialized = serializer.as_json(serializer_options: { project_contributions: true, order_project_contributions_by: 'recents' }) + expect(serialized[:project_contributions].length).to eq(2) + expect(serialized[:project_contributions][0][:project_id]).to eq(user_classification_count.project_id) + expect(serialized[:project_contributions][0][:count]).to eq(user_classification_count.count) + expect(serialized[:project_contributions][1][:project_id]).to eq(classification_count_diff_project_created_yesterday.project_id) + expect(serialized[:project_contributions][1][:count]).to eq(classification_count_diff_project_created_yesterday.count) + end + end + + it 'shows response data bucketed by period when querying project_contributions by count' do serialized = serializer.as_json(serializer_options: { project_contributions: true, period: 'day' }) expect(serialized[:data].length).to eq(2) expect(serialized[:data][0][:period]).to eq(user_diff_period_classification_count.period)