Skip to content

opencdms-process: create pytest tests that verify calls to R functions generate the expected R warnings #31

Open
@lloyddewit

Description

@lloyddewit
  • Python version: 3.8.10
  • R version: 4.1.2
  • Operating System: Ubuntu 20.04

I want to write a 'PyTest' function that checks if a specific call to an R function raises the expected warning(s). For example, the call below should generate warning 'facet_by set to none since no stations are given in data':

    cdms_products.histogram_plot(
        path=output_path_actual,
        file_name=file_name_actual,
        data=niger50,
        date_time="date",
        elements=["tmax"],
        facet_by="stations",
    )

I am using the 'rpy2' library to call the R function. The 'rpy2' library does not yet seem to allow warnings/errors generated in the R environment to be read in the Python environment. I created 'rpy2' issue 894. This describes the problem in detail.

I see two possible ways ahead:

  1. Wait to see if the rpy2 issue is resolved.
  2. Use a workaround like writing the R output to file in the R environment, (example here).

I prefer option 1.

Personal notes on possible approaches if/when 'rpy2' issue above is resolved

The Python wrapper would write any R warnings/errors to a stream that is returned to the calling function. Or it could write errors/warnings to a log file. The log file name would be the same as the output file name, except that the file name extension would be ".log".
Example code (Python wrapper function):

    from rpy2.rinterface_lib.callbacks import logger as rpy2_logger
    file_name_log = os.path.join(path, os.path.splitext(file_name)[0] + ".log")
    log_file_handler = logging.FileHandler(file_name_log)
    rpy2_logger.addHandler(log_file_handler)

Example code ('pytest' function):

    cdms_products.histogram_plot(
        path=output_path_actual,
        file_name=file_name_actual,
        data=niger50,
        date_time="date",
        elements=["tmax"],
        facet_by="stations",
    )
    assert __is_expected_file(file_name_actual)
    assert __is_expected_file(os.path.splitext(file_name_actual)[0]+".log")

Alternatively, the Python wrapper function could ignore the R warnings, and the 'pytest' function could analyse the warnings directly (see pytest warning documentation here and here).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions