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

Commit

Permalink
test Query class
Browse files Browse the repository at this point in the history
  • Loading branch information
hjacobs committed Mar 14, 2019
1 parent 6ff8217 commit d3ce340
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ dist/
.pytest_cache/
.tox/
docs/_build
htmlcov/
3 changes: 3 additions & 0 deletions pykube/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def get_by_name(self, name):
return self.api_obj_class(self.api, r.json())

def get(self, *args, **kwargs):
'''
Get a single object by name, namespace, label, ..
'''
if "name" in kwargs:
return self.get_by_name(kwargs["name"])
clone = self.filter(*args, **kwargs)
Expand Down
42 changes: 42 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest

from unittest.mock import MagicMock

from pykube import Pod, ObjectDoesNotExist

from pykube.query import Query


@pytest.fixture
def api():
return MagicMock()


def test_get(api):
with pytest.raises(ObjectDoesNotExist):
Query(api, Pod).get(namespace='myns')


def test_filter_by_namespace(api):
Query(api, Pod).filter(namespace='myns').execute()
api.get.assert_called_once_with(namespace='myns', url='pods', version='v1')


def test_filter_by_labels_eq(api):
Query(api, Pod).filter(selector={'application': 'myapp', 'component': 'backend'}).execute()
api.get.assert_called_once_with(url='pods?labelSelector=application%3Dmyapp%2Ccomponent%3Dbackend', version='v1')


def test_filter_by_labels_neq(api):
Query(api, Pod).filter(selector={'application__neq': 'myapp'}).execute()
api.get.assert_called_once_with(url='pods?labelSelector=application+%21%3D+myapp', version='v1')


def test_filter_by_labels_in(api):
Query(api, Pod).filter(selector={'application__in': ['foo', 'bar']}).execute()
api.get.assert_called_once_with(url='pods?labelSelector=application+in+%28bar%2Cfoo%29', version='v1')


def test_filter_by_labels_notin(api):
Query(api, Pod).filter(selector={'application__notin': ['foo', 'bar']}).execute()
api.get.assert_called_once_with(url='pods?labelSelector=application+notin+%28bar%2Cfoo%29', version='v1')

0 comments on commit d3ce340

Please sign in to comment.