Last active
December 31, 2024 23:12
-
-
Save elyssonmr/5420f1914247fc61f002d88c8c34d6e7 to your computer and use it in GitHub Desktop.
Cenário 3 do artigo: https://elyssonmr.com/blog/posts/20241231_tests_hints/
This file contains hidden or 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
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 |
This file contains hidden or 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
def get_price(product_id: str) -> float: | |
# should consult price from a API | |
pass |
This file contains hidden or 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
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