Skip to content

Commit

Permalink
Merge pull request #12 from tamu-edu-students/adding-rspec-tests
Browse files Browse the repository at this point in the history
all implemented rspec tests passing
  • Loading branch information
Andres-L-Santiago authored Feb 16, 2024
2 parents 55e409f + 0a3f8de commit 45a15bf
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 38 deletions.
68 changes: 53 additions & 15 deletions rails_root/app/controllers/survey_profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions rails_root/app/helpers/survey_profiles_helper.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions rails_root/app/helpers/survey_responses_helper.rb
Original file line number Diff line number Diff line change
@@ -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
34 changes: 33 additions & 1 deletion rails_root/spec/helpers/survey_profiles_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 33 additions & 1 deletion rails_root/spec/helpers/survey_responses_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 6 additions & 5 deletions rails_root/spec/models/survey_profile_spec.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 5 additions & 5 deletions rails_root/spec/models/survey_response_spec.rb
Original file line number Diff line number Diff line change
@@ -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
37 changes: 26 additions & 11 deletions rails_root/spec/requests/survey_profiles_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 45a15bf

Please sign in to comment.