8000 bugfixes · srijan-deepsource/python-sasctl@308c551 · GitHub
[go: up one dir, main page]

Skip to content

Commit 308c551

Browse files
committed
bugfixes
1 parent f293f17 commit 308c551

File tree

4 files changed

+64
-12
lines changed

4 files changed

+64
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ Unreleased
22
----------
33
**Bugfixes**
44
- SSL warnings no longer repeatedly raised when `verify_ssl=False` but `CAS_CLIENT_SSL_CA_LIST` is specified.
5+
- `model_repository.delete_model_contents()` no longer fails when only one file is found.
56

67
**Improvements**
78
- All `delete_*()` service methods return `None` instead of empty string.
9+
- All `get_*()` service methods issue a warning if multiple items are found when retrieving by name.
810

911
v1.5.7 (2021-05-04)
1012
-------------------

src/sasctl/_services/model_repository.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,16 @@ def get_model_contents(cls, model):
144144
The code designated as the model's score code
145145
146146
"""
147+
147148
link = cls.get_model_link(model, 'contents', refresh=True)
149+
contents = cls.request_link(link, 'contents')
150+
151+
# By default, request_link() will unwrap a length-1 list.
152+
# If that happens, re-wrap so a list is always returned.
153+
if isinstance(contents, list):
154+
return contents
148155

149-
return cls.request_link(link, 'contents')
156+
return [contents]
150157

151158
@classmethod
152159
@sasctl_command('get', 'repositories')

src/sasctl/_services/service.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import logging
1010
import time
11+
import warnings
1112

1213
import six
1314
from six.moves.urllib_parse import quote
@@ -252,18 +253,27 @@ def get_item(cls, item, refresh=False):
252253
return cls.get(path + '/{id}'.format(id=item))
253254
results = list_items(cls, **get_filter(item))
254255

255-
# Not sure why, but as of 19w04 the filter doesn't seem to work
256+
match = None
256257
for result in results:
257258
if result['name'] == str(item):
258-
# Make a request for the specific object so that ETag
259-
# is included, allowing updates.
260-
if cls.get_link(result, 'self'):
261-
return cls.request_link(result, 'self')
262-
263-
id_ = result.get('id', result['name'])
264-
return cls.get(path + '/{id}'.format(id=id_))
265-
266-
return None
259+
# The first result that matches on name should be stored.
260+
# Will be returned after determining that there aren't additional
261+
# matches
262+
if match is None:
263+
# Make a request for the specific object so that ETag
264+
# is included, allowing updates.
265+
if cls.get_link(result, 'self'):
266+
match = cls.request_link(result, 'self')
267+
else:
268+
id_ = result.get('id', result['name'])
269+
match = cls.get(path + '/{id}'.format(id=id_))
270+
# We already found a match so this is a duplicate. Warn the user so they know
271+
# the item returned may not be the one they were expecting.
272+
else:
273+
warnings.warn("Multiple items found with name '%s'. Only the first result is returned." % item)
274+
break
275+
276+
return match
267277

268278
@sasctl_command('update')
269279
def update_item(cls, item):

tests/unit/test_model_repository.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
# SPDX-License-Identifier: Apache-2.0
66

77
import copy
8-
from six.moves import mock
8+
from unittest import mock
99

10+
import pytest
1011
from sasctl import current_session
1112
from sasctl.services import model_repository as mr
1213

@@ -121,3 +122,35 @@ def test_copy_analytic_store():
121122
obj, rel = args
122123
assert obj == get_model.return_value
123124
assert rel == 'copyAnalyticStore'
125+
126+
127+
def test_get_model_by_name():
128+
"""If multiple models exist with the same name, a warning should be raised.
129+
130+
From https://github.com/sassoftware/python-sasctl/issues/92
131+
"""
132+
133+
MODEL_NAME = 'Test Model'
134+
135+
# Create a dummy session
136+
with mock.patch('sasctl.core.requests.Session.request'):
137+
current_session('example.com', 'user', 'password')
138+
139+
mock_responses = [
140+
# First response is for list_items/list_models
141+
[
142+
{'id': 12345, 'name': MODEL_NAME},
143+
{'id': 67890, 'name': MODEL_NAME}
144+
],
145+
146+
# Second response is mock GET for model details
147+
{'id': 12345, 'name': MODEL_NAME}
148+
]
149+
150+
with mock.patch('sasctl._services.model_repository.ModelRepository.request') as request:
151+
request.side_effect = mock_responses
152+
153+
with pytest.warns(Warning):
154+
result = mr.get_model(MODEL_NAME)
155+
assert result['id']== 12345
156+
assert result['name'] == MODEL_NAME

0 commit comments

Comments
 (0)
0