Skip to content

Commit

Permalink
pmdk school information unit test
Browse files Browse the repository at this point in the history
q
  • Loading branch information
MichaelSipayung committed Jan 26, 2024
1 parent 50e9751 commit 001c892
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 27 deletions.
21 changes: 21 additions & 0 deletions app/controllers/pmdk_school_informations_controller.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions app/models/pmdk_school_information.rb
Original file line number Diff line number Diff line change
@@ -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
128 changes: 116 additions & 12 deletions test/controllers/pmdk_school_informations_controller_test.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 6 additions & 2 deletions test/controllers/utbk_school_informations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 12 additions & 10 deletions test/fixtures/pmdk_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
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
38 changes: 35 additions & 3 deletions test/models/pmdk_school_information_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 001c892

Please sign in to comment.