Skip to content

Instantly share code, notes, and snippets.

@mojtabaahn
Created September 2, 2022 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mojtabaahn/bcfa3a3ce9b84224d3c1660d6e5cf3fc to your computer and use it in GitHub Desktop.
Save mojtabaahn/bcfa3a3ce9b84224d3c1660d6e5cf3fc to your computer and use it in GitHub Desktop.
Mocking SDKs in python [5]
from httpx import Response, Client
from app import app
from factories import UserFactory, BasketFactory
from unittest.mock import MagicMock
class QuickMock:
def __init__(self, object_: object, prop: str, value: Any) -> None:
self.__object = object_
self.__prop = prop
self.__value = value
self.__original = object_.__getattribute__(prop)
def __enter__(self):
self.__object.__setattr__(self.__prop, self.__value)
return None
def __exit__(self, exc_type, exc_value, exc_tb):
self.__object.__setattr__(self.__prop, self.__original)
def test_purchase_basket_controller():
with QuickMock(payment_sdk, 'get_gateway_url', MagicMock(return_value='https://sample_bank_url.test')):
with Client(app=app, base_url="http://testserver.test", timeout=0.1) as client:
user = UserFactory.create_one()
basket = BasketFactory.create_one(user_id=user.id)
response = client.post(
url='/basket/purchase',
headers=dict(Accept='application/json')
)
assert response.status_code == 200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment