From 70621da09e9cdddeb2c05645a1f378bdd56c6e5a Mon Sep 17 00:00:00 2001 From: Son CHU Date: Tue, 18 Jul 2017 23:16:30 +0200 Subject: [PATCH] Add `is_nullable` method to check for `NULLABLE` mode (#3620) Resolves: #3548 --- bigquery/google/cloud/bigquery/dbapi/cursor.py | 2 +- bigquery/google/cloud/bigquery/schema.py | 5 +++++ bigquery/tests/unit/test_schema.py | 10 ++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/bigquery/google/cloud/bigquery/dbapi/cursor.py b/bigquery/google/cloud/bigquery/dbapi/cursor.py index 4398eec20b88..bcbb19cfd066 100644 --- a/bigquery/google/cloud/bigquery/dbapi/cursor.py +++ b/bigquery/google/cloud/bigquery/dbapi/cursor.py @@ -76,7 +76,7 @@ def _set_description(self, schema): internal_size=None, precision=None, scale=None, - null_ok=field.mode == 'NULLABLE') + null_ok=field.is_nullable) for field in schema]) def _set_rowcount(self, query_results): diff --git a/bigquery/google/cloud/bigquery/schema.py b/bigquery/google/cloud/bigquery/schema.py index faec69f616da..edd8dd68f3bd 100644 --- a/bigquery/google/cloud/bigquery/schema.py +++ b/bigquery/google/cloud/bigquery/schema.py @@ -65,6 +65,11 @@ def mode(self): """ return self._mode + @property + def is_nullable(self): + """Check whether 'mode' is 'nullable'.""" + return self._mode == 'NULLABLE' + @property def description(self): """Optional[str]: Description for the field.""" diff --git a/bigquery/tests/unit/test_schema.py b/bigquery/tests/unit/test_schema.py index 018736d31bc1..bf3cf2e025d1 100644 --- a/bigquery/tests/unit/test_schema.py +++ b/bigquery/tests/unit/test_schema.py @@ -74,6 +74,16 @@ def test_mode_property(self): schema_field = self._make_one('again', 'FLOAT', mode=mode) self.assertIs(schema_field.mode, mode) + def test_is_nullable(self): + mode = 'NULLABLE' + schema_field = self._make_one('test', 'FLOAT', mode=mode) + self.assertTrue(schema_field.is_nullable) + + def test_is_not_nullable(self): + mode = 'REPEATED' + schema_field = self._make_one('test', 'FLOAT', mode=mode) + self.assertFalse(schema_field.is_nullable) + def test_description_property(self): description = 'It holds some data.' schema_field = self._make_one(