Last active
April 15, 2019 20:04
-
-
Save rtorres90/308bffa7ec53ccd768ef856846961e44 to your computer and use it in GitHub Desktop.
mock example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http.client | |
import json | |
class ApiUsers(object): | |
def getUsers(self): | |
conn = http.client.HTTPSConnection("reqres.in") | |
conn.request("GET", "/api/users") | |
r1 = conn.getresponse().read() | |
return json.loads(r1)['data'] | |
def getUser(self, user_id): | |
conn = http.client.HTTPSConnection("reqres.in") | |
conn.request("GET", "/api/user/%s" % user_id) | |
r1 = conn.getresponse().read() | |
return json.loads(r1)['data'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InfoUsuarios(object): | |
def __init__(self, api_users): | |
self.api = api_users | |
def listar_usuarios(self): | |
print(f'|Ids|{"Nombre":10}|') | |
for user in self.api.getUsers(): | |
print(f'|{user.get("id"):3}|{user.get("first_name"):10}|') | |
def obtener_usuarios(self): | |
return self.api.getUsers() | |
def mostrar_info_usuario(self, id_usuario): | |
print(f'|Ids|{"Nombre":10}|') | |
user = self.api.getUser(id_usuario) | |
print(f'|{user.get("id"):3}|{user.get("first_name"):10}|') | |
def obtener_info_usuario(self, id_usuario): | |
return self.api.getUser(id_usuario) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from unittest.mock import Mock | |
from InfoUsuarios import InfoUsuarios | |
class TestCaseInfoUsuarios(unittest.TestCase): | |
def setUp(self): | |
mock_api_users = Mock() | |
mock_api_users.getUser.return_value = {'first_name': 'George'} | |
mock_api_users.getUsers.return_value = [ | |
{'first_name': 'George'}, | |
{'first_name': 'Peter'}, | |
{'first_name': 'Jon'}, | |
{'first_name': 'Josh'}, | |
{'first_name': 'Bruce'}, | |
{'first_name': 'Robert'}, | |
{'first_name': 'Kyle'}, | |
{'first_name': 'Paul'}, | |
{'first_name': 'Nicole'}, | |
] | |
self.info_usuarios = InfoUsuarios(mock_api_users) | |
def test_mostar_info_usuario(self): | |
self.assertEqual(self.info_usuarios.obtener_info_usuario(0).get('first_name'), "George", "El nombre del usuario no es el esperado") | |
def test_cantidad_obterner_usuarios(self): | |
self.assertEqual(len(self.info_usuarios.obtener_usuarios()), 9, "La cantidad de usuarios no es la esperada.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment