Skip to content

Commit b087966

Browse files
committed
changed to keep both attributes and added corresponding tests
1 parent 0383237 commit b087966

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

canvasapi/canvas_object.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import arrow
22
import pytz
3+
import warnings
34

45

56
class CanvasObject(object):
@@ -59,13 +60,22 @@ def set_attributes(self, attributes):
5960
:type attributes: dict
6061
"""
6162
for attribute, value in attributes.items():
62-
safe_attribute = attribute.replace("-", "_")
63-
self.__setattr__(safe_attribute, value)
63+
self.__setattr__(attribute, value)
64+
if attribute == "content-type":
65+
self.__setattr__("content_type", value)
66+
warnings.warn(
67+
(
68+
"The 'content-type' attribute will be removed "
69+
"in a future version. Please use "
70+
"'content_type' instead."
71+
),
72+
UserWarning,
73+
)
6474

6575
try:
6676
naive = arrow.get(str(value)).datetime
6777
aware = naive.replace(tzinfo=pytz.utc) - naive.utcoffset()
68-
self.__setattr__(safe_attribute + "_date", aware)
78+
self.__setattr__(attribute + "_date", aware)
6979
except arrow.ParserError:
7080
pass
7181
except ValueError:

tests/test_canvas_object.py

+17-27
Original file line numberDiff line numberDiff line change
@@ -52,33 +52,6 @@ def test_set_attributes_valid_date(self, m):
5252

5353
self.assertTrue(hasattr(self.canvas_object, "half_offset_date"))
5454
self.assertEqual(self.canvas_object.half_offset_date, offset_time)
55-
56-
# set_attributes with a hyphen
57-
def test_set_attributes_with_hyphens(self, m):
58-
attributes = {
59-
"content-type": "application/json",
60-
"filename": "example.json",
61-
"start-at": "2012-05-05T00:00:00Z",
62-
"end-at": "2012-08-05",
63-
}
64-
65-
start_date = datetime.strptime(
66-
attributes["start-at"], "%Y-%m-%dT%H:%M:%SZ"
67-
).replace(tzinfo=pytz.utc)
68-
end_date = datetime.strptime(attributes["end-at"], "%Y-%m-%d").replace(
69-
tzinfo=pytz.utc
70-
)
71-
72-
self.canvas_object.set_attributes(attributes)
73-
74-
self.assertTrue(hasattr(self.canvas_object, "start_at_date"))
75-
self.assertEqual(self.canvas_object.start_at_date, start_date)
76-
self.assertTrue(hasattr(self.canvas_object, "end_at_date"))
77-
self.assertEqual(self.canvas_object.end_at_date, end_date)
78-
self.assertTrue(hasattr(self.canvas_object, "content_type"))
79-
self.assertEqual(self.canvas_object.content_type, "application/json")
80-
self.assertTrue(hasattr(self.canvas_object, "filename"))
81-
self.assertEqual(self.canvas_object.filename, "example.json")
8255

8356
def test_set_attributes_invalid_date(self, m):
8457
attributes = {"start_at": "2017-01-01T00:00+00:00:00", "end_at": "2012-08-0"}
@@ -89,3 +62,20 @@ def test_set_attributes_invalid_date(self, m):
8962
self.assertFalse(hasattr(self.canvas_object, "end_at_date"))
9063
self.assertTrue(hasattr(self.canvas_object, "start_at"))
9164
self.assertTrue(hasattr(self.canvas_object, "end_at"))
65+
66+
# set_attributes 'content-type'
67+
def test_set_attributes_with_content_type(self, m):
68+
attributes = {
69+
"content-type": "application/json",
70+
"content_type": "another_application/json",
71+
"filename": "example.json",
72+
}
73+
74+
self.canvas_object.set_attributes(attributes)
75+
76+
self.assertTrue(hasattr(self.canvas_object, "content-type"))
77+
self.assertEqual(getattr(self.canvas_object, 'content-type'), "application/json")
78+
self.assertTrue(hasattr(self.canvas_object, "content_type"))
79+
self.assertEqual(self.canvas_object.content_type, "another_application/json")
80+
self.assertTrue(hasattr(self.canvas_object, "filename"))
81+
self.assertEqual(self.canvas_object.filename, "example.json")

0 commit comments

Comments
 (0)