LiamHecht commented on code in PR #75559: URL: https://github.com/apache/airflow/pull/75559#discussion_r4458258127
########## providers/google/tests/unit/google/cloud/transfers/test_presto_to_gcs.py: ########## @@ -0,0 +1,337 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +from itertools import chain, repeat +from unittest.mock import MagicMock, patch + +import pytest +from prestodb.dbapi import Cursor as PrestoCursor + +from airflow.providers.google.cloud.transfers.presto_to_gcs import ( + PrestoToGCSOperator, + _PrestoToGCSPrestoCursorAdapter, +) + +TASK_ID = "test-presto-to-gcs" +PRESTO_CONN_ID = "my-presto-conn" +GCP_CONN_ID = "my-gcp-conn" +IMPERSONATION_CHAIN = ["ACCOUNT_1", "ACCOUNT_2", "ACCOUNT_3"] +SQL = "SELECT * FROM memory.default.test_multiple_types" +BUCKET = "gs://test" +FILENAME = "test_{}.ndjson" + +NDJSON_LINES = [ + b'{"some_num": 42, "some_str": "mock_row_content_1"}\n', + b'{"some_num": 43, "some_str": "mock_row_content_2"}\n', + b'{"some_num": 44, "some_str": "mock_row_content_3"}\n', +] +CSV_LINES = [ + b"some_num,some_str\r\n", + b"42,mock_row_content_1\r\n", + b"43,mock_row_content_2\r\n", + b"44,mock_row_content_3\r\n", +] +SCHEMA_FILENAME = "schema_test.json" +SCHEMA_JSON = b'[{"name": "some_num", "type": "INT64"}, {"name": "some_str", "type": "STRING"}]' + +ROWS = [ + [42, "mock_row_content_1"], + [43, "mock_row_content_2"], + [44, "mock_row_content_3"], +] + + +def _setup_mock_cursor(mock_presto_hook, str_type="VARCHAR"): + mock_cursor = mock_presto_hook.return_value.get_conn.return_value.cursor + mock_cursor.return_value.description = [ + ("some_num", "INTEGER", None, None, None, None, None), + ("some_str", str_type, None, None, None, None, None), + ] + mock_cursor.return_value.fetchone.side_effect = [*ROWS, None] + return mock_cursor + + +class TestPrestoToGCSPrestoCursorAdapter: + @pytest.fixture + def cursor(self): + cursor = MagicMock(spec=PrestoCursor) + cursor.fetchone.side_effect = chain(ROWS, repeat(None)) + cursor.arraysize = 2 + return cursor + + def test_description_peeks_first_row_without_consuming_it(self, cursor): + adapter = _PrestoToGCSPrestoCursorAdapter(cursor) + + assert adapter.description is cursor.description + assert adapter.initialized + assert cursor.fetchone.call_count == 1 + assert adapter.fetchone() == ROWS[0] + assert cursor.fetchone.call_count == 1 + + def test_description_does_not_peek_twice(self, cursor): + adapter = _PrestoToGCSPrestoCursorAdapter(cursor) + + adapter.description + adapter.description + + assert cursor.fetchone.call_count == 1 + + def test_peekone(self, cursor): + adapter = _PrestoToGCSPrestoCursorAdapter(cursor) + + assert adapter.peekone() == ROWS[0] + assert adapter.fetchone() == ROWS[0] + assert adapter.fetchone() == ROWS[1] + + def test_fetchmany_uses_arraysize_by_default(self, cursor): + adapter = _PrestoToGCSPrestoCursorAdapter(cursor) + + assert adapter.fetchmany() == ROWS[:2] + assert adapter.fetchmany() == ROWS[2:] + assert adapter.fetchmany() == [] + + def test_fetchmany_with_size(self, cursor): + adapter = _PrestoToGCSPrestoCursorAdapter(cursor) + + assert adapter.fetchmany(5) == ROWS + + def test_iteration(self, cursor): + adapter = _PrestoToGCSPrestoCursorAdapter(cursor) + + assert iter(adapter) is adapter + assert list(adapter) == ROWS + + @pytest.mark.parametrize("method", ["execute", "executemany"]) + def test_execute_resets_peeked_rows(self, cursor, method): + adapter = _PrestoToGCSPrestoCursorAdapter(cursor) + adapter.peekone() + + result = getattr(adapter, method)(SQL) + + assert result is getattr(cursor, method).return_value + getattr(cursor, method).assert_called_once_with(SQL) + assert not adapter.initialized + assert adapter.rows == [] Review Comment: It checks that running a new query drops a row peeked from the previous one. I renamed it to test_execute_drops_row_peeked_from_previous_query, added a docstring, and it now checks this through -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
