-
Notifications
You must be signed in to change notification settings - Fork 14.5k
/
test_local_to_gcs.py
150 lines (136 loc) · 5.61 KB
/
test_local_to_gcs.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
#
# 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
import datetime
import os
from glob import glob
from unittest import mock
import pytest
from airflow.models.dag import DAG
from airflow.providers.google.cloud.transfers.local_to_gcs import LocalFilesystemToGCSOperator
class TestFileToGcsOperator:
_config = {"bucket": "dummy", "mime_type": "application/octet-stream", "gzip": False}
def setup_method(self):
args = {"owner": "airflow", "start_date": datetime.datetime(2017, 1, 1)}
self.dag = DAG("test_dag_id", default_args=args)
self.testfile1 = "/tmp/fake1.csv"
with open(self.testfile1, "wb") as f:
f.write(b"x" * 393216)
self.testfile2 = "/tmp/fake2.csv"
with open(self.testfile2, "wb") as f:
f.write(b"x" * 393216)
self.testfiles = [self.testfile1, self.testfile2]
def teardown_method(self):
os.remove(self.testfile1)
os.remove(self.testfile2)
def test_init(self):
operator = LocalFilesystemToGCSOperator(
task_id="file_to_gcs_operator",
dag=self.dag,
src=self.testfile1,
dst="test/test1.csv",
**self._config,
)
assert operator.src == self.testfile1
assert operator.dst == "test/test1.csv"
assert operator.bucket == self._config["bucket"]
assert operator.mime_type == self._config["mime_type"]
assert operator.gzip == self._config["gzip"]
@mock.patch("airflow.providers.google.cloud.transfers.local_to_gcs.GCSHook", autospec=True)
def test_execute(self, mock_hook):
mock_instance = mock_hook.return_value
operator = LocalFilesystemToGCSOperator(
task_id="gcs_to_file_sensor",
dag=self.dag,
src=self.testfile1,
dst="test/test1.csv",
**self._config,
)
operator.execute(None)
mock_instance.upload.assert_called_once_with(
bucket_name=self._config["bucket"],
filename=self.testfile1,
gzip=self._config["gzip"],
mime_type=self._config["mime_type"],
object_name="test/test1.csv",
)
def test_execute_with_empty_src(self):
operator = LocalFilesystemToGCSOperator(
task_id="local_to_sensor",
dag=self.dag,
src="no_file.txt",
dst="test/no_file.txt",
**self._config,
)
with pytest.raises(FileNotFoundError):
operator.execute(None)
@mock.patch("airflow.providers.google.cloud.transfers.local_to_gcs.GCSHook", autospec=True)
def test_execute_multiple(self, mock_hook):
mock_instance = mock_hook.return_value
operator = LocalFilesystemToGCSOperator(
task_id="gcs_to_file_sensor", dag=self.dag, src=self.testfiles, dst="test/", **self._config
)
operator.execute(None)
files_objects = zip(
self.testfiles, ["test/" + os.path.basename(testfile) for testfile in self.testfiles]
)
calls = [
mock.call(
bucket_name=self._config["bucket"],
filename=filepath,
gzip=self._config["gzip"],
mime_type=self._config["mime_type"],
object_name=object_name,
)
for filepath, object_name in files_objects
]
mock_instance.upload.assert_has_calls(calls)
@mock.patch("airflow.providers.google.cloud.transfers.local_to_gcs.GCSHook", autospec=True)
def test_execute_wildcard(self, mock_hook):
mock_instance = mock_hook.return_value
operator = LocalFilesystemToGCSOperator(
task_id="gcs_to_file_sensor", dag=self.dag, src="/tmp/fake*.csv", dst="test/", **self._config
)
operator.execute(None)
object_names = ["test/" + os.path.basename(fp) for fp in glob("/tmp/fake*.csv")]
files_objects = zip(glob("/tmp/fake*.csv"), object_names)
calls = [
mock.call(
bucket_name=self._config["bucket"],
filename=filepath,
gzip=self._config["gzip"],
mime_type=self._config["mime_type"],
object_name=object_name,
)
for filepath, object_name in files_objects
]
mock_instance.upload.assert_has_calls(calls)
@mock.patch("airflow.providers.google.cloud.transfers.local_to_gcs.GCSHook", autospec=True)
def test_execute_negative(self, mock_hook):
mock_instance = mock_hook.return_value
operator = LocalFilesystemToGCSOperator(
task_id="gcs_to_file_sensor",
dag=self.dag,
src="/tmp/fake*.csv",
dst="test/test1.csv",
**self._config,
)
print(glob("/tmp/fake*.csv"))
with pytest.raises(ValueError):
operator.execute(None)
mock_instance.assert_not_called()