Last active
December 31, 2024 23:12
-
-
Save elyssonmr/5472b521cb248ee5aaf2d4469111ed47 to your computer and use it in GitHub Desktop.
Cenário 2 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 typing import Union | |
from io import BytesIO | |
from scenario2.nf_client import get_nf_data | |
from scenario2.pdf_service import generate_pdf | |
def issue_nota_fiscal(nf_id: str) -> BytesIO: | |
nf_data = get_nf_data(nf_id) | |
template = 'nf_pdf.template' | |
return generate_pdf(nf_data, template) |
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 typing import Union | |
def get_nf_data(nf_id: str) -> dict[str, Union[int, float, str, dict]]: | |
# Should retrieve Nota Fiscal from an 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 io import BytesIO | |
from typing import Union | |
def generate_pdf( | |
data: dict[str, Union[int, float, str, dict]], | |
template: str | |
) -> BytesIO: | |
# Should generate PDF with the given template and data | |
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 io import BytesIO | |
from unittest.mock import patch | |
import pytest | |
from scenario2.helpers import issue_nota_fiscal | |
@pytest.fixture | |
def mock_get_nf_data(): | |
with patch('scenario2.helpers.get_nf_data') as patched: | |
yield patched | |
@pytest.fixture | |
def mock_generate_pdf(): | |
with patch('scenario2.helpers.generate_pdf') as patched: | |
yield patched | |
def test_should_issue_nota_fiscal(mock_get_nf_data, mock_generate_pdf): | |
nf_id = '123456' | |
template = 'nf_pdf.template' | |
nf_data = {'nome': 'test_mock', 'products': [{'name': 'product1', 'value': 10.50}]} | |
pdf = BytesIO(b'pdf_bytes') | |
mock_get_nf_data.return_value = nf_data | |
mock_generate_pdf.return_value = pdf | |
generated_pdf = issue_nota_fiscal(nf_id) | |
mock_get_nf_data.assert_called_once_with(nf_id) | |
mock_generate_pdf.assert_called_once_with(nf_data, template) | |
assert generated_pdf == pdf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment