def test_create():
    s = utils.session()
    dataset = utils.dataset()

    attrs = tuple([
        tc.SubAttribute(
            name=str(i),
            is_nullable=True,
            type=tc.attribute.type.Array(tc.attribute.type.STRING),
        ) for i in range(4)
    ])

    attrs_url = tc.URL(path=dataset.url.path + "/attributes")
    url = replace(attrs_url, path=attrs_url.path + "/attr")
    attr_json = utils.load_json("attribute.json")
    responses.add(responses.POST, str(attrs_url), json=attr_json)
    attr = tc.attribute.create(
        s,
        dataset,
        name="attr",
        is_nullable=False,
        type=tc.attribute.type.Record(attributes=attrs),
    )

    assert attr == tc.attribute._from_json(url, attr_json)
Example #2
0
def test_upsert_index_as_primary_key():
    s = utils.session()
    dataset = utils.dataset()

    url = tc.URL(path="datasets/1:updateRecords")
    updates = [
        tc.record._create_command(record, primary_key_name="primary_key")
        for record in _records_with_keys_json_2
    ]
    snoop: Dict = {}
    responses.add_callback(
        responses.POST,
        str(url),
        partial(
            utils.capture_payload, snoop=snoop, status=200, response_json=_response_json
        ),
    )

    df = pd.DataFrame(
        _records_json_2,
        index=[record["primary_key"] for record in _records_with_keys_json_2],
    )
    df.index.name = "primary_key"

    response = tc.dataframe.upsert(s, dataset, df, primary_key_name="primary_key")
    assert response == _response_json
    assert snoop["payload"] == utils.stringify(updates)
Example #3
0
def test_upsert():
    s = utils.session()
    dataset = utils.dataset()

    url = tc.URL(path="datasets/1:updateRecords")
    updates = [
        tc.record._create_command(record, primary_key_name="primary_key")
        for record in _records_json
    ]
    snoop: Dict = {}
    responses.add_callback(
        responses.POST,
        str(url),
        partial(utils.capture_payload,
                snoop=snoop,
                status=200,
                response_json=_response_json),
    )

    response = tc.record.upsert(s,
                                dataset,
                                _records_json,
                                primary_key_name="primary_key")
    assert response == _response_json
    assert snoop["payload"] == utils.stringify(updates)
Example #4
0
def test_upsert_primary_key_not_found():
    s = utils.session()
    dataset = utils.dataset()

    df = pd.DataFrame(_records_json)

    with pytest.raises(tc.primary_key.NotFound):
        tc.dataframe.upsert(s, dataset, df, primary_key_name="wrong_primary_key")
Example #5
0
def test_upsert_primary_key_not_found():
    s = utils.session()
    dataset = utils.dataset()

    with pytest.raises(tc.primary_key.NotFound):
        tc.record.upsert(
            s, dataset, _records_json, primary_key_name="wrong_primary_key"
        )
def test_create_attribute_exists():
    s = utils.session()
    dataset = utils.dataset()

    url = replace(dataset.url, path=dataset.url.path + "/attributes")
    responses.add(responses.POST, str(url), status=409)
    with pytest.raises(tc.attribute.AlreadyExists):
        tc.attribute.create(s, dataset, name="attr", is_nullable=False)
def test_from_resource_id_attribute_not_found():
    s = utils.session()
    dataset = utils.dataset()

    url = replace(dataset.url, path=dataset.url.path + "/attributes/attr")

    responses.add(responses.GET, str(url), status=404)
    with pytest.raises(tc.attribute.NotFound):
        tc.attribute.from_resource_id(s, dataset, "attr")
def test_delete():
    s = utils.session()

    url = tc.URL(path="datasets/1/attributes/RowNum")
    attr_json = utils.load_json("attributes.json")[0]
    attr = tc.attribute._from_json(url, attr_json)

    responses.add(responses.DELETE, str(attr.url), status=204)
    tc.attribute.delete(s, attr)
Example #9
0
def test_from_resource_id_not_found():
    s = utils.session()
    instance = utils.instance()

    url = tc.URL(path="projects/1")
    responses.add(responses.GET, str(url), status=404)

    with pytest.raises(tc.project.NotFound):
        tc.project.from_resource_id(s, instance, "1")
Example #10
0
def test_delete_primary_key_not_found():
    s = utils.session()
    dataset = utils.dataset()

    with pytest.raises(tc.record.PrimaryKeyNotFound):
        tc.record.delete(s,
                         dataset,
                         _records_json,
                         primary_key_name="wrong_primary_key")
Example #11
0
def test_delete_attribute_not_found():
    s = utils.session()

    url = tc.URL(path="datasets/1/attributes/RowNum")
    attr_json = utils.load_json("attributes.json")[0]
    attr = tc.attribute._from_json(url, attr_json)

    responses.add(responses.PUT, str(attr.url), status=404)
    with pytest.raises(tc.attribute.NotFound):
        attr = tc.attribute.update(s, attr)
Example #12
0
def test_from_resource_id():
    s = utils.session()
    dataset = utils.dataset()

    url = tc.URL(path=dataset.url.path + "/attributes/attr")
    attr_json = utils.load_json("attribute.json")
    responses.add(responses.GET, str(url), json=attr_json)
    attr = tc.attribute.from_resource_id(s, dataset, "attr")

    assert attr == tc.attribute._from_json(url, attr_json)
Example #13
0
def test_from_project_dataset_not_found():
    s = utils.session()
    instance = utils.instance()
    project = utils.mastering_project()

    url = tc.URL(path="projects/1/unifiedDataset")
    responses.add(responses.GET, str(url), status=404)

    with pytest.raises(tc.dataset.unified.NotFound):
        tc.dataset.unified.from_project(s, instance, project)
Example #14
0
def test_stream():
    s = utils.session()
    dataset = utils.dataset()

    url = tc.URL(path="datasets/1/records")
    responses.add(
        responses.GET, str(url), body="\n".join(json.dumps(x) for x in _records_json)
    )

    records = tc.record.stream(s, dataset)
    assert list(records) == _records_json
Example #15
0
def test_from_resource_id_mastering():
    s = utils.session()
    instance = utils.instance()

    project_json = utils.load_json("mastering_project.json")
    url = tc.URL(path="projects/1")
    responses.add(responses.GET, str(url), json=project_json)

    project = tc.project.from_resource_id(s, instance, "1")
    assert isinstance(project, tc.MasteringProject)
    assert project.name == "proj"
    assert project.description == "Mastering Project"
Example #16
0
def test_upsert_index_column_name_collision():
    s = utils.session()
    dataset = utils.dataset()

    df = pd.DataFrame(_records_json_2)
    df.index.name = "primary_key"

    # create column in `df` with same name as index and matching "primary_key"
    df.insert(0, df.index.name, df.index)

    with pytest.raises(tc.primary_key.Ambiguous):
        tc.dataframe.upsert(s, dataset, df, primary_key_name="primary_key")
Example #17
0
def test_from_resource_id():
    s = utils.session()
    instance = utils.instance()

    dataset_json = utils.load_json("dataset.json")
    url = tc.URL(path="datasets/1")
    responses.add(responses.GET, str(url), json=dataset_json)

    dataset = tc.dataset.from_resource_id(s, instance, "1")
    assert dataset.name == "dataset 1 name"
    assert dataset.description == "dataset 1 description"
    assert dataset.key_attribute_names == ("tamr_id", )
Example #18
0
def test_apply_changes():
    s = utils.session()
    dataset_json = utils.load_json("dataset.json")
    dataset_url = tc.URL(path="projects/1/unifiedDataset")
    unified_dataset = tc.dataset.unified._from_json(dataset_url, dataset_json)

    operation_json = utils.load_json("operation_pending.json")
    operation_url = tc.URL(path="operations/1")
    url = tc.URL(path="projects/1/unifiedDataset:refresh")
    responses.add(responses.POST, str(url), json=operation_json)

    response = tc.dataset.unified._apply_changes_async(s, unified_dataset)
    assert response == tc.operation._from_json(operation_url, operation_json)
Example #19
0
def test_from_project():
    s = utils.session()
    instance = utils.instance()
    project = utils.mastering_project()

    dataset_json = utils.load_json("dataset.json")
    url = tc.URL(path="projects/1/unifiedDataset")
    responses.add(responses.GET, str(url), json=dataset_json)

    unified_dataset = tc.dataset.unified.from_project(s, instance, project)
    assert unified_dataset.name == "dataset 1 name"
    assert unified_dataset.description == "dataset 1 description"
    assert unified_dataset.key_attribute_names == ("tamr_id", )
Example #20
0
def test_operation_from_url():
    s = utils.session()
    url = tc.URL(path="operations/1")

    operation_json = utils.load_json("operation_succeeded.json")
    responses.add(responses.GET, str(url), json=operation_json)

    op = tc.operation._from_url(s, url)
    assert op.url == url
    assert op.type == operation_json["type"]
    assert op.description == operation_json["description"]
    assert op.status == operation_json["status"]
    assert tc.operation.succeeded(op)
Example #21
0
def test_operation_poll():
    s = utils.session()
    url = tc.URL(path="operations/1")

    pending_operation_json = utils.load_json("operation_pending.json")
    op1 = tc.operation._from_json(url, pending_operation_json)

    succeeded_operation_json = utils.load_json("operation_succeeded.json")
    responses.add(responses.GET, str(url), json=succeeded_operation_json)
    op2 = tc.operation.poll(s, op1)

    assert op2.url == op1.url
    assert not tc.operation.succeeded(op1)
    assert tc.operation.succeeded(op2)
Example #22
0
def test_update():
    s = utils.session()

    url = tc.URL(path="datasets/1/attributes/RowNum")
    attr_json = utils.load_json("attributes.json")[0]
    attr = tc.attribute._from_json(url, attr_json)

    updated_attr_json = utils.load_json("updated_attribute.json")
    responses.add(responses.PUT, str(attr.url), json=updated_attr_json)
    updated_attr = tc.attribute.update(
        s, attr, description=updated_attr_json["description"])

    assert updated_attr == replace(
        attr, description=updated_attr_json["description"])
Example #23
0
def test_ndjson():
    s = utils.session()

    records = [{"a": 1}, {"b": 2}, {"c": 3}]
    url = tc.URL(path="datasets/1/records")
    responses.add(responses.GET,
                  str(url),
                  body="\n".join(json.dumps(x) for x in records))

    r = s.get(str(url))

    ndjson = list(tc.response.ndjson(r))
    assert len(ndjson) == 3
    for record in ndjson:
        assert record in records
Example #24
0
def test_from_dataset_all():
    s = utils.session()
    dataset = utils.dataset()

    attrs_url = replace(dataset.url, path=dataset.url.path + "/attributes")
    attrs_json = utils.load_json("attributes.json")
    responses.add(responses.GET, str(attrs_url), json=attrs_json, status=204)

    attrs = tc.attribute.from_dataset_all(s, dataset)

    row_num = attrs[0]
    assert row_num.name == "RowNum"
    assert row_num.type == tc.attribute.type.STRING

    geom = attrs[1]
    assert geom.name == "geom"
    assert isinstance(geom.type, tc.attribute.type.Record)
Example #25
0
def test_operation_from_response_noop():
    s = utils.session()
    instance = utils.instance()
    url = tc.URL(path="operations/2")
    responses.add(responses.GET, str(url), status=204)

    url_dummy = tc.URL(path="operations/-1")
    responses.add(responses.GET, str(url_dummy), status=404)

    r = s.get(str(url))
    op2 = tc.operation._from_response(instance, r)

    assert op2.url is not None
    assert op2.type == "NOOP"
    assert op2.description is not None
    assert op2.status is not None
    assert op2.status["state"] == "SUCCEEDED"
    assert tc.operation.succeeded(op2)

    op2w = tc.operation.wait(s, op2)
    assert tc.operation.succeeded(op2w)

    with pytest.raises(tc.operation.NotFound):
        tc.operation.poll(s, op2w)
Example #26
0
def test_create_reserved_attribute_name():
    s = utils.session()
    dataset = utils.dataset()

    with pytest.raises(tc.attribute.ReservedName):
        tc.attribute.create(s, dataset, name="clusterId", is_nullable=False)