Skip to content

Commit efa667f

Browse files
committed
Added __repr__
1 parent e22d0e2 commit efa667f

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

test_wls_rest_python.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,29 @@ def test_wls_init_nondefault_timeout():
105105
assert wls.timeout == 832
106106

107107

108+
def test_wls_repr():
109+
collection = {
110+
"version": "12.2.1.3",
111+
"isLatest": True,
112+
"lifecycle": "active",
113+
"links": [
114+
{"rel": "edit", "href": "https://edit-link"},
115+
{"rel": "domainRuntime", "href": "https://domainruntime-link"},
116+
],
117+
}
118+
with requests_mock.mock() as r:
119+
r.get(
120+
"https://wls.example.com:7001/management/weblogic/latest", json=collection
121+
)
122+
wls = wls_rest_python.WLS(
123+
"https://wls.example.com:7001", "weblogic", "Welcome1"
124+
)
125+
assert (
126+
repr(wls)
127+
== "<WLS url='https://wls.example.com:7001/management/weblogic/latest' username='weblogic' version='12.2.1.3'>"
128+
)
129+
130+
108131
def test_wls_get():
109132
fake_wls = MagicMock(spec=wls_rest_python.WLS)
110133
fake_wls.timeout = 372
@@ -567,6 +590,20 @@ def test_wls_object_without_len():
567590
len(wls_obj)
568591

569592

593+
def test_wls_object_repr():
594+
collection = {
595+
"links": [{"rel": "self", "href": "https://self-link"}],
596+
"attr1": False,
597+
"attr2": 9,
598+
"attr3": -7,
599+
"name": "what",
600+
}
601+
fake_wls = MagicMock()
602+
fake_wls.get = MagicMock(return_value=collection)
603+
wls_obj = wls_rest_python.WLSObject("name", "https://url", fake_wls)
604+
assert repr(wls_obj) == "<WLSObject name='name' url='https://url'>"
605+
606+
570607
def test_wls_object_empty_items():
571608
# If there is an empty item array in the response,
572609
# it means it is iterable, only withouth objects
@@ -651,3 +688,9 @@ def test_wls_action_async():
651688
action_answer = wls_action(prefer_async=True)
652689
fake_wls.post.assert_called_once_with("https://url", True, json={})
653690
assert isinstance(action_answer, MagicMock)
691+
692+
693+
def test_wls_action_repr():
694+
fake_wls = MagicMock()
695+
wls_action = wls_rest_python.WLSAction("name", "https://url", fake_wls)
696+
assert repr(wls_action) == "<WLSAction name='name' url='https://url'>"

wls_rest_python.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ def __init__(
128128
link_obj = WLSObject(link["rel"], link["href"], self)
129129
setattr(self, link["rel"], link_obj)
130130

131+
def __repr__(self):
132+
return "<WLS url='{}' username='{}' version='{}'>".format(
133+
self.base_url, self.session.auth[0], self.version
134+
)
135+
131136
def get(self, url, **kwargs):
132137
"""
133138
Does a GET request to the specified URL.
@@ -357,6 +362,9 @@ def __len__(self):
357362

358363
raise TypeError("object of type '{}' has no len()".format(self._name))
359364

365+
def __repr__(self):
366+
return "<WLSObject name='{}' url='{}'>".format(self._name, self._url)
367+
360368
def delete(self, prefer_async=False, **kwargs):
361369
"""
362370
Deletes the resource. Will result in an DELETE request to the self url
@@ -419,5 +427,8 @@ def __init__(self, name, url, wls):
419427
self._name = name
420428
self._wls = wls
421429

430+
def __repr__(self):
431+
return "<WLSAction name='{}' url='{}'>".format(self._name, self._url)
432+
422433
def __call__(self, prefer_async=False, **kwargs):
423434
return self._wls.post(self._url, prefer_async, json=kwargs if kwargs else {})

0 commit comments

Comments
 (0)