Skip to content

Commit

Permalink
integration test on parent
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSipayung committed Jan 18, 2024
1 parent b6d339b commit ccf3fa3
Show file tree
Hide file tree
Showing 15 changed files with 400 additions and 255 deletions.
474 changes: 240 additions & 234 deletions .idea/sample_app.iml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source "https://rubygems.org"

ruby "3.1.4"
ruby "3.2.2"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.1.2"
Expand Down
36 changes: 36 additions & 0 deletions app/controllers/parents_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class ParentsController < ApplicationController
def new
@parent = current_user.build_parent
end

def update
@parent = current_user.parent
if @parent.update(parent_params)
flash[:success] = "Parent data updated"
else
render 'edit'
end
end

def create
@parent = current_user.build_parent(parent_params)
if @parent.save
flash[:success] = "Parent data saved"
else
render 'new'
end
end

def edit
@parent = current_user.parent
end

def show
end
private
def parent_params
params.require(:parent).permit(:nama_ayah, :nama_ibu, :nik_ayah,:nik_ibu, :pendidikan_ayah,
:pendidikan_ibu, :tanggal_lahir_ayah, :tanggal_lahir_ibu,
:pekerjaan_ayah, :pekerjaan_ibu)
end
end
2 changes: 2 additions & 0 deletions app/helpers/parents_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ParentsHelper
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class User < ApplicationRecord
# before user created, using before_create callback
before_create :create_activation_digest
has_one :personal
has_one :parent
#validates the user input
validates(:name, presence: true)
validates(:name, length: {maximum: 50})
Expand Down
2 changes: 2 additions & 0 deletions app/views/parents/create.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Parents#create</h1>
<p>Find me in app/views/parents/create.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/parents/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Parents#edit</h1>
<p>Find me in app/views/parents/edit.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/parents/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Parents#new</h1>
<p>Find me in app/views/parents/new.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/parents/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Parents#show</h1>
<p>Find me in app/views/parents/show.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/parents/update.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Parents#update</h1>
<p>Find me in app/views/parents/update.html.erb</p>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@
resources :microposts, only: [:create, :destroy]
resources :relationships, only: [:create, :destroy]
resources :personals, only: [:new, :create, :edit, :update, :show]
resources :parents, only: [:new, :create, :edit, :update, :show]
end
19 changes: 19 additions & 0 deletions test/controllers/parents_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "test_helper"

class ParentsControllerTest < ActionDispatch::IntegrationTest
def setup
get login_path
post login_path, params: {session: {email: users(:archer).email, password: 'password'}}
end
test "should get new" do
assert is_logged_in?
get new_parent_path
assert_response :success
end

test "should get edit" do
assert is_logged_in?
get edit_parent_path(parents(:myparent))
assert_response :success
end
end
23 changes: 12 additions & 11 deletions test/fixtures/parents.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

#one:
# nama_ayah: MyString
# nama_ibu: MyString
# nik_ayah: 1
# nik_ibu: 1
# pendidikan_ayah: MyString
# pendidikan_ibu: MyString
# tanggal_lahir_ayah: 2024-01-16
# tanggal_lahir_ibu: 2024-01-16
# pekerjaan_ayah: MyString
# pekerjaan_ibu: MyString
myparent:
user: michael
nama_ayah: jono
nama_ibu: nisa
nik_ayah: 1234567896541235
nik_ibu: 1234567896541235
pendidikan_ayah: sma
pendidikan_ibu: slta
tanggal_lahir_ayah: 2024-01-16
tanggal_lahir_ibu: 2024-01-16
pekerjaan_ayah: MyString
pekerjaan_ibu: MyString

#two:
# nama_ayah: MyString
Expand Down
61 changes: 58 additions & 3 deletions test/integration/parent_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,62 @@
require "test_helper"

class ParentTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
def setup
get login_path
post login_path, params: {session: {email: users(:michael).email, password: 'password'}}
end
test "should not create new parent data for invalid data" do
@user = users(:archer)
get login_path
post login_path, params: {session: {email: @user.email, password: 'password'}}
get new_parent_path
assert_template 'parents/new'
assert_no_difference 'Parent.count', -1 do
post parents_path, params: {parent: {nama_ayah: 'example', nama_ibu: 'example', nik_ayah: '123', nik_ibu: '1234',
pendidikan_ayah: 'sma', pendidikan_ibu: 'slta', tanggal_lahir_ayah: '2020-12-11',
tanggal_lahir_ibu: '2013-12-10', pekerjaan_ayah: 'buruh', pekerjaan_ibu: 'guru'}}
end
assert_template 'parents/new'
end
test "should create new parent data for valid valid information" do
@user = users(:archer)
get login_path
post login_path, params: {session: {email: @user.email, password: 'password'}}
get new_parent_path
assert_template 'parents/new'
assert_difference 'Parent.count' do
post parents_path, params: {parent: {
nama_ayah: 'example', nama_ibu: 'example', nik_ayah: '1234567890121345', nik_ibu: '1234567890123415',
pendidikan_ayah: 'sma', pendidikan_ibu: 'slta', tanggal_lahir_ayah: '2020-12-11',
tanggal_lahir_ibu: '2013-12-10', pekerjaan_ayah: 'buruh', pekerjaan_ibu: 'guru'
}}
end
assert_response :success
end
test "should update data for valid information" do
get edit_parent_path(parents(:myparent))
assert_template 'parents/edit'
patch parent_path(parents(:myparent)), params: { parent: {
nama_ayah: 'daniel', nama_ibu: 'nisa', nik_ayah: '1234567890121345', nik_ibu: '1234567890123415',
pendidikan_ayah: 'sma', pendidikan_ibu: 'slta', tanggal_lahir_ayah: '2020-12-11',
tanggal_lahir_ibu: '2013-12-10', pekerjaan_ayah: 'Tentara', pekerjaan_ibu: 'guru sd'
}}
assert_equal 'daniel', parents(:myparent).reload.nama_ayah
assert_equal 'nisa', parents(:myparent).reload.nama_ibu
assert_equal 'Tentara', parents(:myparent).reload.pekerjaan_ayah
assert_equal 'guru sd', parents(:myparent).reload.pekerjaan_ibu
end
test "should not update data for invalid information" do
get edit_parent_path(parents(:myparent))
assert_template 'parents/edit'
patch parent_path(parents(:myparent)), params: { parent: {
nama_ayah: 'daniel', nama_ibu: 'nisa bana', nik_ayah: '1234567890121345', nik_ibu: '1234567890123415',
pendidikan_ayah: '', pendidikan_ibu: '', tanggal_lahir_ayah: '2020-12-11',
tanggal_lahir_ibu: '2013-12-10', pekerjaan_ayah: 'Tentara', pekerjaan_ibu: 'guru sd'
}}
assert_template 'parents/edit'
assert_equal 'sma', parents(:myparent).reload.pendidikan_ayah
assert_equal 'slta', parents(:myparent).reload.pendidikan_ibu
assert_equal 'nisa', parents(:myparent).reload.nama_ibu
end
end
26 changes: 20 additions & 6 deletions test/integration/personal_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

class PersonalTest < ActionDispatch::IntegrationTest
def setup
@user = users(:archer)
get login_path
post login_path, params: {session: {email: users(:michael), password: 'password'}}
# @personal = personals(:one)
get login_path
post login_path, params: { session: { email: users(:michael).email, password: 'password' } }
end
test "should create personal" do
test "should create personal for valid information" do
@user = users(:archer)

get login_path
post login_path, params: { session: { email: @user.email, password: 'password' } }
get new_personal_path
Expand All @@ -18,7 +19,8 @@ def setup
tempat_lahir: "Medan", jenis_kelamin: "Laki-laki", domisili: "Medan"}}
end
end
test "should not create personal" do
test "should not create personal for invalid data" do
@user = users(:archer)
get login_path
post login_path, params: { session: { email: @user.email, password: 'password' } }
get new_personal_path
Expand All @@ -29,8 +31,20 @@ def setup
tempat_lahir: "Medan", jenis_kelamin: "Laki-laki", domisili: "Medan"}}
end
end
test "should update personal" do
test "success update or edit data" do
get edit_personal_path(personals(:one))
assert_template 'personals/edit'
assert_response :success
patch personal_path(personals(:one)), params: {personal: {nama_lengkap: "Michael januari", agama: "Muslim", nik: "1234567890123456",
nisn: "1234567890", no_kps: "123456", tanggal_lahir: "1999-12-12",
tempat_lahir: "Medan", jenis_kelamin: "Laki-laki", domisili: "Medan"}}
assert_equal "Michael januari", personals(:one).reload.nama_lengkap
assert_equal "Muslim", personals(:one).reload.agama
end
test "should not update or edit data for invalid data" do
get edit_personal_path(personals(:one))
assert_template 'personals/edit'
patch personal_path(personals(:one)), params: {personal: {nama_lengkap: 'di'}}
assert_template 'personals/edit'
end
end

0 comments on commit ccf3fa3

Please sign in to comment.