From bebb010e9627ce02afe868cd3d406ff066eec914 Mon Sep 17 00:00:00 2001 From: Jens Diemer Date: Mon, 31 May 2021 15:51:27 +0200 Subject: [PATCH] Fix #27 `Data too long for column 'desc'` --- huey_monitor/tqdm.py | 10 ++++++++ huey_monitor_tests/tests/test_tqdm.py | 35 ++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/huey_monitor/tqdm.py b/huey_monitor/tqdm.py index 3f28118..60d4a3a 100644 --- a/huey_monitor/tqdm.py +++ b/huey_monitor/tqdm.py @@ -1,5 +1,6 @@ import logging +from django.core.exceptions import ValidationError from django.utils import timezone from huey.api import Task @@ -40,6 +41,15 @@ def __init__(self, self.unit_divisor = unit_divisor self.parent_task_id = parent_task_id + if len(self.desc) > 64: + # We call .update() that will not validate the data, so a overlong + # description will raise a database error and maybe a user doesn't know + # what's happen ;) + raise ValidationError( + 'Process info description overlong: %(desc)r', + params={'desc': self.desc}, + ) + TaskModel.objects.filter(task_id=task.id).update( desc=self.desc, total=self.total, diff --git a/huey_monitor_tests/tests/test_tqdm.py b/huey_monitor_tests/tests/test_tqdm.py index 45ef1f7..8bcfdc5 100644 --- a/huey_monitor_tests/tests/test_tqdm.py +++ b/huey_monitor_tests/tests/test_tqdm.py @@ -5,12 +5,14 @@ from bx_django_utils.test_utils.datetime import MockDatetimeGenerator from bx_py_utils.test_utils.datetime import parse_dt +from django.core.exceptions import ValidationError from django.test import TestCase from django.utils import timezone -from huey.api import Result +from huey.api import Result, Task import huey_monitor from huey_monitor.models import SignalInfoModel, TaskModel, TaskProgressModel +from huey_monitor.tqdm import ProcessInfo from huey_monitor_tests.test_app.tasks import linear_processing_task, parallel_task @@ -168,3 +170,34 @@ def __call__(self, *args, **kwargs): assert progress == [ (30, '1.00it'), (33, '1.00it'), (36, '1.00it'), (39, '1.00it'), (42, '1.00it') ] + + def test_process_description_overlong(self): + TaskModel.objects.create(task_id='00000000-0000-0000-0000-000000000001') + + # Test with current max length: + max_length = TaskModel._meta.get_field('desc').max_length + assert max_length == 64 + + task = Task(id='00000000-0000-0000-0000-000000000001') + + # Set max length description: + with self.assertLogs('huey_monitor.tqdm') as logs: + ProcessInfo(task, desc='X' * max_length) + instance = TaskModel.objects.get() + assert instance.desc == 'X' * max_length + + assert logs.output == [ + ( + 'INFO:huey_monitor.tqdm:Init TaskModel Task' + ' - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' + ' 0/Noneit (divisor: 1000)' + ) + ] + + # Overlong description should be cut: + msg = ( + '["Process info description overlong:' + ' \'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY\'"]' + ) + with self.assertRaisesMessage(ValidationError, msg): + ProcessInfo(task, desc='Y' * (max_length + 1), total=999)