Skip to content
This repository has been archived by the owner on Oct 3, 2020. It is now read-only.

Commit

Permalink
add repr
Browse files Browse the repository at this point in the history
  • Loading branch information
hjacobs committed Jul 22, 2019
1 parent c9084c9 commit 4bfdc73
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
11 changes: 9 additions & 2 deletions pykube/query.py
Expand Up @@ -17,10 +17,14 @@ class Table:
Tabular resource representation
See https://kubernetes.io/docs/reference/using-api/api-concepts/#receiving-resources-as-tables
"""
def __init__(self, obj: dict):
def __init__(self, api_obj_class, obj: dict):
assert obj['kind'] == 'Table'
self.api_obj_class = api_obj_class
self.obj = obj

def __repr__(self):
return "<Table of {kind} at {address}>".format(kind=self.api_obj_class.kind, address=hex(id(self)))

@property
def columns(self):
return self.obj['columnDefinitions']
Expand All @@ -39,6 +43,9 @@ def __init__(self, api, api_obj_class, namespace=None):
self.selector = everything
self.field_selector = everything

def __repr__(self):
return "<Query of {kind} at {address}>".format(kind=self.api_obj_class.kind, address=hex(id(self)))

def all(self):
return self._clone()

Expand Down Expand Up @@ -148,7 +155,7 @@ def as_table(self) -> Table:
See https://kubernetes.io/docs/reference/using-api/api-concepts/#receiving-resources-as-tables
"""
response = self.execute(headers={'Accept': 'application/json;as=Table;v=v1beta1;g=meta.k8s.io'})
return Table(response.json())
return Table(self.api_obj_class, response.json())

def iterator(self):
"""
Expand Down
13 changes: 11 additions & 2 deletions tests/test_query.py
Expand Up @@ -17,6 +17,11 @@ def test_get(api):
Query(api, Pod).get(namespace='myns')


def test_repr(api):
query = Query(api, Pod)
assert repr(query).startswith('<Query of Pod at ')


def test_get_one_object(api):
response = MagicMock()
response.json.return_value = {'items': [{'metadata': {'name': 'pod1'}}]}
Expand Down Expand Up @@ -70,8 +75,12 @@ def test_filter_selector_string(api):

def test_as_table(api):
response = MagicMock()
response.json.return_value = {'kind': 'Table'}
response.json.return_value = {'kind': 'Table', 'columnDefinitions': [], 'rows': []}
api.get.return_value = response

Query(api, Pod).filter(selector={'app': 'foo'}).as_table()
table = Query(api, Pod).filter(selector={'app': 'foo'}).as_table()
assert table.columns == []
assert table.rows == []
assert repr(table).startswith('<Table of Pod at')

api.get.assert_called_once_with(url='pods?labelSelector=app%3Dfoo', version='v1', headers={'Accept': 'application/json;as=Table;v=v1beta1;g=meta.k8s.io'})

0 comments on commit 4bfdc73

Please sign in to comment.