-
Notifications
You must be signed in to change notification settings - Fork 179
Added the ability to create and modify fields, including aliases and … #111
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
Changes from 1 commit
8992861
ef10f9d
8293bb3
bd3c60a
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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| from functools import wraps | ||
|
|
||
| def argument_is_one_of(*allowed_values): | ||
| def property_type_decorator(func): | ||
| @wraps(func) | ||
| def wrapper(self, value): | ||
| if value not in allowed_values: | ||
| error = "Invalid argument: {0}. {1} must be one of {2}." | ||
| msg = error.format(value, func.__name__, allowed_values) | ||
| raise ValueError(error) | ||
| return func(self, value) | ||
|
|
||
| return wrapper | ||
|
|
||
| return property_type_decorator |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,11 +63,31 @@ def current_hash(self): | |
| """ | ||
| return hash(ET.tostring(self.tds._datasourceTree.getroot())) | ||
|
|
||
| def check_state_change(self, should_change, msg, field_name): | ||
| """ Check whether the XML has changed and update the current state. | ||
|
|
||
| Args: | ||
| should_change: Whether the XML is supposed to have changed or not. Boolean. | ||
| msg: The message to be displayed in an error case, as key for the MESSAGES dict. String. | ||
| field_name: The field name that will be displayed in the error message. String. | ||
|
|
||
| Returns: | ||
| Nothing. | ||
| """ | ||
| new_state = self.current_hash() | ||
| compare_func = self.assertNotEqual if should_change else self.assertEqual | ||
| compare_func( | ||
| self.state, | ||
| new_state, | ||
| msg=MESSAGES[msg].format(field_name) | ||
| ) | ||
| self.state = new_state | ||
|
|
||
| def test_change_values(self): | ||
|
Contributor
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. There are many similarities between the code in this test and the next one. Is there a way to extract the mostly common code and replace the differences with arguments to the new function?
Contributor
Author
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. Extracted code into a new function. |
||
| """ Test if the value changes of a field are reflected in the object and in the underlying XML structure. | ||
| """ | ||
| field_to_test = "[amount]" | ||
| state = self.current_hash() | ||
| self.state = self.current_hash() | ||
| # change all fields | ||
| for key, value in NEW_VALUES.items(): | ||
| setattr(self.tds.fields[field_to_test], key, value) | ||
|
|
@@ -78,19 +98,13 @@ def test_change_values(self): | |
| msg=MESSAGES['test_change_values1'].format(key) | ||
| ) | ||
| # the new value must be reflected in the xml | ||
| new_state = self.current_hash() | ||
| self.assertNotEqual( | ||
| state, | ||
| new_state, | ||
| msg=MESSAGES['test_change_values2'].format(key) | ||
| ) | ||
| state = new_state | ||
| self.check_state_change(True, 'test_change_values2', key) | ||
|
|
||
| def test_change_values_fail(self): | ||
| """ Test if the value changes of a field are rejected if the wrong arguments are passed. | ||
| """ | ||
| field_to_test = "[amount]" | ||
| state = self.current_hash() | ||
| self.state = self.current_hash() | ||
| # change all fields | ||
| for key, value in WRONG_VALUES.items(): | ||
|
|
||
|
|
@@ -104,42 +118,29 @@ def test_change_values_fail(self): | |
| value, | ||
| msg=MESSAGES['test_change_valuesFail1'].format(key) | ||
| ) | ||
|
|
||
| # the new value must NOT be reflected in the xml | ||
| new_state = self.current_hash() | ||
| self.assertEqual( | ||
| state, | ||
| new_state, | ||
| msg=MESSAGES['test_change_valuesFail2'].format(key) | ||
| ) | ||
| state = new_state | ||
| self.check_state_change(False, 'test_change_valuesFail2', key) | ||
|
|
||
| def test_remove_field(self): | ||
| """ Test if a Field can be removed. | ||
| """ | ||
| field_to_test = "[amount]" | ||
| state = self.current_hash() | ||
| self.state = self.current_hash() | ||
| # change all fields | ||
| field = self.tds.fields["[amount]"] | ||
| self.tds.remove_field(field) | ||
| self.assertNotEqual(state, self.current_hash()) | ||
| self.assertNotEqual(self.state, self.current_hash()) | ||
|
|
||
| def test_change_aliases(self): | ||
|
Contributor
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 one is also very similar to the two above that I previously commented on.
Contributor
Author
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. See above. |
||
| """ Test if the alias changes of a field are reflected in the object and in the underlying XML structure. | ||
| """ | ||
| field_to_test = "[amount]" | ||
| state = self.current_hash() | ||
| self.state = self.current_hash() | ||
| # change all fields | ||
| for key, value in ALIASES.items(): | ||
| self.tds.fields[field_to_test].add_alias(key, value) | ||
| # the new value must be reflected in the xml | ||
| new_state = self.current_hash() | ||
| self.assertNotEqual( | ||
| state, | ||
| new_state, | ||
| msg=MESSAGES['test_change_aliases1'].format(field_to_test) | ||
| ) | ||
| state = new_state | ||
| self.check_state_change(True, 'test_change_aliases1', field_to_test) | ||
|
|
||
| # check whether all fields of ALIASES have been applied | ||
| self.assertEqual( | ||
|
|
@@ -165,7 +166,7 @@ def test_calculation_base(self): | |
| def test_calculation_change(self): | ||
| """ Test whether changing calculations of a field works. | ||
| """ | ||
| state = self.current_hash() | ||
| self.state = self.current_hash() | ||
| new_calc = '33 * 44' | ||
| fld_name = '[Calculation_357754699576291328]' | ||
| self.tds.calculations[fld_name].calculation = new_calc | ||
|
|
@@ -175,7 +176,7 @@ def test_calculation_change(self): | |
|
|
||
| # Check XML representation | ||
| new_state = self.current_hash() | ||
| self.assertNotEqual(state, new_state) | ||
| self.assertNotEqual(self.state, new_state) | ||
|
|
||
| def test_calculation_new(self): | ||
| """ Test if creating a new calculation works. | ||
|
|
||
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.
This looks fine, but xfile is an odd place, I say toss it back in
fields.py-- did @RussTheAerialist recommend it go here?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.
@RussTheAerialist wanted this as a column tag factory so it would read
column = xml_factory.create_column(caption, datatype, role, type_, name)So I put it into xfile, since it's the closest thing to a dedicated XML class that I could find in this project. I'll put it back into fields.py