1
1
from pathlib import Path
2
+ from importlib import reload
2
3
3
4
import pretend
4
5
import pytest
5
6
6
7
import main
7
8
8
-
9
- DATASET = "my-bigquery-dataset"
10
- SIMPLE_TABLE = "my-simple-table"
11
- DOWNLOAD_TABLE = "my-download-table"
9
+ BIGQUERY_DATASET = "my-bigquery-dataset"
10
+ BIGQUERY_SIMPLE_TABLE = "my-simple-table"
11
+ BIGQUERY_DOWNLOAD_TABLE = "my-download-table"
12
12
RESULT_BUCKET = "my-result-bucket"
13
13
14
14
15
+ @pytest .mark .parametrize (
16
+ "bigquery_dataset, expected_dataset_calls" ,
17
+ [
18
+ ("my-bigquery-dataset" , [pretend .call ("my-bigquery-dataset" )]),
19
+ (
20
+ "my-bigquery-dataset some-other-dataset" ,
21
+ [pretend .call ("my-bigquery-dataset" ), pretend .call ("some-other-dataset" )],
22
+ ),
23
+ ],
24
+ )
15
25
@pytest .mark .parametrize (
16
26
"log_filename, table_name, expected" ,
17
27
[
18
28
(
19
29
"downloads-2021-01-07-20-55-2021-01-07T20-55-00.000-B8Hs_G6d6xN61En2ypwk.log.gz" ,
20
- DOWNLOAD_TABLE ,
30
+ BIGQUERY_DOWNLOAD_TABLE ,
21
31
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 '
22
32
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 ' ,
23
33
),
24
34
(
25
35
"simple-2021-01-07-20-55-2021-01-07T20-55-00.000-3wuB00t9tqgbGLFI2fSI.log.gz" ,
26
- SIMPLE_TABLE ,
36
+ BIGQUERY_SIMPLE_TABLE ,
27
37
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 '
28
38
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 ' ,
29
39
),
30
40
],
31
41
)
32
- def test_function (monkeypatch , log_filename , table_name , expected ):
33
- monkeypatch .setattr (main , "DATASET" , DATASET )
34
- monkeypatch .setattr (main , "SIMPLE_TABLE" , SIMPLE_TABLE )
35
- monkeypatch .setattr (main , "DOWNLOAD_TABLE" , DOWNLOAD_TABLE )
36
- monkeypatch .setattr (main , "RESULT_BUCKET" , RESULT_BUCKET )
42
+ def test_function (
43
+ monkeypatch ,
44
+ log_filename ,
45
+ table_name ,
46
+ expected ,
47
+ bigquery_dataset ,
48
+ expected_dataset_calls ,
49
+ ):
50
+ monkeypatch .setenv ("BIGQUERY_DATASET" , bigquery_dataset )
51
+ monkeypatch .setenv ("BIGQUERY_SIMPLE_TABLE" , BIGQUERY_SIMPLE_TABLE )
52
+ monkeypatch .setenv ("BIGQUERY_DOWNLOAD_TABLE" , BIGQUERY_DOWNLOAD_TABLE )
53
+ monkeypatch .setenv ("RESULT_BUCKET" , RESULT_BUCKET )
54
+
55
+ reload (main )
37
56
38
57
def _download_to_file (file_handler ):
39
58
with open (Path ("." ) / "fixtures" / log_filename , "rb" ) as f :
@@ -85,12 +104,11 @@ def _load_table_from_file(fh, *a, **kw):
85
104
86
105
main .process_fastly_log (data , context )
87
106
88
- assert storage_client_stub .bucket .calls == [
89
- pretend .call ("my-bucket" ),
107
+ assert storage_client_stub .bucket .calls == [pretend .call ("my-bucket" )] + [
90
108
pretend .call (RESULT_BUCKET ),
91
- ]
109
+ ] * len ( expected_dataset_calls )
92
110
assert bucket_stub .get_blob .calls == [pretend .call (log_filename )]
93
- assert bigquery_client_stub .dataset .calls == [ pretend . call ( DATASET )]
111
+ assert bigquery_client_stub .dataset .calls == expected_dataset_calls
94
112
assert bigquery_client_stub .load_table_from_file .calls == [
95
113
pretend .call (
96
114
bigquery_client_stub .load_table_from_file .calls [0 ].args [0 ], # shh
@@ -100,8 +118,10 @@ def _load_table_from_file(fh, *a, **kw):
100
118
job_config = job_config_stub ,
101
119
rewind = True ,
102
120
)
103
- ]
104
- assert dataset_stub .table .calls == [pretend .call (table_name )]
121
+ ] * len (expected_dataset_calls )
122
+ assert dataset_stub .table .calls == [pretend .call (table_name )] * len (
123
+ expected_dataset_calls
124
+ )
105
125
assert blob_stub .delete .calls == [pretend .call ()]
106
- assert load_job_stub .result .calls == [pretend .call ()]
126
+ assert load_job_stub .result .calls == [pretend .call ()] * len ( expected_dataset_calls )
107
127
assert load_job_stub ._result == expected
0 commit comments