From e3bc795d66d49960e9c22d11fba5b98bf9d2d48b Mon Sep 17 00:00:00 2001 From: MariusWirtz Date: Wed, 31 Jan 2024 11:22:03 +0100 Subject: [PATCH] Finalization FileService updates --- TM1py/Services/FileService.py | 28 ++++++++++++++++++-------- Tests/FileService_test.py | 37 ++++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/TM1py/Services/FileService.py b/TM1py/Services/FileService.py index 886325f3..20001aeb 100644 --- a/TM1py/Services/FileService.py +++ b/TM1py/Services/FileService.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import json +import warnings from typing import List, Iterable from TM1py.Services import RestService @@ -22,10 +23,21 @@ def __init__(self, tm1_rest: RestService): else: self.version_content_path = 'Blobs' - def get_names(self, return_list: bool=False, **kwargs) -> bytes: + def get_names(self, **kwargs) -> bytes: + warnings.warn( + f"Function get_names will be deprecated. Use get_all_names instead", + DeprecationWarning, + stacklevel=2) + + url = format_url( + "/Contents('{version_content_path}')/Contents?$select=Name", + version_content_path=self.version_content_path) + + return self._rest.GET(url, **kwargs).content + + def get_all_names(self, **kwargs) -> List[str]: """ return list of blob file names - :param return_list: True to return list, otherwise will return bytes """ url = format_url( @@ -34,7 +46,7 @@ def get_names(self, return_list: bool=False, **kwargs) -> bytes: response = self._rest.GET(url, **kwargs).content - return response if not return_list else [file['Name'] for file in json.loads(response)['value']] + return [file['Name'] for file in json.loads(response)['value']] def get(self, file_name: str, **kwargs) -> bytes: @@ -92,8 +104,8 @@ def delete(self, file_name: str, **kwargs): version_content_path=self.version_content_path) return self._rest.DELETE(url, **kwargs) - - def search_string_in_name(self, name_startswith: str = None, name_contains: Iterable = None, + + def search_string_in_name(self, name_startswith: str = None, name_contains: Iterable = None, name_contains_operator: str = 'and', **kwargs) -> List[str]: """ Return list of blob files that match search critera @@ -101,15 +113,15 @@ def search_string_in_name(self, name_startswith: str = None, name_contains: Iter :param name_contains: iterable, found anywhere in name (case insensitive) :param name_contains_operator: 'and' or 'or' """ - + url = format_url( "/Contents('{version_content_path}')/Contents?$select=Name", version_content_path=self.version_content_path) - + name_contains_operator = name_contains_operator.strip().lower() if name_contains_operator not in ("and", "or"): raise ValueError("'name_contains_operator' must be either 'AND' or 'OR'") - + name_filters = [] if name_startswith: diff --git a/Tests/FileService_test.py b/Tests/FileService_test.py index 9f587a79..a672b3b6 100644 --- a/Tests/FileService_test.py +++ b/Tests/FileService_test.py @@ -45,17 +45,36 @@ def test_update_get(self): self.assertEqual(original_file.read(), created_file) @skip_if_insufficient_version(version="11.4") - def test_get_names(self): - self.assertEqual('bytes', type(self.tm1.files.get_names())) - self.assertEqual('list', type(self.tm1.files.get_names(return_list=True))) + def test_get_all_names(self): + result = self.tm1.files.get_all_names() + self.assertIn(self.FILE_NAME1, result) @skip_if_insufficient_version(version="11.4") - def test_search_string_in_name(self): - self.assertTrue(self.FILE_NAME1 in self.tm1.files.search_string_in_name(name_starts_with=self.FILE_NAME1[:5])) - self.assertFalse(self.FILE_NAME1 in self.tm1.files.search_string_in_name(name_starts_with='not_the_file_im_looking_for')) - self.assertTrue(self.FILE_NAME1 in self.tm1.files.search_string_in_name(name_contains=[self.FILE_NAME1[:5], self.FILE_NAME1[-5:]])) - self.assertFalse(self.FILE_NAME1 in self.tm1.files.search_string_in_name(name_contains=[self.FILE_NAME1[:5], 'NotFound'])) - self.assertTrue(self.FILE_NAME1 in self.tm1.files.search_string_in_name(name_contains=[self.FILE_NAME1[:5], 'NotFound']), name_contains_operator='OR') + def test_search_string_in_name__name_startswith(self): + result = self.tm1.files.search_string_in_name(name_startswith=self.FILE_NAME1) + self.assertEqual([self.FILE_NAME1], result) + + @skip_if_insufficient_version(version="11.4") + def test_search_string_in_name__name_startswith_not_existing(self): + result = self.tm1.files.search_string_in_name(name_startswith='not_the_file_im_looking_for') + self.assertEqual([], result) + + @skip_if_insufficient_version(version="11.4") + def test_search_string_in_name__name_contains_both_existing(self): + result = self.tm1.files.search_string_in_name(name_contains=[self.FILE_NAME1[:5], self.FILE_NAME1[-5:]]) + self.assertEqual([self.FILE_NAME1], result) + + @skip_if_insufficient_version(version="11.4") + def test_search_string_in_name__name_contains_mixed_existing_and(self): + result = self.tm1.files.search_string_in_name(name_contains=[self.FILE_NAME1[:5], 'NotFound']) + self.assertEqual([], result) + + @skip_if_insufficient_version(version="11.4") + def test_search_string_in_name__name_contains_mixed_existing_or(self): + result = self.tm1.files.search_string_in_name( + name_contains=[self.FILE_NAME1, 'NotFound'], + name_contains_operator='OR') + self.assertEqual([self.FILE_NAME1], result) @skip_if_insufficient_version(version="11.4") def test_delete_exists(self):