Skip to content
Open
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
23 changes: 15 additions & 8 deletions TM1py/Services/ViewService.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

import collections
from typing import List, Tuple, Union
from typing import List, Tuple, Union, Iterable, Optional

from requests import Response

Expand Down Expand Up @@ -61,17 +61,17 @@ def exists(self, cube_name: str, view_name: str, private: bool = None, **kwargs)
raise e
return tuple(view_types.values())

def get(self, cube_name: str, view_name: str, private: bool = False, **kwargs) -> View:
def get(self, cube_name: str, view_name: str, private: bool = False, element_properties: Optional[Iterable[str]] = ('Name',), **kwargs) -> View:
view_type = "PrivateViews" if private else "Views"
url = format_url("/Cubes('{}')/{}('{}')?$expand=*", cube_name, view_type, view_name)
response = self._rest.GET(url, **kwargs)
view_as_dict = response.json()
if "MDX" in view_as_dict:
return MDXView(cube_name=cube_name, view_name=view_name, MDX=view_as_dict["MDX"])
else:
return self.get_native_view(cube_name=cube_name, view_name=view_name, private=private)
return self.get_native_view(cube_name=cube_name, view_name=view_name, element_properties=element_properties, private=private)

def get_native_view(self, cube_name: str, view_name: str, private=False, **kwargs) -> NativeView:
def get_native_view(self, cube_name: str, view_name: str, private=False, element_properties: Optional[Iterable[str]] = ('Name',), **kwargs) -> NativeView:
""" Get a NativeView from TM1 Server

:param cube_name: string, name of the cube
Expand All @@ -81,19 +81,26 @@ def get_native_view(self, cube_name: str, view_name: str, private=False, **kwarg
:return: instance of TM1py.NativeView
"""
view_type = "PrivateViews" if private else "Views"

if element_properties:
element_properties = ",".join(element_properties)
element_properties = f',Elements($select={element_properties})'
else:
element_properties = ''

url = format_url(
"/Cubes('{}')/{}('{}')?$expand="
"tm1.NativeView/Rows/Subset($expand=Hierarchy($select=Name;"
"$expand=Dimension($select=Name)),Elements($select=Name);"
"$expand=Dimension($select=Name)){};"
"$select=Expression,UniqueName,Name, Alias), "
"tm1.NativeView/Columns/Subset($expand=Hierarchy($select=Name;"
"$expand=Dimension($select=Name)),Elements($select=Name);"
"$expand=Dimension($select=Name)){};"
"$select=Expression,UniqueName,Name,Alias), "
"tm1.NativeView/Titles/Subset($expand=Hierarchy($select=Name;"
"$expand=Dimension($select=Name)),Elements($select=Name);"
"$expand=Dimension($select=Name)){};"
"$select=Expression,UniqueName,Name,Alias), "
"tm1.NativeView/Titles/Selected($select=Name)",
cube_name, view_type, view_name)
cube_name, view_type, view_name, element_properties, element_properties, element_properties)
response = self._rest.GET(url, **kwargs)
native_view = NativeView.from_json(response.text, cube_name)
return native_view
Expand Down
35 changes: 18 additions & 17 deletions Tests/ViewService_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,24 @@ def test_get_all_views(self):
self.assertEqual(len(private_views), len(private_view_names))

def test_get_native_view(self):
for private in (True, False):
# generic get
view = self.tm1.views.get(
cube_name=self.cube_name,
view_name=self.native_view_name,
private=private)

# get native view
native_view = self.tm1.views.get_native_view(
cube_name=self.cube_name,
view_name=self.native_view_name,
private=private)

self.assertIsInstance(view, NativeView)
self.assertEqual(view.name, self.native_view_name)
self.assertIsInstance(native_view, NativeView)
self.assertEqual(view, native_view)
for element_properties in [(), ('Name',), ('Name', 'Index')]:
for private in (True, False):
# generic get
view = self.tm1.views.get(
cube_name=self.cube_name,
view_name=self.native_view_name,
private=private)

# get native view
native_view = self.tm1.views.get_native_view(
cube_name=self.cube_name,
view_name=self.native_view_name,
private=private)

self.assertIsInstance(view, NativeView)
self.assertEqual(view.name, self.native_view_name)
self.assertIsInstance(native_view, NativeView)
self.assertEqual(view, native_view)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here we're not actually testing that the response is different depending on the element_properties provided.
I think it would make sense to create new, separate test cases for common element_properties values.

If we leave this test as is, it will provide good confirmation that the default behavior hasn't changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay, that makes sense. In that case I may need some help with creating the tests. I am unsure how these self.assert functions work/how to use them for this pull request


def test_get_mdx_view(self):
for private in (True, False):
Expand Down