-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests(project, handlers): add tests for tasks and views handler fuctions
Signed-off-by: David Wallace <[email protected]>
- Loading branch information
Showing
3 changed files
with
224 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters