forked from apache/airflow
/
test_hive_to_mysql.py
177 lines (148 loc) · 7.82 KB
/
test_hive_to_mysql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#
# 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 os
import re
import unittest
from unittest.mock import MagicMock, patch
from airflow.providers.apache.hive.transfers.hive_to_mysql import HiveToMySqlOperator
from airflow.utils import timezone
from airflow.utils.operator_helpers import context_to_airflow_vars
from tests.providers.apache.hive import MockHiveServer2Hook, MockMySqlHook, TestHiveEnvironment
DEFAULT_DATE = timezone.datetime(2015, 1, 1)
class TestHiveToMySqlTransfer(TestHiveEnvironment):
def setUp(self):
self.kwargs = dict(
sql='sql',
mysql_table='table',
hiveserver2_conn_id='hiveserver2_default',
mysql_conn_id='mysql_default',
task_id='test_hive_to_mysql',
)
super().setUp()
@patch('airflow.providers.apache.hive.transfers.hive_to_mysql.MySqlHook')
@patch('airflow.providers.apache.hive.transfers.hive_to_mysql.HiveServer2Hook')
def test_execute(self, mock_hive_hook, mock_mysql_hook):
HiveToMySqlOperator(**self.kwargs).execute(context={})
mock_hive_hook.assert_called_once_with(hiveserver2_conn_id=self.kwargs['hiveserver2_conn_id'])
mock_hive_hook.return_value.get_records.assert_called_once_with('sql', hive_conf={})
mock_mysql_hook.assert_called_once_with(mysql_conn_id=self.kwargs['mysql_conn_id'])
mock_mysql_hook.return_value.insert_rows.assert_called_once_with(
table=self.kwargs['mysql_table'], rows=mock_hive_hook.return_value.get_records.return_value
)
@patch('airflow.providers.apache.hive.transfers.hive_to_mysql.MySqlHook')
@patch('airflow.providers.apache.hive.transfers.hive_to_mysql.HiveServer2Hook')
def test_execute_mysql_preoperator(self, mock_hive_hook, mock_mysql_hook):
self.kwargs.update(dict(mysql_preoperator='preoperator'))
HiveToMySqlOperator(**self.kwargs).execute(context={})
mock_mysql_hook.return_value.run.assert_called_once_with(self.kwargs['mysql_preoperator'])
@patch('airflow.providers.apache.hive.transfers.hive_to_mysql.MySqlHook')
@patch('airflow.providers.apache.hive.transfers.hive_to_mysql.HiveServer2Hook')
def test_execute_with_mysql_postoperator(self, mock_hive_hook, mock_mysql_hook):
self.kwargs.update(dict(mysql_postoperator='postoperator'))
HiveToMySqlOperator(**self.kwargs).execute(context={})
mock_mysql_hook.return_value.run.assert_called_once_with(self.kwargs['mysql_postoperator'])
@patch('airflow.providers.apache.hive.transfers.hive_to_mysql.MySqlHook')
@patch('airflow.providers.apache.hive.transfers.hive_to_mysql.NamedTemporaryFile')
@patch('airflow.providers.apache.hive.transfers.hive_to_mysql.HiveServer2Hook')
def test_execute_bulk_load(self, mock_hive_hook, mock_tmp_file_context, mock_mysql_hook):
mock_tmp_file = MagicMock()
mock_tmp_file.name = 'tmp_file'
mock_tmp_file_context.return_value.__enter__.return_value = mock_tmp_file
context = {}
self.kwargs.update(dict(bulk_load=True))
HiveToMySqlOperator(**self.kwargs).execute(context=context)
mock_tmp_file_context.assert_called_once_with()
mock_hive_hook.return_value.to_csv.assert_called_once_with(
self.kwargs['sql'],
'tmp_file',
delimiter='\t',
lineterminator='\n',
output_header=False,
hive_conf=context_to_airflow_vars(context),
)
mock_mysql_hook.return_value.bulk_load.assert_called_once_with(
table=self.kwargs['mysql_table'], tmp_file='tmp_file'
)
mock_tmp_file_context.return_value.__exit__.assert_called_once_with(None, None, None)
@patch('airflow.providers.apache.hive.transfers.hive_to_mysql.MySqlHook')
def test_execute_with_hive_conf(self, mock_mysql_hook):
context = {}
mock_hive_hook = MockHiveServer2Hook()
mock_hive_hook.get_records = MagicMock(return_value='test_hive_results')
self.kwargs.update(dict(hive_conf={'mapreduce.job.queuename': 'fake_queue'}))
with patch(
'airflow.providers.apache.hive.transfers.hive_to_mysql.HiveServer2Hook',
return_value=mock_hive_hook,
):
HiveToMySqlOperator(**self.kwargs).execute(context=context)
hive_conf = context_to_airflow_vars(context)
hive_conf.update(self.kwargs['hive_conf'])
mock_hive_hook.get_records.assert_called_once_with(self.kwargs['sql'], hive_conf=hive_conf)
@unittest.skipIf(
'AIRFLOW_RUNALL_TESTS' not in os.environ, "Skipped because AIRFLOW_RUNALL_TESTS is not set"
)
def test_hive_to_mysql(self):
test_hive_results = 'test_hive_results'
mock_hive_hook = MockHiveServer2Hook()
mock_hive_hook.get_records = MagicMock(return_value=test_hive_results)
mock_mysql_hook = MockMySqlHook()
mock_mysql_hook.run = MagicMock()
mock_mysql_hook.insert_rows = MagicMock()
with patch(
'airflow.providers.apache.hive.transfers.hive_to_mysql.HiveServer2Hook',
return_value=mock_hive_hook,
):
with patch(
'airflow.providers.apache.hive.transfers.hive_to_mysql.MySqlHook',
return_value=mock_mysql_hook,
):
op = HiveToMySqlOperator(
mysql_conn_id='airflow_db',
task_id='hive_to_mysql_check',
sql="""
SELECT name
FROM airflow.static_babynames
LIMIT 100
""",
mysql_table='test_static_babynames',
mysql_preoperator=[
'DROP TABLE IF EXISTS test_static_babynames;',
'CREATE TABLE test_static_babynames (name VARCHAR(500))',
],
dag=self.dag,
)
op.clear(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)
op.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True)
raw_select_name_query = mock_hive_hook.get_records.call_args_list[0][0][0]
actual_select_name_query = re.sub(r'\s{2,}', ' ', raw_select_name_query).strip()
expected_select_name_query = 'SELECT name FROM airflow.static_babynames LIMIT 100'
assert expected_select_name_query == actual_select_name_query
actual_hive_conf = mock_hive_hook.get_records.call_args_list[0][1]['hive_conf']
expected_hive_conf = {
'airflow.ctx.dag_owner': 'airflow',
'airflow.ctx.dag_id': 'test_dag_id',
'airflow.ctx.task_id': 'hive_to_mysql_check',
'airflow.ctx.execution_date': '2015-01-01T00:00:00+00:00',
}
assert expected_hive_conf == actual_hive_conf
expected_mysql_preoperator = [
'DROP TABLE IF EXISTS test_static_babynames;',
'CREATE TABLE test_static_babynames (name VARCHAR(500))',
]
mock_mysql_hook.run.assert_called_with(expected_mysql_preoperator)
mock_mysql_hook.insert_rows.assert_called_with(table='test_static_babynames', rows=test_hive_results)