Skip to content

Commit

Permalink
Add tests for progress and navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenklar committed Sep 28, 2023
1 parent 3f97a95 commit b18342f
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 15 deletions.
67 changes: 67 additions & 0 deletions rdmo/projects/tests/test_navigation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import pytest

from rdmo.projects.models import Project
from rdmo.projects.progress import compute_navigation

sections = (
'http://example.com/terms/questions/catalog/individual',
'http://example.com/terms/questions/catalog/collections',
'http://example.com/terms/questions/catalog/set',
'http://example.com/terms/questions/catalog/conditions',
'http://example.com/terms/questions/catalog/blocks'
)

# (count, total, show) for each page or section (as default fallback)
result_map = {
'http://example.com/terms/questions/catalog/individual': (1, 1, True),
'http://example.com/terms/questions/catalog/individual/autocomplete': (0, 1, True),
'http://example.com/terms/questions/catalog/collections': (1, 1, True),
'http://example.com/terms/questions/catalog/collections/autocomplete': (0, 1, True),
'http://example.com/terms/questions/catalog/set/individual-single': (8, 9, True),
'http://example.com/terms/questions/catalog/set/individual-collection': (9, 10, True),
'http://example.com/terms/questions/catalog/set/collection-single': (14, 18, True),
'http://example.com/terms/questions/catalog/set/collection-collection': (16, 20, True),
'http://example.com/terms/questions/catalog/conditions/input': (2, 2, True),
'http://example.com/terms/questions/catalog/conditions/text_contains': (1, 1, True),
'http://example.com/terms/questions/catalog/conditions/text_empty': (1, 1, False),
'http://example.com/terms/questions/catalog/conditions/text_equal': (1, 1, True),
'http://example.com/terms/questions/catalog/conditions/text_greater_than': (1, 1, False),
'http://example.com/terms/questions/catalog/conditions/text_greater_than_equal': (1, 1, False),
'http://example.com/terms/questions/catalog/conditions/text_lesser_than': (1, 1, False),
'http://example.com/terms/questions/catalog/conditions/text_lesser_than_equal': (1, 1, False),
'http://example.com/terms/questions/catalog/conditions/text_not_empty': (1, 1, True),
'http://example.com/terms/questions/catalog/conditions/text_not_equal': (1, 1, False),
'http://example.com/terms/questions/catalog/conditions/option_empty': (1, 1, False),
'http://example.com/terms/questions/catalog/conditions/option_equal': (1, 1, True),
'http://example.com/terms/questions/catalog/conditions/option_not_empty': (1, 1, True),
'http://example.com/terms/questions/catalog/conditions/option_not_equal': (1, 1, False),
'http://example.com/terms/questions/catalog/conditions/set': (0, 2, True),
'http://example.com/terms/questions/catalog/conditions/set_set': (0, 2, True),
'http://example.com/terms/questions/catalog/conditions/optionset': (0, 2, True),
'http://example.com/terms/questions/catalog/conditions/text_set': (0, 2, True),
'http://example.com/terms/questions/catalog/blocks/set': (9, 18, True),
}


@pytest.mark.parametrize('section_uri', sections)
def test_compute_navigation(db, section_uri):
project = Project.objects.get(id=1)
project.catalog.prefetch_elements()

section = project.catalog.sections.get(uri=section_uri)

navigation = compute_navigation(section, project)
assert [item['id'] for item in navigation] == [element.id for element in project.catalog.elements]

for section in navigation:
if 'pages' in section:
for page in section['pages']:
if page['uri'] in result_map:
count, total, show = result_map[page['uri']]
elif section['uri'] in result_map:
count, total, show = result_map[section['uri']]
else:
raise AssertionError('{uri} not in result_map'.format(**page))
assert page['count'] == count, page['uri']
assert page['total'] == total, page['uri']
assert page['show'] == show, page['uri']
21 changes: 21 additions & 0 deletions rdmo/projects/tests/test_progress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest

from rdmo.projects.models import Project
from rdmo.projects.progress import compute_progress

projects = [1, 11]

results_map = {
1: (58, 87),
11: (0, 29)
}


@pytest.mark.parametrize('project_id', projects)
def test_compute_progress(db, project_id):
project = Project.objects.get(id=project_id)
project.catalog.prefetch_elements()

progress = compute_progress(project)

assert progress == results_map[project_id]
23 changes: 8 additions & 15 deletions rdmo/projects/tests/test_viewset_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,17 @@
'list': 'v1-projects:project-list',
'detail': 'v1-projects:project-detail',
'overview': 'v1-projects:project-overview',
'navigation': 'v1-projects:project-navigation',
'resolve': 'v1-projects:project-resolve',
'progress': 'v1-projects:project-progress'
}

projects = [1, 2, 3, 4, 5]
conditions = [1]

project_values = 37
project_total = 54
catalog_id = 1
catalog_id_not_available = 2

section_id = 1

@pytest.mark.parametrize('username,password', users)
def test_list(db, client, username, password):
Expand Down Expand Up @@ -241,15 +240,15 @@ def test_overview(db, client, username, password, project_id, condition_id):
@pytest.mark.parametrize('username,password', users)
@pytest.mark.parametrize('project_id', projects)
@pytest.mark.parametrize('condition_id', conditions)
def test_resolve(db, client, username, password, project_id, condition_id):
def test_navigation(db, client, username, password, project_id, condition_id):
client.login(username=username, password=password)

url = reverse(urlnames['resolve'], args=[project_id]) + f'?condition={condition_id}'
url = reverse(urlnames['navigation'], args=[project_id, section_id])
response = client.get(url)

if project_id in view_project_permission_map.get(username, []):
assert response.status_code == 200
assert isinstance(response.json(), dict)
assert isinstance(response.json(), list)
else:
if password:
assert response.status_code == 404
Expand All @@ -259,22 +258,16 @@ def test_resolve(db, client, username, password, project_id, condition_id):

@pytest.mark.parametrize('username,password', users)
@pytest.mark.parametrize('project_id', projects)
def test_progress(db, client, username, password, project_id):
@pytest.mark.parametrize('condition_id', conditions)
def test_resolve(db, client, username, password, project_id, condition_id):
client.login(username=username, password=password)

url = reverse(urlnames['progress'], args=[project_id])
url = reverse(urlnames['resolve'], args=[project_id]) + f'?condition={condition_id}'
response = client.get(url)

if project_id in view_project_permission_map.get(username, []):
assert response.status_code == 200
assert isinstance(response.json(), dict)

if project_id == 1:
assert response.json().get('values') == project_values
else:
assert response.json().get('values') == 1

assert response.json().get('total') == project_total
else:
if password:
assert response.status_code == 404
Expand Down
93 changes: 93 additions & 0 deletions rdmo/projects/tests/test_viewset_project_progress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import pytest

from django.urls import reverse

from ..models import Project

users = (
('owner', 'owner'),
('manager', 'manager'),
('author', 'author'),
('guest', 'guest'),
('api', 'api'),
('user', 'user'),
('site', 'site'),
('anonymous', None),
)

view_progress_permission_map = {
'owner': [1, 2, 3, 4, 5, 10],
'manager': [1, 3, 5, 7],
'author': [1, 3, 5, 8],
'guest': [1, 3, 5, 9],
'api': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
'site': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
}

change_progress_permission_map = {
'owner': [1, 2, 3, 4, 5, 10],
'manager': [1, 3, 5, 7],
'author': [1, 3, 5, 8],
'api': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
'site': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
}

urlnames = {
'progress': 'v1-projects:project-progress'
}

projects = [1, 2, 3, 4, 5]

@pytest.mark.parametrize('username,password', users)
@pytest.mark.parametrize('project_id', projects)
def test_progress_get(db, client, username, password, project_id):
client.login(username=username, password=password)

url = reverse(urlnames['progress'], args=[project_id])
response = client.get(url)

if project_id in view_progress_permission_map.get(username, []):
assert response.status_code == 200
assert isinstance(response.json(), dict)

project = Project.objects.get(id=project_id)
assert response.json()['count'] == project.progress_count
assert response.json()['total'] == project.progress_total

else:
if password:
assert response.status_code == 404
else:
assert response.status_code == 401


@pytest.mark.parametrize('username,password', users)
@pytest.mark.parametrize('project_id', projects)
def test_progress_post(db, client, username, password, project_id):
client.login(username=username, password=password)

if project_id in change_progress_permission_map.get(username, []):
# set project count and value to a different value
project = Project.objects.get(id=project_id)
project.progress_count = 0
project.progress_total = 0
project.save()

url = reverse(urlnames['progress'], args=[project_id])
response = client.post(url)

if project_id in change_progress_permission_map.get(username, []):
assert response.status_code == 200
assert isinstance(response.json(), dict)

project.refresh_from_db()
assert response.json()['count'] > 0
assert response.json()['total'] > 0

else:
if project_id in view_progress_permission_map.get(username, []):
assert response.status_code == 403
elif password:
assert response.status_code == 404
else:
assert response.status_code == 401

0 comments on commit b18342f

Please sign in to comment.