def parse(values): if len(values) < 2: raise ValueError("Missing arguments") if values[0] not in ['older', 'newer']: raise ValueError("Wrong or missing state, only older/newer is allowed") state = values[0] values = values[1:] regex = re.compile(r'^(?P<value>\d+)(?P<unit>y|m|d|h|min)$') parts = {} unit_mapping = { "y": "years", "m": "months", "d": "days", "h": "hours", "min": "minutes", } for v in values: part = regex.search(v) if part is None: raise ValueError("Invalid unit " + v) unit = unit_mapping[part.group('unit')] parts[unit] = int(part.group('value')) delta = relativedelta(**parts) date = datetime.datetime.now() - delta return Age(date=date, state=state)
def test_check_request_state_older(self): base_service = BaseService() now = datetime.now() # created four hours ago created_at = now - relativedelta(hours=4) # include pull request that are older than 1 day date = now - relativedelta(days=1) age = Age(date=date, state="older") actual = base_service.check_request_state(created_at, age) self.assertFalse(actual) # include pull request that ale older than 3 hours date = now - relativedelta(hours=3) age = Age(date=date, state="older") actual = base_service.check_request_state(created_at, age) self.assertTrue(actual)
def test_check_request_state_newer(self): base_service = BaseService() # created four hours ago now = datetime.now() created_at = now - relativedelta(hours=4) # include pull request that have been added in the past day date = now - relativedelta(days=1) age = Age(date=date, state="newer") actual = base_service.check_request_state(created_at, age) self.assertTrue(actual) # include pull request that bave been added in the past 3 hours date = now - relativedelta(hours=3) age = Age(date=date, state="newer") actual = base_service.check_request_state(created_at, age) self.assertFalse(actual)