Skip to content

Commit

Permalink
Fix #27 Data too long for column 'desc'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jens Diemer committed May 31, 2021
1 parent 3f2bd0c commit 2a1d8f7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
10 changes: 10 additions & 0 deletions huey_monitor/tqdm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging

from django.core.exceptions import ValidationError
from django.utils import timezone
from huey.api import Task

Expand Down Expand Up @@ -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
# desciption 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,
Expand Down
35 changes: 34 additions & 1 deletion huey_monitor_tests/tests/test_tqdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)

0 comments on commit 2a1d8f7

Please sign in to comment.