Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Element functions #792

Merged
merged 8 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 71 additions & 1 deletion TM1py/Services/ElementService.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,51 @@ def get_leaf_element_names(self, dimension_name: str, hierarchy_name: str, **kwa
response = self._rest.GET(url, **kwargs)
return [e["Name"] for e in response.json()['value']]

def get_consolidated_elements(self, dimension_name: str, hierarchy_name: str, **kwargs) -> List[Element]:
url = format_url(
"/api/v1/Dimensions('{}')/Hierarchies('{}')/Elements?$expand=*&$filter=Type eq 3",
dimension_name,
hierarchy_name)
response = self._rest.GET(url, **kwargs)
return [Element.from_dict(element) for element in response.json()["value"]]

def get_consolidated_element_names(self, dimension_name: str, hierarchy_name: str, **kwargs) -> List[str]:
url = format_url("/api/v1/Dimensions('{}')/Hierarchies('{}')/Elements?$select=Name&$filter=Type eq 3",
dimension_name,
hierarchy_name)
response = self._rest.GET(url, **kwargs)
return [e["Name"] for e in response.json()['value']]

def get_numeric_elements(self, dimension_name: str, hierarchy_name: str, **kwargs) -> List[Element]:
url = format_url(
"/api/v1/Dimensions('{}')/Hierarchies('{}')/Elements?$expand=*&$filter=Type eq 1",
dimension_name,
hierarchy_name)
response = self._rest.GET(url, **kwargs)
return [Element.from_dict(element) for element in response.json()["value"]]

def get_numeric_element_names(self, dimension_name: str, hierarchy_name: str, **kwargs) -> List[str]:
url = format_url("/api/v1/Dimensions('{}')/Hierarchies('{}')/Elements?$select=Name&$filter=Type eq 1",
dimension_name,
hierarchy_name)
response = self._rest.GET(url, **kwargs)
return [e["Name"] for e in response.json()['value']]

def get_string_elements(self, dimension_name: str, hierarchy_name: str, **kwargs) -> List[Element]:
url = format_url(
"/api/v1/Dimensions('{}')/Hierarchies('{}')/Elements?$expand=*&$filter=Type eq 2",
dimension_name,
hierarchy_name)
response = self._rest.GET(url, **kwargs)
return [Element.from_dict(element) for element in response.json()["value"]]

def get_string_element_names(self, dimension_name: str, hierarchy_name: str, **kwargs) -> List[str]:
url = format_url("/api/v1/Dimensions('{}')/Hierarchies('{}')/Elements?$select=Name&$filter=Type eq 2",
dimension_name,
hierarchy_name)
response = self._rest.GET(url, **kwargs)
return [e["Name"] for e in response.json()['value']]

def get_element_names(self, dimension_name: str, hierarchy_name: str, **kwargs) -> List[str]:
""" Get all element names

Expand Down Expand Up @@ -135,6 +180,22 @@ def get_number_of_leaf_elements(self, dimension_name: str, hierarchy_name: str,
response = self._rest.GET(url, **kwargs)
return int(response.text)

def get_number_of_numeric_elements(self, dimension_name: str, hierarchy_name: str, **kwargs) -> int:
url = format_url(
"/api/v1/Dimensions('{}')/Hierarchies('{}')/Elements/$count?$filter=Type eq 1",
dimension_name,
hierarchy_name)
response = self._rest.GET(url, **kwargs)
return int(response.text)

def get_number_of_string_elements(self, dimension_name: str, hierarchy_name: str, **kwargs) -> int:
url = format_url(
"/api/v1/Dimensions('{}')/Hierarchies('{}')/Elements/$count?$filter=Type eq 2",
dimension_name,
hierarchy_name)
response = self._rest.GET(url, **kwargs)
MariusWirtz marked this conversation as resolved.
Show resolved Hide resolved
return int(response.text)

def get_all_leaf_element_identifiers(self, dimension_name: str, hierarchy_name: str,
**kwargs) -> CaseAndSpaceInsensitiveSet:
""" Get all element names and alias values for leaf elements in a hierarchy
Expand Down Expand Up @@ -652,6 +713,15 @@ def get_parents(self, dimension_name: str, hierarchy_name: str, element_name: st

return [record["Name"] for record in response.json()["value"]]

def get_parents_of_all_elements(self, dimension_name: str, hierarchy_name: str, **kwargs) -> Dict[str, List[str]]:
url = format_url(
f"/api/v1/Dimensions('{dimension_name}')/Hierarchies('{hierarchy_name}')/Elements?$select=Name"
f"&$expand=Parents($select=Name)",
)
response = self._rest.GET(url=url, **kwargs)

return {child["Name"]: [parent["Name"] for parent in child["Parents"]] for child in response.json()["value"]}

def get_element_principal_name(self, dimension_name: str, hierarchy_name: str, element_name: str, **kwargs) -> str:
element = self.get(dimension_name, hierarchy_name, element_name, **kwargs)
return element.name
Expand Down Expand Up @@ -770,4 +840,4 @@ def get_process_service(self):

def _get_hierarchy_service(self):
from TM1py import HierarchyService
return HierarchyService(self._rest)
return HierarchyService(self._rest)
90 changes: 90 additions & 0 deletions Tests/ElementService_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,23 @@ def test_get_leaf_element_names(self):
self.assertNotIn(self.extra_year, leaf_element_names)
self.assertNotIn("Total Years", leaf_element_names)

def test_get_consolidated_element_names(self):
consol_element_names = self.tm1.dimensions.hierarchies.elements.get_consolidated_element_names(
self.dimension_name,
self.hierarchy_name)
for consol in consol_element_names:
self.assertNotIn(consol, self.years)
self.assertIn("Total Years", consol_element_names)

def test_get_numeric_element_names(self):
numeric_element_names = self.tm1.dimensions.hierarchies.elements.get_numeric_element_names(
self.dimension_name,
self.hierarchy_name)
for elem in numeric_element_names:
self.assertIn(elem, self.years)
self.assertNotIn(self.extra_year, numeric_element_names)
self.assertNotIn("Total Years", numeric_element_names)

def test_get_leaf_elements(self):
leaf_elements = self.tm1.dimensions.hierarchies.elements.get_leaf_elements(
self.dimension_name,
Expand All @@ -240,6 +257,27 @@ def test_get_leaf_elements(self):
self.assertNotIn(self.extra_year, leaf_element_names)
self.assertNotIn("Total Year", leaf_element_names)

def test_get_numeric_elements(self):
numeric_elements = self.tm1.dimensions.hierarchies.elements.get_numeric_elements(
self.dimension_name,
self.hierarchy_name)
for elem in numeric_elements:
self.assertIn(elem.name, self.years)
self.assertNotEqual(elem.element_type, "Consolidated")
numeric_element_names = [element.name for element in numeric_elements]
self.assertNotIn(self.extra_year, numeric_element_names)
self.assertNotIn("Total Year", numeric_element_names)

def test_get_consolidated_elements(self):
consol_elements = self.tm1.dimensions.hierarchies.elements.get_consolidated_elements(
self.dimension_name,
self.hierarchy_name)
for consol in consol_elements:
self.assertNotIn(consol.name, self.years)
self.assertNotEqual(consol.element_type, "Numeric")
consol_element_names = [element.name for element in consol_elements]
self.assertIn("Total Year", consol_element_names)

def test_element_exists(self):
for year in self.years:
self.assertTrue(self.tm1.dimensions.hierarchies.elements.exists(
Expand Down Expand Up @@ -365,6 +403,50 @@ def test_get_number_of_consolidated_elements(self):

self.assertEqual(number_of_elements, 2)

def test_get_number_of_numeric_elements(self):
number_of_elements = self.tm1.dimensions.hierarchies.elements.get_number_of_numeric_elements(
self.dimension_name, self.hierarchy_name)

self.assertEqual(number_of_elements, 5)

def test_string_element_functions(self):
string_elem = 'string_element'
element = Element(string_elem, "String")
self.tm1.dimensions.hierarchies.elements.create(
self.dimension_name,
self.hierarchy_name,
element)

number_of_elements = self.tm1.dimensions.hierarchies.elements.get_number_of_string_elements(
self.dimension_name, self.hierarchy_name)
self.assertEqual(number_of_elements, 1)

string_element_names = self.tm1.dimensions.hierarchies.elements.get_string_element_names(
self.dimension_name,
self.hierarchy_name)
for elem in string_element_names:
self.assertIn(elem, [string_elem])
self.assertNotIn('Total Years', string_element_names)
self.assertNotIn("1989", string_element_names)

string_elements = self.tm1.dimensions.hierarchies.elements.get_string_elements(
self.dimension_name,
self.hierarchy_name)
for elem in string_elements:
self.assertIn(elem.name, [string_elem])
self.assertNotEqual(elem.element_type, "Consolidated")
self.assertNotEqual(elem.element_type, "Numeric")
string_element_names = [element.name for element in string_elements]
self.assertNotIn('1989', string_element_names)
self.assertNotIn("Total Year", string_element_names)

self.tm1.dimensions.hierarchies.elements.delete(
self.dimension_name,
self.hierarchy_name,
element.name)



def test_create_element_attribute(self):
element_attribute = ElementAttribute("NewAttribute", "String")
self.tm1.dimensions.hierarchies.elements.create_element_attribute(
Expand Down Expand Up @@ -616,6 +698,14 @@ def test_get_parents_not_existing(self):
hierarchy_name=self.hierarchy_name,
element_name="Not Existing Element")

def test_get_parents_of_all_elements_happy_case(self):
parents = self.tm1.elements.get_parents_of_all_elements(
dimension_name=self.dimension_name,
hierarchy_name=self.hierarchy_name
)

self.assertEqual(len(parents), 7)

def test_element_is_parent_dim_not_exist(self):
with self.assertRaises(TM1pyRestException):
self.tm1.elements.element_is_parent(dimension_name=self.dimension_does_not_exist_name,
Expand Down