Skip to content

Commit

Permalink
Reapply and tweak stage validation for `site_address_is_company_addre…
Browse files Browse the repository at this point in the history
…ss` field

Reapplies PR #5928 (commit 51232c9), which also modified the validation for site address fields. These are now only required if `site_address_is_company_address` is not None.

The only difference is this commit keeps the validation for the `actual_uk_regions` field.
  • Loading branch information
oliverjwroberts committed Feb 4, 2025
1 parent e762bb1 commit 1e675ae
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 16 deletions.
1 change: 1 addition & 0 deletions datahub/investment/project/test/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class VerifyWinInvestmentProjectFactory(ActiveInvestmentProjectFactory):
non_fdi_r_and_d_budget = False
new_tech_to_uk = True
export_revenue = True
site_address_is_company_address = False
address_1 = factory.Faker('street_address')
address_town = factory.Faker('city')
address_postcode = factory.Faker('postcode')
Expand Down
66 changes: 63 additions & 3 deletions datahub/investment/project/test/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from datahub.investment.project.test.factories import (
ActiveInvestmentProjectFactory,
InvestmentProjectFactory,
VerifyWinInvestmentProjectFactory,
)
from datahub.investment.project.validate import (
_get_desired_stage_order,
Expand Down Expand Up @@ -271,6 +272,7 @@ def test_validate_verify_win_instance_failure():
total_investment=100,
client_considering_other_countries=False,
client_requirements='client reqs',
site_address_is_company_address=None,
site_decided=False,
strategic_drivers=strategic_drivers,
uk_region_locations=[random_obj_for_model(UKRegion)],
Expand All @@ -287,9 +289,7 @@ def test_validate_verify_win_instance_failure():
'non_fdi_r_and_d_budget': 'This field is required.',
'new_tech_to_uk': 'This field is required.',
'export_revenue': 'This field is required.',
'address_1': 'This field is required.',
'address_town': 'This field is required.',
'address_postcode': 'This field is required.',
'site_address_is_company_address': 'This field is required.',
'actual_uk_regions': 'This field is required.',
'delivery_partners': 'This field is required.',
'client_cannot_provide_foreign_investment': 'This field is required.',
Expand Down Expand Up @@ -345,6 +345,7 @@ def test_validate_verify_win_instance_with_cond_fields():
number_new_jobs=10,
client_considering_other_countries=False,
client_requirements='client reqs',
site_address_is_company_address=False,
site_decided=False,
strategic_drivers=strategic_drivers,
uk_region_locations=[random_obj_for_model(UKRegion)],
Expand Down Expand Up @@ -396,3 +397,62 @@ def test_likelihood_to_land_assign_pm_stage_missing_error():
def test_get_desired_stage_order(desired_stage, next_stage, expected_stage_order):
"""Test get desired stage order."""
assert _get_desired_stage_order(desired_stage, next_stage) == expected_stage_order


def test_site_address_is_company_address_is_marked_incomplete_at_active_stage():
project = ActiveInvestmentProjectFactory(
site_address_is_company_address=None,
address_1=None,
address_town=None,
address_postcode=None,
)
incomplete_fields = project.incomplete_fields # this calls validate(self, next_stage=True)
assert 'site_address_is_company_address' in incomplete_fields
# Address fields are conditionally marked if site_address_is_company_address has a value
assert 'address_1' not in incomplete_fields
assert 'address_town' not in incomplete_fields
assert 'address_postcode' not in incomplete_fields


@pytest.mark.parametrize('value', (True, False))
def test_site_address_fields_are_marked_incomplete_at_active_stage(value):
project = ActiveInvestmentProjectFactory(
site_address_is_company_address=False,
address_1=None,
address_town=None,
address_postcode=None,
)
incomplete_fields = project.incomplete_fields # this calls validate(self, next_stage=True)
assert 'site_address_is_company_address' not in incomplete_fields
# Address fields are conditionally marked if site_address_is_company_address has a value
assert 'address_1' in incomplete_fields
assert 'address_town' in incomplete_fields
assert 'address_postcode' in incomplete_fields


def test_site_address_is_company_address_is_required_at_verify_win_stage():
project = VerifyWinInvestmentProjectFactory(
site_address_is_company_address=None,
)
errors = validate(instance=project)
assert 'site_address_is_company_address' in errors
# Address fields are conditionally required if site_address_is_company_address has a value
assert 'address_1' not in errors
assert 'address_town' not in errors
assert 'address_postcode' not in errors


@pytest.mark.parametrize('value', (True, False))
def test_site_address_fields_are_required_at_verify_win_stage_after_condition_met(value):
project = VerifyWinInvestmentProjectFactory(
site_address_is_company_address=value,
address_1=None,
address_town=None,
address_postcode=None,
)
errors = validate(instance=project)
assert 'site_address_is_company_address' not in errors
# Address fields are conditionally required if site_address_is_company_address has a value
assert 'address_1' in errors
assert 'address_town' in errors
assert 'address_postcode' in errors
5 changes: 2 additions & 3 deletions datahub/investment/project/test/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1537,9 +1537,7 @@ def test_change_stage_verify_win_failure(self):
'non_fdi_r_and_d_budget': ['This field is required.'],
'new_tech_to_uk': ['This field is required.'],
'export_revenue': ['This field is required.'],
'address_1': ['This field is required.'],
'address_town': ['This field is required.'],
'address_postcode': ['This field is required.'],
'site_address_is_company_address': ['This field is required.'],
'actual_uk_regions': ['This field is required.'],
'delivery_partners': ['This field is required.'],
'client_cannot_provide_foreign_investment': ['This field is required.'],
Expand Down Expand Up @@ -1581,6 +1579,7 @@ def test_change_stage_verify_win_success(self, field):
associated_non_fdi_r_and_d_project=InvestmentProjectFactory(),
new_tech_to_uk=True,
export_revenue=True,
site_address_is_company_address=False,
address_1='12 London Road',
address_town='London',
address_postcode='SW1A 2AA',
Expand Down
20 changes: 17 additions & 3 deletions datahub/investment/project/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def _get_to_many_id(instance):
return instance.id


def _is_not_none(value):
return value is not None


class InvestmentProjectStageValidationConfig:
"""Investment Project stage validation config."""

Expand All @@ -54,9 +58,7 @@ def get_required_fields_after_stage(cls):
'non_fdi_r_and_d_budget': Stage.verify_win.value,
'new_tech_to_uk': Stage.verify_win.value,
'export_revenue': Stage.verify_win.value,
'address_1': Stage.verify_win.value,
'address_town': Stage.verify_win.value,
'address_postcode': Stage.verify_win.value,
'site_address_is_company_address': Stage.verify_win.value,
'actual_uk_regions': Stage.verify_win.value,
'delivery_partners': Stage.verify_win.value,
'actual_land_date': Stage.verify_win.value,
Expand Down Expand Up @@ -105,6 +107,18 @@ def get_conditional_rules_after_stage(cls):
CondValRule(
'allow_blank_possible_uk_regions', False, Stage.assign_pm.value,
),
'address_1':
CondValRule(
'site_address_is_company_address', _is_not_none, Stage.verify_win.value,
),
'address_town':
CondValRule(
'site_address_is_company_address', _is_not_none, Stage.verify_win.value,
),
'address_postcode':
CondValRule(
'site_address_is_company_address', _is_not_none, Stage.verify_win.value,
),
'foreign_equity_investment':
CondValRule(
'client_cannot_provide_foreign_investment', not_, Stage.verify_win.value,
Expand Down
17 changes: 10 additions & 7 deletions datahub/search/investment/test/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,11 @@ def test_incomplete_fields_syncs_when_project_changes(opensearch_with_signals):
"""
project = InvestmentProjectFactory(
stage_id=InvestmentProjectStage.won.value.id,
likelihood_to_land_id=None)
likelihood_to_land_id=None,
address_1=None,
address_town=None,
address_postcode=None,
)
adviser = AdviserFactory()

opensearch_with_signals.indices.refresh()
Expand All @@ -367,9 +371,7 @@ def test_incomplete_fields_syncs_when_project_changes(opensearch_with_signals):
'non_fdi_r_and_d_budget',
'new_tech_to_uk',
'export_revenue',
'address_1',
'address_town',
'address_postcode',
'site_address_is_company_address',
'actual_uk_regions',
'delivery_partners',
'actual_land_date',
Expand All @@ -396,9 +398,7 @@ def test_incomplete_fields_syncs_when_project_changes(opensearch_with_signals):
project.non_fdi_r_and_d_budget = True
project.new_tech_to_uk = True
project.export_revenue = True
project.address_1 = 'Downing Street'
project.address_town = 'London'
project.address_postcode = 'SW1A 2AA'
project.site_address_is_company_address = False
project.actual_land_date = date(2020, 1, 1)
project.total_investment = Decimal('100.00')
project.foreign_equity_investment = Decimal('50.00')
Expand All @@ -419,6 +419,9 @@ def test_incomplete_fields_syncs_when_project_changes(opensearch_with_signals):
'likelihood_to_land',
'competitor_countries',
'uk_region_locations',
'address_1',
'address_town',
'address_postcode',
'average_salary',
'associated_non_fdi_r_and_d_project',
]
Expand Down

0 comments on commit 1e675ae

Please sign in to comment.