Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest compatibility #134

Open
andylou2 opened this issue Nov 11, 2024 · 1 comment
Open

pytest compatibility #134

andylou2 opened this issue Nov 11, 2024 · 1 comment

Comments

@andylou2
Copy link

Hi,

In beancount v2 there is a handy utility to incorporate Importer unit tests with pytest: https://github.com/beancount/beancount/blob/v2/beancount/ingest/regression_pytest.py

I couldn't find the equivalent in beangulp. In the examples directory, I saw the tests are driven by a bash script: https://github.com/beancount/beangulp/blob/master/examples/importers/runtests.sh

Are you guys open to adding the pytest compatibility to beangulp, or accepting PRs? Something along the lines of this:

import os
from typing import List, Optional, Union

import beangulp
import beangulp.testing
import click
import pytest
from beangulp import utils
from beangulp.importer import Importer, ImporterProtocol


def run_test_programmatically(
    importer: Union[Importer, ImporterProtocol],
    documents: List[str],
    expected: Optional[str] = None,
    verbose: int = 0,
    quiet: int = 0,
    failfast: bool = False,
    generate: bool = False,
) -> None:
    """
    Programmatically run the test command that would normally be invoked via CLI.

    Args:
        importer: The importer instance to test
        documents: List of document paths to test
        expected: A directory for expected output files
        verbose: Verbosity level (default: 0)
        quiet: Quiet level (default: 0)
        failfast: Whether to stop at first failure (default: False)
        generate: If True, then generates test data
    """
    ctx_obj = beangulp.Ingest([importer])
    command = beangulp.testing._generate if generate else beangulp.testing._test

    with click.Context(command, obj=ctx_obj) as ctx:
        if generate:
            command.callback(documents=documents, expected=expected, verbose=verbose, quiet=quiet, force=True)

            # Verify that the expected files were generated
            for doc in utils.walk(documents):
                if doc.endswith(".beancount"):
                    continue

                expected_filename = f"{doc}.beancount"
                if not os.path.exists(expected_filename):
                    raise FileNotFoundError(f"Did not generate expected file: {expected_filename}")
        else:
            command.callback(documents=documents, expected=expected, verbose=verbose, quiet=quiet, failfast=failfast)

Thanks,
Andy

@blais
Copy link
Member

blais commented Nov 11, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants