From 001c8922b81a8e6ddfcc47202cd94fbca11c3d1e Mon Sep 17 00:00:00 2001 From: Michael Sipayung Date: Fri, 26 Jan 2024 11:13:53 +0700 Subject: [PATCH] pmdk school information unit test q --- .../pmdk_school_informations_controller.rb | 21 +++ app/models/pmdk_school_information.rb | 9 ++ ...mdk_school_informations_controller_test.rb | 128 ++++++++++++++++-- ...tbk_school_informations_controller_test.rb | 8 +- test/fixtures/pmdk_school_informations.yml | 22 +-- test/models/pmdk_school_information_test.rb | 38 +++++- 6 files changed, 199 insertions(+), 27 deletions(-) diff --git a/app/controllers/pmdk_school_informations_controller.rb b/app/controllers/pmdk_school_informations_controller.rb index b490736..8f37efa 100644 --- a/app/controllers/pmdk_school_informations_controller.rb +++ b/app/controllers/pmdk_school_informations_controller.rb @@ -1,16 +1,37 @@ class PmdkSchoolInformationsController < ApplicationController def new + @pmdk_school_information = current_user.build_pmdk_school_information end def update + @pmdk_school_information = current_user.pmdk_school_information + if @pmdk_school_information.update(pmdk_school_information_params) + flash[:success] = "pmdk school information updated" + else + render 'edit' + end end def create + @pmdk_school_information = current_user.build_pmdk_school_information(pmdk_school_information_params) + if @pmdk_school_information.save + flash[:success] = "pmdk schoool information saved" + else + render 'new' + end end def edit + @pmdk_school_information = current_user.pmdk_school_information end def show end + private + def pmdk_school_information_params + params.require(:pmdk_school_information).permit(:asal_sekolah, + :akreditas, + :jumlah_pelajaran_un, + :jumlah_nilai_un) + end end diff --git a/app/models/pmdk_school_information.rb b/app/models/pmdk_school_information.rb index a6875e1..158e4b6 100644 --- a/app/models/pmdk_school_information.rb +++ b/app/models/pmdk_school_information.rb @@ -1,3 +1,12 @@ class PmdkSchoolInformation < ApplicationRecord belongs_to :user + validates :asal_sekolah, presence: true, length: {minimum: 3, maximum: 50} + validates :akreditas, presence: true, length: {minimum: 1, maximum: 20} + validates :jumlah_nilai_un, presence: false, numericality: { + greater_than_or_equal_to: 2, less_than_or_equal_to: 1000 + } + validates :jumlah_pelajaran_un, presence: false, numericality: { + only_integer: true, greater_than_or_equal_to: 2, + less_than_or_equal_to: 10 + } end diff --git a/test/controllers/pmdk_school_informations_controller_test.rb b/test/controllers/pmdk_school_informations_controller_test.rb index b774e63..1e6f30e 100644 --- a/test/controllers/pmdk_school_informations_controller_test.rb +++ b/test/controllers/pmdk_school_informations_controller_test.rb @@ -1,28 +1,132 @@ require "test_helper" class PmdkSchoolInformationsControllerTest < ActionDispatch::IntegrationTest + def setup + get login_path + post login_path, params: {session: { + email: users(:michael).email, password: 'password' + }} + end test "should get new" do - get pmdk_school_informations_new_url + get new_pmdk_school_information_path assert_response :success end - test "should get update" do - get pmdk_school_informations_update_url + test "should update current pmdk school information" do + get login_path + post login_path, params: {session: { + email: users(:archer).email, password: 'password' + }} + assert is_logged_in? + get edit_pmdk_school_information_path(pmdk_school_informations(:pmdk_sc_one)) assert_response :success + patch pmdk_school_information_path(pmdk_school_informations(:pmdk_sc_one)), + params: {pmdk_school_information: { + asal_sekolah: 'sma juanda timur', + akreditas: 'Unggul', + jumlah_nilai_un: 90.78, + jumlah_pelajaran_un: 9 + }} + assert_equal 'sma juanda timur', pmdk_school_informations(:pmdk_sc_one).reload.asal_sekolah + assert_equal 'Unggul', pmdk_school_informations(:pmdk_sc_one).reload.akreditas + assert_equal 90.78, pmdk_school_informations(:pmdk_sc_one).reload.jumlah_nilai_un + assert_equal 9, pmdk_school_informations(:pmdk_sc_one).reload.jumlah_pelajaran_un end - - test "should get create" do - get pmdk_school_informations_create_url + test "should reject to update pmdk school information for invalid data" do + get login_path + post login_path, params: {session: { + email: users(:iana).email, password: 'password' + }} + assert is_logged_in? + get edit_pmdk_school_information_path(pmdk_school_informations(:pmdk_sc_two)) assert_response :success + patch pmdk_school_information_path(pmdk_school_informations(:pmdk_sc_two)), + params: {pmdk_school_information: { + asal_sekolah: 'sma juanda timur', + akreditas: 'Unggul', + jumlah_nilai_un: 90.78, + jumlah_pelajaran_un: 0 + }} + assert_not_equal 0, pmdk_school_informations(:pmdk_sc_two).reload.jumlah_pelajaran_un end - - test "should get edit" do - get pmdk_school_informations_edit_url + test "should create new pmdk school information" do + get new_pmdk_school_information_path assert_response :success + assert_difference 'PmdkSchoolInformation.count' do + post pmdk_school_informations_path, params: { + pmdk_school_information: { + asal_sekolah: 'sma juanda timur', + akreditas: 'Unggul', + jumlah_nilai_un: 90.78, + jumlah_pelajaran_un: 9 + } + } + end + assert_not_nil PmdkSchoolInformation.find_by_asal_sekolah 'sma juanda timur' + assert_not_nil PmdkSchoolInformation.find_by_akreditas 'Unggul' + assert_not_nil PmdkSchoolInformation.find_by_jumlah_nilai_un 90.78 + assert_not_nil PmdkSchoolInformation.find_by_jumlah_pelajaran_un 9 end - - test "should get show" do - get pmdk_school_informations_show_url + test "should reject for non permited params-pmdk school" do + get new_pmdk_school_information_path + assert_response :success + assert_difference 'PmdkSchoolInformation.count' do + post pmdk_school_informations_path, params: { + pmdk_school_information: { + asal_sekolah: 'sma juanda timur', + akreditas: 'Unggul', + jumlah_nilai_un: 90.78, + jumlah_pelajaran_un: 9, + user_id: 90900099 + } + } + end + assert_nil PmdkSchoolInformation.find_by_user_id 90900099 + end + test "should reject for invalid information-pmdk school" do + get new_pmdk_school_information_path + assert_response :success + assert_no_difference 'PmdkSchoolInformation.count' do + post pmdk_school_informations_path, params: { + pmdk_school_information: { + asal_sekolah: 'sma juanda timur', + akreditas: 'Unggul', + jumlah_nilai_un: 90.78, + jumlah_pelajaran_un: 0, + } + } + end + assert_nil PmdkSchoolInformation.find_by_jumlah_nilai_un 90.78 + end + test "should get edit pmdk school information" do + get login_path + post login_path, params: {session: { + email: users(:archer).email, password: 'password' + }} + assert is_logged_in? + get edit_pmdk_school_information_path(pmdk_school_informations(:pmdk_sc_one)) + assert_response :success + end + test "should reject for non permited params during update" do + get login_path + post login_path, params: {session: { + email: users(:archer).email, password: 'password' + }} + get edit_pmdk_school_information_path(pmdk_school_informations(:pmdk_sc_one)) assert_response :success + patch pmdk_school_information_path(pmdk_school_informations(:pmdk_sc_one)), + params: {pmdk_school_information: { + asal_sekolah: 'sma juanda timur', + akreditas: 'Unggul', + jumlah_nilai_un: 90.78, + jumlah_pelajaran_un: 9, + user_id: 90900091 + }} + assert_not_equal 90900091, pmdk_school_informations(:pmdk_sc_one).reload.user_id end + # + # test "should get show" do + # get pmdk_school_informations_show_url + # assert_response :success + # end end diff --git a/test/controllers/utbk_school_informations_controller_test.rb b/test/controllers/utbk_school_informations_controller_test.rb index 458676d..397aff3 100644 --- a/test/controllers/utbk_school_informations_controller_test.rb +++ b/test/controllers/utbk_school_informations_controller_test.rb @@ -45,7 +45,7 @@ def setup jumlah_nilai_un: 200.5, akreditas: 'Baik sekali', user_id: 98777908 } } - assert_nil UtbkSchoolInformation.find_by_user_id 98777908 + assert_not_equal 98777908, utbk_school_informations(:utbk_sc_one).reload.user_id end test "should reject update the utbk school for invalid information" do get login_path @@ -61,7 +61,7 @@ def setup jumlah_nilai_un: 200.5, akreditas: ' ' } } - assert_nil UtbkSchoolInformation.find_by_asal_sekolah 'sma 9 balige' + assert_not_equal 'sma 9 balige', utbk_school_informations(:utbk_sc_one).reload.user_id end test "should create utbk school information" do get new_utbk_school_information_path @@ -106,6 +106,10 @@ def setup assert_nil UtbkSchoolInformation.find_by_user_id 98978 end test "should get edit - utbk school information" do + get login_path + post login_path, params: {session: { + email: users(:archer).email, password: 'password' + }} get edit_utbk_school_information_url(utbk_school_informations(:utbk_sc_one)) assert_response :success end diff --git a/test/fixtures/pmdk_school_informations.yml b/test/fixtures/pmdk_school_informations.yml index 7814065..baceef0 100644 --- a/test/fixtures/pmdk_school_informations.yml +++ b/test/fixtures/pmdk_school_informations.yml @@ -1,13 +1,15 @@ # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html -#one: -# asal_sekolah: MyString -# akreditas: MyString -# jumlah_pelajaran_un: MyString -# jumlah_nilai_un: MyString +pmdk_sc_one: + user: archer + asal_sekolah: MyString + akreditas: MyString + jumlah_pelajaran_un: 2 + jumlah_nilai_un: 89.69 -#two: -# asal_sekolah: MyString -# akreditas: MyString -# jumlah_pelajaran_un: MyString -# jumlah_nilai_un: MyString +pmdk_sc_two: + user: iana + asal_sekolah: MyString + akreditas: MyString + jumlah_pelajaran_un: 7 + jumlah_nilai_un: 63.89 diff --git a/test/models/pmdk_school_information_test.rb b/test/models/pmdk_school_information_test.rb index c7d71ac..52f51d9 100644 --- a/test/models/pmdk_school_information_test.rb +++ b/test/models/pmdk_school_information_test.rb @@ -1,7 +1,39 @@ require "test_helper" class PmdkSchoolInformationTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + def setup + @pmdk_information = PmdkSchoolInformation.new( + user_id: users(:michael).id, asal_sekolah: 'sma tarutung', + jumlah_pelajaran_un: 5, jumlah_nilai_un: 50.5, akreditas: 'A' + ) + end + test "should be a valid utbk information" do + assert @pmdk_information.valid? + end + test "should reject for invalid asal sekolah - utbk" do + @pmdk_information.asal_sekolah='a'*2 + assert_not @pmdk_information.valid? + @pmdk_information.asal_sekolah=' ' + assert_not @pmdk_information.valid? + @pmdk_information.asal_sekolah='a'*51 + assert_not @pmdk_information.valid? + end + test "should reject for invalid jumlah nilai un-utbk" do + @pmdk_information.jumlah_nilai_un= 1000.5 + assert_not @pmdk_information.valid? + @pmdk_information.jumlah_nilai_un= -12 + assert_not @pmdk_information.valid? + end + test "should reject for invalid jumlah pelajaran un-utbk" do + @pmdk_information.jumlah_pelajaran_un = 50 + assert_not @pmdk_information.valid? + @pmdk_information.jumlah_pelajaran_un = -1 + assert_not @pmdk_information.valid? + end + test "should reject for invalid akreditas-utbk" do + @pmdk_information.akreditas = 'a'*21 + assert_not @pmdk_information.valid? + @pmdk_information.akreditas = ' ' + assert_not @pmdk_information.valid? + end end