Skip to content

Commit 5a93926

Browse files
authored
Merge pull request #63 from keboola/PS-2218-and-tags
PS-2218 Support AND tags for import files to workspace
2 parents b4e8255 + 2726d11 commit 5a93926

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

kbcstorage/workspaces.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ def load_files(self, workspace_id, file_mapping):
155155
Args:
156156
workspace_id (int or str): The id of the workspace to which to load
157157
the tables.
158-
file_mapping (:obj:`dict`): contains tags: [], destination: string path without trailing /
158+
file_mapping (:obj:`dict`):
159+
tags: [],
160+
operator: enum('or', 'and') default or,
161+
destination: string path without trailing /
159162
160163
Raises:
161164
requests.HTTPError: If the API request fails.
@@ -164,7 +167,12 @@ def load_files(self, workspace_id, file_mapping):
164167
if (workspace['type'] != 'file' and workspace['connection']['backend'] != 'abs'):
165168
raise Exception('Loading files to workspace is only available for ABS workspaces')
166169
files = Files(self.root_url, self.token)
167-
file_list = files.list(tags=file_mapping['tags'])
170+
if ('operator' in file_mapping and file_mapping['operator'] == 'and'):
171+
query = ' AND '.join(map(lambda tag: 'tags:"' + tag + '"', file_mapping['tags']))
172+
file_list = files.list(q=query)
173+
else:
174+
file_list = files.list(tags=file_mapping['tags'])
175+
168176
jobs = Jobs(self.root_url, self.token)
169177
jobs_list = []
170178
for file in file_list:

tests/functional/test_workspaces.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import warnings
66

77
from azure.storage.blob import BlobServiceClient
8+
from azure.core.exceptions import ResourceNotFoundError
89
from requests import exceptions
910
from kbcstorage.buckets import Buckets
1011
from kbcstorage.jobs import Jobs
@@ -134,6 +135,32 @@ def test_load_files_to_workspace(self):
134135
)
135136
self.assertEqual('fooBar', blob_client_2.download_blob().readall().decode('utf-8'))
136137

138+
# now let's test that we can use the 'and' operator. in this case file2 should not get loaded
139+
self.workspaces.load_files(
140+
workspace['id'],
141+
{
142+
'tags': ['sapi-client-python-tests', 'file1'],
143+
'operator': 'and',
144+
'destination': 'data/in/and_files'
145+
}
146+
)
147+
# file 1 should be there
148+
blob_client_1 = blob_service_client.get_blob_client(
149+
container=workspace['connection']['container'],
150+
blob='data/in/and_files/%s/%s' % (file1['name'], str(file1['id']))
151+
)
152+
self.assertEqual('fooBar', blob_client_1.download_blob().readall().decode('utf-8'))
153+
154+
# file 2 should not
155+
blob_client_2 = blob_service_client.get_blob_client(
156+
container=workspace['connection']['container'],
157+
blob='data/in/and_files/%s/%s' % (file2['name'], str(file2['id']))
158+
)
159+
with self.assertRaises(ResourceNotFoundError) as context:
160+
blob_client_2.download_blob().readall().decode('utf-8')
161+
162+
self.assertTrue('The specified blob does not exist' in str(context.exception))
163+
137164
def __create_table(self, bucket_id, table_name, row):
138165
file, path = tempfile.mkstemp(prefix='sapi-test')
139166
with open(path, 'w') as csv_file:

0 commit comments

Comments
 (0)