diff --git a/django_remote_submission/models.py b/django_remote_submission/models.py index ed50c36adbdbae3851feed1e32fe33da88dfb225..f1ffcf15beea36b72556a33bb33f44f8199234de 100644 --- a/django_remote_submission/models.py +++ b/django_remote_submission/models.py @@ -64,8 +64,8 @@ class ListField(models.TextField): # noqa: D101 return str(value) def value_to_string(self, obj): # noqa: D102 - value = self._get_val_from_obj(obj) - return self.get_db_prep_value(value) + value = self.value_from_object(obj) + return self.get_prep_value(value) class Interpreter(TimeStampedModel): diff --git a/tests/unit/test_models.py b/tests/unit/test_models.py index 4919dbbd5a88393923fd032624371b747d8805c8..653757c1c6f0de906898683147d98de1d2dded52 100644 --- a/tests/unit/test_models.py +++ b/tests/unit/test_models.py @@ -8,7 +8,7 @@ test_django-remote-submission Tests for `django-remote-submission` models module. """ # package imports -from django_remote_submission.models import IdentityFileModel, Interpreter, Log, Job, Result, Server +from django_remote_submission.models import ListField, IdentityFileModel, Interpreter, Log, Job, Result, Server from django_remote_submission.wrapper.remote import IdentityFile # third party imports @@ -20,6 +20,7 @@ import pytest # standard imports from pathlib import Path import os +from unittest.mock import MagicMock @pytest.fixture @@ -72,6 +73,13 @@ def result(job): ) +@pytest.mark.django_db +def test_interpreter_string_representation(interpreter): + assert str(interpreter.name) in str(interpreter) + assert str(interpreter.path) in str(interpreter) + assert str(interpreter.arguments) in str(interpreter) + + @pytest.mark.django_db def test_server_string_representation(server): assert str(server.title) in str(server) @@ -96,6 +104,26 @@ def test_result_string_representation(result): assert str(result.job) in str(result) +class TestListField: + def test_to_python(self): + for null_value in (None, "", [], 0): + assert isinstance(ListField().to_python(null_value), list) + items = [1, 2, 3] + assert id(ListField().to_python(items)) == id(items) + assert ListField().to_python('[1, 2, "three"]') == [1, 2, "three"] + + def test_get_prep_value(self): + for null_value in ("", [], 0): + assert ListField().get_prep_value(null_value) == str(null_value) + + def test_value_to_string(self): + items = ListField() + items.attname = 'mock_object' + obj = MagicMock() + obj.mock_object = 'fake' + assert items.value_to_string(obj) == 'fake' + + class TestIdentityFileModel: r''' @pytest.mark.django_db