Skip to content

Commit d6feded

Browse files
Merge pull request #164 from stphivos/deprecations
Drop Python 2 code and avoid deprecated code
2 parents 0856f4f + 16950e2 commit d6feded

File tree

13 files changed

+36
-77
lines changed

13 files changed

+36
-77
lines changed

django_mock_queries/asserts.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
try:
2-
from unittest.mock import patch, Mock
3-
except ImportError:
4-
from mock import patch, Mock
1+
from unittest.mock import patch, Mock
52

63
from model_bakery import baker
74

django_mock_queries/mocks.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
from django.db.utils import ConnectionHandler, NotSupportedError
1010
from functools import partial
1111
from itertools import chain
12-
try:
13-
from unittest.mock import Mock, MagicMock, patch, PropertyMock
14-
except ImportError:
15-
from mock import Mock, MagicMock, patch, PropertyMock
12+
from unittest.mock import Mock, MagicMock, patch, PropertyMock
1613

1714
from types import MethodType
1815

@@ -107,7 +104,7 @@ def compiler(queryset, connection, using, **kwargs):
107104
Model.refresh_from_db = Mock() # Make this into a noop.
108105

109106

110-
class MockMap(object):
107+
class MockMap:
111108
def __init__(self, original):
112109
""" Wrap a mock mapping around the original one-to-many relation. """
113110
self.map = {}
@@ -254,7 +251,7 @@ def test_dataset(self):
254251
return PatcherChain(patchers, pass_mocks=False)
255252

256253

257-
class PatcherChain(object):
254+
class PatcherChain:
258255
""" Chain a list of mock patchers into one.
259256
260257
The resulting patcher can be used just like one from the mock module:
@@ -328,7 +325,7 @@ def stop(self):
328325
patcher.stop()
329326

330327

331-
class Mocker(object):
328+
class Mocker:
332329
"""
333330
A decorator that patches multiple class methods with a magic mock instance that does nothing.
334331
"""

django_mock_queries/query.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import datetime
22
import random
33
from collections import OrderedDict, namedtuple
4-
from six import with_metaclass
5-
try:
6-
from unittest.mock import Mock, MagicMock, PropertyMock
7-
except ImportError:
8-
from mock import Mock, MagicMock, PropertyMock
4+
from unittest.mock import Mock, MagicMock, PropertyMock
95

106
from .constants import *
117
from .exceptions import *
@@ -21,7 +17,7 @@ def __call__(cls, *initial_items, **kwargs):
2117
return obj
2218

2319

24-
class MockSet(with_metaclass(MockSetMeta, MagicMock)):
20+
class MockSet(MagicMock, metaclass=MockSetMeta):
2521
EVENT_ADDED = 'added'
2622
EVENT_UPDATED = 'updated'
2723
EVENT_SAVED = 'saved'
@@ -472,7 +468,7 @@ def create_model(*fields):
472468
return MockModel(**{f: None for f in fields})
473469

474470

475-
class MockOptions(object):
471+
class MockOptions:
476472
def __init__(self, *field_names):
477473
self.load_fields(*field_names)
478474
self.get_latest_by = None
@@ -494,7 +490,7 @@ def load_fields(self, *field_names):
494490
self.__dict__[key].append(obj)
495491

496492

497-
class MockField(object):
493+
class MockField:
498494
def __init__(self, field):
499495
for key in ('name', 'attname'):
500496
self.__dict__[key] = field

django_mock_queries/utils.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
from datetime import datetime, date
22
from django.core.exceptions import FieldError
3-
try:
4-
from unittest.mock import Mock
5-
except ImportError:
6-
from mock import Mock
3+
from unittest.mock import Mock
74

85
from .comparisons import *
96
from .constants import *

examples/users/analytics/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from django.db.models import Count
44

55

6-
class AnalyticsApi(object):
6+
class AnalyticsApi:
77
def active_users(self):
88
return User.objects.filter(is_active=True).all()
99

requirements/core.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
mock; python_version < '3.3'
21
Django
32
djangorestframework
4-
model-bakery~=1.1.0; python_version < '3'
5-
model-bakery>=1.0.0,<1.4.0; python_version >= '3'
3+
model-bakery>=1.0.0

requirements/dev.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,3 @@ virtualenv==14.0.6
99
pypandoc==1.4
1010
setuptools==39.2.0
1111
twine==1.11.0
12-
more-itertools<6.0.0; python_version < '3'
13-
configparser<5.0.0; python_version < '3'

setup.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,7 @@ def parse_requirements(filename):
3232
'Topic :: Software Development :: Testing :: Unit',
3333
'License :: OSI Approved :: MIT License',
3434
'Programming Language :: Python',
35-
'Programming Language :: Python :: 2',
36-
'Programming Language :: Python :: 2.7',
37-
'Programming Language :: Python :: 3',
38-
'Programming Language :: Python :: 3.3',
39-
'Programming Language :: Python :: 3.4',
40-
'Programming Language :: Python :: 3.5',
41-
'Programming Language :: Python :: 3.6',
35+
'Programming Language :: Python :: 3 :: Only',
4236
],
4337
keywords='django orm mocking unit-testing tdd',
4438
packages=['django_mock_queries'],

tests/test_asserts.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
try:
2-
from unittest.mock import patch
3-
except ImportError:
4-
from mock import patch
1+
from unittest import TestCase, skipIf
2+
from unittest.mock import patch
53

64
from model_bakery import baker
7-
from unittest import TestCase, skipIf
85

96
import django
107
from django_mock_queries.asserts import assert_serializer, SerializerAssert

tests/test_mocks.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
1-
import sys
1+
from unittest import TestCase
2+
from unittest.mock import patch, MagicMock, PropertyMock
3+
24
import django
35
from django.db import connection
46
from django.db.utils import NotSupportedError
57
from django.db.backends.base.creation import BaseDatabaseCreation
6-
try:
7-
from unittest.mock import patch, MagicMock, PropertyMock
8-
except ImportError:
9-
from mock import patch, MagicMock, PropertyMock
10-
11-
from unittest import TestCase
128

139
from django_mock_queries import mocks
1410
from django_mock_queries.mocks import monkey_patch_test_db, mock_django_connection, \
1511
MockOneToOneMap, MockOneToManyMap, PatcherChain, mocked_relations, ModelMocker, Mocker
1612
from django_mock_queries.query import MockSet
1713
from tests.mock_models import Car, Sedan, Manufacturer, CarVariation
1814

19-
BUILTINS = 'builtins' if sys.version_info[0] >= 3 else '__builtin__'
20-
2115

2216
class TestMocks(TestCase):
2317
def test_mock_sql_raises_error(self):
2418
""" Get a clear error if you forget to mock a database query. """
25-
with self.assertRaisesRegexp(
19+
with self.assertRaisesRegex(
2620
NotSupportedError,
2721
"Mock database tried to execute SQL for Car model."):
2822
Car.objects.count()
2923

3024
def test_exists_raises_error(self):
3125
""" Get a clear error if you forget to mock a database query. """
32-
with self.assertRaisesRegexp(
26+
with self.assertRaisesRegex(
3327
NotSupportedError,
3428
"Mock database tried to execute SQL for Car model."):
3529
Car.objects.exists()
@@ -111,7 +105,7 @@ class MockOneToManyTests(TestCase):
111105
def test_not_mocked(self):
112106
m = Manufacturer()
113107

114-
with self.assertRaisesRegexp(
108+
with self.assertRaisesRegex(
115109
NotSupportedError,
116110
'Mock database tried to execute SQL for Car model'):
117111
m.car_set.count()
@@ -123,7 +117,7 @@ def test_mock_is_removed(self):
123117
m.car_set = MockSet(Car(speed=95))
124118
self.assertEqual(1, m.car_set.count())
125119

126-
with self.assertRaisesRegexp(
120+
with self.assertRaisesRegex(
127121
NotSupportedError,
128122
'Mock database tried to execute SQL for Car model'):
129123
m.car_set.count()
@@ -189,8 +183,8 @@ def zero_sum(items):
189183

190184

191185
class PatcherChainTest(TestCase):
192-
patch_mock_max = patch(BUILTINS + '.max')
193-
patch_zero_sum = patch(BUILTINS + '.sum', zero_sum)
186+
patch_mock_max = patch('builtins.max')
187+
patch_zero_sum = patch('builtins.sum', zero_sum)
194188

195189
@patch_zero_sum
196190
def test_patch_dummy(self):
@@ -255,7 +249,7 @@ def test_start(self):
255249
self.assertIs(zero_sum, mocked[1])
256250

257251

258-
@PatcherChain([patch(BUILTINS + '.max'), patch(BUILTINS + '.sum', zero_sum)],
252+
@PatcherChain([patch('builtins.max'), patch('builtins.sum', zero_sum)],
259253
pass_mocks=False)
260254
class PatcherChainOnClassTest(TestCase):
261255
test_example_attribute = 42

0 commit comments

Comments
 (0)