Skip to content

Commit

Permalink
add tests for users command
Browse files Browse the repository at this point in the history
  • Loading branch information
amalfra committed Jun 25, 2022
1 parent e84b9f4 commit c1f4828
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# -*- encoding: utf-8 -*-
1 change: 1 addition & 0 deletions src/tests/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# -*- encoding: utf-8 -*-
File renamed without changes.
77 changes: 77 additions & 0 deletions src/tests/commands/test_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# -*- encoding: utf-8 -*-
"""
Tests users command
"""
import json
from collections import namedtuple

from src.commands.users import run
from ..docker_hub_client import \
NoResultsUsersTestingDockerHubClient, WithUserResultsTestingDockerHubClient
from ..helpers import generate_results


Args = namedtuple('args', 'username page format all_pages')


def test_no_users(capsys):
""" When there is no user profile found by API """
docker_hub_client = NoResultsUsersTestingDockerHubClient()
username = 'test'
run(
docker_hub_client,
Args(
username, page=1, format='json',
all_pages=False
)
)

captured = capsys.readouterr()
assert captured.out == f'Error fetching profile for: {username}\n'

def test_no_users_and_all_pages(capsys):
""" When all_page applied during no user found by API """
docker_hub_client = NoResultsUsersTestingDockerHubClient()
username = 'test'
run(
docker_hub_client,
Args(
username, page=1, format='json',
all_pages=True
)
)

captured = capsys.readouterr()
assert captured.out == f'Error fetching profile for: {username}\n'

def test_with_users(capsys):
""" When there is user returned API """
docker_hub_client = WithUserResultsTestingDockerHubClient()
username = 'test'
run(
docker_hub_client,
Args(
username, page=1, format='json',
all_pages=False
)
)
results = generate_results(1)

captured = capsys.readouterr()
assert json.loads(captured.out) == results

def test_with_users_and_all_pages(capsys):
""" When all_pageas applied there is user returned API """
docker_hub_client = WithUserResultsTestingDockerHubClient()
username = 'test'
run(
docker_hub_client,
Args(
username, page=1, format='json',
all_pages=True
)
)
results = generate_results(1)

captured = capsys.readouterr()
assert json.loads(captured.out) == results
14 changes: 13 additions & 1 deletion src/tests/docker_hub_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""
Tests for Dockerhub API client
"""
from ..libs.docker_hub_client import DockerHubClient
from src.libs.docker_hub_client import DockerHubClient
from .helpers import generate_results


Expand All @@ -20,6 +20,10 @@ def do_request(self, url, method='GET', data=None):
content = self._fake_login()
return {'content': content, 'code': 200}

class NoResultsUsersTestingDockerHubClient(BaseTestingDockerHubClient):
""" When API returns no results for users endpoint """
def do_request(self, url, method='GET', data=None):
return {'content': None, 'code': 404}

class WithResultsTestingDockerHubClient(BaseTestingDockerHubClient):
""" When API returns results """
Expand All @@ -32,3 +36,11 @@ def do_request(self, url, method='GET', data=None):
if 'login' in url:
content = self._fake_login()
return {'content': content, 'code': 200}

class WithUserResultsTestingDockerHubClient(BaseTestingDockerHubClient):
""" When API returns user result """
def do_request(self, url, method='GET', data=None):
content = generate_results(1)[0]
if 'login' in url:
content = self._fake_login()
return {'content': content, 'code': 200}
Empty file.

0 comments on commit c1f4828

Please sign in to comment.