Skip to content

Instantly share code, notes, and snippets.

@rtorres90
Last active April 15, 2019 20:04
Show Gist options
  • Save rtorres90/308bffa7ec53ccd768ef856846961e44 to your computer and use it in GitHub Desktop.
Save rtorres90/308bffa7ec53ccd768ef856846961e44 to your computer and use it in GitHub Desktop.
mock example
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']
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)
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