Skip to content

Commit

Permalink
Fix __eq__ and __ne__. (googleapis#3765)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesneeringer authored and landrito committed Aug 22, 2017
1 parent c2cd74b commit 347ed4b
Show file tree
Hide file tree
Showing 20 changed files with 138 additions and 64 deletions.
5 changes: 5 additions & 0 deletions bigquery/google/cloud/bigquery/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,15 @@ def __init__(self, udf_type, value):
self.value = value

def __eq__(self, other):
if not isinstance(other, UDFResource):
return NotImplemented
return(
self.udf_type == other.udf_type and
self.value == other.value)

def __ne__(self, other):
return not self == other


class UDFResourcesProperty(object):
"""Custom property type, holding :class:`UDFResource` instances."""
Expand Down
5 changes: 5 additions & 0 deletions bigquery/google/cloud/bigquery/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,16 @@ def __init__(self, role, entity_type, entity_id):
self.entity_id = entity_id

def __eq__(self, other):
if not isinstance(other, AccessGrant):
return NotImplemented
return (
self.role == other.role and
self.entity_type == other.entity_type and
self.entity_id == other.entity_id)

def __ne__(self, other):
return not self == other

def __repr__(self):
return '<AccessGrant: role=%s, %s=%s>' % (
self.role, self.entity_type, self.entity_id)
Expand Down
10 changes: 3 additions & 7 deletions bigquery/google/cloud/bigquery/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,12 @@ def _key(self):
)

def __eq__(self, other):
if isinstance(other, SchemaField):
return self._key() == other._key()
else:
if not isinstance(other, SchemaField):
return NotImplemented
return self._key() == other._key()

def __ne__(self, other):
if isinstance(other, SchemaField):
return self._key() != other._key()
else:
return NotImplemented
return not self == other

def __hash__(self):
return hash(self._key())
Expand Down
14 changes: 14 additions & 0 deletions bigquery/tests/unit/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import unittest

import mock


class Test_not_null(unittest.TestCase):

Expand Down Expand Up @@ -815,6 +817,18 @@ def test_instance_getter_empty(self):
instance = klass()
self.assertEqual(instance.udf_resources, [])

def test_resource_equality(self):
from google.cloud.bigquery._helpers import UDFResource

resource1a = UDFResource('resourceUri', 'gs://bucket/file.js')
resource1b = UDFResource('resourceUri', 'gs://bucket/file.js')
resource2 = UDFResource('resourceUri', 'gs://bucket/other.js')

self.assertEqual(resource1a, resource1b)
self.assertNotEqual(resource1a, resource2)
self.assertNotEqual(resource1a, object())
self.assertEqual(resource1a, mock.ANY)

def test_instance_getter_w_non_empty_list(self):
from google.cloud.bigquery._helpers import UDFResource

Expand Down
7 changes: 7 additions & 0 deletions bigquery/tests/unit/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import unittest

import mock


class TestAccessGrant(unittest.TestCase):

Expand Down Expand Up @@ -77,6 +79,11 @@ def test___eq___hit(self):
other = self._make_one('OWNER', 'userByEmail', 'phred@example.com')
self.assertEqual(grant, other)

def test__eq___type_mismatch(self):
grant = self._make_one('OWNER', 'userByEmail', 'silly@example.com')
self.assertNotEqual(grant, object())
self.assertEqual(grant, mock.ANY)


class TestDataset(unittest.TestCase):
PROJECT = 'project'
Expand Down
6 changes: 4 additions & 2 deletions bigquery/tests/unit/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import unittest

import mock


class TestSchemaField(unittest.TestCase):

Expand Down Expand Up @@ -101,7 +103,7 @@ def test___eq___wrong_type(self):
field = self._make_one('test', 'STRING')
other = object()
self.assertNotEqual(field, other)
self.assertIs(field.__eq__(other), NotImplemented)
self.assertEqual(field, mock.ANY)

def test___eq___name_mismatch(self):
field = self._make_one('test', 'STRING')
Expand Down Expand Up @@ -155,7 +157,7 @@ def test___ne___wrong_type(self):
field = self._make_one('toast', 'INTEGER')
other = object()
self.assertNotEqual(field, other)
self.assertIs(field.__ne__(other), NotImplemented)
self.assertEqual(field, mock.ANY)

def test___ne___same_value(self):
field1 = self._make_one('test', 'TIMESTAMP', mode='REPEATED')
Expand Down
4 changes: 2 additions & 2 deletions bigtable/google/cloud/bigtable/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def name(self):

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return NotImplemented
# NOTE: This does not compare the configuration values, such as
# the serve_nodes. Instead, it only compares
# identifying values instance, cluster ID and client. This is
Expand All @@ -170,7 +170,7 @@ def __eq__(self, other):
other._instance == self._instance)

def __ne__(self, other):
return not self.__eq__(other)
return not self == other

def reload(self):
"""Reload the metadata for this cluster."""
Expand Down
27 changes: 18 additions & 9 deletions bigtable/google/cloud/bigtable/column_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ class GarbageCollectionRule(object):
don't support that feature and instead support via native classes.
"""

def __ne__(self, other):
return not self.__eq__(other)


class MaxVersionsGCRule(GarbageCollectionRule):
"""Garbage collection limiting the number of versions of a cell.
Expand All @@ -55,9 +52,12 @@ def __init__(self, max_num_versions):

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return NotImplemented
return other.max_num_versions == self.max_num_versions

def __ne__(self, other):
return not self == other

def to_pb(self):
"""Converts the garbage collection rule to a protobuf.
Expand All @@ -79,9 +79,12 @@ def __init__(self, max_age):

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return NotImplemented
return other.max_age == self.max_age

def __ne__(self, other):
return not self == other

def to_pb(self):
"""Converts the garbage collection rule to a protobuf.
Expand All @@ -104,9 +107,12 @@ def __init__(self, rules):

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return NotImplemented
return other.rules == self.rules

def __ne__(self, other):
return not self == other

def to_pb(self):
"""Converts the union into a single GC rule as a protobuf.
Expand All @@ -130,9 +136,12 @@ def __init__(self, rules):

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return NotImplemented
return other.rules == self.rules

def __ne__(self, other):
return not self == other

def to_pb(self):
"""Converts the intersection into a single GC rule as a protobuf.
Expand Down Expand Up @@ -190,13 +199,13 @@ def name(self):

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return NotImplemented
return (other.column_family_id == self.column_family_id and
other._table == self._table and
other.gc_rule == self.gc_rule)

def __ne__(self, other):
return not self.__eq__(other)
return not self == other

def to_pb(self):
"""Converts the column family to a protobuf.
Expand Down
4 changes: 2 additions & 2 deletions bigtable/google/cloud/bigtable/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def name(self):

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return NotImplemented
# NOTE: This does not compare the configuration values, such as
# the display_name. Instead, it only compares
# identifying values instance ID and client. This is
Expand All @@ -191,7 +191,7 @@ def __eq__(self, other):
other._client == self._client)

def __ne__(self, other):
return not self.__eq__(other)
return not self == other

def reload(self):
"""Reload the metadata for this instance."""
Expand Down
12 changes: 6 additions & 6 deletions bigtable/google/cloud/bigtable/row_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ def from_pb(cls, cell_pb):

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return NotImplemented
return (other.value == self.value and
other.timestamp == self.timestamp and
other.labels == self.labels)

def __ne__(self, other):
return not self.__eq__(other)
return not self == other


class PartialCellData(object):
Expand Down Expand Up @@ -126,12 +126,12 @@ def __init__(self, row_key):

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return NotImplemented
return (other._row_key == self._row_key and
other._cells == self._cells)

def __ne__(self, other):
return not self.__eq__(other)
return not self == other

def to_dict(self):
"""Convert the cells to a dictionary.
Expand Down Expand Up @@ -211,11 +211,11 @@ def __init__(self, response_iterator):

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return NotImplemented
return other._response_iterator == self._response_iterator

def __ne__(self, other):
return not self.__eq__(other)
return not self == other

@property
def state(self):
Expand Down
Loading

0 comments on commit 347ed4b

Please sign in to comment.