Add rpy2 to the dependencies

- add a Jupyter notebook that allows to install all project-external
  dependencies regarding R and R packages
- adjust the GitHub Action workflow to also install R and the R packages
  used within the project
- add a `init_r` module that initializes all R packages globally
  once the `urban_meal_delivery` package is imported
This commit is contained in:
Alexander Hess 2021-01-11 12:24:15 +01:00
commit b0f2fdde10
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
10 changed files with 2152 additions and 52 deletions

View file

@ -29,6 +29,9 @@ def test_database_uri_set(env, monkeypatch):
monkeypatch.setattr(configuration.ProductionConfig, 'DATABASE_URI', uri)
monkeypatch.setattr(configuration.TestingConfig, 'DATABASE_URI', uri)
# Prevent that a warning is emitted for a missing R_LIBS_PATH.
monkeypatch.setattr(configuration.Config, 'R_LIBS_PATH', '.cache/r_libs')
with pytest.warns(None) as record:
configuration.make_config(env)
@ -43,6 +46,9 @@ def test_no_database_uri_set_with_testing_env_var(env, monkeypatch):
monkeypatch.setenv('TESTING', 'true')
# Prevent that a warning is emitted for a missing R_LIBS_PATH.
monkeypatch.setattr(configuration.Config, 'R_LIBS_PATH', '.cache/r_libs')
with pytest.warns(None) as record:
configuration.make_config(env)
@ -57,10 +63,64 @@ def test_no_database_uri_set_without_testing_env_var(env, monkeypatch):
monkeypatch.delenv('TESTING', raising=False)
# Prevent that a warning is emitted for a missing R_LIBS_PATH.
monkeypatch.setattr(configuration.Config, 'R_LIBS_PATH', '.cache/r_libs')
with pytest.warns(UserWarning, match='no DATABASE_URI'):
configuration.make_config(env)
@pytest.mark.parametrize('env', envs)
def test_r_libs_path_set(env, monkeypatch):
"""Package does NOT emit a warning if R_LIBS is set in the environment."""
monkeypatch.setattr(configuration.Config, 'R_LIBS_PATH', '.cache/r_libs')
# Prevent that a warning is emitted for a missing DATABASE_URI.
uri = 'postgresql://user:password@localhost/db'
monkeypatch.setattr(configuration.ProductionConfig, 'DATABASE_URI', uri)
with pytest.warns(None) as record:
configuration.make_config(env)
assert len(record) == 0 # noqa:WPS441,WPS507
@pytest.mark.parametrize('env', envs)
def test_no_r_libs_path_set_with_testing_env_var(env, monkeypatch):
"""Package emits a warning if no R_LIBS is set in the environment ...
... when not testing.
"""
monkeypatch.setattr(configuration.Config, 'R_LIBS_PATH', None)
monkeypatch.setenv('TESTING', 'true')
# Prevent that a warning is emitted for a missing DATABASE_URI.
uri = 'postgresql://user:password@localhost/db'
monkeypatch.setattr(configuration.ProductionConfig, 'DATABASE_URI', uri)
with pytest.warns(None) as record:
configuration.make_config(env)
assert len(record) == 0 # noqa:WPS441,WPS507
@pytest.mark.parametrize('env', envs)
def test_no_r_libs_path_set_without_testing_env_var(env, monkeypatch):
"""Package emits a warning if no R_LIBS is set in the environment ...
... when not testing.
"""
monkeypatch.setattr(configuration.Config, 'R_LIBS_PATH', None)
monkeypatch.delenv('TESTING', raising=False)
# Prevent that a warning is emitted for a missing DATABASE_URI.
uri = 'postgresql://user:password@localhost/db'
monkeypatch.setattr(configuration.ProductionConfig, 'DATABASE_URI', uri)
with pytest.warns(UserWarning, match='no R_LIBS'):
configuration.make_config(env)
def test_random_testing_schema():
"""CLEAN_SCHEMA is randomized if not set explicitly."""
result = configuration.random_schema_name()

19
tests/test_init_r.py Normal file
View file

@ -0,0 +1,19 @@
"""Verify that the R packages are installed correctly."""
import pytest
@pytest.mark.r
def test_r_packages_installed():
"""Import the `urban_meal_delivery.init_r` module.
Doing this raises a `PackageNotInstalledError` if the
mentioned R packages are not importable.
They must be installed externally. That happens either
in the "research/r_dependencies.ipynb" notebook or
in the GitHub Actions CI.
"""
from urban_meal_delivery import init_r # noqa:WPS433
assert init_r is not None