Skip to content

Commit

Permalink
Merge pull request #344 from pcattori/attribute-from-dataset-all
Browse files Browse the repository at this point in the history
Move dataset.attributes -> attribute.from_dataset_all
  • Loading branch information
pcattori committed Mar 20, 2020
2 parents 69d221f + 7af7780 commit 502a6dd
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 52 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- BETA: New attributes package!
- `tc.attribute` module
- `tc.Attribute` type
- functions: `from_resource_id`, `to_json`, `create`, `update`, `delete`
- functions: `from_resource_id`, `from_dataset_all`, `to_json`, `create`, `update`, `delete`
- `tc.attribute_type` module
- `tc.AttributeType` for type annotations
- Primitive Types: `BOOLEAN`, `DOUBLE`, `INT`, `LONG`, `STRING`
Expand All @@ -18,7 +18,7 @@
- BETA: New datasets package!
- `tc.dataset` module
- `tc.Dataset` type
- functions: `from_resource_id`, `attributes`
- functions: `from_resource_id`
- BETA: New `tc.instance` module!
- `tc.Instance` type
- functions: `tc.instance.from_auth`
Expand Down
1 change: 1 addition & 0 deletions docs/beta/attributes/attribute.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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: 0 additions & 1 deletion docs/beta/datasets/dataset.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Dataset
.. autoclass:: tamr_client.Dataset

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

Exceptions
----------
Expand Down
27 changes: 26 additions & 1 deletion tamr_client/attributes/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 dataclass, field, replace
from typing import Optional
from typing import Optional, Tuple

import tamr_client as tc
from tamr_client.types import JsonDict
Expand Down Expand Up @@ -124,6 +124,31 @@ def _from_json(url: tc.URL, data: JsonDict) -> Attribute:
)


def from_dataset_all(session: tc.Session, dataset: tc.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 = tc.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
27 changes: 1 addition & 26 deletions tamr_client/datasets/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
See https://docs.tamr.com/reference/dataset-models
"""
from copy import deepcopy
from dataclasses import dataclass, replace
from dataclasses import dataclass
from typing import Optional, Tuple

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


def attributes(session: tc.Session, dataset: Dataset) -> Tuple["tc.Attribute", ...]:
"""Get attributes for this 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 = tc.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 = tc.attribute._from_json(attr_url, attr_json)
attrs.append(attr)
return tuple(attrs)
20 changes: 20 additions & 0 deletions tests/attributes/test_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ def test_create_reserved_attribute_name():
tc.attribute.create(s, dataset, name="clusterId", is_nullable=False)


@responses.activate
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 isinstance(row_num.type, tc.attribute_type.String)

geom = attrs[1]
assert geom.name == "geom"
assert isinstance(geom.type, tc.attribute_type.Record)


@responses.activate
def test_create_attribute_exists():
s = utils.session()
Expand Down
22 changes: 0 additions & 22 deletions tests/datasets/test_dataset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from dataclasses import replace

import pytest
import responses

Expand Down Expand Up @@ -32,23 +30,3 @@ def test_from_resource_id_dataset_not_found():

with pytest.raises(tc.DatasetNotFound):
tc.dataset.from_resource_id(s, instance, "1")


@responses.activate
def test_attributes():
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.dataset.attributes(s, dataset)

row_num = attrs[0]
assert row_num.name == "RowNum"
assert isinstance(row_num.type, tc.attribute_type.String)

geom = attrs[1]
assert geom.name == "geom"
assert isinstance(geom.type, tc.attribute_type.Record)

0 comments on commit 502a6dd

Please sign in to comment.