Skip to content

Commit

Permalink
Added event location validation dfo-mar-odis#150
Browse files Browse the repository at this point in the history
  • Loading branch information
upsonp committed Jun 17, 2024
1 parent ff07bae commit e9b5134
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
5 changes: 3 additions & 2 deletions core/tests/TestHtmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ def setUp(self) -> None:
self.url = reverse_lazy(self.upload_elog_url, args=('default', self.mission.pk,))
self.client = Client()

@tag('utils_elog_upload_test_elog_uplaod_missing_mid')
def test_elog_uplaod_missing_mid(self):
logger.info("Running test_elog_uplaod_missing_mid")

file_name = 'missing_mid_bad.log'

with open(self.file_location+file_name, 'rb') as fp:
self.client.post(self.url, {'event': fp})
self.client.post(self.url, {'elog_event': fp})

errors = self.mission.file_errors.all()
self.assertTrue(errors.exists())
Expand All @@ -46,7 +47,7 @@ def test_elog_uplaod_missing_fields(self):
file_name = 'bad.log'

with open(os.path.join(self.file_location, file_name), 'rb') as fp:
self.client.post(self.url, {'event': fp})
self.client.post(self.url, {'elog_event': fp})

errors = self.mission.file_errors.all()
self.assertTrue(errors.exists())
Expand Down
28 changes: 22 additions & 6 deletions core/tests/TestValidation.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import datetime
from datetime import datetime

from dart.tests.DartTestCase import DartTestCase
from . import CoreFactoryFloor as core_factory
from django.test import tag

from core.models import ActionType as action_types
from core.validation import validate_event, validate_ctd_event, validate_net_event

from . import CoreFactoryFloor as core_factory

import logging

logger = logging.getLogger('dart.test')
Expand All @@ -15,8 +16,8 @@
@tag('validation', 'validation_general')
class TestGeneralEventValidation(DartTestCase):
def setUp(self) -> None:
self.start_date = datetime.datetime.strptime("2020-01-01 14:30:00", '%Y-%m-%d %H:%M:%S')
self.end_date = datetime.datetime.strptime("2020-02-01 14:30:00", '%Y-%m-%d %H:%M:%S')
self.start_date = datetime.strptime("2020-01-01 14:30:00", '%Y-%m-%d %H:%M:%S')
self.end_date = datetime.strptime("2020-02-01 14:30:00", '%Y-%m-%d %H:%M:%S')
self.mission = core_factory.MissionFactory(
start_date=self.start_date.date(),
end_date=self.end_date.date()
Expand Down Expand Up @@ -62,13 +63,28 @@ def test_validate_actions_other(self):
logger.debug(errors)
self.assertEquals(len(errors), 0)

def test_valid_location(self):
# events must have a valid start location, the start location is determined by an action, but actions
# are allowed to have blank lat/lon so during the validation we should check that the lat/lon is valid
event = core_factory.CTDEventFactoryBlank(mission=self.mission, sample_id=1, end_sample_id=10)
bad_action = core_factory.ActionFactory(event=event, latitude=None, longitude=None, type=action_types.bottom,
date_time=datetime.strptime("2020-01-02 14:30:00", '%Y-%m-%d %H:%M:%S'))

# start_location will return an array [lat, lon] if the action's lat/lon is blank the pair will be [None, None]
self.assertIsNone(event.start_location[0])
self.assertIsNone(event.start_location[1])

errors = validate_event(event)
logger.debug(errors)
self.assertEquals(len(errors), 1)


@tag('validation', 'validation_ctd')
class TestCTDEventValidation(DartTestCase):

def setUp(self) -> None:
self.start_date = datetime.datetime.strptime("2020-01-01 14:30:00", '%Y-%m-%d %H:%M:%S')
self.end_date = datetime.datetime.strptime("2020-02-01 14:30:00", '%Y-%m-%d %H:%M:%S')
self.start_date = datetime.strptime("2020-01-01 14:30:00", '%Y-%m-%d %H:%M:%S')
self.end_date = datetime.strptime("2020-02-01 14:30:00", '%Y-%m-%d %H:%M:%S')
self.mission = core_factory.MissionFactory(
start_date=self.start_date.date(),
end_date=self.end_date.date()
Expand Down
5 changes: 5 additions & 0 deletions core/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ def validate_event(event: core_models.Event) -> [core_models.ValidationError]:
err = core_models.ValidationError(event=event, message=message, type=core_models.ErrorType.validation)
validation_errors.append(err)

if event.start_location == [None, None]:
message = _("Event is missing an action with a valid location") + f' {event.event_id}'
err = core_models.ValidationError(event=event, message=message, type=core_models.ErrorType.validation)
validation_errors.append(err)

# Validate event does not have duplicate action types
mission = event.mission
if event.start_date is None or event.end_date is None:
Expand Down

0 comments on commit e9b5134

Please sign in to comment.