Skip to content

Commit

Permalink
changed to keep both attributes and added corresponding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nayanpaa committed Apr 24, 2024
1 parent 0383237 commit b087966
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
16 changes: 13 additions & 3 deletions canvasapi/canvas_object.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import arrow
import pytz
import warnings


class CanvasObject(object):
Expand Down Expand Up @@ -59,13 +60,22 @@ def set_attributes(self, attributes):
:type attributes: dict
"""
for attribute, value in attributes.items():
safe_attribute = attribute.replace("-", "_")
self.__setattr__(safe_attribute, value)
self.__setattr__(attribute, value)
if attribute == "content-type":
self.__setattr__("content_type", value)
warnings.warn(
(
"The 'content-type' attribute will be removed "
"in a future version. Please use "
"'content_type' instead."
),
UserWarning,
)

try:
naive = arrow.get(str(value)).datetime
aware = naive.replace(tzinfo=pytz.utc) - naive.utcoffset()
self.__setattr__(safe_attribute + "_date", aware)
self.__setattr__(attribute + "_date", aware)
except arrow.ParserError:
pass
except ValueError:
Expand Down
44 changes: 17 additions & 27 deletions tests/test_canvas_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,33 +52,6 @@ def test_set_attributes_valid_date(self, m):

self.assertTrue(hasattr(self.canvas_object, "half_offset_date"))
self.assertEqual(self.canvas_object.half_offset_date, offset_time)

# set_attributes with a hyphen
def test_set_attributes_with_hyphens(self, m):
attributes = {
"content-type": "application/json",
"filename": "example.json",
"start-at": "2012-05-05T00:00:00Z",
"end-at": "2012-08-05",
}

start_date = datetime.strptime(
attributes["start-at"], "%Y-%m-%dT%H:%M:%SZ"
).replace(tzinfo=pytz.utc)
end_date = datetime.strptime(attributes["end-at"], "%Y-%m-%d").replace(
tzinfo=pytz.utc
)

self.canvas_object.set_attributes(attributes)

self.assertTrue(hasattr(self.canvas_object, "start_at_date"))
self.assertEqual(self.canvas_object.start_at_date, start_date)
self.assertTrue(hasattr(self.canvas_object, "end_at_date"))
self.assertEqual(self.canvas_object.end_at_date, end_date)
self.assertTrue(hasattr(self.canvas_object, "content_type"))
self.assertEqual(self.canvas_object.content_type, "application/json")
self.assertTrue(hasattr(self.canvas_object, "filename"))
self.assertEqual(self.canvas_object.filename, "example.json")

def test_set_attributes_invalid_date(self, m):
attributes = {"start_at": "2017-01-01T00:00+00:00:00", "end_at": "2012-08-0"}
Expand All @@ -89,3 +62,20 @@ def test_set_attributes_invalid_date(self, m):
self.assertFalse(hasattr(self.canvas_object, "end_at_date"))
self.assertTrue(hasattr(self.canvas_object, "start_at"))
self.assertTrue(hasattr(self.canvas_object, "end_at"))

# set_attributes 'content-type'
def test_set_attributes_with_content_type(self, m):
attributes = {
"content-type": "application/json",
"content_type": "another_application/json",
"filename": "example.json",
}

self.canvas_object.set_attributes(attributes)

self.assertTrue(hasattr(self.canvas_object, "content-type"))
self.assertEqual(getattr(self.canvas_object, 'content-type'), "application/json")
self.assertTrue(hasattr(self.canvas_object, "content_type"))
self.assertEqual(self.canvas_object.content_type, "another_application/json")
self.assertTrue(hasattr(self.canvas_object, "filename"))
self.assertEqual(self.canvas_object.filename, "example.json")

0 comments on commit b087966

Please sign in to comment.