Skip to content

Commit e657e8f

Browse files
committed
Add deprecation warning.
1 parent 72684a4 commit e657e8f

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

abe/mocks.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import warnings
23

34
from supermutes.dot import dotify
45

@@ -10,6 +11,14 @@ def __init__(self, data):
1011
Initialise an ABE mock from data.
1112
1213
"""
14+
if not isinstance(data, dict):
15+
msg = ('Instanciating an AbeMock by filename is deprecated and '
16+
'will be removed in an upcoming release. '
17+
'Use AbeMock.from_filename instead'.format(data))
18+
warnings.warn(msg, DeprecationWarning)
19+
with open(data, 'r') as f:
20+
data = json.load(f)
21+
1322
# map JSON fields to attributes
1423
self.__dict__ = data
1524

tests/data/sample.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"description": "Retrieve profile of logged in user",
3+
"url": "/accounts/me",
4+
"method": "GET",
5+
"examples": {
6+
"OK": {
7+
"request": {
8+
"url": "/accounts/me"
9+
},
10+
"response": {
11+
"status": 200,
12+
"body": {
13+
"id": 1,
14+
"username": "user-0",
15+
"first_name": "",
16+
"last_name": "",
17+
"email": "[email protected]"
18+
}
19+
}
20+
},
21+
"unauthenticated": {
22+
"description": "I am not logged in",
23+
"request": {
24+
"url": "/accounts/me"
25+
},
26+
"response": {
27+
"status": 403,
28+
"body": {
29+
"detail": "Authentication credentials were not provided."
30+
}
31+
}
32+
}
33+
}
34+
}

tests/test_assertions.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
from os.path import abspath, dirname, join
12
from unittest import TestCase
3+
import warnings
4+
25
from mock import Mock
36

47
from abe.mocks import AbeMock
58
from abe.unittest import AbeTestMixin
69

10+
DATA_DIR = join(dirname(abspath(__file__)), 'data')
11+
712

813
class TestDataListEqual(TestCase, AbeTestMixin):
914

@@ -134,3 +139,21 @@ def test_assertion_error_if_post_data_mismatch(self):
134139
self.assert_matches_request(
135140
self.sample_request, self.mock_wsgi_request
136141
)
142+
143+
144+
class TestFilenameInstantiation(TestCase):
145+
146+
def setUp(self):
147+
self.filename = join(DATA_DIR, 'sample.json')
148+
149+
def test_can_still_use_deprecated_instantiation(self):
150+
with warnings.catch_warnings(record=True) as w:
151+
warnings.simplefilter("always")
152+
153+
mock = AbeMock(self.filename)
154+
155+
self.assertEqual(len(w), 1)
156+
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
157+
158+
def test_from_filename(self):
159+
mock = AbeMock.from_filename(self.filename)

0 commit comments

Comments
 (0)