Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix: django full_clean does not work because of wrong exception is raised #106

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 20 additions & 13 deletions semantic_version/django_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import warnings

import django
from django.core.exceptions import ValidationError
from django.db import models

if django.VERSION >= (3, 0):
Expand Down Expand Up @@ -71,14 +72,17 @@ def deconstruct(self):

def to_python(self, value):
"""Converts any value to a base.Version field."""
if value is None or value == '':
return value
if isinstance(value, base.Version):
return value
if self.coerce:
return base.Version.coerce(value, partial=self.partial)
else:
return base.Version(value, partial=self.partial)
try:
if value is None or value == '':
return value
if isinstance(value, base.Version):
return value
if self.coerce:
return base.Version.coerce(value, partial=self.partial)
else:
return base.Version(value, partial=self.partial)
except ValueError as err:
raise ValidationError(err)


class SpecField(SemVerField):
Expand All @@ -100,8 +104,11 @@ def deconstruct(self):

def to_python(self, value):
"""Converts any value to a base.Spec field."""
if value is None or value == '':
return value
if isinstance(value, base.BaseSpec):
return value
return base.BaseSpec.parse(value, syntax=self.syntax)
try:
if value is None or value == '':
return value
if isinstance(value, base.BaseSpec):
return value
return base.BaseSpec.parse(value, syntax=self.syntax)
except ValueError as err:
raise ValidationError(err)
8 changes: 3 additions & 5 deletions tests/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import unittest

from semantic_version import Version, SimpleSpec, NpmSpec

from .setup_django import django_loaded


if django_loaded: # pragma: no cover
from semantic_version import django_fields
from .django_test_app import models
Expand All @@ -20,12 +18,12 @@
from django.test import TransactionTestCase
from django.test import runner as django_test_runner
from django.test import utils as django_test_utils
from django.core.exceptions import ValidationError

else:
DjangoTestCase = unittest.TestCase
TransactionTestCase = unittest.TestCase


test_state = {}


Expand Down Expand Up @@ -139,10 +137,10 @@ def test_coerce_clean(self):

def test_invalid_input(self):
v = models.VersionModel(version='0.1.1', spec='blah')
self.assertRaises(ValueError, v.full_clean)
self.assertRaises(ValidationError, v.full_clean)

v2 = models.VersionModel(version='0.1', spec='==0.1.1,!=0.1.1-alpha')
self.assertRaises(ValueError, v2.full_clean)
self.assertRaises(ValidationError, v2.full_clean)

def test_partial(self):
obj = models.PartialVersionModel(partial=Version('0.1.0'))
Expand Down