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

VersionField and SpecField shouldn't accept strings, only Version / Spec objects. #97

Open
ejosan opened this issue Mar 27, 2020 · 3 comments
Labels
Status:CodeWelcome This feature design has been approved; next step: write the code! Topic:Django Issues related to Django integration Type:Bug

Comments

@ejosan
Copy link

ejosan commented Mar 27, 2020

problem

running get_or_create twice will result in the first run to fail and the second to pass

the model

from django.db.models import Model
from semantic_version.django_fields import VersionField
class SemVersion(Model):
    version = VersionField(max_length=200)

test script

# -*- coding: utf-8 -*-
# from __future__ import absolute_import, unicode_literals, print_function
import unittest
import django
django.setup()
from apps.version.models import SemVersion

class SemverTest(unittest.TestCase):

    def test_one(self):
        obj1,_ = SemVersion.objects.get_or_create(version='0.1.1+build')
        print(obj1)
        print(obj1.version.major)
        print(obj1.version.minor)
        print(obj1.version.patch)
        print(obj1.version.prerelease)
        print(obj1.version.build)

if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTest(SemverTest('test_one'))

    result = unittest.TextTestRunner(verbosity=2, resultclass=unittest.TextTestResult).run(suite)

output first run:

test_one (__main__.SemverTest) ... SemVersion object
ERROR

======================================================================
ERROR: test_one (__main__.SemverTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/ONT/user/apps/version/tests/test.py", line 20, in test_one
    print(obj1.version.major)
AttributeError: 'str' object has no attribute 'major'

----------------------------------------------------------------------
Ran 1 test in 0.022s

FAILED (errors=1)

output second run:

test_one (__main__.SemverTest) ... SemVersion object
0
1
1
()
('build',)
ok

----------------------------------------------------------------------
Ran 1 test in 0.015s

OK

looks like the the create part of get_or_create returns not an object but a string, the second time get_or_create is run the get part kicks in and returns a correct version object. This is inconsistent and should work. Tested it with python 3.6 and 2.7 both same results. Django 1.11.15

@rbarrois rbarrois added Status:CodeWelcome This feature design has been approved; next step: write the code! Topic:Django Issues related to Django integration Type:Bug labels Jul 7, 2020
@rbarrois
Copy link
Owner

rbarrois commented Jul 7, 2020

Thanks for the report! That's indeed a bug, likely because Django's get_or_create returns the value as is.

rbarrois added a commit that referenced this issue Jul 7, 2020
@rbarrois
Copy link
Owner

rbarrois commented Jul 7, 2020

After taking a deeper look, it seems to be a standard behaviour from Django: if the developer provides a badly-typed-but-coercable value, it might be used properly by the database, but the value on the model won't be changed.

This is, unfortunately, not properly documented in Django.

Here are a couple of links on that topic:

Basically, a VersionField shouldn't accept a string, only a Version object; that's the bug ;)

@rbarrois rbarrois changed the title SemVersion.objects.get_or_create invalid behaviour VersionField and SpecField shouldn't accept strings, only Version / Spec objects. Jul 7, 2020
@ejosan
Copy link
Author

ejosan commented Jul 8, 2020

After taking a deeper look, it seems to be a standard behaviour from Django: if the developer provides a badly-typed-but-coercable value, it might be used properly by the database, but the value on the model won't be changed.

This is, unfortunately, not properly documented in Django.

Here are a couple of links on that topic:

Basically, a VersionField shouldn't accept a string, only a Version object; that's the bug ;)

;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status:CodeWelcome This feature design has been approved; next step: write the code! Topic:Django Issues related to Django integration Type:Bug
Projects
None yet
Development

No branches or pull requests

2 participants