beancount / beangulp

Importers framework for Beancount
GNU General Public License v2.0
59 stars 23 forks source link

pytest compatibility #134

Open andylou2 opened 3 days ago

andylou2 commented 3 days ago

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 commented 2 days ago

SGTM

On Sun, Nov 10, 2024 at 11:50 PM Andy S. Lou @.***> wrote:

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

— Reply to this email directly, view it on GitHub https://github.com/beancount/beangulp/issues/134, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACSBE72UAMKPDGR5H63S5T2AAZRFAVCNFSM6AAAAABRRAM2K2VHI2DSMVQWIX3LMV43ASLTON2WKOZSGY2DQMJUHEYTKMQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>