Skip to content

Commit c267a3c

Browse files
committed
updates some tests and constants
1 parent 2766b2c commit c267a3c

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

google/cloud/bigquery/external_config.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from google.cloud.bigquery._helpers import _int_or_none
3131
from google.cloud.bigquery._helpers import _str_or_none
3232
from google.cloud.bigquery import _helpers
33+
from google.cloud.bigquery.enums import SourceColumnMatch
3334
from google.cloud.bigquery.format_options import AvroOptions, ParquetOptions
3435
from google.cloud.bigquery import schema
3536
from google.cloud.bigquery.schema import SchemaField
@@ -499,14 +500,15 @@ def null_markers(self, value: Optional[List[str]]):
499500
self._properties["nullMarkers"] = value
500501

501502
@property
502-
def source_column_match(self) -> Optional[str]:
503-
"""Optional[str]: Controls the strategy used to match loaded columns to the schema. If not
504-
set, a sensible default is chosen based on how the schema is provided. If
505-
autodetect is used, then columns are matched by name. Otherwise, columns
506-
are matched by position. This is done to keep the behavior
507-
backward-compatible.
503+
def source_column_match(self) -> Optional[SourceColumnMatch]:
504+
"""Optional[SourceColumnMatch]: Controls the strategy used to match loaded
505+
columns to the schema. If not set, a sensible default is chosen based on
506+
how the schema is provided. If autodetect is used, then columns are matched
507+
by name. Otherwise, columns are matched by position. This is done to keep
508+
the behavior backward-compatible.
508509
509510
Acceptable values are:
511+
SOURCE_COLUMN_MATCH_UNSPECIFIED - Unspecified column name match option.
510512
POSITION - matches by position. This assumes that the columns are ordered
511513
the same way as the schema.
512514
NAME - matches by name. This reads the header row as column names and
@@ -515,12 +517,19 @@ def source_column_match(self) -> Optional[str]:
515517
See
516518
https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#ExternalDataConfiguration.FIELDS.source_column_match
517519
"""
518-
result = self._properties.get("sourceColumnMatch")
519-
return typing.cast(str, result)
520+
521+
value = self._properties.get("sourceColumnMatch")
522+
if value is not None:
523+
return SourceColumnMatch(value)
524+
return None
520525

521526
@source_column_match.setter
522-
def source_column_match(self, value: Optional[str]):
523-
self._properties["sourceColumnMatch"] = value
527+
def source_column_match(self, value: Optional[SourceColumnMatch]):
528+
if value is not None and not isinstance(value, SourceColumnMatch):
529+
raise TypeError(
530+
"value must be a google.cloud.bigquery.enums.SourceColumnMatch or None"
531+
)
532+
self._properties["sourceColumnMatch"] = value.value if value else None
524533

525534
def to_api_repr(self) -> dict:
526535
"""Build an API representation of this object.

google/cloud/bigquery/job/load.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -644,12 +644,14 @@ def null_markers(self, value: Optional[List[str]]):
644644

645645
@property
646646
def source_column_match(self) -> Optional[SourceColumnMatch]:
647-
"""Optional[google.cloud.bigquery.enums.SourceColumnMatch]: Controls the strategy used to match
648-
loaded columns to the schema. If not set, a sensible default is chosen based on how the schema
649-
is provided. If autodetect is used, then columns are matched by name. Otherwise, columns
650-
are matched by position. This is done to keep the behavior backward-compatible.
647+
"""Optional[google.cloud.bigquery.enums.SourceColumnMatch]: Controls the
648+
strategy used to match loaded columns to the schema. If not set, a sensible
649+
default is chosen based on how the schema is provided. If autodetect is
650+
used, then columns are matched by name. Otherwise, columns are matched by
651+
position. This is done to keep the behavior backward-compatible.
651652
652653
Acceptable values are:
654+
SOURCE_COLUMN_MATCH_UNSPECIFIED - Unspecified column name match option.
653655
POSITION - matches by position. This assumes that the columns are ordered
654656
the same way as the schema.
655657
NAME - matches by name. This reads the header row as column names and

tests/unit/test_external_config.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from google.cloud.bigquery import external_config
2121
from google.cloud.bigquery import schema
22+
from google.cloud.bigquery.enums import SourceColumnMatch
2223

2324
import pytest
2425

@@ -30,6 +31,8 @@ class TestExternalConfig(unittest.TestCase):
3031
DATETIME_FORMAT = "MM/DD/YYYY HH24:MI:SS"
3132
TIME_FORMAT = "HH24:MI:SS"
3233
TIMESTAMP_FORMAT = "MM/DD/YYYY HH24:MI:SS.FF6 TZR"
34+
NULL_MARKERS = ["", "N/A"]
35+
SOURCE_COLUMN_MATCH = SourceColumnMatch.NAME
3336

3437
BASE_RESOURCE = {
3538
"sourceFormat": "",
@@ -263,9 +266,6 @@ def test_to_api_repr_hive_partitioning(self):
263266
}
264267
self.assertEqual(got_resource, expected_resource)
265268

266-
NULL_MARKERS = ["", "N/A"]
267-
SOURCE_COLUMN_MATCH = "NAME"
268-
269269
def test_from_api_repr_csv(self):
270270
resource = _copy_and_update(
271271
self.BASE_RESOURCE,
@@ -891,6 +891,9 @@ def test_to_api_repr(self):
891891

892892

893893
class CSVOptions(unittest.TestCase):
894+
NULL_MARKERS = ["", "N/A"]
895+
SOURCE_COLUMN_MATCH = SourceColumnMatch.NAME
896+
894897
def test_to_api_repr(self):
895898
options = external_config.CSVOptions()
896899
options.field_delimiter = "\t"
@@ -900,8 +903,8 @@ def test_to_api_repr(self):
900903
options.allow_jagged_rows = False
901904
options.encoding = "UTF-8"
902905
options.preserve_ascii_control_characters = False
903-
options.null_markers = ["NA"]
904-
options.source_column_match = "POSITION"
906+
options.null_markers = self.NULL_MARKERS
907+
options.source_column_match = self.SOURCE_COLUMN_MATCH
905908

906909
resource = options.to_api_repr()
907910

@@ -915,8 +918,8 @@ def test_to_api_repr(self):
915918
"allowJaggedRows": False,
916919
"encoding": "UTF-8",
917920
"preserveAsciiControlCharacters": False,
918-
"nullMarkers": ["NA"],
919-
"sourceColumnMatch": "POSITION",
921+
"nullMarkers": self.NULL_MARKERS,
922+
"sourceColumnMatch": self.SOURCE_COLUMN_MATCH,
920923
},
921924
)
922925

0 commit comments

Comments
 (0)