Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Commit

Permalink
Tests for Job app
Browse files Browse the repository at this point in the history
  • Loading branch information
Monal5031 committed Jun 14, 2018
1 parent 67b84ee commit 992e40d
Show file tree
Hide file tree
Showing 6 changed files with 355 additions and 44 deletions.
2 changes: 1 addition & 1 deletion vms/job/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ class Job(models.Model):
)

def __str__(self):
return self.name
return str(self.name)
195 changes: 161 additions & 34 deletions vms/job/tests/test_jobDetails.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# third party
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

# Django
from django.contrib.staticfiles.testing import LiveServerTestCase
Expand All @@ -11,53 +10,181 @@
from shift.utils import (create_admin, create_event_with_details,
create_job_with_details)

# Class contains failing test cases which have been documented
# Test class commented out to prevent travis build failure
"""

class JobDetails(LiveServerTestCase):
'''
Contains Tests for View Job Details Page
'''
"""
Contains Tests for Job app.
"""

def setUp(self):
create_admin()
self.job_list_page = '/job/list/'
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Firefox()
cls.driver.implicitly_wait(5)
cls.driver.maximize_window()
cls.job_details_page = JobDetailsPage(cls.driver)
cls.authentication_page = AuthenticationPage(cls.driver)
super(JobDetails, cls).setUpClass()

@classmethod
def tearDownClass(cls):
cls.driver.quit()
super(JobDetails, cls).tearDownClass()

self.driver = webdriver.Firefox()
self.driver.implicitly_wait(5)
self.driver.maximize_window()
super(JobDetails, self).setUp()
self.job_details_page = JobDetailsPage(self.driver)
self.authentication_page = AuthenticationPage(self.driver)
def setUp(self):
self.admin = create_admin()
self.login_admin()

def tearDown(self):
self.driver.quit()
super(JobDetails, self).tearDown()
def register_job(self):
# create shift and log hours
created_event = create_event_with_details(
['event', '2017-06-15', '2017-06-17'])
created_job = create_job_with_details(
['job', '2017-06-15', '2017-06-18', '', created_event])
self.authentication_page.logout()

@staticmethod
def register_valid_event():
created_event = create_event_with_details(['event', '2050-06-11', '2050-06-19'])
return created_event

@staticmethod
def register_valid_job(created_event):
created_job = create_job_with_details(['job', '2050-06-15', '2050-06-18', '', created_event])
return created_job

def check_error_messages(self):
job_details_page = self.job_details_page
error_message = job_details_page.FIELD_REQUIRED
self.assertEqual(len(job_details_page.get_help_blocks()), 3)
self.assertEqual(job_details_page.get_job_name_error(), error_message)
self.assertEqual(job_details_page.get_job_start_date_error(), error_message)
self.assertEqual(job_details_page.get_job_end_date_error(), error_message)

def login_admin(self):
authentication_page = self.authentication_page
authentication_page.server_url = self.live_server_url
authentication_page.login({'username': 'admin', 'password': 'admin'})
authentication_page.login(
{
'username': 'admin',
'password': 'admin'
}
)

def test_job_details_view(self):
self.login_admin()
job = self.register_job()
created_event = JobDetails.register_valid_event()

self.job_details_page.navigate_to_event_list_view()

created_job = JobDetails.register_valid_job(created_event)

job_details_page = self.job_details_page
job_details_page.live_server_url = self.live_server_url
job_details_page.navigate_to_job_details_view()

# Verify details
self.assertEqual(job_details_page.get_name(), created_job.name)
self.assertEqual(job_details_page.get_start_date(), 'June 15, 2050')
self.assertEqual(job_details_page.get_end_date(), 'June 18, 2050')
self.assertEqual(job_details_page.get_event_name(), created_event.name)

def test_valid_job_create(self):
created_event = JobDetails.register_valid_event()
self.job_details_page.navigate_to_event_list_view()
created_job = JobDetails.register_valid_job(created_event)

job_details_page = self.job_details_page
job_details_page.live_server_url = self.live_server_url
job_details_page.navigate_to_job_details_view()

# verify details
self.assertEqual(job_details_page.get_name(), job.name)
self.assertEqual(job_details_page.get_start_date(), 'June 15, 2017')
self.assertEqual(job_details_page.get_end_date(), 'June 18, 2017')
self.assertEqual(job_details_page.get_event_name(), job.event.name)
"""
# Check job creation
self.assertEqual(job_details_page.get_name(), created_job.name)
self.assertEqual(job_details_page.get_event_name(), created_event.name)

def test_invalid_job_create(self):
created_event = JobDetails.register_valid_event()
self.job_details_page.navigate_to_event_list_view()

job_details_page = self.job_details_page
job_details_page.live_server_url = self.live_server_url

# Create empty job
job = [created_event.id, '', '', '', '']
job_details_page.navigate_to_job_list_view()
job_details_page.go_to_create_job_page()
job_details_page.fill_job_form(job)

# Check error messages
self.check_error_messages()

def test_invalid_job_edit(self):
created_event = JobDetails.register_valid_event()
self.job_details_page.navigate_to_event_list_view()
JobDetails.register_valid_job(created_event)

job_details_page = self.job_details_page
job_details_page.live_server_url = self.live_server_url
job_details_page.navigate_to_job_list_view()
job_details_page.go_to_edit_job_page()

null_valued_job = [created_event.id, '', '', '', '']
job_details_page.fill_job_form(null_valued_job)
self.check_error_messages()

def test_valid_job_edit(self):
created_event = JobDetails.register_valid_event()
self.job_details_page.navigate_to_event_list_view()
JobDetails.register_valid_job(created_event)

job_details_page = self.job_details_page
job_details_page.live_server_url = self.live_server_url
job_details_page.navigate_to_job_list_view()

edit_job = ['event', 'new job name', 'new-job-description', '2050-06-16', '2050-06-16']
job_details_page.go_to_edit_job_page()
job_details_page.fill_job_form(edit_job)
job_details_page.navigate_to_job_list_view()

self.assertEqual(job_details_page.get_name(), edit_job[1])
self.assertEqual(job_details_page.get_description(), edit_job[2])

def test_job_delete(self):
job_details_page = self.job_details_page
job_details_page.live_server_url = self.live_server_url

created_event = JobDetails.register_valid_event()
job_details_page.navigate_to_event_list_view()
JobDetails.register_valid_job(created_event)

job_details_page.navigate_to_job_list_view()
self.assertEqual(job_details_page.get_delete_element('').text, 'Delete')
job_details_page.get_delete_element('//a').click()
self.assertEqual(job_details_page.get_deletion_context(), 'Delete Job')
job_details_page.submit_form()

def test_create_job_with_no_event_present(self):
job_details_page = self.job_details_page
job_details_page.navigate_to_event_list_view()
job_details_page.click_link(job_details_page.jobs_tab)
self.assertEqual(job_details_page.remove_i18n(self.driver.current_url),
self.live_server_url + job_details_page.job_list_page)
self.assertEqual(job_details_page.get_message_context(),
job_details_page.NO_JOBS_PRESENT)

job_details_page.click_link(job_details_page.create_job_tab)
self.assertEqual(job_details_page.remove_i18n(self.driver.current_url),
self.live_server_url + job_details_page.create_job_page)
self.assertEqual(job_details_page.get_message_context(),
job_details_page.ADD_EVENTS_TO_JOB)

def test_start_date_after_end_date(self):
created_event = JobDetails.register_valid_event()
self.job_details_page.navigate_to_event_list_view()

job_details_page = self.job_details_page
job_details_page.live_server_url = self.live_server_url
job_details_page.navigate_to_job_list_view()
job_details_page.go_to_create_job_page()

job_start_after_end = [created_event.id, 'job name', 'job-description', '2050-06-17', '2050-06-16']
job_details_page.fill_job_form(job_start_after_end)

# Check error.
self.assertEqual(job_details_page.get_job_start_date_error(),
job_details_page.START_BEFORE_END)


132 changes: 132 additions & 0 deletions vms/job/tests/test_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# third party

# Django
from django.core.exceptions import ValidationError
from django.db.models import Q
from django.test.testcases import TestCase

# local Django
from job.models import Job
from event.models import Event
from pom.pages.jobDetailsPage import JobDetailsPage
from shift.utils import create_job_with_details, create_event_with_details


class JobModelTests(TestCase):

def setUp(self):
self.event = self.create_event()

def tearDown(self):
pass

@staticmethod
def create_event():
event = ['event-name', '2050-05-24', '2050-05-28']
created_event = create_event_with_details(event)
return created_event

def create_job(self):
job = ['job-name', '2050-05-25', '2050-05-26', 'job-description', self.event]
created_job = create_job_with_details(job)
return created_job

def test_valid_model_create(self):
job = ['job-name', '2050-05-25', '2050-05-26', 'job-description', self.event]
created_job = create_job_with_details(job)

# Check database for instance creation
self.assertNotEqual(len(Job.objects.all()), 0)

event_in_db = Event.objects.get(Q(name='event-name'))
# Check if event name is correct
self.assertEqual(event_in_db.name, self.event.name)

job_in_db = Job.objects.get(Q(event=event_in_db))
# Check if job details are correct
self.assertEqual(job_in_db.name, job[0])
self.assertEqual(str(job_in_db.start_date), job[1])
self.assertEqual(str(job_in_db.end_date), job[2])
self.assertEqual(job_in_db.description, job[3])

def test_invalid_model_create(self):
job = ['job~name', '2016-05-25', '2016-05-26', 'job-description', self.event]
created_job = create_job_with_details(job)

self.assertRaisesRegexp(ValidationError,
JobDetailsPage.ENTER_VALID_VALUE,
created_job.full_clean)

# Check database for instance creation
self.assertNotEqual(len(Job.objects.all()), 0)

def test_model_edit_with_valid_values(self):
job = self.create_job()

# Check db for instance creation
self.assertEqual(len(Job.objects.all()), 1)

event_in_db = Event.objects.get(Q(name='event-name'))
job_in_db = Job.objects.get(Q(event=event_in_db))
# Check if correct job retrieved
self.assertEqual(job_in_db.name, job.name)

# Edit job
job_in_db.name = 'new-job-name'
job_in_db.save()

job_in_db = Job.objects.get(Q(event=event_in_db))
# Check if save is success
self.assertEqual(job_in_db.name, 'new-job-name')
self.assertEqual(len(Job.objects.all()), 1)

def test_model_edit_with_invalid_values(self):
job = ['job-name', '2016-05-25', '2016-05-26', 'job-description', self.event]
created_job = create_job_with_details(job)

# Check instance created
self.assertEqual(len(Job.objects.all()), 1)

job_in_db = Job.objects.get(Q(name='job-name'))
# Check if correctly stored
self.assertEqual(job_in_db.name, created_job.name)

# Edit job
job_in_db.name = 'new~job~name'
job_in_db.save()

self.assertRaisesRegexp(ValidationError,
JobDetailsPage.ENTER_VALID_VALUE,
job_in_db.full_clean)

# Check database for instance creation
self.assertNotEqual(len(Job.objects.all()), 0)

def test_model_delete(self):
job = self.create_job()

# Check db for instance creation
self.assertEqual(len(Job.objects.all()), 1)

event_in_db = Event.objects.get(Q(name='event-name'))
job_in_db = Job.objects.get(Q(event=event_in_db))
# Check if correct job retrieved
self.assertEqual(job_in_db.name, job.name)

# Delete job
job_in_db.delete()

# Check if delete is successful
self.assertEqual(len(Job.objects.all()), 0)

def test_model_representation(self):
self.create_job()

# Check db for instance creation
self.assertEqual(len(Job.objects.all()), 1)

event_in_db = Event.objects.get(Q(name='event-name'))
job_in_db = Job.objects.get(Q(event=event_in_db))

# Check __str__
self.assertEqual(str(job_in_db), job_in_db.name)
Loading

0 comments on commit 992e40d

Please sign in to comment.