Skip to content

Commit

Permalink
Merge pull request #428 from skalish/attribute-from-dataset
Browse files Browse the repository at this point in the history
TC: Move tc.attribute.from_dataset_all to tc.dataset.attributes
  • Loading branch information
pcattori committed Aug 13, 2020
2 parents 6d067fe + cfbd6d1 commit 1994f0e
Show file tree
Hide file tree
Showing 25 changed files with 464 additions and 169 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Added function to get operation from resource ID
- [#421](https://github.com/Datatamer/tamr-client/pull/421) Added functions for getting and replacing the transformations of a projects via `tc.transformations.get_all()` and `tc.transformations.replace_all()`
- Added new dataclasses `Transformations` and `InputTransformations` to support these functions
- Moved function `tc.attribute.from_dataset_all` to `tc.dataset.attributes`

**NEW FEATURES**
- [#383](https://github.com/Datatamer/tamr-client/issues/383) Now able to create an Operation from Job resource id
Expand Down
1 change: 0 additions & 1 deletion docs/beta/attribute/attribute.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Attribute
.. autoclass:: tamr_client.Attribute

.. autofunction:: tamr_client.attribute.from_resource_id
.. autofunction:: tamr_client.attribute.from_dataset_all
.. autofunction:: tamr_client.attribute.to_json
.. autofunction:: tamr_client.attribute.create
.. autofunction:: tamr_client.attribute.update
Expand Down
1 change: 1 addition & 0 deletions docs/beta/dataset/dataset.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Dataset
.. autoclass:: tamr_client.Dataset

.. autofunction:: tamr_client.dataset.from_resource_id
.. autofunction:: tamr_client.dataset.attributes

Exceptions
----------
Expand Down
2 changes: 2 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def format(session):
session.run("poetry", "install", external=True)
if "--fix" in session.posargs:
session.run("black", ".")
elif "--diff" in session.posargs:
session.run("black", ".", "--diff")
else:
session.run("black", ".", "--check")

Expand Down
1 change: 0 additions & 1 deletion tamr_client/attribute/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
AlreadyExists,
create,
delete,
from_dataset_all,
from_resource_id,
NotFound,
ReservedName,
Expand Down
27 changes: 1 addition & 26 deletions tamr_client/attribute/_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from copy import deepcopy
from dataclasses import replace
from typing import Optional, Tuple
from typing import Optional

from tamr_client import response
from tamr_client._types import Attribute, AttributeType, Dataset, JsonDict, Session, URL
Expand Down Expand Up @@ -105,31 +105,6 @@ def _from_json(url: URL, data: JsonDict) -> Attribute:
)


def from_dataset_all(session: Session, dataset: Dataset) -> Tuple[Attribute, ...]:
"""Get all attributes from a dataset
Args:
dataset: Dataset containing the desired attributes
Returns:
The attributes for the specified dataset
Raises:
requests.HTTPError: If an HTTP error is encountered.
"""
attrs_url = replace(dataset.url, path=dataset.url.path + "/attributes")
r = session.get(str(attrs_url))
attrs_json = response.successful(r).json()

attrs = []
for attr_json in attrs_json:
id = attr_json["name"]
attr_url = replace(attrs_url, path=attrs_url.path + f"/{id}")
attr = _from_json(attr_url, attr_json)
attrs.append(attr)
return tuple(attrs)


def to_json(attr: Attribute) -> JsonDict:
"""Serialize attribute into JSON
Expand Down
2 changes: 1 addition & 1 deletion tamr_client/dataset/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from tamr_client.dataset import dataframe, record, unified
from tamr_client.dataset._dataset import from_resource_id, NotFound
from tamr_client.dataset._dataset import attributes, from_resource_id, NotFound
30 changes: 29 additions & 1 deletion tamr_client/dataset/_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
See https://docs.tamr.com/reference/dataset-models
"""
from copy import deepcopy
from dataclasses import replace
from typing import Tuple

from tamr_client import response
from tamr_client._types import Dataset, Instance, JsonDict, Session, URL
from tamr_client._types import Attribute, Dataset, Instance, JsonDict, Session, URL
from tamr_client.attribute import _from_json as _attribute_from_json
from tamr_client.exception import TamrClientException


Expand Down Expand Up @@ -74,3 +77,28 @@ def _from_json(url: URL, data: JsonDict) -> Dataset:
description=cp.get("description"),
key_attribute_names=tuple(cp["keyAttributeNames"]),
)


def attributes(session: Session, dataset: Dataset) -> Tuple[Attribute, ...]:
"""Get all attributes from a dataset
Args:
dataset: Dataset containing the desired attributes
Returns:
The attributes for the specified dataset
Raises:
requests.HTTPError: If an HTTP error is encountered.
"""
attrs_url = replace(dataset.url, path=dataset.url.path + "/attributes")
r = session.get(str(attrs_url))
attrs_json = response.successful(r).json()

attrs = []
for attr_json in attrs_json:
id = attr_json["name"]
attr_url = replace(attrs_url, path=attrs_url.path + f"/{id}")
attr = _attribute_from_json(attr_url, attr_json)
attrs.append(attr)
return tuple(attrs)
103 changes: 34 additions & 69 deletions tests/tamr_client/attribute/test_attribute.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from dataclasses import replace

import pytest
import responses

import tamr_client as tc
from tests.tamr_client import fake, utils
Expand Down Expand Up @@ -30,7 +27,7 @@ def test_json():
assert attr == tc.attribute._from_json(url, tc.attribute.to_json(attr))


@responses.activate
@fake.json
def test_create():
s = fake.session()
dataset = fake.dataset()
Expand All @@ -46,10 +43,6 @@ def test_create():
]
)

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,
Expand All @@ -58,59 +51,61 @@ def test_create():
type=tc.attribute.type.Record(attributes=attrs),
)

assert attr == tc.attribute._from_json(url, attr_json)
assert attr.name == "attr"
assert not attr.is_nullable
assert isinstance(attr.type, tc.attribute.type.Record)
assert attr.type.attributes == attrs


@responses.activate
@fake.json
def test_update():
s = fake.session()
attr = fake.attribute()

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"]
s, attr, description="Synthetic row number updated"
)

assert updated_attr == replace(attr, description=updated_attr_json["description"])
assert updated_attr.description == "Synthetic row number updated"


@responses.activate
@fake.json
def test_delete():
s = fake.session()
attr = fake.attribute()

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)


@responses.activate
@fake.json
def test_from_resource_id():
s = fake.session()
dataset = fake.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)
attrs = tuple(
[
tc.SubAttribute(
name=str(i),
is_nullable=True,
type=tc.attribute.type.Array(tc.attribute.type.STRING),
)
for i in range(4)
]
)

attr = tc.attribute.from_resource_id(s, dataset, "attr")

assert attr == tc.attribute._from_json(url, attr_json)
assert attr.name == "attr"
assert not attr.is_nullable
assert isinstance(attr.type, tc.attribute.type.Record)
assert attr.type.attributes == attrs


@responses.activate
@fake.json
def test_from_resource_id_attribute_not_found():
s = fake.session()
dataset = fake.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")

Expand All @@ -123,58 +118,28 @@ def test_create_reserved_attribute_name():
tc.attribute.create(s, dataset, name="clusterId", is_nullable=False)


@responses.activate
def test_from_dataset_all():
s = fake.session()
dataset = fake.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)


@responses.activate
@fake.json
def test_create_attribute_exists():
s = fake.session()
dataset = fake.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)


@responses.activate
@fake.json
def test_update_attribute_not_found():
s = fake.session()
attr = fake.attribute()

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):
tc.attribute.update(s, attr)


@responses.activate
@fake.json
def test_delete_attribute_not_found():
s = fake.session()
attr = fake.attribute()

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)
tc.attribute.delete(s, attr)
49 changes: 0 additions & 49 deletions tests/tamr_client/data/attribute.json

This file was deleted.

0 comments on commit 1994f0e

Please sign in to comment.