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

Add tests for new search functions #660

Merged
merged 1 commit into from
Jan 6, 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
40 changes: 21 additions & 19 deletions TM1py/Services/CubeService.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import json
import random
from typing import List, Iterable
from typing import List, Iterable, Dict

from requests import Response

Expand Down Expand Up @@ -189,40 +189,42 @@ def get_dimension_names(self, cube_name: str, skip_sandbox_dimension: bool = Tru
return dimension_names[1:]
return dimension_names

def get_names_with_dimension(self, dimension_name: str, skip_control_cubes: bool = False,
**kwargs) -> List[str]:
def search_for_dimension(self, dimension_name: str, skip_control_cubes: bool = False,
**kwargs) -> List[str]:
""" Ask TM1 Server for list of cube names that contain specific dimension

:param dimension_name: string, valid dimension name (case insensitive)
:param skip_control_cubes: bool, True will exclude control cubes from result
"""
cube_prefix = 'Cubes' if skip_control_cubes == False else 'ModelCubes()'
url = format_url(
"/api/v1/{}?$select=Name&$filter=Dimensions/any(d: toupper(d/Name) eq toupper('{}'))",
cube_prefix, dimension_name
)
"/api/v1/{}?$select=Name&$filter=Dimensions/any(d: replace(tolower(d/Name), ' ', '') eq '{}')",
'ModelCubes()' if skip_control_cubes else 'Cubes',
dimension_name.lower().replace(' ', '')
)
response = self._rest.GET(url, **kwargs)
cubes = list(entry['Name'] for entry in response.json()['value'])
return cubes

def search_for_dimension_substring(self, substring: str, skip_control_cubes: bool = False,
**kwargs) -> Dict[str, List[str]]:
""" Ask TM1 Server for a dictinary of cube names with the dimension whose name contains the substring

def get_names_with_dimension_wildcard(self, wildcard: str, skip_control_cubes: bool = False,
**kwargs) -> List[str]:
""" Ask TM1 Server for list of cube names that contain dimension whose name contains wildcard

:param wildcard: string, wildcard to search for in dim name
:param substring: string to search for in dim name
:param skip_control_cubes: bool, True will exclude control cubes from result
"""
cube_prefix = 'Cubes' if skip_control_cubes == False else 'ModelCubes()'
substring = substring.lower().replace(' ', '')

url = format_url(
"/api/v1/{}?$select=Name&$filter=Dimensions/any(d: contains(toupper(d/Name),toupper('{}')))" \
"&$expand=Dimensions($select=Name;$filter=contains(toupper(Name),toupper('{}')))",
cube_prefix, wildcard, wildcard
)
"/api/v1/{}?$select=Name&$filter=Dimensions/any(d: contains(replace(tolower(d/Name), ' ', ''),'{}'))" +
"&$expand=Dimensions($select=Name;$filter=contains(replace(tolower(Name), ' ', ''), '{}'))",
'ModelCubes()' if skip_control_cubes else 'Cubes',
substring,
substring)

response = self._rest.GET(url, **kwargs)
cube_dict = {entry['Name']:[dim['Name'] for dim in entry['Dimensions']] for entry in response.json()['value']}
cube_dict = {entry['Name']: [dim['Name'] for dim in entry['Dimensions']] for entry in response.json()['value']}
return cube_dict

@require_version(version="11.4")
def get_storage_dimension_order(self, cube_name: str, **kwargs) -> List[str]:
""" Get the storage dimension order of a cube
Expand Down
11 changes: 7 additions & 4 deletions TM1py/Services/ProcessService.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,18 @@ def search_string_in_name(self, name_startswith: str = None, name_contains: Iter
if name_contains:
if isinstance(name_contains, str):
name_filters.append(format_url("contains(toupper(Name),toupper('{}'))", name_contains))
else:

elif isinstance(name_contains, Iterable):
name_contains_filters = [format_url("contains(toupper(Name),toupper('{}'))", wildcard)
for wildcard in name_contains]
name_filters.append("({})".format(f" {name_contains_operator} ".join(name_contains_filters)))

url += "&$filter={}".format(" and ".join(name_filters))
else:
raise ValueError("'name_contains' must be str or iterable")

url += "&$filter={}".format(f" and ".join(name_filters))
response = self._rest.GET(url, **kwargs)
processes = list(process['Name'] for process in response.json()['value'])
return processes
return list(process['Name'] for process in response.json()['value'])

def create(self, process: Process, **kwargs) -> Response:
""" Create a new process on TM1 Server
Expand Down
40 changes: 40 additions & 0 deletions Tests/CubeService_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,46 @@ def test_get_storage_dimension_order(self):
dimensions = self.tm1.cubes.get_storage_dimension_order(cube_name=self.cube_name)
self.assertEqual(dimensions, self.dimension_names)

def test_search_for_dimension_happy_case(self):
cube_names = self.tm1.cubes.search_for_dimension(self.dimension_names[0])
self.assertEqual([self.cube_name], cube_names)

def test_search_for_dimension_no_match(self):
cube_names = self.tm1.cubes.search_for_dimension("NotADimensionName")
self.assertEqual([], cube_names)

def test_search_for_dimension_case_insensitive(self):
cube_names = self.tm1.cubes.search_for_dimension(self.dimension_names[1].upper())
self.assertEqual([self.cube_name], cube_names)

def test_search_for_dimension_space_insensitive(self):
cube_names = self.tm1.cubes.search_for_dimension(" " + self.dimension_names[2] + " ")
self.assertEqual([self.cube_name], cube_names)

def test_search_for_dimension_substring_happy_case(self):
cubes = self.tm1.cubes.search_for_dimension_substring(substring=self.dimension_names[0])
self.assertEqual({self.cube_name: [self.dimension_names[0]]}, cubes)

def test_search_for_dimension_substring_case_insensitive(self):
cubes = self.tm1.cubes.search_for_dimension_substring(substring=self.dimension_names[1].upper())
self.assertEqual({self.cube_name: [self.dimension_names[1]]}, cubes)

def test_search_for_dimension_substring_space_insensitive(self):
cubes = self.tm1.cubes.search_for_dimension_substring(substring=" " + self.dimension_names[2] + " ")
self.assertEqual({self.cube_name: [self.dimension_names[2]]}, cubes)

def test_search_for_dimension_substring_no_match(self):
cubes = self.tm1.cubes.search_for_dimension_substring(substring="NotADimensionName")
self.assertEqual({}, cubes)

def test_search_for_dimension_substring_skip_control_cubes_true(self):
cubes = self.tm1.cubes.search_for_dimension_substring(substring="}cubes", skip_control_cubes=True)
self.assertEqual({}, cubes)

def test_search_for_dimension_substring_skip_control_cubes_false(self):
cubes = self.tm1.cubes.search_for_dimension_substring(substring="}cubes", skip_control_cubes=False)
self.assertEqual(cubes['}CubeProperties'], ['}Cubes'])

def test_get_number_of_cubes(self):
number_of_cubes = self.tm1.cubes.get_number_of_cubes()
self.assertIsInstance(number_of_cubes, int)
Expand Down
26 changes: 26 additions & 0 deletions Tests/ProcessService_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,32 @@ def test_delete_process(self):
self.tm1.processes.create(process)
self.tm1.processes.delete(process.name)

def test_search_string_in_name_no_match_startswith(self):
process_names = self.tm1.processes.search_string_in_name(
name_startswith="NotAProcessName")
self.assertEqual([], process_names)

def test_search_string_in_name_no_match_contains(self):
process_names = self.tm1.processes.search_string_in_name(
name_contains="NotAProcessName")
self.assertEqual([], process_names)

def test_search_string_in_name_startswith_happy_case(self):
process_names = self.tm1.processes.search_string_in_name(name_startswith=self.p_ascii.name)
self.assertEqual([self.p_ascii.name], process_names)

def test_search_string_in_name_contains_happy_case(self):
process_names = self.tm1.processes.search_string_in_name(name_contains=self.p_ascii.name)
self.assertEqual([self.p_ascii.name], process_names)

def test_search_string_in_name_contains_multiple(self):
process_names = self.tm1.processes.search_string_in_name(name_contains=self.p_ascii.name.split("_"))
self.assertEqual([self.p_ascii.name], process_names)

def test_search_string_in_code(self):
process_names = self.tm1.processes.search_string_in_code("sTestProlog = 'test prolog procedure'")
self.assertEqual([self.p_ascii.name], process_names)

@classmethod
def tearDownClass(cls):
cls.tm1.dimensions.subsets.delete(
Expand Down