forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset_test.py
More file actions
112 lines (88 loc) · 3.03 KB
/
dataset_test.py
File metadata and controls
112 lines (88 loc) · 3.03 KB
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
#!/usr/bin/env python
# Copyright 2019 Google LLC
#
# Licensed 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 random
import string
import time
from google.api_core import exceptions
import pytest
import automl_tables_dataset
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
REGION = "us-central1"
STATIC_DATASET = "do_not_delete_this_table_python"
GCS_DATASET = ("gs://python-docs-samples-tests-automl-tables-test"
"/bank-marketing.csv")
ID = "{rand}_{time}".format(
rand="".join(
[random.choice(string.ascii_letters + string.digits) for n in range(4)]
),
time=int(time.time()),
)
def _id(name):
return "{}_{}".format(name, ID)
def ensure_dataset_ready():
dataset = None
name = STATIC_DATASET
try:
dataset = automl_tables_dataset.get_dataset(PROJECT, REGION, name)
except exceptions.NotFound:
dataset = automl_tables_dataset.create_dataset(PROJECT, REGION, name)
if dataset.example_count is None or dataset.example_count == 0:
automl_tables_dataset.import_data(PROJECT, REGION, name, GCS_DATASET)
dataset = automl_tables_dataset.get_dataset(PROJECT, REGION, name)
automl_tables_dataset.update_dataset(
PROJECT,
REGION,
dataset.display_name,
target_column_spec_name="Deposit",
)
return dataset
@pytest.mark.slow
def test_dataset_create_import_delete(capsys):
name = _id("d_cr_dl")
dataset = automl_tables_dataset.create_dataset(PROJECT, REGION, name)
assert dataset is not None
assert dataset.display_name == name
automl_tables_dataset.import_data(PROJECT, REGION, name, GCS_DATASET)
out, _ = capsys.readouterr()
assert "Data imported." in out
automl_tables_dataset.delete_dataset(PROJECT, REGION, name)
with pytest.raises(exceptions.NotFound):
automl_tables_dataset.get_dataset(PROJECT, REGION, name)
def test_dataset_update(capsys):
dataset = ensure_dataset_ready()
automl_tables_dataset.update_dataset(
PROJECT,
REGION,
dataset.display_name,
target_column_spec_name="Deposit",
weight_column_spec_name="Balance",
)
out, _ = capsys.readouterr()
assert "Target column updated." in out
assert "Weight column updated." in out
def test_list_datasets():
ensure_dataset_ready()
assert (
next(
(
d
for d in automl_tables_dataset.list_datasets(PROJECT, REGION)
if d.display_name == STATIC_DATASET
),
None,
)
is not None
)