Skip to content

Commit

Permalink
- Add tests for StaffProfileAdminCreateForm
Browse files Browse the repository at this point in the history
- Explicitly create staffprofile objects
  • Loading branch information
moshthepitt committed Jun 19, 2018
1 parent 052f44a commit 6f9569e
Showing 1 changed file with 88 additions and 24 deletions.
112 changes: 88 additions & 24 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from small_small_hr.forms import (ApplyLeaveForm, ApplyOverTimeForm, LeaveForm,
OverTimeForm, RoleForm, StaffDocumentForm,
StaffProfileAdminCreateForm,
StaffProfileAdminForm, StaffProfileUserForm)
from small_small_hr.models import Leave, OverTime, StaffProfile
from small_small_hr.serializers import StaffProfileSerializer
Expand Down Expand Up @@ -56,7 +57,7 @@ def test_overtime_form_apply(self):
Test OverTimeForm
"""
user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe')
staffprofile = user.staffprofile
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)

request = self.factory.get('/')
request.session = {}
Expand Down Expand Up @@ -95,7 +96,7 @@ def test_overtime_form_process(self):
Test OverTimeForm
"""
user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe')
staffprofile = user.staffprofile
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)

request = self.factory.get('/')
request.session = {}
Expand Down Expand Up @@ -136,7 +137,7 @@ def test_overtime_form_start_end(self):
Test OverTimeForm start end fields
"""
user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe')
staffprofile = user.staffprofile
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)

request = self.factory.get('/')
request.session = {}
Expand Down Expand Up @@ -168,7 +169,7 @@ def test_leaveform_apply(self):
Test LeaveForm apply for leave
"""
user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe')
staffprofile = user.staffprofile
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)
staffprofile.leave_days = 21
staffprofile.sick_days = 10
staffprofile.save()
Expand Down Expand Up @@ -212,7 +213,7 @@ def test_leaveform_admin(self):
Test LeaveForm apply for leave
"""
user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe')
staffprofile = user.staffprofile
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)
staffprofile.leave_days = 21
staffprofile.sick_days = 10
staffprofile.save()
Expand Down Expand Up @@ -258,7 +259,7 @@ def test_leaveform_process(self):
Test LeaveForm process
"""
user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe')
staffprofile = user.staffprofile
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)
staffprofile.leave_days = 21
staffprofile.sick_days = 10
staffprofile.save()
Expand Down Expand Up @@ -304,7 +305,7 @@ def test_sickleave_apply(self):
Test LeaveForm apply for sick leave
"""
user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe')
staffprofile = user.staffprofile
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)
staffprofile.leave_days = 21
staffprofile.sick_days = 10
staffprofile.save()
Expand Down Expand Up @@ -348,7 +349,7 @@ def test_sickleave_process(self):
Test LeaveForm process sick leave
"""
user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe')
staffprofile = user.staffprofile
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)
staffprofile.leave_days = 21
staffprofile.sick_days = 10
staffprofile.save()
Expand Down Expand Up @@ -394,7 +395,7 @@ def test_leaveform_start_end(self):
Test start and end
"""
user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe')
staffprofile = user.staffprofile
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)
staffprofile.leave_days = 21
staffprofile.sick_days = 10
staffprofile.save()
Expand Down Expand Up @@ -458,7 +459,7 @@ def test_leaveform_max_days(self):
Test leave days sufficient
"""
user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe')
staffprofile = user.staffprofile
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)
staffprofile.leave_days = 21
staffprofile.sick_days = 10
staffprofile.save()
Expand Down Expand Up @@ -501,7 +502,7 @@ def test_leaveform_max_sick_days(self):
Test sick days sufficient
"""
user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe')
staffprofile = user.staffprofile
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)
staffprofile.leave_days = 21
staffprofile.sick_days = 10
staffprofile.save()
Expand Down Expand Up @@ -545,7 +546,7 @@ def test_staffdocumentform(self):
Test StaffDocumentForm
"""
user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe')
staffprofile = user.staffprofile
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)

request = self.factory.get('/')
request.session = {}
Expand Down Expand Up @@ -587,6 +588,7 @@ def test_staff_profile_user_form(self):
Test StaffProfileUserForm
"""
user = mommy.make('auth.User')
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)

request = self.factory.get('/')
request.session = {}
Expand All @@ -607,13 +609,12 @@ def test_staff_profile_user_form(self):
'birthday': '1996-01-27'
}

form = StaffProfileUserForm(data=data, instance=user.staffprofile,
form = StaffProfileUserForm(data=data, instance=staffprofile,
request=request)
self.assertTrue(form.is_valid())
form.save()

user.refresh_from_db()
staffprofile = user.staffprofile

self.assertEqual('Bob Mbugua', user.staffprofile.get_name())
self.assertEqual(StaffProfile.MALE, staffprofile.sex)
Expand All @@ -635,17 +636,81 @@ def test_staff_profile_user_form(self):
self.assertEqual('+254722111111',
staffprofile.data['emergency_contact_number'])

def test_staff_profile_admin_create_form(self):
"""
Test StaffProfileAdminCreateForm
"""
user = mommy.make('auth.User')

request = self.factory.get('/')
request.session = {}
request.user = AnonymousUser()

data = {
'user': user.id,
'first_name': 'Bob',
'last_name': 'Mbugua',
'id_number': '123456789',
'sex': StaffProfile.MALE,
'nhif': '111111',
'nssf': '222222',
'pin_number': 'A0000000Y',
'emergency_contact_name': 'Bob Father',
'emergency_contact_number': '+254722111111',
'phone': '+254722111111',
'address': 'This is the address.',
'birthday': '1996-01-27',
'leave_days': 21,
'sick_days': 9,
'overtime_allowed': True,
'start_date': '2017-09-25',
'end_date': '2018-12-31',
}

form = StaffProfileAdminCreateForm(data=data, request=request)
self.assertTrue(form.is_valid())
staffprofile = form.save()

user.refresh_from_db()

self.assertEqual('Bob Mbugua', user.staffprofile.get_name())
self.assertEqual(StaffProfile.MALE, staffprofile.sex)
self.assertEqual('+254722111111', staffprofile.phone.as_e164)
self.assertEqual(21, staffprofile.leave_days)
self.assertEqual(9, staffprofile.sick_days)
self.assertEqual(True, staffprofile.overtime_allowed)

self.assertEqual('This is the address.', staffprofile.address)
self.assertEqual('1996-01-27', str(staffprofile.birthday))
self.assertEqual('2017-09-25', str(staffprofile.start_date))
self.assertEqual('2018-12-31', str(staffprofile.end_date))

self.assertEqual('123456789',
staffprofile.data['id_number'])
self.assertEqual('111111',
staffprofile.data['nhif'])
self.assertEqual('222222',
staffprofile.data['nssf'])
self.assertEqual('A0000000Y',
staffprofile.data['pin_number'])
self.assertEqual('Bob Father',
staffprofile.data['emergency_contact_name'])
self.assertEqual('+254722111111',
staffprofile.data['emergency_contact_number'])

def test_staff_profile_admin_form(self):
"""
Test StaffProfileAdminForm
"""
user = mommy.make('auth.User')
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)

request = self.factory.get('/')
request.session = {}
request.user = AnonymousUser()

data = {
'user': user.id,
'first_name': 'Bob',
'last_name': 'Mbugua',
'id_number': '123456789',
Expand All @@ -665,13 +730,12 @@ def test_staff_profile_admin_form(self):
'end_date': '2018-12-31',
}

form = StaffProfileAdminForm(data=data, instance=user.staffprofile,
form = StaffProfileAdminForm(data=data, instance=staffprofile,
request=request)
self.assertTrue(form.is_valid())
form.save()

user.refresh_from_db()
staffprofile = user.staffprofile

self.assertEqual('Bob Mbugua', user.staffprofile.get_name())
self.assertEqual(StaffProfile.MALE, staffprofile.sex)
Expand Down Expand Up @@ -703,13 +767,13 @@ def test_staffprofile_unique_pin_number(self):
Test unique pin_number
"""
user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe')
staffprofile = user.staffprofile
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)
staffprofile.data['id_number'] = '123456789'
staffprofile.data['pin_number'] = '123456789'
staffprofile.save()

user2 = mommy.make('auth.User', first_name='Kyle', last_name='Ndoe')
staffprofile2 = user2.staffprofile
staffprofile2 = mommy.make('small_small_hr.StaffProfile', user=user2)
staffprofile2.data['id_number'] = '9999999'
staffprofile2.save()

Expand All @@ -735,12 +799,12 @@ def test_staffprofile_unique_id_number(self):
Test unique id_number
"""
user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe')
staffprofile = user.staffprofile
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)
staffprofile.data['id_number'] = '123456789'
staffprofile.save()

user2 = mommy.make('auth.User', first_name='Kyle', last_name='Ndoe')
staffprofile2 = user2.staffprofile
staffprofile2 = mommy.make('small_small_hr.StaffProfile', user=user2)
staffprofile2.save()

request = self.factory.get('/')
Expand All @@ -765,13 +829,13 @@ def test_staffprofile_unique_nssf(self):
Test unique NSSF
"""
user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe')
staffprofile = user.staffprofile
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)
staffprofile.data['id_number'] = '123456789'
staffprofile.data['nssf'] = '123456789'
staffprofile.save()

user2 = mommy.make('auth.User', first_name='Kyle', last_name='Ndoe')
staffprofile2 = user2.staffprofile
staffprofile2 = mommy.make('small_small_hr.StaffProfile', user=user2)
staffprofile2.data['id_number'] = '9999999'
staffprofile2.save()

Expand All @@ -797,13 +861,13 @@ def test_staffprofile_unique_nhif(self):
Test unique NHIF
"""
user = mommy.make('auth.User', first_name='Bob', last_name='Ndoe')
staffprofile = user.staffprofile
staffprofile = mommy.make('small_small_hr.StaffProfile', user=user)
staffprofile.data['id_number'] = '123456789'
staffprofile.data['nhif'] = '123456789'
staffprofile.save()

user2 = mommy.make('auth.User', first_name='Kyle', last_name='Ndoe')
staffprofile2 = user2.staffprofile
staffprofile2 = mommy.make('small_small_hr.StaffProfile', user=user2)
staffprofile2.data['id_number'] = '9999999'
staffprofile2.save()

Expand Down

0 comments on commit 6f9569e

Please sign in to comment.