Skip to content
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

feat: make the same Table* instances equal to each other #867

Merged
merged 6 commits into from Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions google/cloud/bigquery/table.py
Expand Up @@ -1011,6 +1011,13 @@ def _build_resource(self, filter_fields):
"""Generate a resource for ``update``."""
return _helpers._build_resource_from_properties(self, filter_fields)

def __eq__(self, other):
if not isinstance(other, Table):
plamut marked this conversation as resolved.
Show resolved Hide resolved
return NotImplemented
return self._properties["tableReference"] == other._properties["tableReference"]

__hash__ = None

def __repr__(self):
return "Table({})".format(repr(self.reference))

Expand Down
53 changes: 53 additions & 0 deletions tests/unit/test_table.py
Expand Up @@ -581,6 +581,59 @@ def test_num_rows_getter(self):
with self.assertRaises(ValueError):
getattr(table, "num_rows")

def test__eq__wrong_type(self):
table = self._make_one("project_foo.dataset_bar.table_baz")

class TableWannabe:
pass

not_a_table = TableWannabe()
not_a_table._properties = table._properties

assert table != not_a_table # Can't fake it.

def test__eq__same_table_basic(self):
table_1 = self._make_one("project_foo.dataset_bar.table_baz")
table_2 = self._make_one("project_foo.dataset_bar.table_baz")
assert table_1 == table_2

def test__eq__same_table_multiple_properties(self):
from google.cloud.bigquery import SchemaField

table_1 = self._make_one("project_foo.dataset_bar.table_baz")
table_1.require_partition_filter = True
table_1.labels = {"first": "one", "second": "two"}

table_1.schema = [
SchemaField("name", "STRING", "REQUIRED"),
SchemaField("age", "INTEGER", "NULLABLE"),
]

table_2 = self._make_one("project_foo.dataset_bar.table_baz")
table_2.require_partition_filter = True
table_2.labels = {"first": "one", "second": "two"}
table_2.schema = [
SchemaField("name", "STRING", "REQUIRED"),
SchemaField("age", "INTEGER", "NULLABLE"),
]

assert table_1 == table_2

def test__eq__same_table_property_different(self):
table_1 = self._make_one("project_foo.dataset_bar.table_baz")
table_1.description = "This is table baz"

table_2 = self._make_one("project_foo.dataset_bar.table_baz")
table_2.description = "This is also table baz"

assert table_1 == table_2 # Still equal, only table reference is important.

def test__eq__different_table(self):
table_1 = self._make_one("project_foo.dataset_bar.table_baz")
table_2 = self._make_one("project_foo.dataset_bar.table_baz_2")

assert table_1 != table_2

def test_schema_setter_non_sequence(self):
dataset = DatasetReference(self.PROJECT, self.DS_ID)
table_ref = dataset.table(self.TABLE_NAME)
Expand Down