From 55fc99bf0b893a7899f9f4b939746f949bc08463 Mon Sep 17 00:00:00 2001 From: Michael Sipayung Date: Fri, 26 Jan 2024 18:08:30 +0700 Subject: [PATCH] bugs on update usm school information --- .idea/sample_app.iml | 204 +++++++++--------- .../usm_school_informations_controller.rb | 21 ++ app/models/user.rb | 1 + config/routes.rb | 11 +- ...usm_school_informations_controller_test.rb | 119 ++++++++-- test/test_helper.rb | 2 +- 6 files changed, 236 insertions(+), 122 deletions(-) diff --git a/.idea/sample_app.iml b/.idea/sample_app.iml index 11cf93d..43da09d 100644 --- a/.idea/sample_app.iml +++ b/.idea/sample_app.iml @@ -313,33 +313,33 @@ + + + - - - - + - + - + - + - + @@ -350,13 +350,13 @@ - + + - @@ -369,99 +369,101 @@ + + + + - - - - - + + - - - - - - - - - - - - - - - - - - + + - + - + + - - - - + - - + + + + - - - - - - - - - - - - - - - - + + + - - + + - - + + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - @@ -475,56 +477,44 @@ - - - - + - + - - - - - - - - - - + - + - - - + + + - - + + - + + - @@ -541,6 +531,7 @@ + @@ -551,20 +542,15 @@ - - - - - + + - - @@ -589,27 +575,43 @@ - + - - - - + + + + - + - + + + + + + + + + + + + + + + + + diff --git a/app/controllers/usm_school_informations_controller.rb b/app/controllers/usm_school_informations_controller.rb index f9470d8..4c8de9b 100644 --- a/app/controllers/usm_school_informations_controller.rb +++ b/app/controllers/usm_school_informations_controller.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 1017957..c462eb0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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}) diff --git a/config/routes.rb b/config/routes.rb index dc8a0b7..02e84b7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 @@ -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 diff --git a/test/controllers/usm_school_informations_controller_test.rb b/test/controllers/usm_school_informations_controller_test.rb index 07ac5ca..b81593a 100644 --- a/test/controllers/usm_school_informations_controller_test.rb +++ b/test/controllers/usm_school_informations_controller_test.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index ca5c420..ce77d48 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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