Skip to content

Commit

Permalink
bugs on update usm school information
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSipayung committed Jan 26, 2024
1 parent 9d0dc55 commit 55fc99b
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 122 deletions.
204 changes: 103 additions & 101 deletions .idea/sample_app.iml

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions app/controllers/usm_school_informations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
class UsmSchoolInformationsController < ApplicationController
def new
@usm_school = current_user.build_usm_school_information
end

def update
@usm_school = current_user.usm_school_information
if @usm_school.update(usm_school_information_params)
flash[:success] = "usm school information is updated"
else
render 'edit'
end
end

def create
@usm_school = current_user.build_usm_school_information(usm_school_information_params)
if @usm_school.save
flash[:success] = "usm school information is saved"
else
render 'new'
end
end

def edit
@usm_school = current_user.usm_school_information
end

def show
end
private
def usm_school_information_params
params.require(:usm_school_information).permit(
:jurusan_sekolah, :asal_sekolah, :akreditas, :jumlah_nilai_un,
:jumlah_pelajaran_un, :jumlah_pelajaran_semester_5, :jumlah_nilai_semester_5
)
end
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class User < ApplicationRecord

has_one :utbk_score, dependent: :destroy
has_one :utbk_school_information, dependent: :destroy
has_one :usm_school_information
#validates the user input
validates(:name, presence: true)
validates(:name, length: {maximum: 50})
Expand Down
11 changes: 4 additions & 7 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
Rails.application.routes.draw do
get 'usm_school_informations/new'
get 'usm_school_informations/update'
get 'usm_school_informations/create'
get 'usm_school_informations/edit'
get 'usm_school_informations/show'
resources :accreditation_school_lists
resources :pmdk_school_lists
resources :all_school_lists
Expand Down Expand Up @@ -85,6 +80,8 @@
resources :pmdk_each_score_informations, only: [:new, :create, :edit, :update, :show]
resources :pmdk_total_score_informations, only: [:new, :create, :edit, :update, :show]

resources :utbk_scores, only: [:new, :create, :edit, :update, :show]
resources :utbk_school_informations, only: [:new, :create, :edit, :update, :show]
resources :utbk_scores, only: [:new, :create, :edit, :update, :show]
resources :utbk_school_informations, only: [:new, :create, :edit, :update, :show]
resources :usm_school_informations, only: [:new, :create, :edit, :update, :show]

end
119 changes: 106 additions & 13 deletions test/controllers/usm_school_informations_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,121 @@
require "test_helper"

class UsmSchoolInformationsControllerTest < ActionDispatch::IntegrationTest
test "should get new" do
get usm_school_informations_new_url
def setup
get login_path
post login_path, params: {session: {
email: users(:michael).email, password: 'password'
}}
end
test "should get new usm school information" do
assert is_logged_in?
get new_usm_school_information_url
assert_response :success
end

test "should get update" do
get usm_school_informations_update_url
test "should get edit usm school information" do
get login_path
post login_path, params: {session: {
email: users(:archer).email, password: 'password'
}}
assert is_logged_in?
get edit_usm_school_information_url(usm_school_informations(:usm_one))
assert_response :success
end

test "should get create" do
get usm_school_informations_create_url
test "should create usm school information" do
get new_usm_school_information_url
assert_response :success
assert_difference 'UsmSchoolInformation.count' do
post usm_school_informations_path, params: {
usm_school_information: {
jurusan_sekolah: 'ipa MA', asal_sekolah: 'sma santo tomas 2', akreditas: 'terakreditasi',
jumlah_pelajaran_semester_5: 15, jumlah_nilai_semester_5: 799.9
}
}
end
assert_not_nil UsmSchoolInformation.find_by_jurusan_sekolah 'ipa MA'
assert_not_nil UsmSchoolInformation.find_by_asal_sekolah 'sma santo tomas 2'
assert_not_nil UsmSchoolInformation.find_by_jumlah_pelajaran_semester_5 15
assert_not_nil UsmSchoolInformation.find_by_jumlah_nilai_semester_5 799.9
end

test "should get edit" do
get usm_school_informations_edit_url
test "should reject for invalid usm school information" do
get new_usm_school_information_url
assert_response :success
assert_no_difference 'UsmSchoolInformation.count' do
post usm_school_informations_path, params: {
usm_school_information: {
jurusan_sekolah: 'ipa MA', asal_sekolah: 'sma santo tomas 2', akreditas: 'terakreditasi',
jumlah_pelajaran_semester_5: 15, jumlah_nilai_semester_5: 0
}
}
end
end

test "should get show" do
get usm_school_informations_show_url
test "should create usm school information but reject non permited params" do
get new_usm_school_information_url
assert_response :success
assert_difference 'UsmSchoolInformation.count' do
post usm_school_informations_path, params: {
usm_school_information: {
jurusan_sekolah: 'ipa MA', asal_sekolah: 'sma santo tomas 2', akreditas: 'terakreditasi',
jumlah_pelajaran_semester_5: 15, jumlah_nilai_semester_5: 799.9, user_id: 87999822
}
}
end
assert_nil UsmSchoolInformation.find_by_user_id 87999822
end
test "should update the usm school information" do
get login_path
post login_path, params: {session: {
email: users(:archer).email, password: 'password'
}}
assert is_logged_in?
get edit_usm_school_information_url(usm_school_informations(:usm_one))
assert_response :success
patch usm_school_information_path(usm_school_informations(:usm_one)), params: {
usm_school_information: {
jurusan_sekolah: 'ipa MA', asal_sekolah: 'sma santo tomas 2', akreditas: 'terakreditasi',
jumlah_pelajaran_semester_5: 15, jumlah_nilai_semester_5: 799.9
}
}
usm_school_informations(:usm_one).reload
puts usm_school_informations(:usm_one).reload.jurusan_sekolah
assert_equal 'ipa MA', usm_school_informations(:usm_one).reload.jurusan_sekolah
assert_equal 'sma santo tomas 2', usm_school_informations(:usm_one).asal_sekolah
assert_equal 'terakreditasi', usm_school_informations(:usm_one).akreditas
assert_equal 15, usm_school_informations(:usm_one).jumlah_pelajaran_semester_5
assert_equal 799.9, usm_school_informations(:usm_one).jumlah_nilai_semester_5
end
test "should reject non permited params during update usm school information" do
get login_path
post login_path, params: {session: {
email: users(:archer).email, password: 'password'
}}
assert is_logged_in?
get edit_usm_school_information_url(usm_school_informations(:usm_one))
assert_response :success
patch usm_school_information_path(usm_school_informations(:usm_one)), params: {
usm_school_information: {
jurusan_sekolah: 'ipa MA', asal_sekolah: 'sma santo tomas 2', akreditas: 'terakreditasi',
jumlah_pelajaran_semester_5: 15, jumlah_nilai_semester_5: 799.9, user_id: 9990998
}
}
usm_school_informations(:usm_one).reload
assert_not_equal 9990998, usm_school_informations(:usm_one).user_id
end
test "should reject to update usm school information for invalid data" do
get login_path
post login_path, params: {session: {
email: users(:archer).email, password: 'password'
}}
assert is_logged_in?
get edit_usm_school_information_url(usm_school_informations(:usm_one))
assert_response :success
patch usm_school_information_path(usm_school_informations(:usm_one)), params: {
usm_school_information: {
jurusan_sekolah: 'ipa MA', asal_sekolah: 'sm', akreditas: 'terakreditasi',
jumlah_pelajaran_semester_5: 15, jumlah_nilai_semester_5: 799.9
}
}
assert_not_equal 'sm', usm_school_informations(:usm_one).reload.asal_sekolah
end
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require_relative "../config/environment"
require "rails/test_help"
require "minitest/reporters"
Minitest::Reporters.use!
# Minitest::Reporters.use!
#for convenience, but conflict on rubymine idea

module ActiveSupport
Expand Down

0 comments on commit 55fc99b

Please sign in to comment.