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

Add official support for Django 2.2. #46

Merged
merged 5 commits into from
Jan 27, 2020
Merged
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
5 changes: 5 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[run]
source = picklefield
branch = True

[report]
exclude_lines =
pragma: no cover
except ImportError:
60 changes: 15 additions & 45 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,54 +1,24 @@
dist: trusty
dist: bionic
sudo: false
language: python
env:
matrix:
- TOXENV=flake8
- TOXENV=isort
cache: pip
matrix:
python:
- 2.7
- 3.5
- 3.6
- 3.7
- 3.8
stages:
- lint
- test
jobs:
fast_finish: true
include:
- python: 2.7
env: TOXENV=py27-1.11
- python: 3.4
env: TOXENV=py34-1.11
- python: 3.4
env: TOXENV=py34-2.0
- python: 3.5
env: TOXENV=py35-1.11
- python: 3.5
env: TOXENV=py35-2.0
- python: 3.5
env: TOXENV=py35-2.1
- python: 3.5
env: TOXENV=py35-master
- python: 3.6
env: TOXENV=py36-1.11
- python: 3.6
env: TOXENV=py36-2.0
- python: 3.6
env: TOXENV=py36-2.1
- python: 3.6
env: TOXENV=py36-master
- python: 3.7
env: TOXENV=py37-2.0
dist: xenial
sudo: true
- python: 3.7
env: TOXENV=py37-2.1
dist: xenial
sudo: true
- python: 3.7
env: TOXENV=py37-master
dist: xenial
sudo: true
allow_failures:
- env: TOXENV=py35-master
- env: TOXENV=py36-master
- env: TOXENV=py37-master
- { stage: lint, env: TOXENV=flake8, python: 3.6 }
- { stage: lint, env: TOXENV=isort, python: 3.6 }

install:
- pip install tox coveralls
- pip install tox coveralls tox-travis
script:
- tox
after_success:
Expand Down
18 changes: 9 additions & 9 deletions picklefield/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
from copy import deepcopy
from zlib import compress, decompress

import django
from django import VERSION as DJANGO_VERSION
from django.core import checks
from django.db import models
from django.utils.encoding import force_text

from .constants import DEFAULT_PROTOCOL

try:
from cPickle import loads, dumps
from cPickle import loads, dumps # pragma: no cover
except ImportError:
from pickle import loads, dumps
from pickle import loads, dumps # pragma: no cover


class PickledObject(str):
Expand Down Expand Up @@ -158,7 +158,7 @@ def to_python(self, value):
value = dbsafe_decode(value, self.compress)
except Exception:
# If the value is a definite pickle; and an error is raised in
# de-pickling it should be allowed to propogate.
# de-pickling it should be allowed to propagate.
if isinstance(value, PickledObject):
raise
else:
Expand All @@ -170,12 +170,12 @@ def pre_save(self, model_instance, add):
value = super(PickledObjectField, self).pre_save(model_instance, add)
return wrap_conflictual_object(value)

if django.VERSION < (2, 0):
def from_db_value(self, value, expression, connection, context):
return self.to_python(value)
if DJANGO_VERSION < (2, 0):
def from_db_value(self, value, expression, connection, context): # pragma: no cover
return self.to_python(value) # pragma: no cover
else:
def from_db_value(self, value, expression, connection):
return self.to_python(value)
def from_db_value(self, value, expression, connection): # pragma: no cover
return self.to_python(value) # pragma: no cover

def get_db_prep_value(self, value, connection=None, prepared=False):
"""
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
'Environment :: Web Environment',
'Framework :: Django',
'Framework :: Django :: 1.11',
'Framework :: Django :: 2.0',
'Framework :: Django :: 2.1',
'Framework :: Django :: 2.2',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
Expand All @@ -34,6 +33,7 @@
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Topic :: Software Development :: Libraries :: Application Frameworks',
'Topic :: Software Development :: Libraries :: Python Modules',
],
Expand Down
1 change: 1 addition & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class TestingModel(models.Model):
default_pickle_field = PickledObjectField(default=(D1, S1, T1, L1))
callable_pickle_field = PickledObjectField(default=date.today)
non_copying_field = PickledObjectField(copy=False, default=TestCopyDataType('boom!'))
nullable_pickle_field = PickledObjectField(null=True)


class MinimalTestingModel(models.Model):
Expand Down
21 changes: 21 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
TestCustomDataType, TestingModel,
)

try:
from unittest.mock import patch # pragma: no cover
except ImportError:
from mock import patch # pragma: no cover


class PickledObjectFieldTests(TestCase):
def setUp(self):
Expand All @@ -35,6 +40,7 @@ def test_data_integrity(self):
# the same data, even thought it's stored differently in the DB.
self.assertEqual(value, model_test.pickle_field)
self.assertEqual(value, model_test.compressed_pickle_field)
self.assertIsNone(model_test.nullable_pickle_field)
# Make sure we can also retrieve the model
model_test.save()
model_test.delete()
Expand Down Expand Up @@ -181,6 +187,21 @@ def test_empty_strings_not_allowed(self):
with self.assertRaises(IntegrityError):
MinimalTestingModel.objects.create()

def test_decode_error(self):
def mock_decode_error(*args, **kwargs):
raise Exception()

model = MinimalTestingModel.objects.create(pickle_field={'foo': 'bar'})
model.save()

self.assertEqual(
{'foo': 'bar'}, MinimalTestingModel.objects.get(pk=model.pk).pickle_field
)

with patch('picklefield.fields.dbsafe_decode', mock_decode_error):
encoded_value = dbsafe_encode({'foo': 'bar'})
self.assertEqual(encoded_value, MinimalTestingModel.objects.get(pk=model.pk).pickle_field)

Comment on lines +190 to +204
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this patch have to do with Django 2.2 support?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing, just coverage improvement 😄


@isolate_apps('tests')
class PickledObjectFieldCheckTests(SimpleTestCase):
Expand Down
23 changes: 16 additions & 7 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,36 @@ envlist =
flake8,
isort,
py27-1.11,
py34-{1.11,2.0},
py35-{1.11,2.0,2.1,master},
py36-{1.11,2.0,2.1,master},
py37-{1.11,2.0,2.1,master}
py35-{1.11,2.2},
py36-{1.11,2.2,master},
py37-{1.11,2.2,master},
py38-{2.2,master},

[tox:travis]
2.7 = py27
3.5 = py35
3.6 = py36
3.7 = py37
3.8 = py38

[testenv]
basepython =
py27: python2.7
py34: python3.4
py35: python3.5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 2.2 LTS supports 3.5

py36: python3.6
py37: python3.7
py38: python3.8
usedevelop = true
commands =
{envpython} -R -Wonce {envbindir}/coverage run -m django test -v2 --settings=tests.settings {posargs}
coverage report
{envpython} -R -Wonce {envbindir}/coverage run --branch -m django test -v2 --settings=tests.settings {posargs}
coverage report -m
deps =
py27: mock
coverage
1.11: Django>=1.11,<2.0
2.0: Django>=2.0,<2.1
2.1: Django>=2.1,<2.2
2.2: Django>=2.2,<3.0
master: https://github.com/django/django/archive/master.tar.gz

[testenv:flake8]
Expand Down