From 7a94dd8a8526f27c358c93b49c04c700b94a3570 Mon Sep 17 00:00:00 2001 From: Son CHU Date: Tue, 18 Jul 2017 12:36:17 +0200 Subject: [PATCH 1/2] Add `is_nullable` method to check for `NULLABLE` mode Resolves: #3548 --- bigquery/google/cloud/bigquery/dbapi/cursor.py | 2 +- bigquery/google/cloud/bigquery/schema.py | 5 +++++ 2 files changed, 6 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.""" From 152b78e26ca7886ac61e1862cafbbf96b9c01440 Mon Sep 17 00:00:00 2001 From: Son CHU Date: Tue, 18 Jul 2017 22:58:15 +0200 Subject: [PATCH 2/2] Add tests for the `is_nullable` property Resolves: #3548 --- bigquery/tests/unit/test_schema.py | 10 ++++++++++ 1 file changed, 10 insertions(+) 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(