From 992e40d31b18c8d65832ce7db2a94312b03ddfb5 Mon Sep 17 00:00:00 2001 From: Monal5031 Date: Thu, 31 May 2018 16:40:35 +0530 Subject: [PATCH] Tests for Job app --- vms/job/models.py | 2 +- vms/job/tests/test_jobDetails.py | 195 +++++++++++++++++++++++++------ vms/job/tests/test_model.py | 132 +++++++++++++++++++++ vms/job/tests/test_services.py | 16 +-- vms/pom/pages/basePage.py | 2 + vms/pom/pages/jobDetailsPage.py | 52 ++++++++- 6 files changed, 355 insertions(+), 44 deletions(-) create mode 100644 vms/job/tests/test_model.py diff --git a/vms/job/models.py b/vms/job/models.py index cc881de..ba1a45e 100644 --- a/vms/job/models.py +++ b/vms/job/models.py @@ -26,4 +26,4 @@ class Job(models.Model): ) def __str__(self): - return self.name + return str(self.name) diff --git a/vms/job/tests/test_jobDetails.py b/vms/job/tests/test_jobDetails.py index 588dbc2..9294207 100644 --- a/vms/job/tests/test_jobDetails.py +++ b/vms/job/tests/test_jobDetails.py @@ -1,6 +1,5 @@ # third party from selenium import webdriver -from selenium.common.exceptions import NoSuchElementException # Django from django.contrib.staticfiles.testing import LiveServerTestCase @@ -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) + + diff --git a/vms/job/tests/test_model.py b/vms/job/tests/test_model.py new file mode 100644 index 0000000..d74bc3f --- /dev/null +++ b/vms/job/tests/test_model.py @@ -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) diff --git a/vms/job/tests/test_services.py b/vms/job/tests/test_services.py index 32f98a5..0fe6b67 100644 --- a/vms/job/tests/test_services.py +++ b/vms/job/tests/test_services.py @@ -1,7 +1,6 @@ # standard library import datetime import unittest -# from datetime import date # local Django from job.services import (delete_job, check_edit_job, get_job_by_id, @@ -50,9 +49,9 @@ def tearDownModule(): class JobTests(unittest.TestCase): - ''' + """ Contains tests which require only job objects, no shifts - ''' + """ @classmethod def setup_test_data(cls): @@ -83,14 +82,14 @@ def test_get_job_by_id(self): self.assertIsNone(get_job_by_id(200)) def test_job_not_empty(self): - """ Test job_not_empty(j_id) + """ Test job_not_empty(j_id) Uses jobs j1,j2 """ self.assertTrue(job_not_empty(self.j1.id)) self.assertTrue(job_not_empty(self.j2.id)) self.assertFalse(job_not_empty(100)) def test_get_jobs_by_event_id(self): - """ Test get_jobs_by_event_id(e_id) + """ Test get_jobs_by_event_id(e_id) Uses jobs j1,j2,j3 and event e1, e2 """ # test typical case @@ -163,9 +162,9 @@ def test_delete_job(self): class JobWithShiftTests(unittest.TestCase): - ''' + """ Contains tests which require shift objects - ''' + """ @classmethod def setup_test_data(cls): @@ -257,7 +256,8 @@ def test_check_edit_job(self): def test_get_signed_up_jobs_for_volunteer(self): """ Uses jobs j1,j3, shifts s1,s2,s3 and volunteers v1,v2""" - # volunteer 1 registers for 3 shifts belonging to two jobs - registers for s1 first to check if sorting is successful + # volunteer 1 registers for 3 shifts belonging to two jobs + # - registers for s1 first to check if sorting is successful register(self.v1.id, self.s1.id) register(self.v1.id, self.s3.id) register(self.v1.id, self.s2.id) diff --git a/vms/pom/pages/basePage.py b/vms/pom/pages/basePage.py index 25797c6..b20b293 100644 --- a/vms/pom/pages/basePage.py +++ b/vms/pom/pages/basePage.py @@ -1,6 +1,8 @@ class BasePage(object): """Base class to initialize the base page that will be called from all pages""" ENTER_VALID_VALUE = 'Enter a valid value.' + FIELD_REQUIRED = 'This field is required.' + START_BEFORE_END = 'Start date must be before the end date' def __init__(self, driver): self.driver = driver diff --git a/vms/pom/pages/jobDetailsPage.py b/vms/pom/pages/jobDetailsPage.py index d369f0c..14ef5d5 100644 --- a/vms/pom/pages/jobDetailsPage.py +++ b/vms/pom/pages/jobDetailsPage.py @@ -1,12 +1,18 @@ # local Django +from pom.pageUrls import PageUrls from pom.pages.basePage import BasePage from pom.locators.jobDetailsPageLocators import JobDetailsPageLocators from pom.pages.eventsPage import EventsPage class JobDetailsPage(BasePage): - live_server_url = '' + job_list_page = PageUrls.job_list_page + create_job_page = '/job/create/' + jobs_tab = 'Jobs' + create_job_tab = 'Create Job' + NO_JOBS_PRESENT = 'There are currently no jobs. Please create jobs first.' + ADD_EVENTS_TO_JOB = 'Please add events to associate with jobs first.' def __init__(self, driver): self.driver = driver @@ -19,6 +25,34 @@ def navigate_to_job_details_view(self): self.events_page.navigate_to_job_list_view() self.element_by_xpath(self.elements.VIEW_DETAILS).click() + def navigate_to_event_list_view(self): + self.events_page.go_to_events_page() + + def navigate_to_job_list_view(self): + self.events_page.live_server_url = self.live_server_url + self.events_page.navigate_to_job_list_view() + + def go_to_edit_job_page(self): + self.events_page.go_to_edit_job_page() + + def go_to_create_job_page(self): + self.events_page.go_to_create_job_page() + + def get_help_blocks(self): + return self.events_page.get_help_blocks() + + def get_job_name_error(self): + return self.events_page.get_job_name_error() + + def get_job_start_date_error(self): + return self.events_page.get_job_start_date_error() + + def get_job_end_date_error(self): + return self.events_page.get_job_end_date_error() + + def fill_job_form(self, edit_job): + self.events_page.fill_job_form(edit_job) + def get_start_date(self): return self.element_by_xpath(self.elements.JOB_START_DATE).text @@ -30,3 +64,19 @@ def get_name(self): def get_event_name(self): return self.element_by_xpath(self.elements.JOB_EVENT).text + + def get_description(self): + self.element_by_xpath(self.elements.VIEW_DETAILS).click() + return self.element_by_xpath('//div[@class="panel-body"]').text + + def get_delete_element(self, relative): + return self.events_page.element_by_xpath(self.events_page.elements.DELETE_JOB + relative) + + def get_deletion_context(self): + return self.events_page.get_deletion_context() + + def submit_form(self): + self.events_page.submit_form() + + def get_message_context(self): + return self.events_page.get_message_context()