Skip to content

Commit

Permalink
Merge pull request #271 from juliamcclellan/dataset_create
Browse files Browse the repository at this point in the history
Dataset spec for creation
  • Loading branch information
pcattori committed Aug 16, 2019
2 parents 79256ea + 5d14b51 commit c191861
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 19 deletions.
38 changes: 34 additions & 4 deletions tamr_unify_client/dataset/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ def version(self):
@property
def tags(self):
""":type: list[str]"""
return self._data.get("tags")
return self._data.get("tags")[:]

@property
def key_attribute_names(self):
""":type: list[str]"""
return self._data.get("keyAttributeNames")
return self._data.get("keyAttributeNames")[:]

@property
def attributes(self):
Expand Down Expand Up @@ -465,6 +465,15 @@ def of(resource):
"""
return DatasetSpec(resource.client, deepcopy(resource._data), resource.api_path)

@staticmethod
def new():
"""Creates a blank spec that could be used to construct a new dataset.
:return: The empty spec.
:rtype: :class:`~tamr_unify_client.dataset.resource.DatasetSpec`
"""
return DatasetSpec(None, {}, None)

def from_data(self, data):
"""Creates a spec with the same client and API path as this one, but new data.
Expand All @@ -483,6 +492,16 @@ def to_dict(self):
"""
return deepcopy(self._data)

def with_name(self, new_name):
"""Creates a new spec with the same properties, updating name.
:param new_name: The new name.
:type new_name: str
:return: A new spec.
:rtype: :class:`~tamr_unify_client.dataset.resource.DatasetSpec`
"""
return self.from_data({**self._data, "name": new_name})

def with_external_id(self, new_external_id):
"""Creates a new spec with the same properties, updating external ID.
Expand All @@ -503,6 +522,18 @@ def with_description(self, new_description):
"""
return self.from_data({**self._data, "description": new_description})

def with_key_attribute_names(self, new_key_attribute_names):
"""Creates a new spec with the same properties, updating key attribute names.
:param new_key_attribute_names: The new key attribute names.
:type new_key_attribute_names: list[str]
:return: A new spec.
:rtype: :class:`~tamr_unify_client.dataset.resource.DatasetSpec`
"""
return self.from_data(
{**self._data, "keyAttributeNames": new_key_attribute_names}
)

def with_tags(self, new_tags):
"""Creates a new spec with the same properties, updating tags.
Expand All @@ -526,6 +557,5 @@ def __repr__(self):
return (
f"{self.__class__.__module__}."
f"{self.__class__.__qualname__}("
f"relative_id={self._data['relativeId']!r}, "
f"name={self._data['name']!r})"
f"dict={self._data})"
)
57 changes: 42 additions & 15 deletions tests/unit/test_create_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,26 @@
from tamr_unify_client import Client
from tamr_unify_client.auth import UsernamePasswordAuth
from tamr_unify_client.dataset.collection import CreationError

from tamr_unify_client.dataset.resource import DatasetSpec

auth = UsernamePasswordAuth("username", "password")
tamr = Client(auth)


@responses.activate
def test_create_dataset():
creation_spec = {
"id": "unify://unified-data/v1/datasets/1",
"name": "dataset",
"keyAttributeNames": ["F1"],
"description": "So much data in here!",
"externalId": "Dataset created with pubapi",
}
def create_callback(request, snoop):
snoop["payload"] = json.loads(request.body)
return 201, {}, json.dumps(_dataset_json)

dataset_url = _datasets_url + "/1"
responses.add(responses.POST, _datasets_url, json=creation_spec, status=201)
responses.add(responses.GET, dataset_url, json=creation_spec)
snoop_dict = {}
responses.add_callback(
responses.POST, _datasets_url, partial(create_callback, snoop=snoop_dict)
)
responses.add(responses.GET, dataset_url, json=_dataset_json)

u = tamr.datasets.create(creation_spec)
u = tamr.datasets.create(_creation_spec)
p = tamr.datasets.by_resource_id("1")

assert u.name == p.name
Expand Down Expand Up @@ -139,6 +138,37 @@ def test_dataset_deletion_failure():
tamr.datasets.create_from_dataframe(_dataframe, "attribute1", "Dataset")


@responses.activate
def test_create_from_spec():
def create_callback(request, snoop):
snoop["payload"] = json.loads(request.body)
return 201, {}, json.dumps(_dataset_json)

snoop_dict = {}
responses.add_callback(
responses.POST, _datasets_url, partial(create_callback, snoop=snoop_dict)
)

spec = (
DatasetSpec.new()
.with_name(_creation_spec["name"])
.with_key_attribute_names(_creation_spec["keyAttributeNames"])
.with_description(_creation_spec["description"])
.with_external_id(_creation_spec["externalId"])
)
d = tamr.datasets.create(spec.to_dict())

assert snoop_dict["payload"] == _creation_spec
assert d.relative_id == _dataset_json["relativeId"]


_creation_spec = {
"name": "Dataset",
"keyAttributeNames": ["F1"],
"description": "So much data in here!",
"externalId": "Dataset created with pubapi",
}

_datasets_url = f"http://localhost:9100/api/versioned/v1/datasets"
_dataset_url = _datasets_url + "/1"
_attribute_url = _dataset_url + "/attributes"
Expand All @@ -163,11 +193,9 @@ def test_dataset_deletion_failure():
}

_dataset_json = {
**_creation_spec,
"id": "unify://unified-data/v1/datasets/1",
"name": "Dataset",
"description": "",
"version": "1",
"keyAttributeNames": ["attribute1"],
"tags": [],
"created": {
"username": "admin",
Expand All @@ -181,7 +209,6 @@ def test_dataset_deletion_failure():
},
"relativeId": "datasets/1",
"upstreamDatasetIds": [],
"externalId": "Dataset",
}

_attribute_json = {
Expand Down

0 comments on commit c191861

Please sign in to comment.