def test_returns_inner_tasks(self): task_id = self.task_storage.create(self.task).id inner_task = TaskFactory() inner_task.parent_task_id = task_id self.task_storage.create(inner_task) inner_tasks = self.task_storage.inner(task_id) self.assertEqual(len(inner_tasks), 1)
def test_remove_null_columns_removes_columns_if_all_nulls( self, session ): # If we don't assign a project and tags to the tasks they are all # going to be null. desired_columns = self.columns.copy() desired_labels = self.labels.copy() project_index = desired_columns.index('project_id') desired_columns.pop(project_index) desired_labels.pop(project_index) tags_index = desired_columns.index('tags') desired_columns.pop(tags_index) desired_labels.pop(tags_index) due_index = desired_columns.index('due') desired_columns.pop(due_index) desired_labels.pop(due_index) TaskFactory.create_batch(100, due=None) tasks = session.query(Task).filter_by(state='open') columns, labels = self.report._remove_null_columns( tasks, self.columns, self.labels ) assert desired_columns == columns assert desired_labels == labels
def test_processes_task_plans(self): user_id = 10 repeated_task = TaskFactory() repeated_task.status = Status.TEMPLATE.value repeated_task.user_id = user_id task_id = self.task_storage.create(repeated_task).id before_tasks_count = len(self.task_storage.user_tasks(user_id)) interval = 300 big_interval = interval * 10 last_created_at = datetime.datetime.now() - datetime.timedelta( seconds=interval + 5) """ repeated_task_plan after processing should create new task repeated_task_plan_big_interval should not create new task because of bit interval """ repeated_task_plan = TaskPlan(user_id=user_id, task_id=task_id, last_created_at=last_created_at, interval=interval) repeated_task_plan_big_interval = TaskPlan( user_id=user_id, task_id=task_id, last_created_at=last_created_at, interval=big_interval) self.task_plan_storage.create(repeated_task_plan) self.task_plan_storage.create(repeated_task_plan_big_interval) self.task_plan_storage.process_plans(self.task_storage) self.assertEqual(len(self.task_storage.user_tasks(user_id)), before_tasks_count + 1)
def setup(self, session): self.factory = TaskFactory self.dummy_instance = TaskFactory.create() self.model = Task( id=self.dummy_instance.id, agile=self.dummy_instance.agile, title=self.dummy_instance.title, state=self.dummy_instance.state, due=self.dummy_instance.due, priority=self.dummy_instance.priority, ) self.model_attributes = [ 'agile', 'body', 'estimate', 'due', 'fun', 'id', 'priority', 'state', 'title', 'value', 'wait', 'willpower', ]
def test_task_report_print_doesnt_fail_if_some_tasks_doesnt_have_attr( self, session ): # The Task tasks don't have `recurrence` attribute columns = ['id', 'recurrence'] labels = ['Id', 'Recurrence'] self.report = TaskReport(session, RecurrentTask) TaskFactory.create_batch(10, state='open') RecurrentTaskFactory.create_batch(10, state='open') tasks = session.query(Task).filter_by(state='open') self.report.print(tasks, columns, labels)
def test_archive_task(): user = UserFactory.create() task = TaskFactory.create() task.archive() assert task.is_archived is True
def setup(self, session): self.report = Tags(session) self.columns = config.get('report.tags.columns') self.labels = config.get('report.tags.labels') self.tasks = TaskFactory.create_batch(20, state='open') yield 'setup'
def setup(self, session): self.report = Projects(session) self.columns = self.config.get('report.projects.columns').split(', ') self.labels = self.config.get('report.projects.labels').split(', ') self.tasks = TaskFactory.create_batch(20, state='open') yield 'setup'
def test_list_prints_id_title_and_project_if_project_existent(self): project = ProjectFactory.create() tasks = TaskFactory.create_batch(2, state='open') tasks[0].project = project self.session.commit() id_index = self.columns.index('id') project_index = self.columns.index('project_id') columns = [ self.columns[id_index], self.columns[project_index], ] labels = [ self.labels[id_index], self.labels[project_index], ] self.report.print(columns=columns, labels=labels) # Prepare desired report report_data = [] for task in sorted(tasks, key=lambda k: k.id, reverse=True): task_report = [] for attribute in columns: if attribute == 'id': task_report.append(task.sulid) else: task_report.append(task.__getattribute__(attribute)) report_data.append(task_report) self.tabulate.assert_called_once_with(report_data, headers=labels, tablefmt='simple') self.print.assert_called_once_with(self.tabulate.return_value)
def test_shift_manager_for_facility(self): """ checks that get_days_with_shifts() returns only dates later than datetime.now() """ now = datetime.now() yesterday_start = now - timedelta(1) yesterday_end = yesterday_start + timedelta(hours=1) tomorrow_start = now + timedelta(1) tomorrow_end = tomorrow_start + timedelta(hours=1) facility = FacilityFactory.create() task = TaskFactory.create(facility=facility) yesterday_shift = ShiftFactory.create(facility=facility, task=task, starting_time=yesterday_start, ending_time=yesterday_end) tomorrow_shift = ShiftFactory.create(facility=facility, task=task, starting_time=tomorrow_start, ending_time=tomorrow_end) assert Facility.objects.count() == 1, "test case assumes that shifts have been created for the same facility, as the ShiftFactory indeed does at the time of writing of this test case" assert Facility.objects.get() == task.facility shifts = Shift.open_shifts.filter(facility=facility) assert shifts.count() == 1, "only 1 shift should be found with Shifts.open_shifts" shift = shifts.get() assert shift == tomorrow_shift, "wrong shift was found" assert shift.ending_time > now, "the time has to be in the future"
def test_task(graph_client): query = ''' query($id: ID!) { task(id: $id) { name description owner { username } category { name } } } ''' task = TaskFactory.create() result = graph_client.execute(query, variable_values={'id': model_instance_id_to_base64(task)}) assert result == { 'data': { 'task': { 'name': task.name, 'description': task.description, 'owner': { 'username': task.owner.username }, 'category': { 'name': task.category.name } } } }
def test_task_report_print_id_and_due_if_present(self): tasks = TaskFactory.create_batch(10, state='open') id_index = self.columns.index('id') due_index = self.columns.index('due') columns = [ self.columns[id_index], self.columns[due_index], ] labels = [ self.labels[id_index], self.labels[due_index], ] tasks_query = self.session.query(Task).filter_by(state='open') self.report.print(tasks=tasks_query, columns=columns, labels=labels) # Prepare desired report report_data = [] for task in sorted(tasks, key=lambda k: k.id, reverse=True): task_report = [] for attribute in columns: if attribute == 'id': task_report.append(task.sulid) else: task_report.append(self.report._date2str(task.due)) report_data.append(task_report) self.tabulate.assert_called_once_with( report_data, headers=labels, tablefmt='simple' ) self.print.assert_called_once_with(self.tabulate.return_value)
def test_shift_manager_for_facility(self): """ checks that get_days_with_shifts() returns only dates later than datetime.now() """ now = datetime.now() yesterday_start = now - timedelta(1) yesterday_end = yesterday_start + timedelta(hours=1) tomorrow_start = now + timedelta(1) tomorrow_end = tomorrow_start + timedelta(hours=1) facility = FacilityFactory.create() task = TaskFactory.create(facility=facility) yesterday_shift = ShiftFactory.create(facility=facility, task=task, starting_time=yesterday_start, ending_time=yesterday_end) tomorrow_shift = ShiftFactory.create(facility=facility, task=task, starting_time=tomorrow_start, ending_time=tomorrow_end) assert Facility.objects.count( ) == 1, "test case assumes that shifts have been created for the same facility, as the ShiftFactory indeed does at the time of writing of this test case" assert Facility.objects.get() == task.facility shifts = Shift.open_shifts.filter(facility=facility) assert shifts.count( ) == 1, "only 1 shift should be found with Shifts.open_shifts" shift = shifts.get() assert shift == tomorrow_shift, "wrong shift was found" assert shift.ending_time > now, "the time has to be in the future"
def test_task_delete(client, user_credentials): client.login(**user_credentials) task = TaskFactory() assert Task.objects.filter(id=task.id).exists() response = client.post('/tasks/{id}/delete/'.format(id=task.id)) assert response.status_code == HTTPStatus.FOUND assert not Task.objects.filter(id=task.id).exists()
def test_task_detail(client, user_credentials): client.login(**user_credentials) task = TaskFactory() response = client.get('/tasks/{id}/'.format(id=task.id)) response_body = response.content.decode() assert response.status_code == HTTPStatus.OK assert task.name in response_body
def test_task_update_page(client, user_credentials): client.login(**user_credentials) task = TaskFactory() response = client.get('/tasks/{id}/edit/'.format(id=task.id)) response_body = response.content.decode() assert response.status_code == HTTPStatus.OK assert 'Update task' in response_body
def test_task_list(client, user_credentials): client.login(**user_credentials) tasks = TaskFactory.create_batch(5) response = client.get('/') response_body = response.content.decode() assert response.status_code == HTTPStatus.OK for task in tasks: assert task.name in response_body
def test_task_list_with_filter(client, user_credentials): client.login(**user_credentials) selected_status = TaskStatusFactory() not_selected_status = TaskStatusFactory() current_user = User.objects.get(username=user_credentials['username']) another_user = UserFactory() my_tasks_with_selected_status = TaskFactory.create_batch( 3, assigned_to=current_user, status=selected_status, ) my_tasks_without_selected_status = TaskFactory.create_batch( 3, assigned_to=current_user, status=not_selected_status, ) not_my_task_with_selected_status = TaskFactory.create_batch( 3, assigned_to=another_user, status=selected_status, ) response = client.get('/', { 'status': selected_status.id, 'my_task': 'on', }) response_body = response.content.decode() assert response.status_code == HTTPStatus.OK for task in my_tasks_with_selected_status: assert task.name in response_body for task in my_tasks_without_selected_status: assert task.name not in response_body for task in not_my_task_with_selected_status: assert task.name not in response_body
def setUp(self): self.database = ':memory:' self.category_storage = CategoryStorage(self.database) self.task_storage = TaskStorage(self.database) self.task_plan_storage = TaskPlanStorage(self.database) self.notification_storage = NotificationStorage(self.database) self.category = CategoryFactory(user_id=10) self.task = TaskFactory() self.task_plan = TaskPlanFactory() self.notification = NotificationFactory() DatabaseConnector(self.database).create_tables()
def test_create_task(): user = UserFactory.create() # Check there are 0 tasks before a new task is added number_tasks = Task.objects.filter(user_id=user.id).count() assert number_tasks == 0 task = TaskFactory.create() # Check there is 1 task after a new task is added number_tasks = Task.objects.filter(user_id=user.id).count() assert number_tasks == 1
def test_report_does_not_print_projects_without_open_tasks(self): project = ProjectFactory.create() project_with_closed_tasks = ProjectFactory.create() completed_task = TaskFactory.create(state='completed') completed_task.project = project_with_closed_tasks deleted_task = TaskFactory.create(state='deleted') deleted_task.project = project_with_closed_tasks # Project assignment for task in self.tasks: task.project = project self.session.commit() self.report.print(columns=self.columns, labels=self.labels) self.tabulate.assert_called_once_with([ [project.id, 20, project.description], ], headers=self.labels, tablefmt='simple') self.print.assert_called_once_with(self.tabulate.return_value)
def test_report_does_not_print_tags_without_open_tasks(self): tag = TagFactory.create() tag_with_closed_tasks = TagFactory.create() completed_task = TaskFactory.create(state='completed') completed_task.tags = [tag_with_closed_tasks] deleted_task = TaskFactory.create(state='deleted') deleted_task.tags = [tag_with_closed_tasks] # Tag assignment for task in self.tasks: task.tags = [tag] self.session.commit() self.report.print(columns=self.columns, labels=self.labels) self.tabulate.assert_called_once_with([ [tag.id, 20, tag.description], ], headers=self.labels, tablefmt='simple') self.print.assert_called_once_with(self.tabulate.return_value)
def test_remove_null_columns_doesnt_fail_if_column_doesnt_exist( self, session ): desired_columns = ['id', 'title'] columns = desired_columns.copy() columns.append('unexistent_column') desired_labels = ['Id', 'Title'] labels = desired_labels.copy() labels.append('unexistent_label') TaskFactory.create_batch(100, due=None) tasks = session.query(Task).filter_by(state='open') result_columns, result_labels = self.report._remove_null_columns( tasks, columns, labels ) assert result_columns == desired_columns assert result_labels == desired_labels
def test_task_report_print_fills_empty_if_task_doesnt_have_attr( self, session ): # If it doesn't get filled by an empty value, it will get filled with # the next attribute data. # The Task tasks don't have `recurrence` attribute columns = ['id', 'recurrence'] labels = ['Id', 'Recurrence'] self.report = TaskReport(session, RecurrentTask) TaskFactory.create_batch(1, state='open') # RecurrentTaskFactory.create_batch(1, state='open') tasks = session.query(Task).filter_by(state='open') self.report.print(tasks, columns, labels) self.tabulate.assert_called_once_with( [['a', '']], headers=labels, tablefmt='simple', )
def test_create_task(): user = UserFactory.create() # Check there are 0 tasks before a new task is added number_tasks = Task.objects.filter(user_id=user.id).count() assert number_tasks == 0 task = TaskFactory.create() # Check there is 1 task after a new task is added and that it's not archived number_tasks = Task.objects.filter(user_id=user.id).count() task = Task.objects.get(user_id=user.id) assert number_tasks == 1 assert task.is_archived is False
def test_returns_user_tasks(self): first_task = TaskFactory() second_task = TaskFactory() user_id = 10 first_task.user_id = user_id second_task.user_id = user_id self.task_storage.create(first_task) self.task_storage.create(second_task) tasks = self.task_storage.user_tasks(user_id) self.assertEqual(len(tasks), 2)
def test_returns_inner_tasks_recursive(self): task_id = self.task_storage.create(self.task).id inner_task = TaskFactory() inner_task.parent_task_id = task_id inner_task_id = self.task_storage.create(inner_task).id second_level_inner_task = TaskFactory() second_level_inner_task.parent_task_id = inner_task_id self.task_storage.create(second_level_inner_task) inner_tasks = self.task_storage.inner(task_id, True) self.assertEqual(len(inner_tasks), 2)
def test_task_update(client, user_credentials): client.login(**user_credentials) task = TaskFactory() task_new_name = 'new_test_task' task_from_db = Task.objects.get(id=task.id) assert task_from_db.name != task_new_name response = client.post('/tasks/{id}/edit/'.format(id=task.id), { 'name': task_new_name, 'status': task_from_db.status.id, }) updated_task_from_db = Task.objects.get(id=task.id) assert response.status_code == HTTPStatus.FOUND assert updated_task_from_db.name == task_new_name
def setUp(self): self.database = ':memory:' self.user_id = 10 self.categories_controller = CategoriesController( self.user_id, CategoryStorage(self.database)) self.tasks_controller = TasksController(self.user_id, TaskStorage(self.database)) self.task_plans_controller = TaskPlansController( self.user_id, TaskPlanStorage(self.database)) self.notifications_controller = NotificationsController( self.user_id, NotificationStorage(self.database)) self.category = CategoryFactory() self.task = TaskFactory() self.task_plan = TaskPlanFactory() self.notification = NotificationFactory() DatabaseConnector(self.database).create_tables()
def test_task_report_print_id_title_and_tags_if_present(self): tasks = TaskFactory.create_batch(4, state='open') tags = TagFactory.create_batch(2) # Add tags to task tasks[0].tags = tags self.session.commit() # Select columns to print id_index = self.columns.index('id') tags_index = self.columns.index('tags') columns = [ self.columns[id_index], self.columns[tags_index], ] labels = [ self.labels[id_index], self.labels[tags_index], ] tasks_query = self.session.query(Task).filter_by(state='open') self.report.print(tasks=tasks_query, columns=columns, labels=labels) # Prepare desired report report_data = [] for task in sorted(tasks, key=lambda k: k.id, reverse=True): task_report = [] for attribute in columns: if attribute == 'id': task_report.append(task.sulid) elif attribute == 'tags': if len(task.tags) > 0: task_report.append( ', '.join([tag.id for tag in tags]) ) else: task_report.append('') report_data.append(task_report) self.tabulate.assert_called_once_with( report_data, headers=labels, tablefmt='simple' ) self.print.assert_called_once_with(self.tabulate.return_value)
def test_complete_task_by_fulid(self): closed = self.fake.date_time() self.datetime.datetime.now.return_value = closed task = TaskFactory.create(state='open') assert self.session.query(Task).one() self.manager.complete(task.id) modified_task = self.session.query(Task).get(task.id) assert modified_task.closed == closed assert modified_task.title == task.title assert modified_task.state == 'completed' self.log_debug.assert_called_with( 'Completed task {}: {}'.format( modified_task.id, modified_task.title, ) )