Skip to content

Commit

Permalink
utbk information unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSipayung committed Jan 26, 2024
1 parent 865bc1c commit 50e9751
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 37 deletions.
20 changes: 20 additions & 0 deletions app/controllers/utbk_school_informations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
class UtbkSchoolInformationsController < ApplicationController
def new
@utbk_school_information = current_user.build_utbk_school_information
end

def update
@utbk_school_information = current_user.utbk_school_information
if @utbk_school_information.update(utbk_school_information_params)
flash[:success] = "UTBK School information saved"
else
render 'edit'
end
end

def create
@utbk_school_information = current_user.build_utbk_school_information(utbk_school_information_params)
if @utbk_school_information.save
flash[:success] = "UTBK School information is saved"
else
render 'new'
end
end

def edit
@utbk_school_information = current_user.utbk_school_information
end

def show
end
private
def utbk_school_information_params
params.require(:utbk_school_information).permit(:asal_sekolah, :akreditas,
:jumlah_pelajaran_un,
:jumlah_nilai_un)
end
end
10 changes: 10 additions & 0 deletions app/models/utbk_school_information.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
class UtbkSchoolInformation < 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
115 changes: 102 additions & 13 deletions test/controllers/utbk_school_informations_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,117 @@
require "test_helper"

class UtbkSchoolInformationsControllerTest < ActionDispatch::IntegrationTest
test "should get new" do
get utbk_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 - utbk school information" do
get new_utbk_school_information_path
assert_response :success
end

test "should get update" do
get utbk_school_informations_update_url
test "should update - utbk school information" do
get login_path
post login_path, params: {session: {
email: users(:archer).email, password: 'password'
}}
assert is_logged_in?
get edit_utbk_school_information_url(utbk_school_informations(:utbk_sc_one))
assert_response :success
patch utbk_school_information_path(utbk_school_informations(:utbk_sc_one)), params: {
utbk_school_information: {
asal_sekolah: 'sma 2 balige', jumlah_pelajaran_un: 3,
jumlah_nilai_un: 200.5, akreditas: 'Baik sekali'
}
}
assert_equal 'sma 2 balige', utbk_school_informations(:utbk_sc_one).reload.asal_sekolah
assert_equal 'Baik sekali', utbk_school_informations(:utbk_sc_one).reload.akreditas
assert_equal 3, utbk_school_informations(:utbk_sc_one).reload.jumlah_pelajaran_un
assert_equal 200.5, utbk_school_informations(:utbk_sc_one).reload.jumlah_nilai_un
end

test "should get create" do
get utbk_school_informations_create_url
test "should reject update non permited params-utbk school" do
get login_path
post login_path, params: {session: {
email: users(:archer).email, password: 'password'
}}
assert is_logged_in?
get edit_utbk_school_information_url(utbk_school_informations(:utbk_sc_one))
assert_response :success
patch utbk_school_information_path(utbk_school_informations(:utbk_sc_one)), params: {
utbk_school_information: {
asal_sekolah: 'sma 2 balige', jumlah_pelajaran_un: 3,
jumlah_nilai_un: 200.5, akreditas: 'Baik sekali', user_id: 98777908
}
}
assert_nil UtbkSchoolInformation.find_by_user_id 98777908
end

test "should get edit" do
get utbk_school_informations_edit_url
test "should reject update the utbk school for invalid information" do
get login_path
post login_path, params: {session: {
email: users(:archer).email, password: 'password'
}}
assert is_logged_in?
get edit_utbk_school_information_url(utbk_school_informations(:utbk_sc_one))
assert_response :success
patch utbk_school_information_path(utbk_school_informations(:utbk_sc_one)), params: {
utbk_school_information: {
asal_sekolah: 'sma 9 balige', jumlah_pelajaran_un: 3,
jumlah_nilai_un: 200.5, akreditas: ' '
}
}
assert_nil UtbkSchoolInformation.find_by_asal_sekolah 'sma 9 balige'
end

test "should get show" do
get utbk_school_informations_show_url
test "should create utbk school information" do
get new_utbk_school_information_path
assert_response :success
assert_difference 'UtbkSchoolInformation.count' do
post utbk_school_informations_path, params: {
utbk_school_information: {
asal_sekolah: 'sma 2 tarutung', jumlah_pelajaran_un: 9,
jumlah_nilai_un: 100.5, akreditas: 'Baik'
}
}
end
assert_not_nil UtbkSchoolInformation.find_by_asal_sekolah 'sma 2 tarutung'
assert_not_nil UtbkSchoolInformation.find_by_akreditas 'Baik'
assert_not_nil UtbkSchoolInformation.find_by_jumlah_pelajaran_un 9
assert_not_nil UtbkSchoolInformation.find_by_jumlah_nilai_un 100.5
end
test "should reject to create - utbk school information" do
get new_utbk_school_information_path
assert_response :success
assert_no_difference 'UtbkSchoolInformation.count' do
post utbk_school_informations_path, params: {
utbk_school_information: {
asal_sekolah: 'sm', jumlah_pelajaran_un: 9,
jumlah_nilai_un: 100.5, akreditas: 'Baik'
}
}
end
assert_nil UtbkSchoolInformation.find_by_asal_sekolah 'sm'
end
test "should reject to create non permited params-utbk school" do
get new_utbk_school_information_path
assert_response :success
assert_difference 'UtbkSchoolInformation.count' do
post utbk_school_informations_path, params: {
utbk_school_information: {
asal_sekolah: 'sma 1 nauli', jumlah_pelajaran_un: 9,
jumlah_nilai_un: 100.5, akreditas: 'Baik', user_id: 98978
}
}
end
assert_nil UtbkSchoolInformation.find_by_user_id 98978
end
test "should get edit - utbk school information" do
get edit_utbk_school_information_url(utbk_school_informations(:utbk_sc_one))
assert_response :success
end

# test "should get show" do
# get utbk_school_informations_show_url
# assert_response :success
# end
end
89 changes: 79 additions & 10 deletions test/controllers/utbk_scores_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,96 @@ def setup
email: users(:michael).email, password: 'password'
}}
end
test "should get new" do
test "should get new score" do
assert is_logged_in?
get new_utbk_score_url
assert_response :success
end

# test "should get update" do
# get utbk_scores_update_url
# assert_response :success
# end
#
# test "should get create" do
# get utbk_scores_create_url
# assert_response :success
# end
test "should update the current logged in user score utbk" do
get login_path
post login_path, params: {session: {
email: users(:archer).email, password: 'password'
}}
get edit_utbk_score_url utbk_scores(:utbk_score_one)
assert_response :success
patch utbk_score_path, params: {
utbk_score: {no_peserta: '200-1bx-2', tanggal_ujian: '2021-12-11',
jumlah_nilai_semester_6: 1000.5, jumlah_pelajaran_semester_6: 15,
nilai_penalaran_umum: 190.5,
nilai_pengetahuan_kuantitatif: 110.5,
nilai_kemampuan_memahami_bacaan_dan_menulis: 100.5,
nilai_pengetahuan_dan_pemahaman_umum: 150.5
}}
assert_equal '200-1bx-2', utbk_scores(:utbk_score_one).reload.no_peserta
assert_equal 1000.5, utbk_scores(:utbk_score_one).jumlah_nilai_semester_6
assert_equal 15, utbk_scores(:utbk_score_one).jumlah_pelajaran_semester_6

assert_equal 190.5, utbk_scores(:utbk_score_one).reload.nilai_penalaran_umum
assert_equal 110.5, utbk_scores(:utbk_score_one).reload.nilai_pengetahuan_kuantitatif
assert_equal 100.5, utbk_scores(:utbk_score_one).reload.nilai_kemampuan_memahami_bacaan_dan_menulis
assert_equal 150.5, utbk_scores(:utbk_score_one).reload.nilai_pengetahuan_dan_pemahaman_umum
end
test "should create new score for current login user" do
get new_utbk_score_path
assert_response :success
assert_difference 'UtbkScore.count' do
post utbk_scores_path, params: {
utbk_score: {no_peserta: '200-1bx-2', tanggal_ujian: '2020-12-11',
jumlah_nilai_semester_6: 1000.5, jumlah_pelajaran_semester_6: 15,
nilai_penalaran_umum: 190.5,
nilai_pengetahuan_kuantitatif: 110.5,
nilai_kemampuan_memahami_bacaan_dan_menulis: 100.5,
nilai_pengetahuan_dan_pemahaman_umum: 150.5
}
}
end
assert_not_nil UtbkScore.find_by_no_peserta '200-1bx-2'
assert_not_nil UtbkScore.find_by_jumlah_pelajaran_semester_6 15
assert_not_nil UtbkScore.find_by_jumlah_nilai_semester_6 1000.5
assert_not_nil UtbkScore.find_by_nilai_penalaran_umum 190.5
assert_not_nil UtbkScore.find_by_nilai_pengetahuan_kuantitatif 110.5
assert_not_nil UtbkScore.find_by_nilai_kemampuan_memahami_bacaan_dan_menulis 100.5
assert_not_nil UtbkScore.find_by_nilai_pengetahuan_dan_pemahaman_umum 150.5
end
#
test "should get edit" do
assert is_logged_in?
get edit_utbk_score_url(utbk_scores(:utbk_score_two))
assert_response :success
end
test "should reject non permited params while created utbk score" do
get new_utbk_score_path
assert_response :success
assert_difference 'UtbkScore.count' do
post utbk_scores_path, params: {
utbk_score: {no_peserta: '200-1bx-2', tanggal_ujian: '2020-12-11',
jumlah_nilai_semester_6: 1000.5, jumlah_pelajaran_semester_6: 15,
nilai_penalaran_umum: 190.5,
nilai_pengetahuan_kuantitatif: 110.5,
nilai_kemampuan_memahami_bacaan_dan_menulis: 100.5,
nilai_pengetahuan_dan_pemahaman_umum: 150.5, user_id: 9000098
}
}
end
assert_nil UtbkScore.find_by_user_id 9000098
end
test "should reject non permited params while update the utbk score" do
get new_utbk_score_path
assert_response :success
assert_difference 'UtbkScore.count' do
post utbk_scores_path, params: {
utbk_score: {no_peserta: '200-1bx-2', tanggal_ujian: '2020-12-11',
jumlah_nilai_semester_6: 1000.5, jumlah_pelajaran_semester_6: 15,
nilai_penalaran_umum: 190.5,
nilai_pengetahuan_kuantitatif: 110.5,
nilai_kemampuan_memahami_bacaan_dan_menulis: 100.5,
nilai_pengetahuan_dan_pemahaman_umum: 150.5, user_id: 90898008
}
}
end
assert_nil UtbkScore.find_by_user_id 90898008
end
#
# test "should get show" do
# get utbk_scores_show_url
Expand Down
22 changes: 12 additions & 10 deletions test/fixtures/utbk_school_informations.yml
Original file line number Diff line number Diff line change
@@ -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
utbk_sc_one:
user: archer
asal_sekolah: MyString
akreditas: MyString
jumlah_pelajaran_un: 2
jumlah_nilai_un: 90.8

#two:
# asal_sekolah: MyString
# akreditas: MyString
# jumlah_pelajaran_un: MyString
# jumlah_nilai_un: MyString
utbk_sc_two:
user: iana
asal_sekolah: MyString
akreditas: MyString
jumlah_pelajaran_un: 4
jumlah_nilai_un: 90.9
38 changes: 35 additions & 3 deletions test/models/utbk_school_information_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
require "test_helper"

class UtbkSchoolInformationTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
def setup
@utbk_information = UtbkSchoolInformation.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 @utbk_information.valid?
end
test "should reject for invalid asal sekolah - utbk" do
@utbk_information.asal_sekolah='a'*2
assert_not @utbk_information.valid?
@utbk_information.asal_sekolah=' '
assert_not @utbk_information.valid?
@utbk_information.asal_sekolah='a'*51
assert_not @utbk_information.valid?
end
test "should reject for invalid jumlah nilai un-utbk" do
@utbk_information.jumlah_nilai_un= 1000.5
assert_not @utbk_information.valid?
@utbk_information.jumlah_nilai_un= -12
assert_not @utbk_information.valid?
end
test "should reject for invalid jumlah pelajaran un-utbk" do
@utbk_information.jumlah_pelajaran_un = 50
assert_not @utbk_information.valid?
@utbk_information.jumlah_pelajaran_un = -1
assert_not @utbk_information.valid?
end
test "should reject for invalid akreditas-utbk" do
@utbk_information.akreditas = 'a'*21
assert_not @utbk_information.valid?
@utbk_information.akreditas = ' '
assert_not @utbk_information.valid?
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 50e9751

Please sign in to comment.