Skip to content

Commit

Permalink
tdd for source of information, majors
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSipayung committed Jan 22, 2024
1 parent d18aa79 commit ee3a7dc
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 12 deletions.
34 changes: 34 additions & 0 deletions app/controllers/majors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class MajorsController < ApplicationController
def new
@major = current_user.build_major
end

def update
@major = current_user.major
if @major.update(major_params)
flash[:success] = "Major updated"
else
render 'edit'
end
end

def create
@major = current_user.build_major(major_params)
if @major.save
flash[:success] = "Major saved"
else
render 'new'
end
end

def edit
@major = current_user.major
end

def show
end
private
def major_params
params.require(:major).permit(:jurusan_1, :jurusan_2, :jurusan_3, :gelombang)
end
end
4 changes: 2 additions & 2 deletions app/controllers/sources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def new
end

def update
@source = current_user.build_source
@source = current_user.source
if @source.update(source_params)
flash[:success] = "source of information is updated"
else
Expand All @@ -22,7 +22,7 @@ def create
end

def edit
@source = current_user.build_source
@source = current_user.source
end

def show
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/majors_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module MajorsHelper
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class User < ApplicationRecord
has_one :personal
has_one :source
has_one :parent
has_one :major
has_many :addresses
has_many :languages
has_many :achievements
Expand Down
2 changes: 2 additions & 0 deletions app/views/majors/create.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Majors#create</h1>
<p>Find me in app/views/majors/create.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/majors/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Majors#edit</h1>
<p>Find me in app/views/majors/edit.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/majors/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Majors#new</h1>
<p>Find me in app/views/majors/new.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/majors/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Majors#show</h1>
<p>Find me in app/views/majors/show.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/majors/update.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Majors#update</h1>
<p>Find me in app/views/majors/update.html.erb</p>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@
resources :extras, only: [:new, :create, :edit, :update, :show]
resources :organizations, only: [:new, :create, :edit, :update, :show]
resources :sources, only: [:new, :create, :edit, :update, :show]
resources :majors, only: [:new, :create, :edit, :update, :show]


end
24 changes: 24 additions & 0 deletions test/controllers/majors_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "test_helper"

class MajorsControllerTest < ActionDispatch::IntegrationTest
def setup
@user = users(:archer)
get login_path
post login_path, params: {session: {
email: @user.email, password: 'password'
}}
end
test "should get new" do
get new_major_path
assert_response :success
end
test "should get edit" do
@user = users(:michael)
get login_path
post login_path, params: {session: {
email: @user.email, password: 'password'
}}
get edit_major_path majors(:two)
assert_response :success
end
end
22 changes: 12 additions & 10 deletions test/fixtures/majors.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:
# jurusan_1: MyString
# jurusan_2: MyString
# jurusan_3: MyString
# gelombang: MyString
one:
user: michael
jurusan_1: fisika
jurusan_2: biologi
jurusan_3: sains data
gelombang: pmdk sumut

#two:
# jurusan_1: MyString
# jurusan_2: MyString
# jurusan_3: MyString
# gelombang: MyString
two:
user: iana
jurusan_1: teknik informatika
jurusan_2: sistem informasi
jurusan_3: teknik komputer
gelombang: utbk
101 changes: 101 additions & 0 deletions test/integration/major_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
require "test_helper"

class MajorTest < ActionDispatch::IntegrationTest
def setup
get login_path
post login_path, params: {session: {
email: users(:archer).email, password: 'password'
}}
end
test "should create a new major" do
get new_major_path
assert_template 'majors/new'
assert_difference 'Major.count' do
post majors_path, params: {major: {
jurusan_1: 'fisika', jurusan_2: 'biology', jurusan_3: 'math',
gelombang: 'pmdk sumut'
}}
end
assert_not_nil Major.find_by_jurusan_1 'fisika'
assert_not_nil Major.find_by_jurusan_2 'biology'
assert_not_nil Major.find_by_jurusan_3 'math'
assert_not_nil Major.find_by_gelombang 'pmdk sumut'
end
test "should not create major if one major equal to each other" do
get new_major_path
assert_template 'majors/new'
assert_no_difference 'Major.count' do
post majors_path, params: {major: {
jurusan_1: 'fisika 1', jurusan_2: 'fisika 1', jurusan_3: 'math',
gelombang: 'pmdk sumut'
}}
end
assert_nil Major.find_by_jurusan_1 'fisika 1'
end
test "should not create major for invalid information" do
get new_major_path
assert_template 'majors/new'
assert_no_difference 'Major.count' do
post majors_path, params: {major: {
jurusan_1: 'fisika 1', jurusan_2: 'fisika 2', jurusan_3: 'mat',
gelombang: 'pmdk sumut'
}}
end
assert_nil Major.find_by_jurusan_1 'mat'
end
test "should reject non param while create major" do
get new_major_path
assert_template 'majors/new'
assert_difference 'Major.count' do
post majors_path, params: {major: {
jurusan_1: 'fisika 1', jurusan_2: 'fisika 2', jurusan_3: 'math',
gelombang: 'pmdk sumut', user_id: 90989098
}}
end
assert_nil Major.find_by_user_id 90989098
end
test "should update the major" do
get login_path
post login_path, params: {session: {
email: users(:michael).email, password: 'password'
}}
get edit_major_path majors(:one)
assert_template 'majors/edit'
patch major_path(majors(:one)), params: {major: {
jurusan_1: 'fisika 1', jurusan_2: 'fisika 2', jurusan_3: 'math 1',
gelombang: 'pmdk sumut baru'
}}
assert_equal 'fisika 1', majors(:one).reload.jurusan_1
assert_equal 'fisika 2', majors(:one).reload.jurusan_2
assert_equal 'math 1', majors(:one).reload.jurusan_3
assert_equal 'pmdk sumut baru', majors(:one).reload.gelombang
end
test "should reject to update major" do
get login_path
post login_path, params: {session: {
email: users(:michael).email, password: 'password'
}}
get edit_major_path majors(:one)
assert_template 'majors/edit'
patch major_path(majors(:one)), params: {major: {
jurusan_1: '1', jurusan_2: '2', jurusan_3: 'math 1',
gelombang: 'pmdk sumut baru'
}}
assert_nil Major.find_by_jurusan_1 '1'
assert_nil Major.find_by_jurusan_2 '2'
assert_nil Major.find_by_jurusan_3 'math 1'
end
test "should reject to update for non permited params" do
get login_path
post login_path, params: {session: {
email: users(:michael).email, password: 'password'
}}
get edit_major_path majors(:one)
assert_template 'majors/edit'
patch major_path(majors(:one)), params: {major: {
jurusan_1: 'fisika walker', jurusan_2: 'fisika halidaty', jurusan_3: 'math 1',
gelombang: 'pmdk sumut baru', user_id: 19800089
}}
assert_nil Major.find_by_user_id 19800089
end
end
39 changes: 39 additions & 0 deletions test/integration/source_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,43 @@ def setup
assert_not_nil Source.find_by_sumber_informasi 'facebook'
assert_nil Source.find_by_user_id 1231212109
end
test "should update the source" do
@user = users(:michael)
get login_path
post login_path, params: {session: {email: @user.email, password: 'password'}}
get edit_source_path sources(:one)
patch source_path(sources(:one)), params: {source: {
sumber_informasi: 'facebook', jumlah_n: 3, motivasi: 'pendidikan'
}}
assert_equal 'facebook', sources(:one).reload.sumber_informasi
assert_equal 3, sources(:one).reload.jumlah_n
assert_equal 'pendidikan', sources(:one).reload.motivasi
end
test "should update but prevent the non permited params" do
@user = users(:michael)
get login_path
post login_path, params: {session: {email: @user.email, password: 'password'}}
get edit_source_path sources(:one)
patch source_path(sources(:one)), params: {source: {
sumber_informasi: 'facebook', jumlah_n: 3, motivasi: 'pendidikan', user_id: 90890909
}}
assert_equal 'facebook', sources(:one).reload.sumber_informasi
assert_equal 3, sources(:one).reload.jumlah_n
assert_equal 'pendidikan', sources(:one).reload.motivasi
assert_nil Source.find_by_user_id 90890909
end
test "should reject to update for invalid information" do
@user = users(:michael)
get login_path
post login_path, params: {session: {email: @user.email, password: 'password'}}
get edit_source_path sources(:one)
patch source_path(sources(:one)), params: {source: {
sumber_informasi: 'facebook', jumlah_n: 56, motivasi: 'pendidikan'
}}
sources(:one).reload
assert_nil Source.find_by_sumber_informasi 'facebook'
assert_nil Source.find_by_motivasi 'pendidikan'
assert_nil Source.find_by_jumlah_n 20
end

end

0 comments on commit ee3a7dc

Please sign in to comment.