Skip to content

Instantly share code, notes, and snippets.

@elyssonmr
Last active December 31, 2024 23:12
Show Gist options
  • Save elyssonmr/5420f1914247fc61f002d88c8c34d6e7 to your computer and use it in GitHub Desktop.
Save elyssonmr/5420f1914247fc61f002d88c8c34d6e7 to your computer and use it in GitHub Desktop.
from scenario3.price_client import get_price
def retrieve_prices(products_id: list[str]) -> dict[str, float]:
prices = []
for product_id in products_id:
price = get_price(product_id)
prices.append({'product_id': product_id, 'price': price})
return prices
def get_price(product_id: str) -> float:
# should consult price from a API
pass
from unittest.mock import patch, call
import pytest
from scenario3.helpers import retrieve_prices
@pytest.fixture
def mock_get_price():
with patch('scenario3.helpers.get_price') as patched:
yield patched
def test_should_retrieve_prices(mock_get_price):
mock_get_price.side_effect = [10.5, 9.83]
products_ids = ['123456', '654321']
products_prices = retrieve_prices(products_ids)
assert products_prices == [{
'product_id': '123456',
'price': 10.5,
}, {
'product_id': '654321',
'price': 9.83,
}]
mock_get_price.assert_has_calls([call('123456'), call('654321')])
assert mock_get_price.call_count == 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment