Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amalfra committed May 18, 2019
1 parent 4be8ef4 commit 88c9771
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
22 changes: 18 additions & 4 deletions src/commands/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,26 @@
def run(docker_hub_client, args):
""" The command to list tags for given repo on docker hub
>>> from ..tests.docker_hub_client import TestingDockerHubClient
>>> from ..tests.docker_hub_client import \
NoResultsTestingDockerHubClient, WithResultsTestingDockerHubClient
>>> from collections import namedtuple
>>> args = namedtuple('args', 'orgname reponame page')
>>> docker_hub_client = TestingDockerHubClient()
>>> args = namedtuple('args', 'orgname reponame page format')
>>> docker_hub_client = NoResultsTestingDockerHubClient()
>>> run(docker_hub_client,
... args(orgname='docker', reponame='docker', page='1'))
... args(orgname='docker', reponame='docker', page='1',
... format='json'))
This repo has no tags
>>> docker_hub_client = WithResultsTestingDockerHubClient()
>>> run(docker_hub_client,
... args(orgname='docker', reponame='docker', page='1',
... format='json'))
[
{
"Last updated": "2018-12-12 14:40",
"Name": "1.4.2-alpine",
"Size": "15.09 MB"
}
]
"""
config = Config()
orgname = args.orgname or config.get('orgname')
Expand Down
2 changes: 1 addition & 1 deletion src/libs/docker_hub_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def do_request(self, url, method='GET', data={}):
headers['Authorization'] = 'JWT ' + self.auth_token
request_method = getattr(requests, method.lower())
if len(data) > 0:
data = json.dumps(data)
data = json.dumps(data, indent=2, sort_keys=True)
resp = request_method(url, data, headers=headers)
else:
resp = request_method(url, headers=headers)
Expand Down
3 changes: 2 additions & 1 deletion src/libs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def print_result(format, rows=[], header=[], count=0, page=1, heading=False,
(count, page, total_pages))
print_table(header, rows)
else:
json_result = json.dumps([dict(zip(header, row)) for row in rows])
json_result = json.dumps([dict(zip(header, row)) for row in rows],
indent=2, sort_keys=True)
print(json_result)


Expand Down
13 changes: 12 additions & 1 deletion src/tests/docker_hub_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@
from ..libs.docker_hub_client import DockerHubClient


class TestingDockerHubClient(DockerHubClient):
class BaseTestingDockerHubClient(DockerHubClient):
""" Fake wrapper to simulate communication with docker hub API """

def _fake_login(self):
return {'token': 'random-token'}


class NoResultsTestingDockerHubClient(BaseTestingDockerHubClient):
def do_request(self, url, method='GET', data={}):
content = {'count': 0}
if 'login' in url:
content = self._fake_login()
return {'content': content, 'code': 200}


class WithResultsTestingDockerHubClient(BaseTestingDockerHubClient):
def do_request(self, url, method='GET', data={}):
content = {'count': 1, 'results': [{'last_updated': '2018-12-12 14:40',
'name': '1.4.2-alpine', 'full_size': 15820065}]}
if 'login' in url:
content = self._fake_login()
return {'content': content, 'code': 200}

0 comments on commit 88c9771

Please sign in to comment.