/
authorized_view_tutorial_test.py
77 lines (65 loc) · 2.43 KB
/
authorized_view_tutorial_test.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
# Copyright 2018 Google Inc.
#
# 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 uuid
from google.cloud import bigquery
import pytest
import authorized_view_tutorial
@pytest.fixture(scope="module")
def client():
return bigquery.Client()
@pytest.fixture
def datasets_to_delete(client):
doomed = []
yield doomed
for item in doomed:
client.delete_dataset(item, delete_contents=True, not_found_ok=True)
def test_authorized_view_tutorial(client, datasets_to_delete):
override_values = {
"source_dataset_id": "github_source_data_{}".format(
str(uuid.uuid4()).replace("-", "_")
),
"shared_dataset_id": "shared_views_{}".format(
str(uuid.uuid4()).replace("-", "_")
),
}
source_dataset_ref = "{}.{}".format(
client.project, override_values["source_dataset_id"]
)
shared_dataset_ref = "{}.{}".format(
client.project, override_values["shared_dataset_id"]
)
datasets_to_delete.extend(
[override_values["source_dataset_id"], override_values["shared_dataset_id"]]
)
authorized_view_tutorial.run_authorized_view_tutorial(override_values)
source_dataset = client.get_dataset(source_dataset_ref)
shared_dataset = client.get_dataset(shared_dataset_ref)
analyst_email = "example-analyst-group@google.com"
analyst_entries = [
entry
for entry in shared_dataset.access_entries
if entry.entity_id == analyst_email
]
assert len(analyst_entries) == 1
assert analyst_entries[0].role == "READER"
authorized_view_entries = [
entry for entry in source_dataset.access_entries if entry.entity_type == "view"
]
expected_view_ref = {
"projectId": client.project,
"datasetId": override_values["shared_dataset_id"],
"tableId": "github_analyst_view",
}
assert len(authorized_view_entries) == 1
assert authorized_view_entries[0].entity_id == expected_view_ref