Skip to content

Commit

Permalink
unit testing for achievement
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSipayung committed Jan 19, 2024
1 parent 5359589 commit 53ddd93
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class User < ApplicationRecord
has_one :parent
has_many :addresses
has_many :languages
has_many :achievements
#validates the user input
validates(:name, presence: true)
validates(:name, length: {maximum: 50})
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@
resources :parents, only: [:new, :create, :edit, :update, :show]
resources :addresses, only: [:new, :create, :edit, :update, :show]
resource :languages, only: [:new, :create, :edit, :update, :show]
resource :achievements, only: [:new, :create, :edit, :update, :show]
end
19 changes: 12 additions & 7 deletions test/fixtures/achievements.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

#one:
# user: michael
# nama_prestasi: MyString
# tahun: 2024-01-16
# tingkat: MyString
# kategori: MyString

first_achievement:
user: michael
nama_prestasi: porseni
tahun: 2024-01-16
tingkat: nasioanl
kategori: akademik
second_achievement:
user: michael
nama_prestasi: paskibra
tahun: 2024-01-16
tingkat: provinsi
kategori: non akademik
7 changes: 4 additions & 3 deletions test/fixtures/languages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ bahasa:
nama_bahasa: MyString
tingkat: MyString

#two:
# nama_bahasa: MyString
# tingkat: MyString
batak:
user: michael
nama_bahasa: batak
tingkat: pemula
40 changes: 40 additions & 0 deletions test/integration/language_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,44 @@ def setup
unpermited_id = Language.find_by_user_id(138974)
assert_nil unpermited_id
end
test "should update language" do
get login_path
post login_path, params: {session: {email: users(:michael).email, password: 'password'}}
get edit_languages_path languages(:bahasa)
assert_template 'languages/edit'
patch languages_path(languages(:bahasa)), params: {language: {
nama_bahasa: 'spanyol', tingkat: 'advanced'
}}
assert_equal 'spanyol', languages(:bahasa).reload.nama_bahasa
assert_equal 'advanced', languages(:bahasa).reload.tingkat
#update multiple times
get edit_languages_path languages(:batak)
assert_template 'languages/edit'
patch languages_path(languages(:batak)), params: {language: {
nama_bahasa: 'batak mandailing', tingkat: 'mahir'
}}
assert_equal 'batak mandailing', languages(:batak).reload.nama_bahasa
assert_equal 'mahir', languages(:batak).reload.tingkat
end
test "should reject to update language" do
get login_path
post login_path, params: {session: {email: users(:michael).email, password: 'password'}}
get edit_languages_path languages(:bahasa)
assert_template 'languages/edit'
patch languages_path(languages(:bahasa)), params: {language: {
nama_bahasa: 'spa', tingkat: 'adv'
}}
assert_not_equal 'spa', languages(:bahasa).reload.nama_bahasa
assert_not_equal 'adv', languages(:bahasa).reload.tingkat
end
test "should not allow unpermited attribute to update" do
get login_path
post login_path, params: {session: {email: users(:michael).email, password: 'password'}}
get edit_languages_path languages(:bahasa)
assert_template 'languages/edit'
patch languages_path(languages(:bahasa)), params: {language: {
nama_bahasa: 'spanyol', tingkat: 'advanced', user_id: 139685142
}}
assert_not_equal 139685142, languages(:bahasa).reload.user_id
end
end

0 comments on commit 53ddd93

Please sign in to comment.