Example #1
0
def test_ctor_string(target_class):
    from google.cloud.bigquery import ModelReference

    model_id = "my-proj.my_dset.my_model"
    ref = ModelReference.from_string(model_id)
    got = target_class(model_id)
    assert got.reference == ref
Example #2
0
def test_from_api_repr_w_minimal_resource(target_class):
    from google.cloud.bigquery import ModelReference

    resource = {
        "modelReference": {
            "projectId": "my-project",
            "datasetId": "my_dataset",
            "modelId": "my_model",
        }
    }
    got = target_class.from_api_repr(resource)
    assert got.reference == ModelReference.from_string("my-project.my_dataset.my_model")
    assert got.location == ""
    assert got.etag == ""
    assert got.created is None
    assert got.modified is None
    assert got.expires is None
    assert got.description is None
    assert got.friendly_name is None
    assert got.model_type == types.Model.ModelType.MODEL_TYPE_UNSPECIFIED
    assert got.labels == {}
    assert got.encryption_configuration is None
    assert len(got.training_runs) == 0
    assert len(got.feature_columns) == 0
    assert len(got.label_columns) == 0
Example #3
0
def test_ctor_string(target_class):
    from google.cloud.bigquery import ModelReference

    model_id = "my-proj.my_dset.my_model"
    ref = ModelReference.from_string(model_id)
    got = target_class(model_id)
    assert got.reference == ref
Example #4
0
def test_from_api_repr_w_unknown_fields(target_class):
    from google.cloud.bigquery import ModelReference

    resource = {
        "modelReference": {
            "projectId": "my-project",
            "datasetId": "my_dataset",
            "modelId": "my_model",
        },
        "thisFieldIsNotInTheProto": "just ignore me",
    }
    got = target_class.from_api_repr(resource)
    assert got.reference == ModelReference.from_string("my-project.my_dataset.my_model")
    assert got._properties is resource
def test_from_api_repr_w_unknown_fields(target_class):
    from google.cloud.bigquery import ModelReference

    resource = {
        "modelReference": {
            "projectId": "my-project",
            "datasetId": "my_dataset",
            "modelId": "my_model",
        },
        "thisFieldIsNotInTheProto": "just ignore me",
    }
    got = target_class.from_api_repr(resource)
    assert got.reference == ModelReference.from_string("my-project.my_dataset.my_model")
    assert got._properties is resource
Example #6
0
def test_from_api_repr_w_unknown_type(target_class):
    from google.cloud.bigquery import ModelReference

    resource = {
        "modelReference": {
            "projectId": "my-project",
            "datasetId": "my_dataset",
            "modelId": "my_model",
        },
        "modelType": "BE_A_GOOD_ROLE_MODEL",
    }
    got = target_class.from_api_repr(resource)
    assert got.reference == ModelReference.from_string(
        "my-project.my_dataset.my_model")
    assert got.model_type == 0
    assert got._properties is resource
Example #7
0
def test_from_api_repr_w_minimal_resource(target_class):
    from google.cloud.bigquery import ModelReference

    resource = {
        "modelReference": {
            "projectId": "my-project",
            "datasetId": "my_dataset",
            "modelId": "my_model",
        }
    }
    got = target_class.from_api_repr(resource)
    assert got.reference == ModelReference.from_string("my-project.my_dataset.my_model")
    assert got.location == ""
    assert got.etag == ""
    assert got.created is None
    assert got.modified is None
    assert got.expires is None
    assert got.description is None
    assert got.friendly_name is None
    assert got.model_type == enums.Model.ModelType.MODEL_TYPE_UNSPECIFIED
    assert got.labels == {}
    assert len(got.training_runs) == 0
    assert len(got.feature_columns) == 0
    assert len(got.label_columns) == 0
Example #8
0
def test_from_api_repr(target_class):
    from google.cloud.bigquery import ModelReference

    creation_time = datetime.datetime(
        2010, 5, 19, 16, 0, 0, tzinfo=google.cloud._helpers.UTC
    )
    modified_time = datetime.datetime(
        2011, 10, 1, 16, 0, 0, tzinfo=google.cloud._helpers.UTC
    )
    expiration_time = datetime.datetime(
        2012, 12, 21, 16, 0, 0, tzinfo=google.cloud._helpers.UTC
    )
    resource = {
        "modelReference": {
            "projectId": "my-project",
            "datasetId": "my_dataset",
            "modelId": "my_model",
        },
        "location": "US",
        "etag": "abcdefg",
        "creationTime": str(google.cloud._helpers._millis(creation_time)),
        "lastModifiedTime": str(google.cloud._helpers._millis(modified_time)),
        "expirationTime": str(google.cloud._helpers._millis(expiration_time)),
        "description": "A friendly description.",
        "friendlyName": "A friendly name.",
        "modelType": "LOGISTIC_REGRESSION",
        "labels": {"greeting": u"こんにちは"},
        "trainingRuns": [
            {
                "trainingOptions": {"initialLearnRate": 1.0},
                "startTime": str(
                    google.cloud._helpers._datetime_to_rfc3339(creation_time)
                ),
            },
            {
                "trainingOptions": {"initialLearnRate": 0.5},
                "startTime": str(
                    google.cloud._helpers._datetime_to_rfc3339(modified_time)
                ),
            },
            {
                "trainingOptions": {"initialLearnRate": 0.25},
                # Allow milliseconds since epoch format.
                # TODO: Remove this hack once CL 238585470 hits prod.
                "startTime": str(google.cloud._helpers._millis(expiration_time)),
            },
        ],
        "featureColumns": [],
        "encryptionConfiguration": {"kmsKeyName": KMS_KEY_NAME},
    }
    got = target_class.from_api_repr(resource)

    assert got.project == "my-project"
    assert got.dataset_id == "my_dataset"
    assert got.model_id == "my_model"
    assert got.reference == ModelReference.from_string("my-project.my_dataset.my_model")
    assert got.path == "/projects/my-project/datasets/my_dataset/models/my_model"
    assert got.location == "US"
    assert got.etag == "abcdefg"
    assert got.created == creation_time
    assert got.modified == modified_time
    assert got.expires == expiration_time
    assert got.description == u"A friendly description."
    assert got.friendly_name == u"A friendly name."
    assert got.model_type == types.Model.ModelType.LOGISTIC_REGRESSION
    assert got.labels == {"greeting": u"こんにちは"}
    assert got.encryption_configuration.kms_key_name == KMS_KEY_NAME
    assert got.training_runs[0].training_options.initial_learn_rate == 1.0
    assert (
        got.training_runs[0]
        .start_time.ToDatetime()
        .replace(tzinfo=google.cloud._helpers.UTC)
        == creation_time
    )
    assert got.training_runs[1].training_options.initial_learn_rate == 0.5
    assert (
        got.training_runs[1]
        .start_time.ToDatetime()
        .replace(tzinfo=google.cloud._helpers.UTC)
        == modified_time
    )
    assert got.training_runs[2].training_options.initial_learn_rate == 0.25
    assert (
        got.training_runs[2]
        .start_time.ToDatetime()
        .replace(tzinfo=google.cloud._helpers.UTC)
        == expiration_time
    )
Example #9
0
def test_from_api_repr(target_class):
    from google.cloud.bigquery import ModelReference

    creation_time = datetime.datetime(
        2010, 5, 19, 16, 0, 0, tzinfo=google.cloud._helpers.UTC
    )
    modified_time = datetime.datetime(
        2011, 10, 1, 16, 0, 0, tzinfo=google.cloud._helpers.UTC
    )
    expiration_time = datetime.datetime(
        2012, 12, 21, 16, 0, 0, tzinfo=google.cloud._helpers.UTC
    )
    resource = {
        "modelReference": {
            "projectId": "my-project",
            "datasetId": "my_dataset",
            "modelId": "my_model",
        },
        "location": "US",
        "etag": "abcdefg",
        "creationTime": str(google.cloud._helpers._millis(creation_time)),
        "lastModifiedTime": str(google.cloud._helpers._millis(modified_time)),
        "expirationTime": str(google.cloud._helpers._millis(expiration_time)),
        "description": "A friendly description.",
        "friendlyName": "A friendly name.",
        "modelType": "LOGISTIC_REGRESSION",
        "labels": {"greeting": u"こんにちは"},
        "trainingRuns": [
            {
                "trainingOptions": {"initialLearnRate": 1.0},
                "startTime": str(
                    google.cloud._helpers._datetime_to_rfc3339(creation_time)
                ),
            },
            {
                "trainingOptions": {"initialLearnRate": 0.5},
                "startTime": str(
                    google.cloud._helpers._datetime_to_rfc3339(modified_time)
                ),
            },
            {
                "trainingOptions": {"initialLearnRate": 0.25},
                # Allow milliseconds since epoch format.
                # TODO: Remove this hack once CL 238585470 hits prod.
                "startTime": str(google.cloud._helpers._millis(expiration_time)),
            },
        ],
        "featureColumns": [],
    }
    got = target_class.from_api_repr(resource)

    assert got.project == "my-project"
    assert got.dataset_id == "my_dataset"
    assert got.model_id == "my_model"
    assert got.reference == ModelReference.from_string("my-project.my_dataset.my_model")
    assert got.path == "/projects/my-project/datasets/my_dataset/models/my_model"
    assert got.location == "US"
    assert got.etag == "abcdefg"
    assert got.created == creation_time
    assert got.modified == modified_time
    assert got.expires == expiration_time
    assert got.description == u"A friendly description."
    assert got.friendly_name == u"A friendly name."
    assert got.model_type == enums.Model.ModelType.LOGISTIC_REGRESSION
    assert got.labels == {"greeting": u"こんにちは"}
    assert got.training_runs[0].training_options.initial_learn_rate == 1.0
    assert (
        got.training_runs[0]
        .start_time.ToDatetime()
        .replace(tzinfo=google.cloud._helpers.UTC)
        == creation_time
    )
    assert got.training_runs[1].training_options.initial_learn_rate == 0.5
    assert (
        got.training_runs[1]
        .start_time.ToDatetime()
        .replace(tzinfo=google.cloud._helpers.UTC)
        == modified_time
    )
    assert got.training_runs[2].training_options.initial_learn_rate == 0.25
    assert (
        got.training_runs[2]
        .start_time.ToDatetime()
        .replace(tzinfo=google.cloud._helpers.UTC)
        == expiration_time
    )