From d7084370e3cecc4086ea2547beb7e6788aa886c6 Mon Sep 17 00:00:00 2001 From: David Wallace Date: Tue, 7 Jan 2025 18:02:14 +0100 Subject: [PATCH] tests(project, handlers): add tests for tasks and views handler fuctions Signed-off-by: David Wallace --- rdmo/projects/tests/helpers.py | 5 + rdmo/projects/tests/test_handlers_tasks.py | 145 +++++++++++++++++++-- rdmo/projects/tests/test_handlers_views.py | 97 ++++++++++++-- testing/config/settings/base.py | 8 -- 4 files changed, 224 insertions(+), 31 deletions(-) diff --git a/rdmo/projects/tests/helpers.py b/rdmo/projects/tests/helpers.py index e69de29bb2..db04dd17da 100644 --- a/rdmo/projects/tests/helpers.py +++ b/rdmo/projects/tests/helpers.py @@ -0,0 +1,5 @@ + + +def assert_other_projects_unchanged(other_projects, initial_tasks_state): + for other_project in other_projects: + assert set(other_project.tasks.values_list('id', flat=True)) == set(initial_tasks_state[other_project.id]) diff --git a/rdmo/projects/tests/test_handlers_tasks.py b/rdmo/projects/tests/test_handlers_tasks.py index c381ff08cf..cf710fd09a 100644 --- a/rdmo/projects/tests/test_handlers_tasks.py +++ b/rdmo/projects/tests/test_handlers_tasks.py @@ -1,30 +1,151 @@ +from django.contrib.auth.models import Group from rdmo.projects.models import Project -from rdmo.questions.models import Catalog from rdmo.tasks.models import Task -task_id = 1 +from .helpers import assert_other_projects_unchanged +project_id = 10 +task_id = 1 +group_name = 'view_test' -def test_project_views_sync_when_adding_or_removing_a_catalog_to_or_from_a_task(db, settings): +def test_project_tasks_sync_when_adding_or_removing_a_catalog_to_or_from_a_task(db, settings): assert settings.PROJECT_TASKS_SYNC # Setup: Create a catalog, a task, and a project using the catalog - catalog = Catalog.objects.first() - task = Task.objects.get(pk=task_id) - task.catalogs.set([]) - project = Project.objects.create(title="Test Project", catalog=catalog) + project = Project.objects.get(id=project_id) + catalog = project.catalog + other_projects = Project.objects.exclude(catalog=catalog) # All other projects + task = Task.objects.get(id=task_id) # This task does not have catalogs in the fixture + task.catalogs.clear() + initial_project_tasks = project.tasks.values_list('id', flat=True) + + # Save initial state of tasks for other projects + initial_other_project_tasks = { + i.id: list(i.tasks.values_list('id', flat=True)) + for i in other_projects + } - # Initially, the project should not have the task + # Ensure the project does not have the task initially assert task not in project.tasks.all() - # Add the catalog to the task + ## Tests for .add and .remove + # Add the catalog to the task and assert that the project now includes the task task.catalogs.add(catalog) - # After adding the catalog, the project should now include the task assert task in project.tasks.all() + assert_other_projects_unchanged(other_projects, initial_other_project_tasks) - # Remove the catalog from the task + # Remove the catalog from the task and assert that the project no longer includes the task task.catalogs.remove(catalog) - # After removing the catalog, the project should no longer include the task assert task not in project.tasks.all() + assert_other_projects_unchanged(other_projects, initial_other_project_tasks) + + ## Tests for .set and .clear + # Add the catalog to the task and assert that the project now includes the task + task.catalogs.set([catalog]) + assert task in project.tasks.all() + assert_other_projects_unchanged(other_projects, initial_other_project_tasks) + + # Remove all catalogs from the task and assert that the project no longer includes the task + task.catalogs.clear() + assert task not in project.tasks.all() + assert_other_projects_unchanged(other_projects, initial_other_project_tasks) + + # Assert that the initial project tasks are unchanged + assert set(project.tasks.values_list('id', flat=True)) == set(initial_project_tasks) + + +def test_project_tasks_sync_when_adding_or_removing_a_site_to_or_from_a_task(db, settings): + assert settings.PROJECT_TASKS_SYNC + + # Setup: Get an existing project, its associated site, and create a task + project = Project.objects.get(id=project_id) + site = project.site + other_projects = Project.objects.exclude(site=site) # All other projects + task = Task.objects.get(id=task_id) # This task does not have sites in the fixture + task.sites.clear() # Ensure the task starts without any sites + project.tasks.remove(task) + initial_project_tasks = project.tasks.values_list('id', flat=True) + + # Save initial state of tasks for other projects + initial_other_project_tasks = { + i.id: list(i.tasks.values_list('id', flat=True)) + for i in other_projects + } + + # Ensure the project does not have the task initially + assert task not in project.tasks.all() + + ## Tests for .add and .remove + # Add the site to the task and assert that the project now includes the task + task.sites.add(site) + assert task in project.tasks.all() + assert_other_projects_unchanged(other_projects, initial_other_project_tasks) + + # Remove the site from the task and assert that the project no longer includes the task + task.sites.remove(site) + assert task not in project.tasks.all() + assert_other_projects_unchanged(other_projects, initial_other_project_tasks) + + ## Tests for .set and .clear + # Add the site to the task and assert that the project now includes the task + task.sites.set([site]) + assert task in project.tasks.all() + assert_other_projects_unchanged(other_projects, initial_other_project_tasks) + + # Clear all sites from the task and assert that the project no longer includes the task + task.sites.clear() + assert task not in project.tasks.all() + assert_other_projects_unchanged(other_projects, initial_other_project_tasks) + + # Assert that the initial project tasks are unchanged + assert set(project.tasks.values_list('id', flat=True)) == set(initial_project_tasks) + + +def test_project_tasks_sync_when_adding_or_removing_a_group_to_or_from_a_task(db, settings): + assert settings.PROJECT_TASKS_SYNC + + # Setup: Get an existing project, its associated group, and create a task + project = Project.objects.get(id=project_id) + user = project.owners.first() # Get the first user associated with the project + group = Group.objects.filter(name=group_name).first() # Get a test group + user.groups.add(group) + other_projects = Project.objects.exclude(memberships__user=user) # All other projects + task = Task.objects.get(id=task_id) # This task does not have groups in the fixture + task.groups.clear() # Ensure the task starts without any groups + initial_project_tasks = project.tasks.values_list('id', flat=True) + + # Save initial state of tasks for other projects + initial_other_project_tasks = { + i.id: list(i.tasks.values_list('id', flat=True)) + for i in other_projects + } + + # Ensure the project does not have the task initially + assert task not in project.tasks.all() + + ## Tests for .add and .remove + # Add the group to the task and assert that the project now includes the task + task.groups.add(group) + assert task in project.tasks.all() + assert_other_projects_unchanged(other_projects, initial_other_project_tasks) + + # Remove the group from the task and assert that the project no longer includes the task + task.groups.remove(group) + assert task not in project.tasks.all() + assert_other_projects_unchanged(other_projects, initial_other_project_tasks) + + ## Tests for .set and .clear + # Add the group to the task and assert that the project now includes the task + task.groups.set([group]) + assert task in project.tasks.all() + assert_other_projects_unchanged(other_projects, initial_other_project_tasks) + + # Clear all groups from the task and assert that the project no longer includes the task + task.groups.clear() + assert task not in project.tasks.all() + assert_other_projects_unchanged(other_projects, initial_other_project_tasks) + + # Assert that the initial project tasks are unchanged + assert set(project.tasks.values_list('id', flat=True)) == set(initial_project_tasks) diff --git a/rdmo/projects/tests/test_handlers_views.py b/rdmo/projects/tests/test_handlers_views.py index 6b872040f4..d88c6ca20c 100644 --- a/rdmo/projects/tests/test_handlers_views.py +++ b/rdmo/projects/tests/test_handlers_views.py @@ -1,24 +1,25 @@ - +from django.contrib.auth.models import Group from rdmo.projects.models import Project -from rdmo.questions.models import Catalog from rdmo.views.models import View +project_id = 10 +view_id = 3 +group_name = 'view_test' def test_project_views_sync_when_adding_or_removing_a_catalog_to_or_from_a_view(db, settings): assert settings.PROJECT_VIEWS_SYNC # Setup: Create a catalog, a view, and a project using the catalog - catalog = Catalog.objects.first() - view = View.objects.get(pk=3) - # view.catalogs.clear() - project = Project.objects.create(title="Test Project", catalog=catalog) - pr10 = Project.objects.get(pk=10) + project = Project.objects.get(id=project_id) + catalog = project.catalog + view = View.objects.get(id=view_id) # this view does not have catalogs in fixture + view.catalogs.clear() + initial_project_views = project.views.values_list('id', flat=True) # # Initially, the project should not have the view - # assert view not in project.views.all() - # assert view not in pr10.views.all() + assert view not in project.views.all() ## Tests for .add and .remove # Add the catalog to the view and assert that the project now includes the view @@ -28,7 +29,6 @@ def test_project_views_sync_when_adding_or_removing_a_catalog_to_or_from_a_view( # Remove the catalog from the view and assert that the project should no longer include the view view.catalogs.remove(catalog) assert view not in project.views.all() - assert view not in pr10.views.all() ## Tests for .set and .clear # Add the catalog to the view and assert that the project now includes the view @@ -38,4 +38,79 @@ def test_project_views_sync_when_adding_or_removing_a_catalog_to_or_from_a_view( # Remove the catalog from the view and assert that the project should no longer include the view view.catalogs.clear() assert view not in project.views.all() - assert view not in pr10.views.all() + + # assert that the initial project views are unchanged + assert set(project.views.values_list('id', flat=True)) == set(initial_project_views) + + +def test_project_views_sync_when_adding_or_removing_a_site_to_or_from_a_view(db, settings): + assert settings.PROJECT_VIEWS_SYNC + + # Setup: Get an existing project and its associated site and create a view + project = Project.objects.get(id=project_id) + site = project.site + view = View.objects.get(id=view_id) # This view does not have sites in the fixture + view.sites.clear() # Ensure the view starts without any sites + initial_project_views = project.views.values_list('id', flat=True) + + # Ensure initial state: The project should not have the view + assert view not in project.views.all() + + ## Tests for .add and .remove + # Add the site to the view and assert that the project now includes the view + view.sites.add(site) + assert view in project.views.all() + + # Remove the site from the view and assert that the project should no longer include the view + view.sites.remove(site) + assert view not in project.views.all() + + ## Tests for .set and .clear + # Add the site to the view and assert that the project now includes the view + view.sites.set([site]) + assert view in project.views.all() + + # Clear all sites from the view and assert that the project should no longer include the view + view.sites.clear() + assert view not in project.views.all() + + # Assert that the initial project views are unchanged + assert set(project.views.values_list('id', flat=True)) == set(initial_project_views) + + +def test_project_views_sync_when_adding_or_removing_a_group_to_or_from_a_view(db, settings): + assert settings.PROJECT_VIEWS_SYNC + + # Setup: Get an existing project, its associated group, and create a view + project = Project.objects.get(id=project_id) + # breakpoint() + user = project.owners.first() # Get the first user associated with the project + group = Group.objects.filter(name=group_name).first() # Get the first group the user belongs to + user.groups.add(group) + view = View.objects.get(id=view_id) # This view does not have groups in the fixture + view.groups.clear() # Ensure the view starts without any groups + initial_project_views = project.views.values_list('id', flat=True) + + # Ensure initial state: The project should not have the view + assert view not in project.views.all() + + ## Tests for .add and .remove + # Add the group to the view and assert that the project now includes the view + view.groups.add(group) + assert view in project.views.all() + + # Remove the group from the view and assert that the project should no longer include the view + view.groups.remove(group) + assert view not in project.views.all() + + ## Tests for .set and .clear + # Add the group to the view and assert that the project now includes the view + view.groups.set([group]) + assert view in project.views.all() + + # Clear all groups from the view and assert that the project should no longer include the view + view.groups.clear() + assert view not in project.views.all() + + # Assert that the initial project views are unchanged + assert set(project.views.values_list('id', flat=True)) == set(initial_project_views) diff --git a/testing/config/settings/base.py b/testing/config/settings/base.py index 0b8ef01a52..4133ff8ec4 100644 --- a/testing/config/settings/base.py +++ b/testing/config/settings/base.py @@ -69,17 +69,9 @@ PROJECT_SEND_INVITE = True -PROJECT_REMOVE_VIEWS = True -PROJECT_ADD_VIEWS = True - PROJECT_VIEWS_SYNC = True - -PROJECT_REMOVE_TASKS = True -PROJECT_ADD_TASKS = True - PROJECT_TASKS_SYNC = True - PROJECT_SNAPSHOT_EXPORTS = [ ('xml', _('RDMO XML'), 'rdmo.projects.exports.RDMOXMLExport'), ]