Content-Length: 523935 | pFad | https://www.github.com/googleapis/python-bigquery/commit/0d2a88d8072154cfc9152afd6d26a60ddcdfbc73

E72 feat(bigquery): add __eq__ method for class PartitionRange and RangeP… · googleapis/python-bigquery@0d2a88d · GitHub
Skip to content

Commit 0d2a88d

Browse files
feat(bigquery): add __eq__ method for class PartitionRange and RangePartitioning (#162)
* feat(bigquery): add __eq__ method for class PartitionRange and RangePartitioning * feat(bigquery): change class object to unhashable * feat(bigquery): change the assertion
1 parent dbaf3bd commit 0d2a88d

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

google/cloud/bigquery/table.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,10 +1891,20 @@ def interval(self, value):
18911891
def _key(self):
18921892
return tuple(sorted(self._properties.items()))
18931893

1894+
def __eq__(self, other):
1895+
if not isinstance(other, PartitionRange):
1896+
return NotImplemented
1897+
return self._key() == other._key()
1898+
1899+
def __ne__(self, other):
1900+
return not self == other
1901+
18941902
def __repr__(self):
18951903
key_vals = ["{}={}".format(key, val) for key, val in self._key()]
18961904
return "PartitionRange({})".format(", ".join(key_vals))
18971905

1906+
__hash__ = None
1907+
18981908

18991909
class RangePartitioning(object):
19001910
"""Range-based partitioning configuration for a table.
@@ -1961,10 +1971,20 @@ def field(self, value):
19611971
def _key(self):
19621972
return (("field", self.field), ("range_", self.range_))
19631973

1974+
def __eq__(self, other):
1975+
if not isinstance(other, RangePartitioning):
1976+
return NotImplemented
1977+
return self._key() == other._key()
1978+
1979+
def __ne__(self, other):
1980+
return not self == other
1981+
19641982
def __repr__(self):
19651983
key_vals = ["{}={}".format(key, repr(val)) for key, val in self._key()]
19661984
return "RangePartitioning({})".format(", ".join(key_vals))
19671985

1986+
__hash__ = None
1987+
19681988

19691989
class TimePartitioningType(object):
19701990
"""Specifies the type of time partitioning to perform."""

tests/unit/test_table.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3525,6 +3525,37 @@ def test_constructor_w_resource(self):
35253525
assert object_under_test.end == 1234567890
35263526
assert object_under_test.interval == 1000000
35273527

3528+
def test___eq___start_mismatch(self):
3529+
object_under_test = self._make_one(start=1, end=10, interval=2)
3530+
other = self._make_one(start=2, end=10, interval=2)
3531+
self.assertNotEqual(object_under_test, other)
3532+
3533+
def test___eq___end__mismatch(self):
3534+
object_under_test = self._make_one(start=1, end=10, interval=2)
3535+
other = self._make_one(start=1, end=11, interval=2)
3536+
self.assertNotEqual(object_under_test, other)
3537+
3538+
def test___eq___interval__mismatch(self):
3539+
object_under_test = self._make_one(start=1, end=10, interval=2)
3540+
other = self._make_one(start=1, end=11, interval=3)
3541+
self.assertNotEqual(object_under_test, other)
3542+
3543+
def test___eq___hit(self):
3544+
object_under_test = self._make_one(start=1, end=10, interval=2)
3545+
other = self._make_one(start=1, end=10, interval=2)
3546+
self.assertEqual(object_under_test, other)
3547+
3548+
def test__eq___type_mismatch(self):
3549+
object_under_test = self._make_one(start=1, end=10, interval=2)
3550+
self.assertNotEqual(object_under_test, object())
3551+
self.assertEqual(object_under_test, mock.ANY)
3552+
3553+
def test_unhashable_object(self):
3554+
object_under_test1 = self._make_one(start=1, end=10, interval=2)
3555+
3556+
with six.assertRaisesRegex(self, TypeError, r".*unhashable type.*"):
3557+
hash(object_under_test1)
3558+
35283559
def test_repr(self):
35293560
object_under_test = self._make_one(start=1, end=10, interval=2)
35303561
assert repr(object_under_test) == "PartitionRange(end=10, interval=2, start=1)"
@@ -3574,6 +3605,57 @@ def test_range_w_wrong_type(self):
35743605
with pytest.raises(ValueError, match="PartitionRange"):
35753606
object_under_test.range_ = object()
35763607

3608+
def test___eq___field_mismatch(self):
3609+
from google.cloud.bigquery.table import PartitionRange
3610+
3611+
object_under_test = self._make_one(
3612+
range_=PartitionRange(start=1, end=10, interval=2), field="integer_col"
3613+
)
3614+
other = self._make_one(
3615+
range_=PartitionRange(start=1, end=10, interval=2), field="float_col"
3616+
)
3617+
self.assertNotEqual(object_under_test, other)
3618+
3619+
def test___eq___range__mismatch(self):
3620+
from google.cloud.bigquery.table import PartitionRange
3621+
3622+
object_under_test = self._make_one(
3623+
range_=PartitionRange(start=1, end=10, interval=2), field="integer_col"
3624+
)
3625+
other = self._make_one(
3626+
range_=PartitionRange(start=2, end=20, interval=2), field="float_col"
3627+
)
3628+
self.assertNotEqual(object_under_test, other)
3629+
3630+
def test___eq___hit(self):
3631+
from google.cloud.bigquery.table import PartitionRange
3632+
3633+
object_under_test = self._make_one(
3634+
range_=PartitionRange(start=1, end=10, interval=2), field="integer_col"
3635+
)
3636+
other = self._make_one(
3637+
range_=PartitionRange(start=1, end=10, interval=2), field="integer_col"
3638+
)
3639+
self.assertEqual(object_under_test, other)
3640+
3641+
def test__eq___type_mismatch(self):
3642+
from google.cloud.bigquery.table import PartitionRange
3643+
3644+
object_under_test = self._make_one(
3645+
range_=PartitionRange(start=1, end=10, interval=2), field="integer_col"
3646+
)
3647+
self.assertNotEqual(object_under_test, object())
3648+
self.assertEqual(object_under_test, mock.ANY)
3649+
3650+
def test_unhashable_object(self):
3651+
from google.cloud.bigquery.table import PartitionRange
3652+
3653+
object_under_test1 = self._make_one(
3654+
range_=PartitionRange(start=1, end=10, interval=2), field="integer_col"
3655+
)
3656+
with six.assertRaisesRegex(self, TypeError, r".*unhashable type.*"):
3657+
hash(object_under_test1)
3658+
35773659
def test_repr(self):
35783660
from google.cloud.bigquery.table import PartitionRange
35793661

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://www.github.com/googleapis/python-bigquery/commit/0d2a88d8072154cfc9152afd6d26a60ddcdfbc73

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy