-
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.
Add tests for progress and navigation
- Loading branch information
1 parent
3f97a95
commit b18342f
Showing
4 changed files
with
189 additions
and
15 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,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'] |
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,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] |
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
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,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 |