def __init__(self): super().__init__() self.task_list = TaskList() self.auth_service = AuthService() self.task_service = TaskService() self.task_validator = TaskValidator() self.user = None self.token = None self.loading = False
def testRetrieveTask(self): mock_data = { "topic": "Test Service", "description": "Test Service", "start_at": "2019-06-02T06:00:00Z", "end_at": "2019-06-02T06:00:00Z", "status": False, "location": "KMITL", "user": 1 } result = TaskService.create_task(mock_data) task = TaskService.retrieve_task(result.id) self.assertEqual(task.id, result.id)
def update_task(self, task_id: int, details: Dict): details.update({'user': self.user['id']}) is_valid = self.task_validator.validate(details) if is_valid: updated_task = TaskService.update_task(task_id, details) self.task_list.update_task(task_id, updated_task) self._notify_observers() return True return False
def login(self, username, password) -> bool: response = AuthService.login(username, password) if response: self.set_current_user(response['user']) self.set_current_token(response['key']) task_list = TaskService.list_task(self.user["id"]) self.set_task_list(task_list) return True return False
def delete_task(self, task_id): deleted_task = self.task_list.delete_task(task_id) is_delete_success = False if deleted_task is not None: is_delete_success = TaskService.delete_task(task_id) if is_delete_success: self._notify_observers() return True return False
def testUpdateTask(self): mock_data = { "topic": "Test Update Service", "description": "Test Service", "start_at": "2019-06-02T06:00:00Z", "end_at": "2019-06-02T06:00:00Z", "status": True, "location": "KMITL", "user": 1 } task = TaskService.update_task(26, mock_data) self.assertEqual(task.id, 26) self.assertEqual(task.topic, mock_data["topic"]) self.assertEqual(task.description, mock_data["description"]) self.assertEqual(task.status, mock_data["status"]) self.assertEqual(task.location, mock_data["location"])
def testCreateTask(self): mock_data = { "topic": "Test Service", "description": "Test Service", "start_at": "2019-06-02T06:00:00Z", "end_at": "2019-06-02T06:00:00Z", "status": False, "location": "KMITL", "user": 1 } result = TaskService.create_task(mock_data) self.assertEqual(result.topic, mock_data["topic"]) self.assertEqual(result.description, mock_data["description"]) self.assertEqual(result.start_at, mock_data["start_at"]) self.assertEqual(result.end_at, mock_data["end_at"]) self.assertEqual(result.status, mock_data["status"]) self.assertEqual(result.location, mock_data["location"]) self.assertEqual(result.user, mock_data["user"])
def testListAllTask(self): task_list = TaskService.list_task(1) self.assertEqual(type(task_list), TaskList)
class TaskLedgerSystem(Observable): def __init__(self): super().__init__() self.task_list = TaskList() self.auth_service = AuthService() self.task_service = TaskService() self.task_validator = TaskValidator() self.user = None self.token = None self.loading = False def _notify_observers(self): for observer in self._observers: observer.update_data() def set_current_user(self, user: Dict): self.user = user def set_current_token(self, token: str): self.token = token def get_task_list(self): return self.task_list def set_task_list(self, task_list: TaskList): self.task_list = task_list self._notify_observers() def login(self, username, password) -> bool: response = AuthService.login(username, password) if response: self.set_current_user(response['user']) self.set_current_token(response['key']) task_list = TaskService.list_task(self.user["id"]) self.set_task_list(task_list) return True return False def is_authenticated(self) -> bool: return self.token is not None def register(self, username, password1, password2) -> Dict: response = AuthService.register(username, password1, password2) if len(response) == 0: return {} else: return response def create_task(self, details: Dict) -> bool: details.update({'user': self.user['id']}) is_valid = self.task_validator.validate(details) if is_valid: new_task = self.task_service.create_task(details) self.task_list.add_task(new_task) self._notify_observers() return True return False def update_task(self, task_id: int, details: Dict): details.update({'user': self.user['id']}) is_valid = self.task_validator.validate(details) if is_valid: updated_task = TaskService.update_task(task_id, details) self.task_list.update_task(task_id, updated_task) self._notify_observers() return True return False def delete_task(self, task_id): deleted_task = self.task_list.delete_task(task_id) is_delete_success = False if deleted_task is not None: is_delete_success = TaskService.delete_task(task_id) if is_delete_success: self._notify_observers() return True return False def set_loading(self, loading_status): self.loading = loading_status def is_busy(self, date: QDate) -> bool: return self.task_list.is_busy(date)