Skip to content

Commit

Permalink
Merge pull request #270 from juliamcclellan/ac_create
Browse files Browse the repository at this point in the history
add to attribute configuration spec to allow creation
  • Loading branch information
pcattori committed Aug 16, 2019
2 parents 18dff0e + 1fb2e07 commit 79256ea
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 17 deletions.
29 changes: 20 additions & 9 deletions tamr_unify_client/project/attribute_configuration/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ def of(resource):
resource.client, deepcopy(resource._data), resource.api_path
)

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

def from_data(self, data):
"""Creates a spec with the same client and API path as this one, but new data.
Expand Down Expand Up @@ -177,6 +186,16 @@ def with_numeric_field_resolution(self, new_numeric_field_resolution):
{**self._data, "numericFieldResolution": new_numeric_field_resolution}
)

def with_attribute_name(self, new_attribute_name):
"""Creates a new spec with the same properties, updating new attribute name.
:param new_attribute_name: The new attribute name.
:type new_attribute_name: str
:return: A new spec.
:rtype: :class:`~tamr_unify_client.project.attribute_configuration.resource.AttributeConfigurationSpec`
"""
return self.from_data({**self._data, "attributeName": new_attribute_name})

def put(self):
"""Updates the attribute configuration on the server.
Expand All @@ -190,13 +209,5 @@ def __repr__(self):
return (
f"{self.__class__.__module__}."
f"{self.__class__.__qualname__}("
f"relative_id={self._data['relativeId']!r}, "
f"id={self._data['id']!r}, "
f"relative_attribute_id={self._data['relativeAttributeId']!r}, "
f"attribute_role={self._data['attributeRole']!r}, "
f"similarity_function={self._data['similarityFunction']!r}, "
f"enabled_for_ml={self._data['enabledForMl']!r}, "
f"tokenizer={self._data['tokenizer']!r}, "
f"numeric_field_resolution={self._data['numericFieldResolution']!r}, "
f"attribute_name={self._data['attributeName']!r})"
f"dict={self._data})"
)
60 changes: 52 additions & 8 deletions tests/unit/test_attribute_configuration_collection.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

import responses
Expand All @@ -7,6 +9,10 @@
from tamr_unify_client.project.attribute_configuration.collection import (
AttributeConfigurationCollection,
)
from tamr_unify_client.project.attribute_configuration.resource import (
AttributeConfigurationSpec,
)
from tamr_unify_client.project.resource import Project


class TestAttributeConfigurationCollection(TestCase):
Expand Down Expand Up @@ -37,20 +43,54 @@ def test_by_resource_id(self):

@responses.activate
def test_create(self):
def create_callback(request, snoop):
snoop["payload"] = json.loads(request.body)
return 204, {}, json.dumps(self.created_json)

url = (
f"http://localhost:9100/api/versioned/v1/projects/1/attributeConfigurations"
)
project_url = f"http://localhost:9100/api/versioned/v1/projects/1"
responses.add(responses.GET, project_url, json=self.project_json)
responses.add(responses.POST, url, json=self.create_json, status=204)
responses.add(responses.GET, url, json=self.create_json)
snoop_dict = {}
responses.add_callback(
responses.POST, url, partial(create_callback, snoop=snoop_dict)
)
responses.add(responses.GET, url, json=self.created_json)

attributeconfig = self.tamr.projects.by_resource_id(
"1"
).attribute_configurations()
create = attributeconfig.create(self.create_json)

self.assertEqual(create.relative_id, self.create_json["relativeId"])
self.assertEqual(create.relative_id, self.created_json["relativeId"])
self.assertEqual(snoop_dict["payload"], self.create_json)

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

url = (
f"http://localhost:9100/api/versioned/v1/projects/1/attributeConfigurations"
)
snoop_dict = {}
responses.add_callback(
responses.POST, url, partial(create_callback, snoop=snoop_dict)
)

configs = Project(self.tamr, self.project_json).attribute_configurations()
spec = (
AttributeConfigurationSpec.new()
.with_attribute_name(self.create_json["attributeName"])
.with_enabled_for_ml(self.create_json["enabledForMl"])
.with_similarity_function(self.create_json["similarityFunction"])
)
create = configs.create(spec.to_dict())

self.assertEqual(create.relative_id, self.created_json["relativeId"])
self.assertEqual(snoop_dict["payload"], self.create_json)

@responses.activate
def test_stream(self):
Expand All @@ -65,15 +105,19 @@ def test_stream(self):
self.assertEqual(self.acc_json, stream_content)

create_json = {
"id": "unify://unified-data/v1/projects/1/attributeConfigurations/35",
"relativeId": "projects/1/attributeConfigurations/35",
"relativeAttributeId": "datasets/79/attributes/Tester",
"attributeRole": "",
"similarityFunction": "ABSOLUTE_DIFF",
"enabledForMl": False,
"attributeName": "Tester",
}

created_json = {
**create_json,
"attributeRole": "",
"tokenizer": "",
"numericFieldResolution": [],
"attributeName": "Tester",
"id": "unify://unified-data/v1/projects/1/attributeConfigurations/35",
"relativeId": "projects/1/attributeConfigurations/35",
"relativeAttributeId": "datasets/79/attributes/Tester",
}

project_json = {
Expand Down

0 comments on commit 79256ea

Please sign in to comment.