-
Notifications
You must be signed in to change notification settings - Fork 186
Fixed file objects with hyphen issue (ucfopen#647 ) and added test cases #650
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
base: develop
Are you sure you want to change the base?
Changes from 2 commits
0383237
b087966
ae6c387
5c02d7e
51e59d1
66680c6
583e9c3
fd067da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import arrow | ||
| import pytz | ||
| import warnings | ||
|
|
||
|
|
||
| class CanvasObject(object): | ||
|
|
@@ -60,6 +61,16 @@ def set_attributes(self, attributes): | |
| """ | ||
| for attribute, value in attributes.items(): | ||
| self.__setattr__(attribute, value) | ||
| if attribute == "content-type": | ||
| self.__setattr__("content_type", value) | ||
|
Comment on lines
+74
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While the problem here is with If there are major concerns with a broader approach, I'm happy to adopt a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On a cursory glance, I couldn't see any other response attributes that had hyphens. There are some other endpoints where camelCase is used (like the result and score endpoints), but those are only to report to IMS and LTI tools. I think the question for me is which is more Pythonic - changing all the returned properties into class attributes or keeping the object as close to the returned structure (ie, keeping a hyphenated name and requiring the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was also thinking of making it more generic, like if the value contained a hyphen to set it as a value with an underscore. I think I'm fine for making this into a future issue. It really might be easiest to change this class to store all of the variables in a dictionary. Then the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original PR proposed the general replace-all-hyphens-with-underscores. I also favour that solution. Canvas might add new attributes that have hyphens instead of underscores. I also think it makes the most sense to keep the hyphenated version and provide the underscored version for convenience, to match the documentation as mentioned above. But it's important to get it right so that they sync, in case they're changed (although it doesn't make sense to change this particular attribute). Edit: Looked at @jonespm's solution below, I also think that's the way to do it. |
||
| 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 | ||
|
|
||
bennettscience marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,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") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test brings up some bigger questions about how we want to handle the case where both the It's pretty unlikely that Canvas would send back two attributes with only the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I thought about this too, I think we can solve both of those problems but maybe in a separate issue. I don't think it's super likely either. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be worth adding tests that detect if Canvas would ever do this? I do quite some API "reverse engineering" of APIs I want to use but don't have any official documentation (they're for "internal" use of the web UI). Then I add tests that will fail whenever some return value from the server changes. Such tests must be added as a separate test suite run for this particular purpose. It would require a Canvas instance to run them against and, consequently, a login and token to the API. So not the ordinary test setup. I'm leaning towards not worth it. Canvas is pretty stable and documents everything. So the likelihood of being useful is close to zero. |
||
| self.assertTrue(hasattr(self.canvas_object, "filename")) | ||
| self.assertEqual(self.canvas_object.filename, "example.json") | ||
Uh oh!
There was an error while loading. Please reload this page.