Skip to content

Commit

Permalink
execute_mdx_cell_values_only
Browse files Browse the repository at this point in the history
- Execute_mdx_cell_values_only function to query cell-values only.
- Test improvements
  • Loading branch information
MariusWirtz committed Jun 4, 2018
1 parent 154f58f commit 1d3de44
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
15 changes: 11 additions & 4 deletions TM1py/Services/CellService.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,21 @@ def execute_mdx(self, mdx, cell_properties=None, top=None):
'Cells($select={}{})'.format(';$top=' + str(top) if top else '',
','.join(cell_properties),
';$top=' + str(top) if top else '')
data = {
'MDX': mdx
}
response = self._rest.POST(request=request, data=json.dumps(data, ensure_ascii=False))
response = self._rest.POST(request=request, data=json.dumps({'MDX': mdx}, ensure_ascii=False))
return Utils.build_content_from_cellset(raw_cellset_as_dict=response.json(),
cell_properties=cell_properties,
top=top)

def execute_mdx_cell_values_only(self, mdx):
""" Optimized for performance. Query only raw values. Coordinates are omitted.
:param mdx: a Valid MDX Query
:return: Generator of cell values
"""
request = '/api/v1/ExecuteMDX?$expand=Cells($select=Value)'
response = self._rest.POST(request=request, data=json.dumps({'MDX': mdx}, ensure_ascii=False))
return (cell["Value"] for cell in response.json()["Cells"])

def execute_view(self, cube_name, view_name, cell_properties=None, private=True, top=None):
""" get view content as dictionary with sweet and concise structure.
Works on NativeView and MDXView !
Expand Down
53 changes: 38 additions & 15 deletions Tests/Cell.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import random
import os
from functools import reduce
import unittest
import uuid
import configparser
Expand Down Expand Up @@ -93,20 +94,12 @@ def test4_execute_mdx(self):
"NON EMPTY [" + dimension_names[2] + "].MEMBERS ON COLUMNS " \
"FROM [" + cube_name + "]"
data = self.tm1.cubes.cells.execute_mdx(mdx)
# Check if total value is the same AND coordinates are the same
check_value = 0
for coordinates, value in data.items():
# grid can have null values in cells as rows and columns are populated with elements
if value['Value']:
# extract the element name from the element unique name
element_names = Utils.element_names_from_element_unqiue_names(coordinates)
self.assertIn(element_names, self.target_coordinates)
check_value += value['Value']
# Check the check-sum
self.assertEqual(check_value, self.total_value)
# Check if total value is the same AND coordinates are the same. Handle None
self.assertEqual(self.total_value,
sum([v["Value"] for v in data.values() if v["Value"]]))

# Define MDX Query with calculated MEMBER
mdx = "WITH MEMBER[{}].[{}] AS 1 " \
mdx = "WITH MEMBER[{}].[{}] AS 2 " \
"SELECT[{}].MEMBERS ON ROWS, " \
"NON EMPTY {{[{}].[{}]}} ON COLUMNS " \
"FROM[{}] " \
Expand All @@ -115,8 +108,38 @@ def test4_execute_mdx(self):

data = self.tm1.cubes.cells.execute_mdx(mdx)
self.assertEqual(1000, len(data))
data = self.tm1.cubes.cells.execute_mdx(mdx)
self.assertEqual(2000, sum(v["Value"] for v in data.values()))
self.assertEqual(
sum(range(0, 1000)),
sum(v["Ordinal"] for v in data.values()))

def test5_execute_view(self):
def test5_execute_mdx_cell_values_only(self):
mdx = "SELECT " \
"NON EMPTY [" + dimension_names[0] + "].Members * [" + dimension_names[1] + "].Members ON ROWS," \
"NON EMPTY [" + dimension_names[2] + "].MEMBERS ON COLUMNS " \
"FROM [" + cube_name + "]"

cell_values = self.tm1.cubes.cells.execute_mdx_cell_values_only(mdx)

# Check if total value is the same AND coordinates are the same. Handle None.
self.assertEqual(self.total_value,
sum([v for v in cell_values if v]))

# Define MDX Query with calculated MEMBER
mdx = "WITH MEMBER[{}].[{}] AS 2 " \
"SELECT[{}].MEMBERS ON ROWS, " \
"NON EMPTY {{[{}].[{}]}} ON COLUMNS " \
"FROM[{}] " \
"WHERE([{}].DefaultMember)".format(dimension_names[1], "Calculated Member", dimension_names[0],
dimension_names[1], "Calculated Member", cube_name, dimension_names[2])

data = self.tm1.cubes.cells.execute_mdx_cell_values_only(mdx)
self.assertEqual(1000, len(list(data)))
data = self.tm1.cubes.cells.execute_mdx_cell_values_only(mdx)
self.assertEqual(2000, sum(data))

def test6_execute_view(self):
data = self.tm1.cubes.cells.execute_view(cube_name, view_name, private=False)
# Check if total value is the same AND coordinates are the same
check_value = 0
Expand All @@ -130,11 +153,11 @@ def test5_execute_view(self):
# Check the check-sum
self.assertEqual(check_value, self.total_value)

def test6_execute_view(self):
def test7_execute_view(self):
data = self.tm1.cubes.cells.execute_view(cube_name, view_name, private=False, top=3)
self.assertEqual(len(data.keys()), 3)

def test7_write_values_through_cellset(self):
def test8_write_values_through_cellset(self):
mdx_skeleton = "SELECT {} ON ROWS, {} ON COLUMNS FROM {} WHERE ({})"
mdx = mdx_skeleton.format(
"{{[{}].[{}]}}".format(dimension_names[0], "element2"),
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from setuptools import setup


SCHEDULE_VERSION = '1.0.1'
SCHEDULE_VERSION = '1.0.2'
SCHEDULE_DOWNLOAD_URL = (
'https://github.com/Cubewise-code/TM1py/tarball/' + SCHEDULE_VERSION
)
Expand Down

0 comments on commit 1d3de44

Please sign in to comment.