-
-
Notifications
You must be signed in to change notification settings - Fork 90
Testes
Os testes do pacote em python da basedosdados foram implementados usando o módulo pytest. Neste documento, apresentamos convenções e padrões utilizados na nossa implementação dos testes:
No âmbito do pytest, as fixtures são valores que o usuário define com o decorador @pytest.fixture que ficam globalmente disponíveis para os outros testes definidos no módulo. Exemplo:
import pytest @pytest.fixture def input_value(): input = 39 return input def test_divisible_by_3(input_value): assert input_value % 3 == 0
Ocorre que, pelo nosso coding standard, evitamos sobrescrever variáveis locais com nomes globais em uso. Para manter o uso das fixtures e compatibilizar os arquivos dos módulos de testes com nosso coding standard, adotamos a convenção sugerida na documentação do pytest:
If a fixture is used in the same module in which it is defined, the function name of the fixture will be shadowed by the function arg that requests the fixture; one way to resolve this is to name the decorated function fixture_ and then use @pytest.fixture(name='').
Para o exemplo mostrado acima, temos:
@pytest.fixture(name="input_value") def fixture_input_vallue():
''' Tests for the CLI module. ''' from pathlib import Path import shutil import os from click.core import Context from basedosdados.cli.cli import cli_table from importlib_metadata import metadata from more_itertools import bucket import pytest import click.testing from basedosdados import Storage, Dataset, Table import basedosdados as bd from basedosdados.exceptions import BaseDosDadosException from google.api_core.exceptions import NotFound DATASET_ID = "pytest" TABLE_ID = "pytest" TABLE_FILES = ["publish.sql", "table_config.yaml"] templates=Path(__file__).parent / "templates" bucket_name = "basedosdados-dev" metadata_path = Path(__file__).parent / "tmp_bases" ctx = Context(command=cli_table.commands['create'], obj=dict(templates=templates,bucket_name=bucket_name,metadata_path=metadata_path)) def test_create_no_path_error(testdir): """ Teste if error is raised when no path is provided """ shutil.rmtree(testdir / DATASET_ID / TABLE_ID, ignore_errors=True) runner = click.testing.CliRunner() result = runner.invoke(cli=cli_table.get_command(ctx=ctx, cmd_name='create'), args=f'''--if_table_exists=replace {DATASET_ID} {TABLE_ID}''') assert isinstance(result.exception, BaseDosDadosException)