-
Notifications
You must be signed in to change notification settings - Fork 300
Implemented constraint equality. #3749
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
6 commits
Select commit
Hold shift + click to select a range
4af39c1
Implemented constraint equality.
pp-mo 2351245
Simplified.
pp-mo 5dc188b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 601e3a9
Review changes.
pp-mo c4121b7
Review changes.
pp-mo c8be527
Added whatsnew.
pp-mo 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
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
274 changes: 274 additions & 0 deletions
274
lib/iris/tests/unit/constraints/test_Constraint_equality.py
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,274 @@ | ||
| # Copyright Iris contributors | ||
| # | ||
| # This file is part of Iris and is released under the LGPL license. | ||
| # See COPYING and COPYING.LESSER in the root of the repository for full | ||
| # licensing details. | ||
| """Unit tests for equality testing of different constraint types.""" | ||
|
|
||
| # Import iris.tests first so that some things can be initialised before | ||
| # importing anything else. | ||
| import iris.tests as tests # isort:skip | ||
|
|
||
| from iris._constraints import AttributeConstraint, Constraint, NameConstraint | ||
|
|
||
|
|
||
| class Test_Constraint__hash__(tests.IrisTest): | ||
| def test_empty(self): | ||
| c1 = Constraint() | ||
| c2 = Constraint() | ||
| self.assertEqual(hash(c1), hash(c1)) | ||
| self.assertNotEqual(hash(c1), hash(c2)) | ||
|
|
||
|
|
||
| class Test_Constraint__eq__(tests.IrisTest): | ||
| def test_empty_same(self): | ||
| c1 = Constraint() | ||
| c2 = Constraint() | ||
| self.assertEqual(c1, c2) | ||
| self.assertIsNot(c1, c2) | ||
|
|
||
| def test_emptyname_same(self): | ||
| c1 = Constraint("") | ||
| c2 = Constraint("") | ||
| self.assertEqual(c1, c2) | ||
|
|
||
| def test_empty_emptyname_differ(self): | ||
| c1 = Constraint() | ||
| c2 = Constraint("") | ||
| self.assertNotEqual(c1, c2) | ||
|
|
||
| def test_names_same(self): | ||
| c1 = Constraint("a") | ||
| c2 = Constraint("a") | ||
| self.assertEqual(c1, c2) | ||
|
|
||
| def test_names_differ(self): | ||
| c1 = Constraint("a") | ||
| c2 = Constraint("b") | ||
| self.assertNotEqual(c1, c2) | ||
|
|
||
| def test_funcs_same(self): | ||
| # *Same* functions match | ||
| def func(cube): | ||
| return False | ||
|
|
||
| c1 = Constraint(cube_func=func) | ||
| c2 = Constraint(cube_func=func) | ||
| self.assertEqual(c1, c2) | ||
|
|
||
| def test_funcs_differ(self): | ||
| # Identical but different funcs do not match. | ||
| c1 = Constraint(cube_func=lambda c: False) | ||
| c2 = Constraint(cube_func=lambda c: False) | ||
| self.assertNotEqual(c1, c2) | ||
|
|
||
| def test_coord_names_same(self): | ||
| c1 = Constraint(some_coordname=3) | ||
| c2 = Constraint(some_coordname=3) | ||
| self.assertEqual(c1, c2) | ||
|
|
||
| def test_coord_names_differ(self): | ||
| c1 = Constraint(some_coordname_A=3) | ||
| c2 = Constraint(some_coordname_B=3) | ||
| self.assertNotEqual(c1, c2) | ||
|
|
||
| def test_coord_values_differ(self): | ||
| c1 = Constraint(some_coordname=3) | ||
| c2 = Constraint(some_coordname=4) | ||
| self.assertNotEqual(c1, c2) | ||
|
|
||
| def test_coord_orders_differ(self): | ||
| # We *could* maybe ignore Coordinate order, but at present we don't. | ||
| c1 = Constraint(coordname_1=1, coordname_2=2) | ||
| c2 = Constraint(coordname_2=2, coordname_1=1) | ||
| self.assertNotEqual(c1, c2) | ||
|
|
||
| def test_coord_values_functions_same(self): | ||
| def func(coord): | ||
| return False | ||
|
|
||
| c1 = Constraint(some_coordname=func) | ||
| c2 = Constraint(some_coordname=func) | ||
| self.assertEqual(c1, c2) | ||
|
|
||
| def test_coord_values_functions_differ(self): | ||
| # Identical functions are not the same. | ||
| c1 = Constraint(some_coordname=lambda c: True) | ||
| c2 = Constraint(some_coordname=lambda c: True) | ||
| self.assertNotEqual(c1, c2) | ||
|
|
||
| def test_coord_values_and_keys_same(self): | ||
| # **kwargs and 'coord_values=' are combined without distinction. | ||
| c1 = Constraint(coord_values={"a": [2, 3]}) | ||
| c2 = Constraint(a=[2, 3]) | ||
| self.assertEqual(c1, c2) | ||
|
|
||
|
|
||
| class Test_AttributeConstraint__hash__(tests.IrisTest): | ||
| def test_empty(self): | ||
| c1 = AttributeConstraint() | ||
| c2 = AttributeConstraint() | ||
| self.assertEqual(hash(c1), hash(c1)) | ||
| self.assertNotEqual(hash(c1), hash(c2)) | ||
|
|
||
|
|
||
| class Test_AttributeConstraint__eq__(tests.IrisTest): | ||
| def test_empty_same(self): | ||
| c1 = AttributeConstraint() | ||
| c2 = AttributeConstraint() | ||
| self.assertEqual(c1, c2) | ||
| self.assertIsNot(c1, c2) | ||
|
|
||
| def test_attribute_plain_empty_diff(self): | ||
| c1 = AttributeConstraint() | ||
| c2 = Constraint() | ||
| self.assertNotEqual(c1, c2) | ||
|
|
||
| def test_names_same(self): | ||
| c1 = AttributeConstraint(a=1) | ||
| c2 = AttributeConstraint(a=1) | ||
| self.assertEqual(c1, c2) | ||
|
|
||
| def test_names_diff(self): | ||
| c1 = AttributeConstraint(a=1) | ||
| c2 = AttributeConstraint(a=1, b=1) | ||
| self.assertNotEqual(c1, c2) | ||
|
|
||
| def test_values_diff(self): | ||
| c1 = AttributeConstraint(a=1, b=1) | ||
| c2 = AttributeConstraint(a=1, b=2) | ||
| self.assertNotEqual(c1, c2) | ||
|
|
||
| def test_func_same(self): | ||
| def func(attrs): | ||
| return False | ||
|
|
||
| c1 = AttributeConstraint(a=func) | ||
| c2 = AttributeConstraint(a=func) | ||
| self.assertEqual(c1, c2) | ||
|
|
||
| def test_func_diff(self): | ||
| c1 = AttributeConstraint(a=lambda a: False) | ||
| c2 = AttributeConstraint(a=lambda a: False) | ||
| self.assertNotEqual(c1, c2) | ||
|
|
||
|
|
||
| class Test_NameConstraint__hash__(tests.IrisTest): | ||
| def test_empty(self): | ||
| c1 = NameConstraint() | ||
| c2 = NameConstraint() | ||
| self.assertEqual(hash(c1), hash(c1)) | ||
| self.assertNotEqual(hash(c1), hash(c2)) | ||
|
|
||
|
|
||
| class Test_NameConstraint__eq__(tests.IrisTest): | ||
| def test_empty_same(self): | ||
| c1 = NameConstraint() | ||
| c2 = NameConstraint() | ||
| self.assertEqual(c1, c2) | ||
| self.assertIsNot(c1, c2) | ||
|
|
||
| def test_attribute_plain_empty_diff(self): | ||
| c1 = NameConstraint() | ||
| c2 = Constraint() | ||
| self.assertNotEqual(c1, c2) | ||
|
|
||
| def test_names_same(self): | ||
| c1 = NameConstraint(standard_name="air_temperature") | ||
| c2 = NameConstraint(standard_name="air_temperature") | ||
| self.assertEqual(c1, c2) | ||
|
|
||
| def test_full_same(self): | ||
| c1 = NameConstraint( | ||
| standard_name="air_temperature", | ||
| long_name="temp", | ||
| var_name="tair", | ||
| STASH="m01s02i003", | ||
| ) | ||
| c2 = NameConstraint( | ||
| standard_name="air_temperature", | ||
| long_name="temp", | ||
| var_name="tair", | ||
| STASH="m01s02i003", | ||
| ) | ||
| self.assertEqual(c1, c2) | ||
|
|
||
| def test_missing_diff(self): | ||
| c1 = NameConstraint(standard_name="air_temperature", var_name="tair") | ||
| c2 = NameConstraint(standard_name="air_temperature") | ||
| self.assertNotEqual(c1, c2) | ||
|
|
||
| def test_standard_name_diff(self): | ||
| c1 = NameConstraint(standard_name="air_temperature") | ||
| c2 = NameConstraint(standard_name="height") | ||
| self.assertNotEqual(c1, c2) | ||
|
|
||
| def test_long_name_diff(self): | ||
| c1 = NameConstraint(long_name="temp") | ||
| c2 = NameConstraint(long_name="t3") | ||
| self.assertNotEqual(c1, c2) | ||
|
|
||
| def test_var_name_diff(self): | ||
| c1 = NameConstraint(var_name="tair") | ||
| c2 = NameConstraint(var_name="xxx") | ||
| self.assertNotEqual(c1, c2) | ||
|
|
||
| def test_stash_diff(self): | ||
| c1 = NameConstraint(STASH="m01s02i003") | ||
| c2 = NameConstraint(STASH="m01s02i777") | ||
| self.assertNotEqual(c1, c2) | ||
|
|
||
| def test_func_same(self): | ||
| def func(name): | ||
| return True | ||
|
|
||
| c1 = NameConstraint(STASH="m01s02i003", long_name=func) | ||
| c2 = NameConstraint(STASH="m01s02i003", long_name=func) | ||
| self.assertEqual(c1, c2) | ||
|
|
||
| def test_func_diff(self): | ||
| c1 = NameConstraint(STASH="m01s02i003", long_name=lambda n: True) | ||
| c2 = NameConstraint(STASH="m01s02i003", long_name=lambda n: True) | ||
| self.assertNotEqual(c1, c2) | ||
|
|
||
|
|
||
| class Test_ConstraintCombination__hash__(tests.IrisTest): | ||
| def test_empty(self): | ||
| c1 = Constraint() & Constraint() | ||
| c2 = Constraint() & Constraint() | ||
| self.assertEqual(hash(c1), hash(c1)) | ||
| self.assertNotEqual(hash(c1), hash(c2)) | ||
|
|
||
| def test_identical_construction(self): | ||
| c1, c2 = Constraint(a=1), Constraint(b=1) | ||
| cc1 = c1 & c2 | ||
| cc2 = c1 & c2 | ||
| self.assertNotEqual(hash(cc1), hash(cc2)) | ||
|
|
||
|
|
||
| class Test_ConstraintCombination__eq__(tests.IrisTest): | ||
| def test_empty_same(self): | ||
| c1 = Constraint() & Constraint() | ||
| c2 = Constraint() & Constraint() | ||
| self.assertEqual(c1, c2) | ||
| self.assertIsNot(c1, c2) | ||
|
|
||
| def test_multi_components_same(self): | ||
| c1 = Constraint("a") & Constraint(b=1) | ||
| c2 = Constraint("a") & Constraint(b=1) | ||
| self.assertEqual(c1, c2) | ||
|
|
||
| def test_multi_components_diff(self): | ||
| c1 = Constraint("a") & Constraint(b=1, c=2) | ||
| c2 = Constraint("a") & Constraint(b=1) | ||
| self.assertNotEqual(c1, c2) | ||
stephenworsley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def test_different_component_order(self): | ||
| c1, c2 = Constraint("a"), Constraint(b=1) | ||
| cc1 = c1 & c2 | ||
| cc2 = c2 & c1 | ||
| self.assertNotEqual(cc1, cc2) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| tests.main() | ||
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.
Uh oh!
There was an error while loading. Please reload this page.