Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

875: ensure that site.species site-settings does not remove in-use taxonomy #926

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions app/modules/site_settings/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,18 @@
AutogeneratedNameType,
)

from .models import SiteSetting
from .models import SiteSetting, Taxonomy

assert isinstance(value, list)
# we have to start with *all* autogen names (not just auto_species) for sake of validation
new_autogen_names = SiteSetting.get_value('autogenerated_names') or {}

existing = {str(guid) for guid in Taxonomy.usage().keys() if guid}
for spec in value:
existing.discard(spec.get('id'))
if len(existing):
raise ValueError(f'Missing taxonomies in use: {existing}')

species_fields = [
('commonNames', list, True),
('scientificName', str, True),
Expand Down Expand Up @@ -93,7 +99,13 @@
AutogeneratedNameType,
)

from .models import SiteSetting
from .models import SiteSetting, Taxonomy

existing = {str(guid) for guid in Taxonomy.usage().keys() if guid}
for spec in value:
existing.discard(spec.get('id'))
if len(existing):
raise ValueError(f'Missing taxonomies in use: {existing}')

Check warning on line 108 in app/modules/site_settings/helpers.py

View check run for this annotation

Codecov / codecov/patch

app/modules/site_settings/helpers.py#L108

Added line #L108 was not covered by tests

# we have to start with *all* autogen names (not just auto_species) for sake of setting
new_autogen_names = SiteSetting.get_value('autogenerated_names') or {}
Expand Down
19 changes: 19 additions & 0 deletions app/modules/site_settings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,25 @@
return
raise ValueError('unknown id')

# this will return taxonomy_guid values *even if they are not* in site_settings! ymmv?
@classmethod
def usage(cls):
from app.extensions import db

counts = {}
# these are the places taxonomy_guid can be used:
tables = ['individual', 'encounter', 'sighting_taxonomies']
for table_name in tables:
res = db.session.execute(
f'SELECT taxonomy_guid, COUNT(*) FROM {table_name} GROUP BY taxonomy_guid'
)
for row in res:
if row[0] in counts:
counts[row[0]] += row[1]

Check warning on line 1060 in app/modules/site_settings/models.py

View check run for this annotation

Codecov / codecov/patch

app/modules/site_settings/models.py#L1060

Added line #L1060 was not covered by tests
else:
counts[row[0]] = row[1]
return counts

@classmethod
def get_configuration_value(cls):
from app.modules.site_settings.models import SiteSetting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def test_read_site_settings(flask_app_client, researcher_1):
@pytest.mark.skipif(
module_unavailable('site_settings'), reason='Site-settings module disabled'
)
def test_alter_settings(flask_app_client, admin_user):
def test_alter_settings(flask_app_client, admin_user, db):
from app.modules.individuals.models import Individual

response = conf_utils.read_main_settings(flask_app_client, admin_user)
assert 'value' in response.json['site.species']
vals = response.json['site.species']['value']
Expand All @@ -65,6 +67,22 @@ def test_alter_settings(flask_app_client, admin_user):
'site.species',
)

# create a faux individual with random taxonomy_guid to test failing update site.species without it
indiv = Individual()
indiv.taxonomy_guid = '00000000-eb82-471e-b3be-000000000000'
with db.session.begin():
db.session.add(indiv)
with pytest.raises(ValueError) as ve:
response = conf_utils.modify_main_settings(
flask_app_client,
admin_user,
vals,
'site.species',
)
assert 'Missing taxonomies' in str(ve)
with db.session.begin():
db.session.delete(indiv)


# TODO sort this out as part of DEX 1306
# note: some of this may have been picked up by test_alter_custom_field_categories() below
Expand Down
Loading