From 5ea4ce6f838c70c67f99b26d31b2ea01fd5df0c8 Mon Sep 17 00:00:00 2001 From: Emilie Zawadzki Date: Thu, 5 Jan 2017 19:03:05 +0100 Subject: [PATCH] [Timesheet] : test case utils + util function to get nb of half days in worked week days --- app/organization/core/no_db_settings.py | 7 +++++ app/organization/core/tests.py | 12 ++++++++- app/organization/network/tests.py | 28 ++++++++++++++++++-- app/organization/network/utils.py | 35 +++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 app/organization/core/no_db_settings.py create mode 100644 app/organization/network/utils.py diff --git a/app/organization/core/no_db_settings.py b/app/organization/core/no_db_settings.py new file mode 100644 index 00000000..bd411627 --- /dev/null +++ b/app/organization/core/no_db_settings.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +try: + from local_settings import * +except ImportError: + pass +# Test runner with no database creation +TEST_RUNNER = 'organization.core.tests.NoDbTestRunner' diff --git a/app/organization/core/tests.py b/app/organization/core/tests.py index fa8859d7..85b5bf83 100644 --- a/app/organization/core/tests.py +++ b/app/organization/core/tests.py @@ -20,5 +20,15 @@ # along with this program. If not, see . from django.test import TestCase +from django.test.runner import DiscoverRunner -# Create your tests here. +class NoDbTestRunner(DiscoverRunner): + """ A test runner to test without database creation """ + + def setup_databases(self, **kwargs): + """ Override the database creation defined in parent class """ + pass + + def teardown_databases(self, old_config, **kwargs): + """ Override the database teardown defined in parent class """ + pass diff --git a/app/organization/network/tests.py b/app/organization/network/tests.py index fa8859d7..43ddb800 100644 --- a/app/organization/network/tests.py +++ b/app/organization/network/tests.py @@ -19,6 +19,30 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from django.test import TestCase +from django.test import SimpleTestCase +import datetime +from organization.network.utils import get_nb_half_days_by_period -# Create your tests here. +class NbOfHalfDaysInPeriodTestCase(SimpleTestCase): + + def setUp(self): + self.date_from = datetime.date(2016,12,1) + self.date_to = datetime.date(2016,12,31) + + def test_nbhalf_half_days(self): + + expected = { + "monday_am": 4, + "monday_pm": 4, + "tuesday_am": 4, + "tuesday_pm": 4, + "wednesday_am": 4, + "wednesday_pm": 4, + "thursday_am": 5, + "thursday_pm": 5, + "friday_am": 5, + "friday_pm": 5, + } + + result = get_nb_half_days_by_period(self.date_from, self.date_to) + self.assertEquals(result, expected) diff --git a/app/organization/network/utils.py b/app/organization/network/utils.py new file mode 100644 index 00000000..ec47083f --- /dev/null +++ b/app/organization/network/utils.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +import pandas as pd + +def get_nb_half_days_by_period(date_from, date_to): + day_list = pd.date_range(date_from, date_to).tolist() + day_dict = { + "monday_am": 0, + "monday_pm": 0, + "tuesday_am": 0, + "tuesday_pm": 0, + "wednesday_am": 0, + "wednesday_pm": 0, + "thursday_am": 0, + "thursday_pm": 0, + "friday_am": 0, + "friday_pm": 0, + } + for day in day_list : + if day.dayofweek == 0: + day_dict['monday_am'] += 1 + day_dict['monday_pm'] += 1 + if day.dayofweek == 1: + day_dict['tuesday_am'] += 1 + day_dict['tuesday_pm'] += 1 + if day.dayofweek == 2: + day_dict['wednesday_am'] += 1 + day_dict['wednesday_pm'] += 1 + if day.dayofweek == 3: + day_dict['thursday_am'] += 1 + day_dict['thursday_pm'] += 1 + if day.dayofweek == 4: + day_dict['friday_am'] += 1 + day_dict['friday_pm'] += 1 + + return day_dict -- 2.39.5