From bec8940a868f082209d405d21e718a359cc7a4e4 Mon Sep 17 00:00:00 2001 From: Fang Lin Date: Tue, 28 May 2019 15:59:58 -0700 Subject: [PATCH 1/3] Enable mock a future date --- uw_hrp/dao.py | 3 +++ uw_hrp/models.py | 16 +++++++++++++--- uw_hrp/tests/test_dao.py | 1 + uw_hrp/tests/test_models.py | 7 ++++++- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/uw_hrp/dao.py b/uw_hrp/dao.py index f879b48..7b74a76 100644 --- a/uw_hrp/dao.py +++ b/uw_hrp/dao.py @@ -10,3 +10,6 @@ def service_name(self): def service_mock_paths(self): return [abspath(os.path.join(dirname(__file__), "resources"))] + + def is_using_file_dao(self): + return self.get_implementation().is_mock() diff --git a/uw_hrp/models.py b/uw_hrp/models.py index fadd50f..66db50b 100644 --- a/uw_hrp/models.py +++ b/uw_hrp/models.py @@ -1,7 +1,13 @@ -from datetime import datetime, timezone +from datetime import datetime, timedelta, timezone from dateutil.parser import parse import json from restclients_core import models +from uw_hrp.dao import HRP_DAO + + +def get_now(): + # return time-zone-aware datetime + return datetime.now(timezone.utc) def date_to_str(d_obj): @@ -122,7 +128,7 @@ class WorkerPosition(models.Model): title = models.CharField(max_length=128, null=True, default=None) def is_active_position(self): - now = datetime.now(timezone.utc) + now = get_now() return self.end_date is None or self.end_date > now def to_json(self): @@ -173,7 +179,11 @@ def __init__(self, *args, **kwargs): self.start_date = parse_date(data["PositionStartDate"]) if data.get("PositionEndDate") is not None: - self.end_date = parse_date(data["PositionEndDate"]) + if (HRP_DAO().is_using_file_dao() and + data["PositionEndDate"] == "future"): + self.end_date = get_now() + timedelta(days=30) + else: + self.end_date = parse_date(data["PositionEndDate"]) if data.get("PositionSupervisor") is not None: self.supervisor_eid = data["PositionSupervisor"]["EmployeeID"] diff --git a/uw_hrp/tests/test_dao.py b/uw_hrp/tests/test_dao.py index 9602f3b..e3e220e 100644 --- a/uw_hrp/tests/test_dao.py +++ b/uw_hrp/tests/test_dao.py @@ -8,3 +8,4 @@ def test_dao(self): dao = HRP_DAO() self.assertEqual(dao.service_name(), "hrpws") self.assertTrue(len(dao.service_mock_paths()) > 0) + self.assertTrue(dao.is_using_file_dao()) diff --git a/uw_hrp/tests/test_models.py b/uw_hrp/tests/test_models.py index 8e59a10..93e7d55 100644 --- a/uw_hrp/tests/test_models.py +++ b/uw_hrp/tests/test_models.py @@ -1,5 +1,4 @@ from unittest import TestCase -from datetime import datetime, timedelta, timezone from uw_hrp.models import ( EmploymentStatus, JobProfile, SupervisoryOrganization, Worker, WorkerPosition, parse_date) @@ -129,6 +128,12 @@ def test_worker_position(self): self.assertFalse(work_position.is_active_position()) self.assertIsNotNone(str(work_position)) + work_position = WorkerPosition( + data={"PositionStartDate": "1994-10-01T00:00:00.000Z", + "PositionEndDate": "future", + "PositionFTEPercent": "100.00000"}) + self.assertTrue(work_position.is_active_position()) + def test_worker(self): worker = Worker(netid='none', regid="10000000", From 5ed965de62a11766621e2fbef96e530945b33817 Mon Sep 17 00:00:00 2001 From: Fang Lin Date: Tue, 28 May 2019 16:21:42 -0700 Subject: [PATCH 2/3] have 24 hr before the end_date for a position to be active --- uw_hrp/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uw_hrp/models.py b/uw_hrp/models.py index 66db50b..207ad9f 100644 --- a/uw_hrp/models.py +++ b/uw_hrp/models.py @@ -128,7 +128,7 @@ class WorkerPosition(models.Model): title = models.CharField(max_length=128, null=True, default=None) def is_active_position(self): - now = get_now() + now = get_now() + timedelta(days=1) return self.end_date is None or self.end_date > now def to_json(self): From 9fe8674ef40cfd4775dec2f2345eafeaeb1fc612 Mon Sep 17 00:00:00 2001 From: Fang Lin Date: Thu, 30 May 2019 11:03:01 -0700 Subject: [PATCH 3/3] Revise active position check. --- uw_hrp/models.py | 21 ++++++++------------- uw_hrp/tests/test_models.py | 13 ++++++++++++- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/uw_hrp/models.py b/uw_hrp/models.py index 207ad9f..f2de25a 100644 --- a/uw_hrp/models.py +++ b/uw_hrp/models.py @@ -1,8 +1,7 @@ -from datetime import datetime, timedelta, timezone +from datetime import datetime, timezone from dateutil.parser import parse import json from restclients_core import models -from uw_hrp.dao import HRP_DAO def get_now(): @@ -128,8 +127,10 @@ class WorkerPosition(models.Model): title = models.CharField(max_length=128, null=True, default=None) def is_active_position(self): - now = get_now() + timedelta(days=1) - return self.end_date is None or self.end_date > now + return self.end_date is None or self.end_date > get_now() + + def is_future_position(self): + return self.start_date is not None and self.start_date > get_now() def to_json(self): data = {'start_date': date_to_str(self.start_date), @@ -177,14 +178,8 @@ def __init__(self, *args, **kwargs): self.fte_percent = float(data.get("PositionFTEPercent")) if data.get("PositionStartDate") is not None: self.start_date = parse_date(data["PositionStartDate"]) - if data.get("PositionEndDate") is not None: - if (HRP_DAO().is_using_file_dao() and - data["PositionEndDate"] == "future"): - self.end_date = get_now() + timedelta(days=30) - else: - self.end_date = parse_date(data["PositionEndDate"]) - + self.end_date = parse_date(data["PositionEndDate"]) if data.get("PositionSupervisor") is not None: self.supervisor_eid = data["PositionSupervisor"]["EmployeeID"] @@ -220,8 +215,8 @@ def __str__(self): def __init__(self, *args, **kwargs): data = kwargs.get("data") self.employee_status = None - self.primary_position = None - self.other_active_positions = [] + self.primary_position = None # only 1 primary position + self.other_active_positions = [] # include the future position if data is None: return super(Worker, self).__init__(*args, **kwargs) diff --git a/uw_hrp/tests/test_models.py b/uw_hrp/tests/test_models.py index 93e7d55..ef7d284 100644 --- a/uw_hrp/tests/test_models.py +++ b/uw_hrp/tests/test_models.py @@ -1,4 +1,5 @@ from unittest import TestCase +from datetime import datetime, timedelta, timezone from uw_hrp.models import ( EmploymentStatus, JobProfile, SupervisoryOrganization, Worker, WorkerPosition, parse_date) @@ -130,9 +131,19 @@ def test_worker_position(self): work_position = WorkerPosition( data={"PositionStartDate": "1994-10-01T00:00:00.000Z", - "PositionEndDate": "future", + "PositionEndDate": str(datetime.now(timezone.utc) + + timedelta(minutes=1)), "PositionFTEPercent": "100.00000"}) self.assertTrue(work_position.is_active_position()) + self.assertFalse(work_position.is_future_position()) + + work_position = WorkerPosition( + data={"PositionStartDate": str(datetime.now(timezone.utc) + + timedelta(minutes=1)), + "PositionEndDate": None, + "PositionFTEPercent": "100.00000"}) + self.assertTrue(work_position.is_active_position()) + self.assertTrue(work_position.is_future_position()) def test_worker(self): worker = Worker(netid='none',