|
1 | 1 | import pytest
|
2 | 2 |
|
3 | 3 | from django.shortcuts import reverse
|
| 4 | +from django.contrib.contenttypes.models import ContentType |
4 | 5 |
|
5 |
| -from topobank.manager.tests.utils import Topography1DFactory, UserFactory |
| 6 | +from topobank.manager.tests.utils import Topography1DFactory, UserFactory, SurfaceFactory |
6 | 7 | from topobank.manager.utils import subjects_to_json
|
7 |
| -from .utils import AnalysisFunctionFactory |
| 8 | +from topobank.manager.models import Analysis, Topography, Surface |
| 9 | + |
| 10 | +from .utils import AnalysisFunctionFactory, TopographyAnalysisFactory, SurfaceAnalysisFactory,\ |
| 11 | + AnalysisFunctionImplementationFactory |
8 | 12 | from ..views import card_view_class, SimpleCardView, PlotCardView, PowerSpectrumCardView
|
9 | 13 |
|
10 | 14 |
|
@@ -84,6 +88,52 @@ def test_card_templates_for_power_spectrum(client, mocker, handle_usage_statisti
|
84 | 88 | assert response.template_name == ['analysis/powerspectrum_card_detail.html']
|
85 | 89 |
|
86 | 90 |
|
| 91 | +@pytest.mark.django_db |
| 92 | +def test_plot_card_if_no_successful_topo_analysis(client, handle_usage_statistics): |
| 93 | + # |
| 94 | + # Create database objects |
| 95 | + # |
| 96 | + password = "secret" |
| 97 | + user = UserFactory(password=password) |
| 98 | + topography_ct = ContentType.objects.get_for_model(Topography) |
| 99 | + surface_ct = ContentType.objects.get_for_model(Surface) |
| 100 | + func1 = AnalysisFunctionFactory(card_view_flavor='power spectrum') |
| 101 | + AnalysisFunctionImplementationFactory(function=func1, subject_type=topography_ct) |
| 102 | + AnalysisFunctionImplementationFactory(function=func1, subject_type=surface_ct) |
| 103 | + |
| 104 | + surf = SurfaceFactory(creator=user) |
| 105 | + topo = Topography1DFactory(surface=surf) # also generates the surface |
| 106 | + |
| 107 | + # There is a successful surface analysis, but no successful topography analysis |
| 108 | + SurfaceAnalysisFactory(task_state='su', subject_id=topo.surface.id, |
| 109 | + subject_type_id=surface_ct.id, function=func1, users=[user]) |
| 110 | + |
| 111 | + # add a failed analysis for the topography |
| 112 | + TopographyAnalysisFactory(task_state='fa', subject_id=topo.id, |
| 113 | + subject_type_id=topography_ct.id, function=func1, users=[user]) |
| 114 | + |
| 115 | + |
| 116 | + assert Analysis.objects.filter(function=func1, subject_id=topo.id, subject_type_id=topography_ct.id, |
| 117 | + task_state='su').count() == 0 |
| 118 | + assert Analysis.objects.filter(function=func1, subject_id=topo.id, subject_type_id=topography_ct.id, |
| 119 | + task_state='fa').count() == 1 |
| 120 | + assert Analysis.objects.filter(function=func1, subject_id=topo.surface.id, subject_type_id=surface_ct.id, |
| 121 | + task_state='su').count() == 1 |
| 122 | + |
| 123 | + # login and request plot card view |
| 124 | + assert client.login(username=user.username, password=password) |
| 125 | + |
| 126 | + response = client.post(reverse('analysis:card'), data={ |
| 127 | + 'function_id': func1.id, |
| 128 | + 'card_id': 'card', |
| 129 | + 'template_flavor': 'list', |
| 130 | + 'subjects_ids_json': subjects_to_json([topo, topo.surface]), # also request results for surface here |
| 131 | + }, HTTP_X_REQUESTED_WITH='XMLHttpRequest') # we need an AJAX request |
| 132 | + |
| 133 | + # should return without errors |
| 134 | + assert response.status_code == 200 |
| 135 | + |
| 136 | + |
87 | 137 | def test_card_view_class():
|
88 | 138 | assert card_view_class('simple') == SimpleCardView
|
89 | 139 | assert card_view_class('plot') == PlotCardView
|
|
0 commit comments