Skip to content

Commit

Permalink
unit testing finished for pmdk
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSipayung committed Jan 26, 2024
1 parent b0e8ea0 commit a88da16
Show file tree
Hide file tree
Showing 6 changed files with 475 additions and 122 deletions.
204 changes: 101 additions & 103 deletions .idea/sample_app.iml

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions app/controllers/pmdk_each_score_informations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
class PmdkEachScoreInformationsController < ApplicationController
def new
@pmdk_each_score = current_user.build_pmdk_each_score_information
end

def update
@pmdk_each_score = current_user.pmdk_each_score_information
if @pmdk_each_score.update(pmdk_each_score_information_params)
flash[:success] = "Pmdk each score information is updated"
else
render 'edit'
end
end

def create
@pmdk_each_score = current_user.build_pmdk_each_score_information(pmdk_each_score_information_params)
if @pmdk_each_score.save
flash[:success] = "Pmdk each score information saved"
else
render 'new'
end
end

def edit
@pmdk_each_score = current_user.pmdk_each_score_information
end

def show
end
private
def pmdk_each_score_information_params
params.require(:pmdk_each_score_information).permit(
:matematika_semester_1, :matematika_semester_2, :matematika_semester_3, :matematika_semester_4,
:matematika_semester_5, :kimia_semester_1, :kimia_semester_2, :kimia_semester_3, :kimia_semester_4,
:kimia_semester_5, :fisika_semester_1, :fisika_semester_2, :fisika_semester_3, :fisika_semester_4,
:fisika_semester_5, :bahasa_inggris_semester_1, :bahasa_inggris_semester_2, :bahasa_inggris_semester_3,
:bahasa_inggris_semester_4, :bahasa_inggris_semester_5
)
end
end
6 changes: 6 additions & 0 deletions app/models/pmdk_each_score_information.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
class PmdkEachScoreInformation < ApplicationRecord
belongs_to :user
validates :matematika_semester_1, :matematika_semester_2, :matematika_semester_3, :matematika_semester_4,
:matematika_semester_5, :kimia_semester_1, :kimia_semester_2, :kimia_semester_3, :kimia_semester_4,
:kimia_semester_5, :fisika_semester_1, :fisika_semester_2, :fisika_semester_3, :fisika_semester_4,
:fisika_semester_5, :bahasa_inggris_semester_1, :bahasa_inggris_semester_2, :bahasa_inggris_semester_3,
:bahasa_inggris_semester_4, :bahasa_inggris_semester_5, presence: true,
numericality: {only_integer: false, greater_than_or_equal_to: 2, less_than_or_equal_to: 100}
end
173 changes: 161 additions & 12 deletions test/controllers/pmdk_each_score_informations_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,177 @@
require "test_helper"

class PmdkEachScoreInformationsControllerTest < ActionDispatch::IntegrationTest
test "should get new" do
get pmdk_each_score_informations_new_url
def setup
get login_path
post login_path, params: {session: {
email: users(:michael).email, password: 'password'
}}
end
test "should get new pmdk each score information" do
get new_pmdk_each_score_information_path
assert_response :success
end

test "should get update" do
get pmdk_each_score_informations_update_url
test "should get edit pmdk each score information" do
get edit_pmdk_each_score_information_path(pmdk_each_score_informations(:pmdk_each_score_one))
assert_response :success
end
test "should create new pmdk each score information" do
get new_pmdk_each_score_information_path
assert_response :success
assert_difference 'PmdkEachScoreInformation.count' do
post pmdk_each_score_informations_path, params: {pmdk_each_score_information: {
matematika_semester_1: 90, matematika_semester_2: 80,
matematika_semester_3: 89, matematika_semester_4: 90.8,
matematika_semester_5: 88.9, kimia_semester_1: 98.9,
kimia_semester_2: 98.3, kimia_semester_3: 88.7,
kimia_semester_4: 88.5,kimia_semester_5: 88.1,
fisika_semester_1: 34.9, fisika_semester_2: 99.9,
fisika_semester_3: 88.9, fisika_semester_4: 99.7,
fisika_semester_5: 67.7, bahasa_inggris_semester_1: 88.5,
bahasa_inggris_semester_2: 56.8, bahasa_inggris_semester_3: 77.2,
bahasa_inggris_semester_4: 88.1,bahasa_inggris_semester_5: 99.3
}}
end
assert_not_nil PmdkEachScoreInformation.find_by_user_id users(:michael).id
assert_not_nil PmdkEachScoreInformation.find_by_fisika_semester_1 34.9
assert_not_nil PmdkEachScoreInformation.find_by_fisika_semester_2 99.9
assert_not_nil PmdkEachScoreInformation.find_by_fisika_semester_3 88.9
assert_not_nil PmdkEachScoreInformation.find_by_fisika_semester_4 99.7
assert_not_nil PmdkEachScoreInformation.find_by_fisika_semester_5 67.7

test "should get create" do
get pmdk_each_score_informations_create_url
assert_not_nil PmdkEachScoreInformation.find_by_matematika_semester_1 90
assert_not_nil PmdkEachScoreInformation.find_by_matematika_semester_2 80
assert_not_nil PmdkEachScoreInformation.find_by_matematika_semester_3 89
assert_not_nil PmdkEachScoreInformation.find_by_matematika_semester_4 90.8
assert_not_nil PmdkEachScoreInformation.find_by_matematika_semester_5 88.9

assert_not_nil PmdkEachScoreInformation.find_by_kimia_semester_1 98.9
assert_not_nil PmdkEachScoreInformation.find_by_kimia_semester_2 98.3
assert_not_nil PmdkEachScoreInformation.find_by_kimia_semester_3 88.7
assert_not_nil PmdkEachScoreInformation.find_by_kimia_semester_4 88.5
assert_not_nil PmdkEachScoreInformation.find_by_kimia_semester_5 88.1

assert_not_nil PmdkEachScoreInformation.find_by_bahasa_inggris_semester_1 88.5
assert_not_nil PmdkEachScoreInformation.find_by_bahasa_inggris_semester_2 56.8
assert_not_nil PmdkEachScoreInformation.find_by_bahasa_inggris_semester_3 77.2
assert_not_nil PmdkEachScoreInformation.find_by_bahasa_inggris_semester_4 88.1
assert_not_nil PmdkEachScoreInformation.find_by_bahasa_inggris_semester_5 99.3
end
test "should create new pmdk each score information but reject non-permited params" do
get new_pmdk_each_score_information_path
assert_response :success
assert_difference 'PmdkEachScoreInformation.count' do
post pmdk_each_score_informations_path, params: {pmdk_each_score_information: {
matematika_semester_1: 90, matematika_semester_2: 80,
matematika_semester_3: 89, matematika_semester_4: 90.8,
matematika_semester_5: 88.9, kimia_semester_1: 98.9,
kimia_semester_2: 98.3, kimia_semester_3: 88.7,
kimia_semester_4: 88.5,kimia_semester_5: 88.1,
fisika_semester_1: 34.9, fisika_semester_2: 99.9,
fisika_semester_3: 88.9, fisika_semester_4: 99.7,
fisika_semester_5: 67.7, bahasa_inggris_semester_1: 88.5,
bahasa_inggris_semester_2: 56.8, bahasa_inggris_semester_3: 77.2,
bahasa_inggris_semester_4: 88.1,bahasa_inggris_semester_5: 99.3, user_id: 9888090
}}
end
assert_nil PmdkEachScoreInformation.find_by_user_id 9888090
end

test "should get edit" do
get pmdk_each_score_informations_edit_url
test "should reject to create new pmdk each score information for invalid data" do
get new_pmdk_each_score_information_path
assert_response :success
assert_no_difference 'PmdkEachScoreInformation.count' do
post pmdk_each_score_informations_path, params: {pmdk_each_score_information: {
matematika_semester_1: 90.12, matematika_semester_2: 0,
matematika_semester_3: 89, matematika_semester_4: 90.8,
matematika_semester_5: 88.9, kimia_semester_1: 98.9,
kimia_semester_2: 98.3, kimia_semester_3: 88.7,
kimia_semester_4: 88.5,kimia_semester_5: 88.1,
fisika_semester_1: 34.9, fisika_semester_2: 99.9,
fisika_semester_3: 88.9, fisika_semester_4: 99.7,
fisika_semester_5: 67.7, bahasa_inggris_semester_1: 88.5,
bahasa_inggris_semester_2: 56.8, bahasa_inggris_semester_3: 77.2,
bahasa_inggris_semester_4: 88.1,bahasa_inggris_semester_5: 99.3
}}
end
assert_nil PmdkEachScoreInformation.find_by_matematika_semester_1 90.12
end
test "should update each pmdk score information" do
get login_path
post login_path, params: {session: {
email: users(:archer).email, password: 'password'
}}
get edit_pmdk_each_score_information_path(pmdk_each_score_informations(:pmdk_each_score_one))
assert_response :success
patch pmdk_each_score_information_path(pmdk_each_score_informations(:pmdk_each_score_one)),
params: {pmdk_each_score_information: {
matematika_semester_1: 90.12, matematika_semester_2: 17.8,
matematika_semester_3: 89, matematika_semester_4: 90.8,
matematika_semester_5: 88.9, kimia_semester_1: 98.9,
kimia_semester_2: 98.3, kimia_semester_3: 88.7,
kimia_semester_4: 88.5,kimia_semester_5: 88.1,
fisika_semester_1: 34.9, fisika_semester_2: 99.9,
fisika_semester_3: 88.9, fisika_semester_4: 99.7,
fisika_semester_5: 67.7, bahasa_inggris_semester_1: 88.5,
bahasa_inggris_semester_2: 56.8, bahasa_inggris_semester_3: 77.2,
bahasa_inggris_semester_4: 88.1,bahasa_inggris_semester_5: 99.3
}}
pmdk_each_score_informations(:pmdk_each_score_one).reload
assert_equal 90.12, pmdk_each_score_informations(:pmdk_each_score_one).matematika_semester_1
assert_equal 17.8, pmdk_each_score_informations(:pmdk_each_score_one).matematika_semester_2
assert_equal 89, pmdk_each_score_informations(:pmdk_each_score_one).matematika_semester_3
assert_equal 90.8, pmdk_each_score_informations(:pmdk_each_score_one).matematika_semester_4
assert_equal 88.9, pmdk_each_score_informations(:pmdk_each_score_one).matematika_semester_5

test "should get show" do
get pmdk_each_score_informations_show_url
assert_equal 99.3, pmdk_each_score_informations(:pmdk_each_score_one).bahasa_inggris_semester_5
assert_equal 88.1, pmdk_each_score_informations(:pmdk_each_score_one).bahasa_inggris_semester_4
assert_equal 77.2, pmdk_each_score_informations(:pmdk_each_score_one).bahasa_inggris_semester_3
assert_equal 56.8, pmdk_each_score_informations(:pmdk_each_score_one).bahasa_inggris_semester_2
assert_equal 88.5, pmdk_each_score_informations(:pmdk_each_score_one).bahasa_inggris_semester_1
end
test "should reject to update each pmdk score for invalid data" do
get login_path
post login_path, params: {session: {
email: users(:archer).email, password: 'password'
}}
get edit_pmdk_each_score_information_path(pmdk_each_score_informations(:pmdk_each_score_one))
assert_response :success
patch pmdk_each_score_information_path(pmdk_each_score_informations(:pmdk_each_score_one)),
params: {pmdk_each_score_information: {
matematika_semester_1: 90.12, matematika_semester_2: 17.8,
matematika_semester_3: 89, matematika_semester_4: 90.8,
matematika_semester_5: 88.9, kimia_semester_1: 98.9,
kimia_semester_2: 98.3, kimia_semester_3: 88.7,
kimia_semester_4: 88.5,kimia_semester_5: 88.1,
fisika_semester_1: 34.9, fisika_semester_2: 99.9,
fisika_semester_3: 88.9, fisika_semester_4: 99.7,
fisika_semester_5: 67.7, bahasa_inggris_semester_1: 88.5,
bahasa_inggris_semester_2: 56.8, bahasa_inggris_semester_3: 77.2,
bahasa_inggris_semester_4: 88.1,bahasa_inggris_semester_5: 0
}}
pmdk_each_score_informations(:pmdk_each_score_one).reload
assert_not_equal 0, pmdk_each_score_informations(:pmdk_each_score_one).bahasa_inggris_semester_5
end
test "should reject non-permited params during update each pmdk score" do
get login_path
post login_path, params: {session: {
email: users(:archer).email, password: 'password'
}}
get edit_pmdk_each_score_information_path(pmdk_each_score_informations(:pmdk_each_score_one))
assert_response :success
patch pmdk_each_score_information_path(pmdk_each_score_informations(:pmdk_each_score_one)),
params: {pmdk_each_score_information: {
matematika_semester_1: 90.12, matematika_semester_2: 17.8,
matematika_semester_3: 89, matematika_semester_4: 90.8,
matematika_semester_5: 88.9, kimia_semester_1: 98.9,
kimia_semester_2: 98.3, kimia_semester_3: 88.7,
kimia_semester_4: 88.5,kimia_semester_5: 88.1,
fisika_semester_1: 34.9, fisika_semester_2: 99.9,
fisika_semester_3: 88.9, fisika_semester_4: 99.7,
fisika_semester_5: 67.7, bahasa_inggris_semester_1: 88.5,
bahasa_inggris_semester_2: 56.8, bahasa_inggris_semester_3: 77.2,
bahasa_inggris_semester_4: 88.1,bahasa_inggris_semester_5: 89, user_id: 888999569
}}
pmdk_each_score_informations(:pmdk_each_score_one).reload
assert_not_equal 888999569, pmdk_each_score_informations(:pmdk_each_score_one).user_id
end
end
10 changes: 6 additions & 4 deletions test/fixtures/pmdk_each_score_informations.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

#one:
# matematika_semester_1: MyString
pmdk_each_score_one:
user: archer
matematika_semester_1: MyString

#two:
# matematika_semester_1: MyString
pmdk_each_score_two:
user: iana
matematika_semester_1: MyString
Loading

0 comments on commit a88da16

Please sign in to comment.