-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Phase 1 for storing schemas for later use. #7761
Changes from 8 commits
9d3198b
894bb26
136cfcb
092e121
79e965b
22bf1ab
bb2ca79
5451852
f4ae0c1
5791049
fa331c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5161,3 +5161,166 @@ def test__do_multipart_upload_wrong_size(self): | |
|
||
with pytest.raises(ValueError): | ||
client._do_multipart_upload(file_obj, {}, file_obj_len + 1, None) | ||
|
||
def test_schema_from_json_with_file_path(self): | ||
from google.cloud.bigquery.schema import SchemaField | ||
|
||
file_content = """[ | ||
{ | ||
"description": "quarter", | ||
"mode": "REQUIRED", | ||
"name": "qtr", | ||
"type": "STRING" | ||
}, | ||
{ | ||
"description": "sales representative", | ||
"mode": "NULLABLE", | ||
"name": "rep", | ||
"type": "STRING" | ||
}, | ||
{ | ||
"description": "total sales", | ||
"mode": "NULLABLE", | ||
"name": "sales", | ||
"type": "FLOAT" | ||
} | ||
]""" | ||
|
||
expected = [ | ||
SchemaField("qtr", "STRING", "REQUIRED", "quarter"), | ||
SchemaField("rep", "STRING", "NULLABLE", "sales representative"), | ||
SchemaField("sales", "FLOAT", "NULLABLE", "total sales"), | ||
] | ||
|
||
client = self._make_client() | ||
mock_file_path = "/mocked/file.json" | ||
tswast marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
open_patch = mock.patch( | ||
"builtins.open", new=mock.mock_open(read_data=file_content) | ||
) | ||
|
||
with open_patch as _mock_file: | ||
actual = client.schema_from_json(mock_file_path) | ||
_mock_file.assert_called_once_with(mock_file_path) | ||
tswast marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# This assert is to make sure __exit__ is called in the context | ||
# manager that opens the file in the function | ||
_mock_file().__exit__.assert_called_once() | ||
|
||
assert expected == actual | ||
|
||
def test_schema_from_json_with_file_object(self): | ||
from google.cloud.bigquery.schema import SchemaField | ||
|
||
file_content = """[ | ||
{ | ||
"description": "quarter", | ||
"mode": "REQUIRED", | ||
"name": "qtr", | ||
"type": "STRING" | ||
}, | ||
{ | ||
"description": "sales representative", | ||
"mode": "NULLABLE", | ||
"name": "rep", | ||
"type": "STRING" | ||
}, | ||
{ | ||
"description": "total sales", | ||
"mode": "NULLABLE", | ||
"name": "sales", | ||
"type": "FLOAT" | ||
} | ||
]""" | ||
|
||
expected = [ | ||
SchemaField("qtr", "STRING", "REQUIRED", "quarter"), | ||
SchemaField("rep", "STRING", "NULLABLE", "sales representative"), | ||
SchemaField("sales", "FLOAT", "NULLABLE", "total sales"), | ||
] | ||
|
||
client = self._make_client() | ||
|
||
fake_file = io.StringIO(file_content) | ||
actual = client.schema_from_json(fake_file) | ||
|
||
assert expected == actual | ||
|
||
def test_schema_to_json_with_file_path(self): | ||
from google.cloud.bigquery.schema import SchemaField | ||
|
||
file_content = [ | ||
{ | ||
"description": "quarter", | ||
"mode": "REQUIRED", | ||
"name": "qtr", | ||
"type": "STRING", | ||
}, | ||
{ | ||
"description": "sales representative", | ||
"mode": "NULLABLE", | ||
"name": "rep", | ||
"type": "STRING", | ||
}, | ||
{ | ||
"description": "total sales", | ||
"mode": "NULLABLE", | ||
"name": "sales", | ||
"type": "FLOAT", | ||
}, | ||
] | ||
|
||
schema_list = [ | ||
SchemaField("qtr", "STRING", "REQUIRED", "quarter"), | ||
SchemaField("rep", "STRING", "NULLABLE", "sales representative"), | ||
SchemaField("sales", "FLOAT", "NULLABLE", "total sales"), | ||
] | ||
|
||
client = self._make_client() | ||
mock_file_path = "/mocked/file.json" | ||
|
||
open_patch = mock.patch("builtins.open", mock.mock_open()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the test logs, it looks like this is tripping up Python 2. https://stackoverflow.com/a/34677735/101923
Remember you can run against Python 2 by using We could check the Python version with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed all the issues and the code is compatible with python 2 now. |
||
with open_patch as mock_file, mock.patch("json.dump") as mock_dump: | ||
client.schema_to_json(schema_list, mock_file_path) | ||
mock_file.assert_called_once_with(mock_file_path, mode="w") | ||
# This assert is to make sure __exit__ is called in the context | ||
# manager that opens the file in the function | ||
mock_file().__exit__.assert_called_once() | ||
mock_dump.assert_called_with( | ||
file_content, mock_file.return_value, indent=2, sort_keys=True | ||
) | ||
|
||
def test_schema_to_json_with_file_object(self): | ||
from google.cloud.bigquery.schema import SchemaField | ||
|
||
file_content = [ | ||
{ | ||
"description": "quarter", | ||
"mode": "REQUIRED", | ||
"name": "qtr", | ||
"type": "STRING", | ||
}, | ||
{ | ||
"description": "sales representative", | ||
"mode": "NULLABLE", | ||
"name": "rep", | ||
"type": "STRING", | ||
}, | ||
{ | ||
"description": "total sales", | ||
"mode": "NULLABLE", | ||
"name": "sales", | ||
"type": "FLOAT", | ||
}, | ||
] | ||
|
||
schema_list = [ | ||
SchemaField("qtr", "STRING", "REQUIRED", "quarter"), | ||
SchemaField("rep", "STRING", "NULLABLE", "sales representative"), | ||
SchemaField("sales", "FLOAT", "NULLABLE", "total sales"), | ||
] | ||
|
||
fake_file = io.StringIO() | ||
client = self._make_client() | ||
|
||
client.schema_to_json(schema_list, fake_file) | ||
assert file_content == json.loads(fake_file.getvalue()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, coverage is failing on this line and the similar line in the other function. We'd need a test where
open()
fails.Honestly, I'd be okay removing the
try
block and letting these errors just raise, too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the
try
block as suggested for both functions.