def test_close_run_timestamp(self): now = datetime.now() status = Status(bot_name="TestBot") time.sleep(0.002) status.close_run(success=False, finish=False) self.assertTrue( timedelta(seconds=0.004) > abs(now - status.finish_time)) self.assertTrue(timedelta(seconds=0) < abs(now - status.finish_time))
def test_init(self): # bot_name is a necessary parameter with self.assertRaises(TypeError): Status() # pylint: disable=no-value-for-parameter status = Status(bot_name="TestBot") compare("TestBot", status.bot_name) compare(False, status.success) compare(False, status.finish)
def __init__(self, bot_name: str): self._dynamodb: DynamoDBServiceResource = boto3.resource( 'dynamodb', region_name='eu-central-1') self._manage_table = self._dynamodb.Table(MANAGE_TABLE) # pylint: disable=no-member self.current_run = Status(bot_name) self.bot_name = bot_name self._manage_table.put_item( Item=self.current_run.to_dict()) # type: ignore self._last_runs: List[Status] = []
def test_to_dict(self): status = Status(bot_name="TestBot") compare( { "bot_name": "TestBot", "success": False, "finish": False, "start_time": '2001-01-01T00:00:00', "finish_time": '0001-01-01T00:00:00', "output": None }, status.to_dict())
class StatusManager: def __init__(self, bot_name: str): self._dynamodb: DynamoDBServiceResource = boto3.resource( 'dynamodb', region_name='eu-central-1') self._manage_table = self._dynamodb.Table(MANAGE_TABLE) # pylint: disable=no-member self.current_run = Status(bot_name) self.bot_name = bot_name self._manage_table.put_item( Item=self.current_run.to_dict()) # type: ignore self._last_runs: List[Status] = [] @property def last_runs(self) -> List[Status]: if not self._last_runs: raw_list = self._manage_table.query(KeyConditionExpression=Key( 'bot_name').eq(self.bot_name))["Items"] # type: ignore self._last_runs = [ Status.from_dict(status_dict) for status_dict in raw_list[:-1][::-1] if status_dict["start_time"] != self.current_run.start_time.isoformat() ] return self._last_runs @property def last_run(self) -> Optional[Status]: if self.last_runs: return self.last_runs[0] return None @property def last_finished_runs(self) -> List[Status]: return [status for status in self.last_runs if status.finish] @property def last_successful_runs(self) -> List[Status]: return [status for status in self.last_runs if status.success] @property def last_successful_run(self) -> Optional[Status]: if self.last_successful_runs: return self.last_successful_runs[0] return None def finish_run(self, success: bool = False): self.current_run.finish = True self.current_run.finish_time = datetime.now() if success: self.current_run.success = success self._manage_table.put_item( Item=self.current_run.to_dict()) # type: ignore
def test_finish_success(self): status = Status(bot_name="TestBot") return_dict = status.close_run(success=True, finish=True) compare(True, status.finish) compare(True, status.success) compare( { "bot_name": "TestBot", "success": True, "finish": True, "start_time": '2001-01-01T00:00:00', "finish_time": '2001-01-01T00:00:15', "output": None }, return_dict)
def test_from_dict(self): status = Status.from_dict({ "bot_name": "TestBot", "start_time": '2000-01-01T00:00:00', "finish_time": '2000-01-01T00:10:00' }) compare("TestBot", status.bot_name) compare(datetime(year=2000, month=1, day=1), status.start_time) compare(datetime(year=2000, month=1, day=1, minute=10), status.finish_time)
def last_runs(self) -> List[Status]: if not self._last_runs: raw_list = self._manage_table.query(KeyConditionExpression=Key( 'bot_name').eq(self.bot_name))["Items"] # type: ignore self._last_runs = [ Status.from_dict(status_dict) for status_dict in raw_list[:-1][::-1] if status_dict["start_time"] != self.current_run.start_time.isoformat() ] return self._last_runs
def test_output(self): status = Status(bot_name="TestBot") compare(None, status.output) status.output = {"a": 1, "b": ["c", "d"]} compare({"a": 1, "b": ["c", "d"]}, status.to_dict()["output"])
def test_create_start_time(self): now = datetime.now() status = Status(bot_name="TestBot") self.assertTrue( timedelta(seconds=0.001) > abs(now - status.start_time)) compare(datetime.min, status.finish_time)