From 0a3f8de5101c00d2d27b376af56777f85f3c9bf9 Mon Sep 17 00:00:00 2001 From: jacobtoddmathes Date: Fri, 16 Feb 2024 13:06:18 -0600 Subject: [PATCH] all implemented rspec tests passing --- .../controllers/survey_profiles_controller.rb | 68 +++++++++++++++---- .../app/helpers/survey_profiles_helper.rb | 16 +++++ .../app/helpers/survey_responses_helper.rb | 18 +++++ .../helpers/survey_profiles_helper_spec.rb | 34 +++++++++- .../helpers/survey_responses_helper_spec.rb | 34 +++++++++- rails_root/spec/models/survey_profile_spec.rb | 11 +-- .../spec/models/survey_response_spec.rb | 10 +-- .../spec/requests/survey_profiles_spec.rb | 37 +++++++--- 8 files changed, 190 insertions(+), 38 deletions(-) diff --git a/rails_root/app/controllers/survey_profiles_controller.rb b/rails_root/app/controllers/survey_profiles_controller.rb index b30fb1e..4c5cd24 100644 --- a/rails_root/app/controllers/survey_profiles_controller.rb +++ b/rails_root/app/controllers/survey_profiles_controller.rb @@ -21,32 +21,70 @@ def edit; end # POST /survey_profiles or /survey_profiles.json def create - @survey_profile = SurveyProfile.new(survey_profile_params) + # If any of the survey_profile_params values are nil, then the form is invalid + if survey_profile_params.values.any?(&:nil?) + respond_to do |format| + format.html do + redirect_to new_survey_profile_url, notice: 'invalid form', status: :unprocessable_entity + end + format.json { render json: { error: 'invalid form' }, status: :unprocessable_entity } + end - respond_to do |format| - if @survey_profile.save + # if user_id is not unique, then the form is invalid + elsif SurveyProfile.find_by(user_id: survey_profile_params[:user_id]) + respond_to do |format| format.html do - redirect_to survey_profile_url(@survey_profile), notice: 'Survey profile was successfully created.' + redirect_to new_survey_profile_url, notice: 'user_id is not unique', status: :unprocessable_entity + end + format.json { render json: { error: 'user_id is not unique' }, status: :unprocessable_entity } + end + + else + @survey_profile = SurveyProfile.new(survey_profile_params) + + respond_to do |format| + if @survey_profile.save + format.html do + redirect_to survey_profile_url(@survey_profile), notice: 'Survey profile was successfully created.' + end + format.json { render :show, status: :created, location: @survey_profile } + end - format.json { render :show, status: :created, location: @survey_profile } - else - format.html { render :new, status: :unprocessable_entity } - format.json { render json: @survey_profile.errors, status: :unprocessable_entity } end end end # PATCH/PUT /survey_profiles/1 or /survey_profiles/1.json def update - respond_to do |format| - if @survey_profile.update(survey_profile_params) + # if anything in survey_profile_params is nil, then the form is invalid + + if survey_profile_params.values.any?(&:nil?) + respond_to do |format| + format.html do + redirect_to edit_survey_profile_url(@survey_profile), notice: 'invalid form', status: :unprocessable_entity + end + format.json { render json: { error: 'invalid form' }, status: :unprocessable_entity } + end + + # check if trying access a non-existing user_id + elsif SurveyProfile.find_by(user_id: survey_profile_params[:user_id]).nil? + respond_to do |format| format.html do - redirect_to survey_profile_url(@survey_profile), notice: 'Survey profile was successfully updated.' + redirect_to edit_survey_profile_url(@survey_profile), notice: 'user_id does not exist', + status: :unprocessable_entity + end + format.json { render json: { error: 'user_id does not exist' }, status: :unprocessable_entity } + end + + else + respond_to do |format| + if @survey_profile.update(survey_profile_params) + format.html do + redirect_to survey_profile_url(@survey_profile), notice: 'Survey profile was successfully updated.' + end + format.json { render :show, status: :ok, location: @survey_profile } + end - format.json { render :show, status: :ok, location: @survey_profile } - else - format.html { render :edit, status: :unprocessable_entity } - format.json { render json: @survey_profile.errors, status: :unprocessable_entity } end end end diff --git a/rails_root/app/helpers/survey_profiles_helper.rb b/rails_root/app/helpers/survey_profiles_helper.rb index 22efde9..75f7328 100644 --- a/rails_root/app/helpers/survey_profiles_helper.rb +++ b/rails_root/app/helpers/survey_profiles_helper.rb @@ -1,4 +1,20 @@ # frozen_string_literal: true module SurveyProfilesHelper + def full_name(survey_profile) + "#{survey_profile.first_name} #{survey_profile.last_name}" + end + + def campus_name(survey_profile) + "#{survey_profile.campus_name}" + end + + def district_name(survey_profile) + "#{survey_profile.district_name}" + end + + def get_survey_profile_id(user_id) + survey_profile = SurveyProfile.find_by(user_id: user_id) + survey_profile.id + end end diff --git a/rails_root/app/helpers/survey_responses_helper.rb b/rails_root/app/helpers/survey_responses_helper.rb index 5d78163..3f7c9e6 100644 --- a/rails_root/app/helpers/survey_responses_helper.rb +++ b/rails_root/app/helpers/survey_responses_helper.rb @@ -1,4 +1,22 @@ # frozen_string_literal: true module SurveyResponsesHelper + # method to calculate the average score of a survey response + def average_score(survey_response) + score_fields = %i[leads_by_example ability_to_juggle communicator lifelong_learner high_expectations + cooperative empathetic people_oriented] + scores = score_fields.map { |field| survey_response.send(field) } + scores.sum.to_f / scores.size + end + + # method to format the date of a survey response + def formatted_date(survey_response) + survey_response.created_at.strftime('%B %d, %Y') + end + + # method to find the user of a survey response + def user_of_response(survey_response) + # returns user_id of the survey response + survey_response.user_id + end end diff --git a/rails_root/spec/helpers/survey_profiles_helper_spec.rb b/rails_root/spec/helpers/survey_profiles_helper_spec.rb index 2cc9d6b..d80c859 100644 --- a/rails_root/spec/helpers/survey_profiles_helper_spec.rb +++ b/rails_root/spec/helpers/survey_profiles_helper_spec.rb @@ -13,5 +13,37 @@ # end # end RSpec.describe SurveyProfilesHelper, type: :helper do - pending "add some examples to (or delete) #{__FILE__}" + let(:survey_profile) do + SurveyProfile.create!( + user_id: 1, + first_name: 'John', + last_name: 'Doe', + campus_name: 'Campus', + district_name: 'District' + ) + end + + describe '#full_name' do + it 'returns the full name of a survey profile' do + expect(helper.full_name(survey_profile)).to eq('John Doe') + end + end + + describe '#campus_name' do + it 'returns the campus name of a survey profile' do + expect(helper.campus_name(survey_profile)).to eq('Campus') + end + end + + describe '#district_name' do + it 'returns the district name of a survey profile' do + expect(helper.district_name(survey_profile)).to eq('District') + end + end + + describe '#get_survey_profile_id' do + it 'returns the id of a survey profile' do + expect(helper.get_survey_profile_id(survey_profile.user_id)).to eq(survey_profile.id) + end + end end diff --git a/rails_root/spec/helpers/survey_responses_helper_spec.rb b/rails_root/spec/helpers/survey_responses_helper_spec.rb index 8883c93..3ea8246 100644 --- a/rails_root/spec/helpers/survey_responses_helper_spec.rb +++ b/rails_root/spec/helpers/survey_responses_helper_spec.rb @@ -13,5 +13,37 @@ # end # end RSpec.describe SurveyResponsesHelper, type: :helper do - pending "add some examples to (or delete) #{__FILE__}" + let(:survey_response) do + SurveyResponse.create!( + user_id: 1, + leads_by_example: 1, + ability_to_juggle: 1, + communicator: 1, + lifelong_learner: 1, + high_expectations: 1, + cooperative: 1, + empathetic: 1, + people_oriented: 1 + ) + end + + describe '#average_score' do + it 'returns the average score of a survey response' do + expect(helper.average_score(survey_response)).to eq(1.0) + end + end + + require 'rails_helper' + + describe '#formatted_date' do + it 'returns the formatted date of a survey response' do + expect(helper.formatted_date(survey_response)).to eq(survey_response.created_at.strftime('%B %d, %Y')) + end + end + + describe '#user_of_response' do + it 'returns the user of a survey response' do + expect(helper.user_of_response(survey_response)).to eq(survey_response.user_id) + end + end end diff --git a/rails_root/spec/models/survey_profile_spec.rb b/rails_root/spec/models/survey_profile_spec.rb index 158de05..247c10b 100644 --- a/rails_root/spec/models/survey_profile_spec.rb +++ b/rails_root/spec/models/survey_profile_spec.rb @@ -1,7 +1,8 @@ -# frozen_string_literal: true +# # frozen_string_literal: true -require 'rails_helper' +# require 'rails_helper' -RSpec.describe SurveyProfile, type: :model do - pending "add some examples to (or delete) #{__FILE__}" -end +# RSpec.describe SurveyProfile, type: :model do +# pending "add some examples to (or delete) #{__FILE__}" + +# end diff --git a/rails_root/spec/models/survey_response_spec.rb b/rails_root/spec/models/survey_response_spec.rb index 3e3d457..279c37e 100644 --- a/rails_root/spec/models/survey_response_spec.rb +++ b/rails_root/spec/models/survey_response_spec.rb @@ -1,7 +1,7 @@ -# frozen_string_literal: true +# # frozen_string_literal: true -require 'rails_helper' +# require 'rails_helper' -RSpec.describe SurveyResponse, type: :model do - pending "add some examples to (or delete) #{__FILE__}" -end +# RSpec.describe SurveyResponse, type: :model do +# pending "add some examples to (or delete) #{__FILE__}" +# end diff --git a/rails_root/spec/requests/survey_profiles_spec.rb b/rails_root/spec/requests/survey_profiles_spec.rb index d932f5d..8a66870 100644 --- a/rails_root/spec/requests/survey_profiles_spec.rb +++ b/rails_root/spec/requests/survey_profiles_spec.rb @@ -31,15 +31,14 @@ end let(:invalid_attributes) do - skip('Add a hash of attributes invalid for your model') - - # { - # user_id: 1, - # first_name: nil, - # last_name: nil, - # campus_name: nil, - # district_name: nil - # } + # if any field is nil + { + user_id: 1, + first_name: 'John', + last_name: 'Doe', + campus_name: 'Campus', + district_name: nil + } end describe 'GET /index' do @@ -104,14 +103,24 @@ describe 'PATCH /update' do context 'with valid parameters' do let(:new_attributes) do - skip('Add a hash of attributes valid for your model') + # skip('Add a hash of attributes valid for your model') + { + user_id: 1, + first_name: 'Johnson Micheal', + last_name: 'Doe', + campus_name: 'TAMU', + district_name: 'College Station' + } end it 'updates the requested survey_profile' do survey_profile = SurveyProfile.create! valid_attributes patch survey_profile_url(survey_profile), params: { survey_profile: new_attributes } survey_profile.reload - skip('Add assertions for updated state') + # skip('Add assertions for updated state') + expect(survey_profile.first_name).to eq('Johnson Micheal') + expect(survey_profile.campus_name).to eq('TAMU') + expect(survey_profile.district_name).to eq('College Station') end it 'redirects to the survey_profile' do @@ -128,6 +137,12 @@ patch survey_profile_url(survey_profile), params: { survey_profile: invalid_attributes } expect(response).to have_http_status(:unprocessable_entity) end + + it 'tries to update non-existing profile' do + survey_profile = SurveyProfile.create! valid_attributes + patch survey_profile_url(survey_profile), params: { survey_profile: { user_id: 1000 } } + expect(response).to have_http_status(:unprocessable_entity) + end end end