Skip to content

Commit

Permalink
Merge pull request #278 from charlottemoremen/pcc_with_put
Browse files Browse the repository at this point in the history
Fix #226 - Update published cluster configurations with put
  • Loading branch information
pcattori committed Aug 20, 2019
2 parents 337f775 + 6a3f2a7 commit 06582ab
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [#273](https://github.com/Datatamer/unify-client-python/issues/273) Attribute type spec to allow for attribute creation
- [#219](https://github.com/Datatamer/tamr-client/issues/219) Delete a resource from collection.
- [#277](https://github.com/Datatamer/unify-client-python/issues/277) Attribute mapping spec
- [#226](https://github.com/Datatamer/tamr-client/issues/226) Update published cluster configurations with put

**BUG FIXES**
- [#235](https://github.com/Datatamer/unify-client-python/issues/235) Making `AttributeCollection` retrieve attributes directly instead of by streaming
Expand Down
61 changes: 61 additions & 0 deletions tamr_unify_client/mastering/published_cluster/configuration.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from copy import deepcopy

from tamr_unify_client.base_resource import BaseResource


Expand Down Expand Up @@ -35,10 +37,69 @@ def versions_time_to_live(self):
""":type: str"""
return self._data.get("versionsTimeToLive")

def spec(self):
"""Returns a spec representation of this published cluster
:return: the published cluster spec
:rtype: :class`~tamr_unify_client.mastering.published_cluster.configuration.PublishedClusterConfigurationSpec"""
return PublishedClusterConfigurationSpec.of(self)

def __repr__(self):
return (
f"{self.__class__.__module__}."
f"{self.__class__.__qualname__}("
f"relative_id={self.relative_id!r}, "
f"versions_time_to_live={self.versions_time_to_live!r})"
)


class PublishedClusterConfigurationSpec:
"""A representation of the server view of a published cluster."""

def __init__(self, client, data, api_path):
self.client = client
self._data = data
self.api_path = api_path

@staticmethod
def of(resource):
"""Creates an published cluster spec from published cluster.
:param resource: The existing published cluster.
:type resource: :class:`~tamr_unify_client.mastering.published_cluster.configuration.PublishedClusterConfiguration`
:return: The corresponding published cluster spec.
:rtype: :class:`~tamr_unify_client.mastering.published_cluster.configuration.PublishedClusterConfigurationSpec`
"""
return PublishedClusterConfigurationSpec(
resource.client, deepcopy(resource._data), resource.api_path
)

def from_data(self, data):
"""Creates a spec with new data.
:param data: The data for the new spec.
:type data: dict
:return: The new spec.
:rtype: :class:`~tamr_unify_client.mastering.published_cluster.configuration.PublishedClusterConfigurationSpec`
"""
return PublishedClusterConfigurationSpec(self.client, data, self.api_path)

def to_dict(self):
"""Returns a version of this spec that conforms to the API representation.
:returns: The spec's dict.
:rtype: dict
"""
return deepcopy(self._data)

def with_versions_time_to_live(self, new_versions_time_to_live):
"""Creates a new spec with the same properties, updating versions time to live.
:param new_versions_time_to_live: The new versions time to live.
:type new_versions_time_to_live: str
:return: A new spec.
:rtype: :class:`~tamr_unify_client.mastering.published_cluster.configuration.PublishedClusterConfigurationSpec`
"""
return self.from_data(
{**self._data, "versionsTimeToLive": new_versions_time_to_live}
)
20 changes: 20 additions & 0 deletions tests/unit/test_published_clusters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from functools import partial
import json
from unittest import TestCase

from requests import HTTPError
Expand Down Expand Up @@ -70,6 +72,22 @@ def test_delete_published_clusters_configuration(self):
config = p.published_clusters_configuration()
self.assertRaises(HTTPError, config.delete)

@responses.activate
def test_update_published_clusters_configuration(self):
def create_callback(request, snoop):
snoop["payload"] = request.body
return 200, {}, json.dumps(self.update_info)

url = "http://localhost/api/versioned/v1/projects/1/publishedClustersConfiguration"
snoop_dict = {}
responses.add(responses.GET, url, self._config_json)
responses.add_callback(
responses.PUT, url, partial(create_callback, snoop=snoop_dict)
)
clusters = PublishedClustersConfiguration(self.tamr, self._config_json)
new_cluster = clusters.spec().with_versions_time_to_live(self.update_info)
self.assertEqual(new_cluster._data, {"versionsTimeToLive": "PT100H"})

@responses.activate
def test_refresh_ids(self):
unified_dataset_url = f"{self._base_url}/projects/1/unifiedDataset"
Expand Down Expand Up @@ -208,3 +226,5 @@ def test_refresh_stats(self):
}

_config_json = {"versionsTimeToLive": "P4D"}

update_info = "PT100H"

0 comments on commit 06582ab

Please sign in to comment.