| 1 |
# -*- coding: utf-8 -*- |
| 2 |
from __future__ import absolute_import, print_function, division |
| 3 |
|
| 4 |
import datetime |
| 5 |
import os |
| 6 |
import json |
| 7 |
import time |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from petl.compat import text_type |
| 12 |
from petl.io.gsheet import fromgsheet, togsheet, appendgsheet |
| 13 |
from petl.test.helpers import ieq, get_env_vars_named |
| 14 |
|
| 15 |
gspread = pytest.importorskip("gspread") |
| 16 |
uuid = pytest.importorskip("uuid") |
| 17 |
|
| 18 |
# region helpers |
| 19 |
|
| 20 |
|
| 21 |
def _get_gspread_credentials(): |
| 22 |
json_path = os.getenv("PETL_GCP_JSON_PATH", None) |
| 23 |
if json_path is not None and os.path.exists(json_path): |
| 24 |
return json_path |
| 25 |
json_props = get_env_vars_named("PETL_GCP_CREDS_") |
| 26 |
if json_props is not None: |
| 27 |
return json_props |
| 28 |
user_path = os.path.expanduser("~/.config/gspread/service_account.json") |
| 29 |
if os.path.isfile(user_path) and os.path.exists(user_path): |
| 30 |
return user_path |
| 31 |
return None |
| 32 |
|
| 33 |
|
| 34 |
found_gcp_credentials = pytest.mark.skipif( |
| 35 |
_get_gspread_credentials() is None, |
| 36 |
reason="""SKIPPED. to/from gspread needs json credentials for testing. |
| 37 |
In order to run google spreadsheet tests, follow the steps bellow: |
| 38 |
1. Create a json authorization file, following the steps described at |
| 39 |
http://gspread.readthedocs.io/en/latest/oauth2.html, and save to a local path |
| 40 |
2. Point the envvar `PETL_GCP_JSON_PATH` to the json authorization file path |
| 41 |
2. Or fill the properties inside the json authorization file in envrionment |
| 42 |
variables named with prefix PETL_GCP_CREDS_: PETL_GCP_CREDS_project_id=petl |
| 43 |
3. Or else save the file in one of the following paths: |
| 44 |
unix: ~/.config/gspread/service_account.json |
| 45 |
windows: %APPDATA%\\gspread\\service_account.json""" |
| 46 |
) |
| 47 |
|
| 48 |
|
| 49 |
def _get_env_credentials(): |
| 50 |
creds = _get_gspread_credentials() |
| 51 |
if isinstance(creds, dict): |
| 52 |
return creds |
| 53 |
if isinstance(creds, text_type): |
| 54 |
with open(creds, encoding="utf-8") as json_file: |
| 55 |
creds = json.load(json_file) |
| 56 |
return creds |
| 57 |
return None |
| 58 |
|
| 59 |
|
| 60 |
def _get_gspread_client(): |
| 61 |
credentials = _get_env_credentials() |
| 62 |
try: |
| 63 |
if credentials is None: |
| 64 |
gspread_client = gspread.service_account() |
| 65 |
else: |
| 66 |
gspread_client = gspread.service_account_from_dict(credentials) |
| 67 |
except gspread.exceptions.APIError as ex: |
| 68 |
pytest.skip("SKIPPED. to/from gspread authentication error: %s" % ex) |
| 69 |
return None |
| 70 |
return gspread_client |
| 71 |
|
| 72 |
|
| 73 |
def _get_env_sharing_emails(): |
| 74 |
emails = get_env_vars_named("PETL_GSHEET_EMAIL", remove_prefix=False) |
| 75 |
if emails is not None: |
| 76 |
return list(emails.values()) |
| 77 |
return [] |
| 78 |
|
| 79 |
|
| 80 |
def _get_gspread_test_params(): |
| 81 |
filename = "test-{}".format(str(uuid.uuid4())) |
| 82 |
gspread_client = _get_gspread_client() |
| 83 |
emails = _get_env_sharing_emails() |
| 84 |
return filename, gspread_client, emails |
| 85 |
|
| 86 |
|
| 87 |
def _test_to_fromg_sheet(table, sheetname, cell_range, expected): |
| 88 |
filename, gspread_client, emails = _get_gspread_test_params() |
| 89 |
# test to from gsheet |
| 90 |
spread_id = togsheet( |
| 91 |
table, gspread_client, filename, worksheet=sheetname, share_emails=emails |
| 92 |
) |
| 93 |
try: |
| 94 |
result = fromgsheet( |
| 95 |
gspread_client, filename, worksheet=sheetname, cell_range=cell_range |
| 96 |
) |
| 97 |
# make sure the expected_result matches the result |
| 98 |
ieq(expected, result) |
| 99 |
finally: |
| 100 |
# clean up created table |
| 101 |
gspread_client.del_spreadsheet(spread_id) |
| 102 |
|
| 103 |
|
| 104 |
def _test_append_from_gsheet(table_list, expected, sheetname=None): |
| 105 |
filename, gspread_client, emails = _get_gspread_test_params() |
| 106 |
# append from the second table from the list |
| 107 |
table1 = table_list[0] |
| 108 |
other_tables = table_list[1:] |
| 109 |
# create the spreadshteet and the 1st sheet |
| 110 |
spread_id = togsheet( |
| 111 |
table1, gspread_client, filename, worksheet=sheetname, share_emails=emails |
| 112 |
) |
| 113 |
try: |
| 114 |
for tableN in other_tables: |
| 115 |
appendgsheet( |
| 116 |
tableN, gspread_client, spread_id, worksheet=sheetname, |
| 117 |
open_by_key=True |
| 118 |
) |
| 119 |
# read the result appended to the sheet |
| 120 |
result = fromgsheet( |
| 121 |
gspread_client, spread_id, worksheet=sheetname, open_by_key=True |
| 122 |
) |
| 123 |
# make sure the expected_result matches the result |
| 124 |
ieq(expected, result) |
| 125 |
finally: |
| 126 |
# clean up created table |
| 127 |
gspread_client.del_spreadsheet(spread_id) |
| 128 |
|
| 129 |
|
| 130 |
def teardown_function(): |
| 131 |
# try to avoid: User rate limit exceeded. |
| 132 |
time.sleep(3) |
| 133 |
|
| 134 |
|
| 135 |
# endregion |
| 136 |
|
| 137 |
# region test cases data |
| 138 |
|
| 139 |
TEST_TABLE = [ |
| 140 |
["foo", "bar"], |
| 141 |
["A", "1"], |
| 142 |
["B", "2"], |
| 143 |
["C", "3"], |
| 144 |
["D", "random_stuff-in+_名字"], |
| 145 |
["é", "3/4/2012"], |
| 146 |
["F", "6"], |
| 147 |
] |
| 148 |
|
| 149 |
# endregion |
| 150 |
|
| 151 |
# region test cases execution |
| 152 |
|
| 153 |
|
| 154 |
@found_gcp_credentials |
| 155 |
def test_tofromgsheet_01_basic(): |
| 156 |
_test_to_fromg_sheet( TEST_TABLE[:], None, None, TEST_TABLE[:] ) |
| 157 |
|
| 158 |
|
| 159 |
@found_gcp_credentials |
| 160 |
def test_tofromgsheet_02_uneven_row(): |
| 161 |
test_table_t1 = [x + ["3"] if i in [2] else x for i, x in enumerate(TEST_TABLE[:])] |
| 162 |
test_table_f1 = [x + [""] if len(x) < 3 else x for x in test_table_t1[:]] |
| 163 |
_test_to_fromg_sheet( test_table_t1, None, None, test_table_f1 ) |
| 164 |
|
| 165 |
|
| 166 |
@found_gcp_credentials |
| 167 |
def test_tofromgsheet_03_empty_table(): |
| 168 |
_test_to_fromg_sheet( (), None, None, () ) |
| 169 |
|
| 170 |
|
| 171 |
@found_gcp_credentials |
| 172 |
def test_tofromgsheet_04_cell_range(): |
| 173 |
test_table_f2 = [[x[1]] for x in TEST_TABLE[0:4]] |
| 174 |
_test_to_fromg_sheet( TEST_TABLE[:], None, "B1:B4", test_table_f2 ) |
| 175 |
|
| 176 |
|
| 177 |
@found_gcp_credentials |
| 178 |
def test_tofromgsheet_05_sheet_title(): |
| 179 |
_test_to_fromg_sheet( TEST_TABLE[:], "random_stuff-in+_名字", None, TEST_TABLE[:] ) |
| 180 |
|
| 181 |
|
| 182 |
@found_gcp_credentials |
| 183 |
@pytest.mark.xfail( |
| 184 |
raises=TypeError, |
| 185 |
reason="When this stop failing, uncomment datetime.date in TEST1 and TEST2" |
| 186 |
) |
| 187 |
def test_tofromgsheet_06_datetime_date(): |
| 188 |
test_table_dt = [[x[0], datetime.date(2012, 5, 6)] if i in [5] else x for i, x in enumerate(TEST_TABLE[:])] |
| 189 |
_test_to_fromg_sheet( test_table_dt[:], None, "B1:B4", test_table_dt[:] ) |
| 190 |
|
| 191 |
|
| 192 |
@found_gcp_credentials |
| 193 |
def test_tofromgsheet_07_open_by_key(): |
| 194 |
filename, gspread_client, emails = _get_gspread_test_params() |
| 195 |
# test to from gsheet |
| 196 |
table = TEST_TABLE[:] |
| 197 |
# test to from gsheet |
| 198 |
spread_id = togsheet(table, gspread_client, filename, share_emails=emails) |
| 199 |
try: |
| 200 |
result = fromgsheet(gspread_client, spread_id, open_by_key=True) |
| 201 |
# make sure the expected_result matches the result |
| 202 |
ieq(table, result) |
| 203 |
finally: |
| 204 |
# clean up created table |
| 205 |
gspread_client.del_spreadsheet(spread_id) |
| 206 |
|
| 207 |
|
| 208 |
@found_gcp_credentials |
| 209 |
def test_tofromgsheet_08_recreate(): |
| 210 |
filename, gspread_client, emails = _get_gspread_test_params() |
| 211 |
# test to from gsheet |
| 212 |
table1 = TEST_TABLE[:] |
| 213 |
table2 = [[ x[0] , text_type(i)] if i > 0 else x for i, x in enumerate(table1)] |
| 214 |
# test to from gsheet |
| 215 |
spread_id = togsheet(table1, gspread_client, filename, share_emails=emails) |
| 216 |
try: |
| 217 |
result1 = fromgsheet(gspread_client, spread_id, open_by_key=True) |
| 218 |
ieq(table1, result1) |
| 219 |
spread_id2 = togsheet(table2, gspread_client, filename, share_emails=emails) |
| 220 |
try: |
| 221 |
result2 = fromgsheet(gspread_client, spread_id2, open_by_key=True) |
| 222 |
ieq(table2, result2) |
| 223 |
finally: |
| 224 |
gspread_client.del_spreadsheet(spread_id2) |
| 225 |
# make sure the expected_result matches the result |
| 226 |
finally: |
| 227 |
# clean up created table |
| 228 |
gspread_client.del_spreadsheet(spread_id) |
| 229 |
|
| 230 |
|
| 231 |
def _get_testcase_for_append(): |
| 232 |
table_list = [TEST_TABLE[:], TEST_TABLE[:]] |
| 233 |
expected = TEST_TABLE[:] + TEST_TABLE[1:] |
| 234 |
return table_list, expected |
| 235 |
|
| 236 |
|
| 237 |
@found_gcp_credentials |
| 238 |
def test_appendgsheet_10_double(): |
| 239 |
table_list, expected = _get_testcase_for_append() |
| 240 |
_test_append_from_gsheet(table_list, expected) |
| 241 |
|
| 242 |
|
| 243 |
@found_gcp_credentials |
| 244 |
def test_appendgsheet_11_named_sheet(): |
| 245 |
table_list, expected = _get_testcase_for_append() |
| 246 |
_test_append_from_gsheet(table_list, expected, sheetname="petl_append") |
| 247 |
|
| 248 |
|
| 249 |
@found_gcp_credentials |
| 250 |
def test_appendgsheet_12_other_sheet(): |
| 251 |
filename, gspread_client, emails = _get_gspread_test_params() |
| 252 |
# test to append gsheet |
| 253 |
table = TEST_TABLE[:] |
| 254 |
table2 = TEST_TABLE[1:] |
| 255 |
spread_id = togsheet(table, gspread_client, filename, share_emails=emails) |
| 256 |
try: |
| 257 |
appendgsheet(table, gspread_client, filename, worksheet="petl") |
| 258 |
# get the results from the 2 sheets |
| 259 |
result1 = fromgsheet(gspread_client, filename, worksheet=None) |
| 260 |
ieq(result1, table) |
| 261 |
result2 = fromgsheet(gspread_client, filename, worksheet="petl") |
| 262 |
ieq(result2, table2) |
| 263 |
finally: |
| 264 |
gspread_client.del_spreadsheet(spread_id) |
| 265 |
|
| 266 |
|
| 267 |
# endregion |