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

fix: handle all whitespaces in column names #17

Merged
merged 7 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tap_google_sheets/streams.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Stream type classes for tap-google-sheets."""

import re
from itertools import zip_longest
from pathlib import Path
from typing import Iterable
Expand Down Expand Up @@ -37,7 +38,7 @@ def parse_response(self, response: requests.Response) -> Iterable[dict]:
data_rows.append(
dict(
[
(h.replace(" ", "_"), v or "")
(re.sub(r"\s+", "_", h.strip()), v or "")
for m, h, v in zip_longest(mask, headings, values)
if m
]
Expand Down
5 changes: 4 additions & 1 deletion tap_google_sheets/tap.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""google_sheets tap class."""

import re
from typing import List

import requests
Expand Down Expand Up @@ -102,7 +103,9 @@ def get_schema(self, google_sheet_data: requests.Response):
schema = th.PropertiesList()
for column in headings:
if column:
schema.append(th.Property(column.replace(" ", "_"), th.StringType))
schema.append(
th.Property(re.sub(r"\s+", "_", column.strip()), th.StringType)
)

return schema.to_dict()

Expand Down
30 changes: 27 additions & 3 deletions tap_google_sheets/tests/test_underscoring_column_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,21 @@ def setUp(self):

@responses.activate()
def test_underscoring_column_names(self):
self.column_response = {"values": [["Column One", "Column Two"], ["1", "1"]]}
self.column_response = {
"values": [
[
"Column One",
" Column Two",
" Column Three ",
"Column\nFour",
"Column \n\tFive\n\n\t ",
"Multi Column One",
" Multi Column Two ",
"\tMulti \r\nColumn\f\vThree",
],
["1"] * 8,
]
}

responses.add(
responses.POST,
Expand All @@ -39,7 +53,7 @@ def test_underscoring_column_names(self):
responses.add(
responses.GET,
"https://sheets.googleapis.com/v4/spreadsheets/12345/values/!1:1",
json={"range": "Sheet1!1:1", "values": [["Column_One", "Column_Two"]]},
json={"range": "Sheet1!1:1", "values": [self.column_response["values"][0]]},
status=200,
),
responses.add(
Expand All @@ -61,5 +75,15 @@ def test_underscoring_column_names(self):

# Assert that column names have been underscored
self.assertEquals(
test_utils.SINGER_MESSAGES[2].record, {"Column_One": "1", "Column_Two": "1"}
test_utils.SINGER_MESSAGES[2].record,
{
"Column_One": "1",
"Column_Two": "1",
"Column_Three": "1",
"Column_Four": "1",
"Column_Five": "1",
"Multi_Column_One": "1",
"Multi_Column_Two": "1",
"Multi_Column_Three": "1",
},
)
Loading