Skip to content

Commit

Permalink
Testing in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
EPedley committed Dec 5, 2024
1 parent e950a14 commit 819e8a0
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 64 deletions.
6 changes: 3 additions & 3 deletions barriers/forms/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class UpdatePreliminaryAssessmentForm(APIFormMixin, forms.Form):
},
)

preliminary_value_explanation = forms.CharField(
preliminary_value_details = forms.CharField(
widget=forms.Textarea,
label="Provide an explanation on how the value has been assessed",
error_messages={"required": "Enter details of the preliminary value"},
Expand All @@ -83,13 +83,13 @@ def save(self):
client.preliminary_assessment.patch_preliminary_assessment(
barrier_id=self.barrier.id,
value=self.cleaned_data["preliminary_value"],
details=self.cleaned_data["preliminary_value_explanation"],
details=self.cleaned_data["preliminary_value_details"],
)
else:
client.preliminary_assessment.create_preliminary_assessment(
barrier_id=self.barrier.id,
value=self.cleaned_data["preliminary_value"],
details=self.cleaned_data["preliminary_value_explanation"],
details=self.cleaned_data["preliminary_value_details"],
)


Expand Down
3 changes: 2 additions & 1 deletion barriers/models/history/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
ActionPlanTaskHistoryItem,
)

from .assessments.economic import EconomicAssessmentHistoryItem
from .assessments.economic import EconomicAssessmentHistoryItem, PreliminaryAssessmentHistoryItem
from .assessments.economic_impact import EconomicImpactAssessmentHistoryItem
from .assessments.resolvability import ResolvabilityAssessmentHistoryItem
from .assessments.strategic import StrategicAssessmentHistoryItem
Expand Down Expand Up @@ -45,5 +45,6 @@ class HistoryItem(PolymorphicBase):
ProgressUpdateHistoryItem,
BarrierTopPrioritySummaryItem,
ProgrammeFundsHistoryItem,
PreliminaryAssessmentHistoryItem,
)
class_lookup = {}
21 changes: 21 additions & 0 deletions barriers/models/history/assessments/economic.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,24 @@ class EconomicAssessmentHistoryItem(PolymorphicBase):
)
default_subclass = GenericHistoryItem
class_lookup = {}


class PreliminaryAssessmentValueHistoryItem(BaseHistoryItem):
field = "value"
field_name = "Preliminary assessment value"


class PreliminaryAssessmentDetailsHistoryItem(BaseHistoryItem):
field = "details"
field_name = "Preliminary assessment details"


class PreliminaryAssessmentHistoryItem(PolymorphicBase):
model = "preliminary_assessment"
key = "field"
subclasses = (
PreliminaryAssessmentValueHistoryItem,
PreliminaryAssessmentDetailsHistoryItem,
)
default_subclass = GenericHistoryItem
class_lookup = {}
2 changes: 1 addition & 1 deletion barriers/views/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def get_initial(self):
if self.preliminary_assessment:
return {
"preliminary_value": self.preliminary_assessment.value,
"preliminary_value_explanation": self.preliminary_assessment.details,
"preliminary_value_details": self.preliminary_assessment.details,
}
else:
return
Expand Down
1 change: 1 addition & 0 deletions barriers/views/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ def get_full_history(self):
barrier_id = self.kwargs.get("barrier_id")
full_history = client.barriers.get_full_history(barrier_id=barrier_id)
full_history.sort(key=lambda object: object.date, reverse=True)
print(full_history)
return full_history
20 changes: 20 additions & 0 deletions core/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
ReportsResource,
UserProfileResource,
UsersResource,
PreliminaryAssessmentResource,
)


Expand Down Expand Up @@ -182,6 +183,7 @@ def setUp(self):
self.init_get_public_barrier_patcher()
self.init_get_action_plans_patcher()
self.init_get_profile_patcher()
self.init_get_preliminary_assessment_patcher()

def init_session(self):
session = self.client.session
Expand Down Expand Up @@ -260,6 +262,14 @@ def init_get_profile_patcher(self):
self.mock_get_profile.return_value = self.profile
self.addCleanup(self.get_profile_patcher.stop)

def init_get_preliminary_assessment_patcher(self):
self.get_preliminary_assessment_patcher = patch("utils.api.resources.PreliminaryAssessmentResource.get_preliminary_assessment")
print('HELLO')
self.mock_get_preliminary_assessment = self.get_preliminary_assessment_patcher.start()
self.mock_get_preliminary_assessment.return_value = self.preliminary_assessment
print(self.mock_get_preliminary_assessment.return_value.value)
self.addCleanup(self.get_preliminary_assessment_patcher.stop)

def delete_session_key(self, key):
try:
del self.client.session[key]
Expand Down Expand Up @@ -485,3 +495,13 @@ def profile(self):
"overseas_regions": [],
}
)

@property
def preliminary_assessment(self):
return PreliminaryAssessmentResource.model(
{
"id": 1,
"value": "1",
"details": "test details",
}
)
2 changes: 1 addition & 1 deletion templates/barriers/assessments/preliminary_assessment.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{% csrf_token %}

{% include "partials/forms/radio_input.html" with field=form.preliminary_value %}
{% include "partials/forms/textarea.html" with field=form.preliminary_value_explanation %}
{% include "partials/forms/textarea.html" with field=form.preliminary_value_details %}

<button class="govuk-button" data-module="govuk-button">Save and return</button>
<a href="{% url 'barriers:assessment_detail' barrier.id %}" class="form-cancel">Cancel</a>
Expand Down
89 changes: 34 additions & 55 deletions tests/assessments/test_preliminary_assessment.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,53 @@
from http import HTTPStatus

from django.urls import reverse
from mock import patch
from mock import Mock, patch

from core.tests import MarketAccessTestCase


class EditCommercialValueTestCase(MarketAccessTestCase):
def test_commercial_value_has_initial_data(self):
class EditPreliminaryAssessmentTestCase(MarketAccessTestCase):

@patch("utils.api.resources.PreliminaryAssessmentResource.get_preliminary_assessment")
def test_preliminary_assessment_has_initial_data(self, mock_get_preliminary_assessment: Mock
):
mock_get_preliminary_assessment.return_value = self.preliminary_assessment
response = self.client.get(
reverse(
"barriers:edit_commercial_value",
"barriers:edit_preliminary_assessment",
kwargs={"barrier_id": self.barrier["id"]},
)
),
)

assert response.status_code == HTTPStatus.OK

assert "form" in response.context
form = response.context["form"]
assert form.initial["commercial_value"] == self.barrier["commercial_value"]
assert (
form.initial["commercial_value_explanation"]
== self.barrier["commercial_value_explanation"]
)

@patch("utils.api.resources.APIResource.patch")
def test_edit_commercial_value_calls_api(self, mock_patch):
response = self.client.post(
reverse(
"barriers:edit_commercial_value",
kwargs={"barrier_id": self.barrier["id"]},
),
data={
"commercial_value": "500003",
"commercial_value_explanation": "Wibble, wobble.",
assert form.initial["preliminary_value"] == self.preliminary_assessment.value
assert form.initial["preliminary_value_details"] == self.preliminary_assessment.details

@patch("utils.api.resources.PreliminaryAssessmentResource.patch_preliminary_assessment")
def test_edit_preliminary_assessment_calls_api(self, mock_patch_preliminary_assessment: Mock):
mock_patch_preliminary_assessment.return_value = True
barrier_id = self.barrier["id"]

url = reverse(
"barriers:edit_preliminary_assessment",
kwargs={
"barrier_id": barrier_id,
},
)
mock_patch.assert_called_with(
id=self.barrier["id"],
commercial_value=500003,
commercial_value_explanation="Wibble, wobble.",
response = self.client.patch(
url,
data={
"value": "2",
"details": "updated description",
}
)
assert response.status_code == HTTPStatus.FOUND

@patch("utils.api.resources.APIResource.patch")
def test_commercial_value_bad_value(self, mock_patch):
response = self.client.post(
reverse(
"barriers:edit_commercial_value",
kwargs={"barrier_id": self.barrier["id"]},
),
data={"commercial_value": "10-0"},
)
assert response.status_code == HTTPStatus.OK
form = response.context["form"]
assert form.is_valid() is False
assert "commercial_value" in form.errors
assert mock_patch.called is False
assert response.status_code == 200
assert response.url == f"/barriers/{barrier_id}/assessments"

assert mock_patch_preliminary_assessment.call_args[0][0] == barrier_id
assert mock_patch_preliminary_assessment.call_args[0][1] == 2
assert mock_patch_preliminary_assessment.call_args[0][2] == "updated description"

@patch("utils.api.resources.APIResource.patch")
def test_commercial_value_cannot_be_empty(self, mock_patch):
response = self.client.post(
reverse(
"barriers:edit_commercial_value",
kwargs={"barrier_id": self.barrier["id"]},
),
data={"commercial_value": ""},
)
assert response.status_code == HTTPStatus.OK
form = response.context["form"]
assert form.is_valid() is False
assert "commercial_value" in form.errors
assert mock_patch.called is False
2 changes: 2 additions & 0 deletions users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def permissions(self):
return self.data.get("permissions", [])

def has_permission(self, permission_name):
print('PERMISSIONS')
print(self.permissions)
if not self.is_active:
return False
return self.is_superuser or permission_name in self.permissions
Expand Down
3 changes: 0 additions & 3 deletions utils/api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,6 @@ class PreliminaryAssessmentResource(APIResource):

def get_preliminary_assessment(self, barrier_id):
url = f"barriers/{barrier_id}/preliminary-assessment"
print("API CALL")
print(self.client.get(url))
print(self.model(self.client.get(url)))
return self.model(self.client.get(url))

def create_preliminary_assessment(self, barrier_id, *args, **kwargs):
Expand Down

0 comments on commit 819e8a0

Please sign in to comment.