Skip to content

Commit

Permalink
tests(project, handlers): add tests for tasks and views handler fuctions
Browse files Browse the repository at this point in the history
Signed-off-by: David Wallace <[email protected]>
  • Loading branch information
MyPyDavid committed Jan 7, 2025
1 parent aa42691 commit 15fd305
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 23 deletions.
5 changes: 5 additions & 0 deletions rdmo/projects/tests/helpers.py
Original file line number Diff line number Diff line change
@@ -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])
145 changes: 133 additions & 12 deletions rdmo/projects/tests/test_handlers_tasks.py
Original file line number Diff line number Diff line change
@@ -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)
97 changes: 86 additions & 11 deletions rdmo/projects/tests/test_handlers_views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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)

0 comments on commit 15fd305

Please sign in to comment.