JavierLopezT commented on a change in pull request #6670: [AIRFLOW-4816]MySqlToS3Operator URL: https://github.com/apache/airflow/pull/6670#discussion_r361658822
########## File path: tests/operators/test_mysql_to_s3_operator.py ########## @@ -0,0 +1,71 @@ +# -*- coding: utf-8 -*- +# +# 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. +# +import unittest +from io import StringIO +from unittest import mock + +import numpy as np +import pandas as pd +from boto3.session import Session + +from airflow.operators.mysql_to_s3_operator import MySQLToS3Operator +from airflow.utils.tests import assertEqualIgnoreMultipleSpaces + + +class TestMySqlToS3Operator(unittest.TestCase): + + @mock.patch("boto3.session.Session") + @mock.patch("airflow.hooks.mysql_hook.MySqlHook.run") + def test_execute(self, mock_run, mock_session,): + access_key = "aws_access_key_id" + secret_key = "aws_secret_access_key" + mock_session.return_value = Session(access_key, secret_key) + query = "query" + s3_bucket = "bucket" + s3_key = "key" + header = False + index = False + + MySQLToS3Operator(query=query, + s3_bucket=s3_bucket, + s3_key=s3_key, + mysql_conn_id="mysql_conn_id", + aws_conn_id="aws_conn_id", + task_id="task_id", + dag=None + ).execute(None) + + df = mock_run.get_pandas_df(query) + for col in df: + if "float" in df[col].dtype.name and df[col].hasnans: + # inspect values to determine if dtype of non-null values is int or float + notna_series = df[col].dropna().values + if np.isclose(notna_series, notna_series.astype(int)).all(): + # set to dtype that retains integers and supports NaNs + df[col] = np.where(df[col].isnull(), None, df[col]).astype(pd.Int64Dtype) + + file_obj = StringIO() + df.to_csv(file_obj, header=header, index=index) + mock_session.load_file_obj(file_obj=file_obj, + key=s3_key, + bucket_name=s3_bucket) Review comment: Thank you very much for your help =). In your previous answer you suggest to use return_value a couple of times for testing whether or not the get_pandas_df was called with expected parameters. However, why is not enough to just use `mock_hook.assert_called_once_with(sql=query)` after op.execute? Considering that I have to use, as you pointed, these two statements ``` get_pandas_df_mock = mock_hook.return_value.get_pandas_df. get_pandas_df_mock.return_value = SomeValue ``` Which value should I use in SomeValue? I guess that there is no need to generate a random dataframe, is it? In the test of dataproc there are submit_job and wait_for_job commands,and I have not found in google any information about. Are they necessary at all in my case? Thank you very much again and sorry for having so many questions ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
