1
+ import contextlib
1
2
import datetime
2
3
from pathlib import Path
3
4
from importlib import reload
@@ -114,28 +115,26 @@ def upload_from_file(self, file_handler, rewind=False):
114
115
],
115
116
)
116
117
@pytest .mark .parametrize (
117
- "log_filename, table_name, expected " ,
118
+ "blobs, expected_load_jobs, expected_delete_calls " ,
118
119
[
120
+ ({"simple" : [], "downloads" : ["blob0" , "blob1" , "blob2" ]}, 1 , 3 ),
121
+ ({"simple" : ["blob0" , "blob1" , "blob2" ], "downloads" : []}, 1 , 3 ),
119
122
(
120
- "downloads-2021-01-07-20-55-2021-01-07T20-55-00.000-B8Hs_G6d6xN61En2ypwk.log.gz" ,
121
- BIGQUERY_DOWNLOAD_TABLE ,
122
- b'{"timestamp": "2021-01-07 20:54:54 +00:00", "url": "/packages/f7/12/ec3f2e203afa394a149911729357aa48affc59c20e2c1c8297a60f33f133/threadpoolctl-2.1.0-py3-none-any.whl", "project": "threadpoolctl", "file": {"filename": "threadpoolctl-2.1.0-py3-none-any.whl", "project": "threadpoolctl", "version": "2.1.0", "type": "bdist_wheel"}, "tls_protocol": "TLSv1.2", "tls_cipher": "ECDHE-RSA-AES128-GCM-SHA256", "country_code": "US", "details": {"installer": {"name": "pip", "version": "20.1.1"}, "python": "3.7.9", "implementation": {"name": "CPython", "version": "3.7.9"}, "distro": {"name": "Debian GNU/Linux", "version": "9", "id": "stretch", "libc": {"lib": "glibc", "version": "2.24"}}, "system": {"name": "Linux", "release": "4.15.0-112-generic"}, "cpu": "x86_64", "openssl_version": "OpenSSL 1.1.0l 10 Sep 2019", "setuptools_version": "47.1.0", "ci": null}}\n '
123
- b'{"timestamp": "2021-01-07 20:54:54 +00:00", "url": "/packages/cd/f9/8fad70a3bd011a6be7c5c6067278f006a25341eb39d901fbda307e26804c/django_crum-0.7.9-py2.py3-none-any.whl", "project": "django-crum", "file": {"filename": "django_crum-0.7.9-py2.py3-none-any.whl", "project": "django-crum", "version": "0.7.9", "type": "bdist_wheel"}, "tls_protocol": "TLSv1.2", "tls_cipher": "ECDHE-RSA-AES128-GCM-SHA256", "country_code": "US", "details": {"installer": {"name": "pip", "version": "20.0.2"}, "python": "3.8.5", "implementation": {"name": "CPython", "version": "3.8.5"}, "distro": {"name": "Ubuntu", "version": "16.04", "id": "xenial", "libc": {"lib": "glibc", "version": "2.23"}}, "system": {"name": "Linux", "release": "4.4.0-1113-aws"}, "cpu": "x86_64", "openssl_version": "OpenSSL 1.0.2g 1 Mar 2016", "setuptools_version": "44.1.0", "ci": null}}\n ' ,
124
- ),
125
- (
126
- "simple-2021-01-07-20-55-2021-01-07T20-55-00.000-3wuB00t9tqgbGLFI2fSI.log.gz" ,
127
- BIGQUERY_SIMPLE_TABLE ,
128
- b'{"timestamp": "2021-01-07 20:54:52 +00:00", "url": "/simple/azureml-model-management-sdk/", "project": "azureml-model-management-sdk", "tls_protocol": "TLSv1.3", "tls_cipher": "AES256-GCM", "country_code": "US", "details": {"installer": {"name": "pip", "version": "20.0.2"}, "python": "3.7.5", "implementation": {"name": "CPython", "version": "3.7.5"}, "distro": {"name": "Ubuntu", "version": "18.04", "id": "bionic", "libc": {"lib": "glibc", "version": "2.27"}}, "system": {"name": "Linux", "release": "4.15.0-1092-azure"}, "cpu": "x86_64", "openssl_version": "OpenSSL 1.1.1 11 Sep 2018", "setuptools_version": "45.2.0", "ci": null}}\n '
129
- b'{"timestamp": "2021-01-07 20:54:52 +00:00", "url": "/simple/pyrsistent/", "project": "pyrsistent", "tls_protocol": "TLSv1.3", "tls_cipher": "AES256-GCM", "country_code": "US", "details": {"installer": {"name": "pip", "version": "20.0.2"}, "python": "3.8.5", "implementation": {"name": "CPython", "version": "3.8.5"}, "distro": {"name": "Ubuntu", "version": "20.04", "id": "focal", "libc": {"lib": "glibc", "version": "2.31"}}, "system": {"name": "Linux", "release": "5.4.72-flatcar"}, "cpu": "x86_64", "openssl_version": "OpenSSL 1.1.1f 31 Mar 2020", "setuptools_version": "45.2.0", "ci": true}}\n ' ,
123
+ {
124
+ "simple" : ["blob0" , "blob1" , "blob2" ],
125
+ "downloads" : ["blob0" , "blob1" , "blob2" ],
126
+ },
127
+ 2 ,
128
+ 6 ,
130
129
),
131
130
],
132
131
)
133
132
def test_load_processed_files_into_bigquery (
134
133
monkeypatch ,
135
- log_filename ,
136
- table_name ,
137
- expected ,
138
134
bigquery_dataset ,
135
+ blobs ,
136
+ expected_load_jobs ,
137
+ expected_delete_calls ,
139
138
):
140
139
monkeypatch .setenv ("GCP_PROJECT" , GCP_PROJECT )
141
140
monkeypatch .setenv ("BIGQUERY_DATASET" , bigquery_dataset )
@@ -145,23 +144,33 @@ def test_load_processed_files_into_bigquery(
145
144
146
145
reload (main )
147
146
148
- blob_lists = {}
149
-
150
147
bucket = pretend .stub (name = RESULT_BUCKET )
151
148
152
- blob_stub = pretend .stub (name = "blobname" , bucket = bucket )
149
+ blob_stub = pretend .stub (
150
+ name = "blobname" , bucket = bucket , delete = pretend .call_recorder (lambda : None )
151
+ )
153
152
154
153
def _generate_blob_list (prefix , max_results ):
155
- blob_list = [blob_stub ]
156
- blob_lists [prefix ] = blob_list
154
+ if "simple" in prefix :
155
+ _blobs = blobs ["simple" ]
156
+ elif "downloads" in prefix :
157
+ _blobs = blobs ["downloads" ]
158
+ else :
159
+ _blobs = []
160
+ blob_list = [blob_stub for b in _blobs ]
157
161
return blob_list
158
162
159
163
bucket_stub = pretend .stub (
160
164
list_blobs = pretend .call_recorder (_generate_blob_list ),
161
- delete_blobs = pretend .call_recorder (lambda * a , ** kw : None ),
162
165
)
166
+
167
+ @contextlib .contextmanager
168
+ def fake_batch (* a , ** kw ):
169
+ yield True
170
+
163
171
storage_client_stub = pretend .stub (
164
172
bucket = pretend .call_recorder (lambda a : bucket_stub ),
173
+ batch = fake_batch ,
165
174
)
166
175
monkeypatch .setattr (
167
176
main , "storage" , pretend .stub (Client = lambda : storage_client_stub )
@@ -208,9 +217,6 @@ def _generate_blob_list(prefix, max_results):
208
217
]
209
218
assert (
210
219
load_job_stub .result .calls
211
- == [pretend .call ()] * len (bigquery_dataset .split ()) * 2
212
- )
213
- assert (
214
- bucket_stub .delete_blobs .calls
215
- == [pretend .call (blobs = [f"gs://{ RESULT_BUCKET } /{ blob_stub .name } " ])] * 2
220
+ == [pretend .call ()] * len (bigquery_dataset .split ()) * expected_load_jobs
216
221
)
222
+ assert blob_stub .delete .calls == [pretend .call ()] * expected_delete_calls
0 commit comments