def to_dataset(project, model):
     access_entries = model.access_entries
     if access_entries:
         access_entries = tuple(
             BigQueryAccessEntry.to_access_entry(a) for a in access_entries)
     else:
         access_entries = ()
     dataset_ref = DatasetReference(project, model.dataset_id)
     dataset = Dataset(dataset_ref)
     dataset.friendly_name = model.friendly_name
     dataset.description = model.description
     dataset.default_table_expiration_ms = model.default_table_expiration_ms
     dataset.location = model.location
     dataset.access_entries = access_entries
     dataset.labels = model.labels if model.labels is not None else dict()
     return dataset
Ejemplo n.º 2
0
def test_create_dataset_w_attrs(client, PROJECT, DS_ID):
    from google.cloud.bigquery.dataset import AccessEntry

    PATH = "projects/%s/datasets" % PROJECT
    DESCRIPTION = "DESC"
    FRIENDLY_NAME = "FN"
    LOCATION = "US"
    USER_EMAIL = "*****@*****.**"
    LABELS = {"color": "red"}
    VIEW = {
        "projectId": "my-proj",
        "datasetId": "starry-skies",
        "tableId": "northern-hemisphere",
    }
    RESOURCE = {
        "datasetReference": {
            "projectId": PROJECT,
            "datasetId": DS_ID
        },
        "etag": "etag",
        "id": "%s:%s" % (PROJECT, DS_ID),
        "description": DESCRIPTION,
        "friendlyName": FRIENDLY_NAME,
        "location": LOCATION,
        "defaultTableExpirationMs": "3600",
        "labels": LABELS,
        "access": [{
            "role": "OWNER",
            "userByEmail": USER_EMAIL
        }, {
            "view": VIEW
        }],
    }
    conn = client._connection = make_connection(RESOURCE)
    entries = [
        AccessEntry("OWNER", "userByEmail", USER_EMAIL),
        AccessEntry(None, "view", VIEW),
    ]

    ds_ref = DatasetReference(PROJECT, DS_ID)
    before = Dataset(ds_ref)
    before.access_entries = entries
    before.description = DESCRIPTION
    before.friendly_name = FRIENDLY_NAME
    before.default_table_expiration_ms = 3600
    before.location = LOCATION
    before.labels = LABELS
    after = client.create_dataset(before)

    assert after.dataset_id == DS_ID
    assert after.project == PROJECT
    assert after.etag == RESOURCE["etag"]
    assert after.full_dataset_id == RESOURCE["id"]
    assert after.description == DESCRIPTION
    assert after.friendly_name == FRIENDLY_NAME
    assert after.location == LOCATION
    assert after.default_table_expiration_ms == 3600
    assert after.labels == LABELS

    conn.api_request.assert_called_once_with(
        method="POST",
        path="/%s" % PATH,
        data={
            "datasetReference": {
                "projectId": PROJECT,
                "datasetId": DS_ID
            },
            "description":
            DESCRIPTION,
            "friendlyName":
            FRIENDLY_NAME,
            "location":
            LOCATION,
            "defaultTableExpirationMs":
            "3600",
            "access": [{
                "role": "OWNER",
                "userByEmail": USER_EMAIL
            }, {
                "view": VIEW
            }],
            "labels":
            LABELS,
        },
        timeout=DEFAULT_TIMEOUT,
    )