-
Notifications
You must be signed in to change notification settings - Fork 448
Added decorators for checking values of input to property setters (Issue-38) #46
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e9aa592
Add property decorators
LGraber 6897738
Add decorator files
LGraber 1a0b945
Add test cases and combine decorators into one file
5ead147
Remove * and be explicit on imports
LGraber 48a5f49
Ignore PyCharm related files
LGraber 8c17ecc
Fix Travis errors. Fix error text for property_not_empty
LGraber 05ff9fd
Trying again to fix travis complaints
LGraber a13d708
Rename property decorators
LGraber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,6 +66,9 @@ docs/_build/ | |
| # PyBuilder | ||
| target/ | ||
|
|
||
| # PyCharm stuff | ||
| .idea/ | ||
|
|
||
| # IPython Notebook | ||
| .ipynb_checkpoints | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| from functools import wraps | ||
|
|
||
|
|
||
| def property_is_enum(enum_type): | ||
| def property_type_decorator(func): | ||
| @wraps(func) | ||
| def wrapper(self, value): | ||
| if value is not None and not hasattr(enum_type, value): | ||
| error = "Invalid value: {0}. {1} must be of type {2}.".format(value, func.__name__, enum_type.__name__) | ||
| raise ValueError(error) | ||
| return func(self, value) | ||
|
|
||
| return wrapper | ||
|
|
||
| return property_type_decorator | ||
|
|
||
|
|
||
| def property_is_boolean(func): | ||
| @wraps(func) | ||
| def wrapper(self, value): | ||
| if not isinstance(value, bool): | ||
| error = "Boolean expected for {0} flag.".format(func.__name__) | ||
| raise ValueError(error) | ||
| return func(self, value) | ||
|
|
||
| return wrapper | ||
|
|
||
|
|
||
| def property_not_nullable(func): | ||
| @wraps(func) | ||
| def wrapper(self, value): | ||
| if value is None: | ||
| error = "{0} must be defined.".format(func.__name__) | ||
| raise ValueError(error) | ||
| return func(self, value) | ||
|
|
||
| return wrapper | ||
|
|
||
|
|
||
| def property_not_empty(func): | ||
| @wraps(func) | ||
| def wrapper(self, value): | ||
| if not value: | ||
| error = "{0} must not be empty.".format(func.__name__) | ||
| raise ValueError(error) | ||
| return func(self, value) | ||
|
|
||
| return wrapper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import unittest | ||
| import tableauserverclient as TSC | ||
|
|
||
|
|
||
| class DatasourceModelTests(unittest.TestCase): | ||
| def test_invalid_project_id(self): | ||
| self.assertRaises(ValueError, TSC.DatasourceItem, None) | ||
| datasource = TSC.DatasourceItem("10") | ||
| with self.assertRaises(ValueError): | ||
| datasource.project_id = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import unittest | ||
| import tableauserverclient as TSC | ||
|
|
||
|
|
||
| class GroupModelTests(unittest.TestCase): | ||
| def test_invalid_name(self): | ||
| self.assertRaises(ValueError, TSC.GroupItem, None) | ||
| self.assertRaises(ValueError, TSC.GroupItem, "") | ||
| group = TSC.GroupItem("grp") | ||
| with self.assertRaises(ValueError): | ||
| group.name = None | ||
|
|
||
| with self.assertRaises(ValueError): | ||
| group.name = "" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import unittest | ||
| import tableauserverclient as TSC | ||
|
|
||
|
|
||
| class ProjectModelTests(unittest.TestCase): | ||
| def test_invalid_name(self): | ||
| self.assertRaises(ValueError, TSC.ProjectItem, None) | ||
| self.assertRaises(ValueError, TSC.ProjectItem, "") | ||
| project = TSC.ProjectItem("proj") | ||
| with self.assertRaises(ValueError): | ||
| project.name = None | ||
|
|
||
| with self.assertRaises(ValueError): | ||
| project.name = "" | ||
|
|
||
| def test_invalid_content_permissions(self): | ||
| project = TSC.ProjectItem("proj") | ||
| with self.assertRaises(ValueError): | ||
| project.content_permissions = "Hello" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first glance the code feels ok, but I think there might be a better name than "property_decorator" which both have meanings of their own.
I'll do a more detailed review in a bit