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

Unable to mock Variable in python unittest #38944

Closed
1 of 2 tasks
ivanwakeup opened this issue Apr 11, 2024 · 3 comments
Closed
1 of 2 tasks

Unable to mock Variable in python unittest #38944

ivanwakeup opened this issue Apr 11, 2024 · 3 comments

Comments

@ivanwakeup
Copy link

ivanwakeup commented Apr 11, 2024

What do you see as an issue?

The airflow Best Practices documentation page suggests using unittest.mock.patch along with setting os.environ values to mock Variables and Connections. This doesn't seem to work in practice (testing against airflow 2.8.1)--airflow attempts to read Variable values from variable table in DB instead of using os.environ overrides.

Solving the problem

example non-working code:

from unittest.mock import patch
from airflow.models import Variable

def test_variable_fetch():
    with patch.dict('os.environ', AIRFLOW_VAR_my_test_var="my_test_value"):
        print(Variable.get("my_test_var"))

error stack:

/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py:8: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
  from pkg_resources import iter_entry_points
Launching pytest with arguments test_nlx_bqs_tasks.py::test_variable_fetch --no-header --no-summary -q in /Users/ivanwakeup/projects/nl-airflow-dags/tests

============================= test session starts ==============================
collecting ... collected 1 item

test_nlx_bqs_tasks.py::test_variable_fetch FAILED                        [100%][2024-04-11T11:56:44.440-0700] {variable.py:283} ERROR - Unable to retrieve variable from secrets backend (MetastoreBackend). Checking subsequent secrets backend.
Traceback (most recent call last):
  File "/Users/ivanwakeup/Library/Caches/pypoetry/virtualenvs/nl-airflow-dags-VBs6ACil-py3.9/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1910, in _execute_context
    self.dialect.do_execute(
  File "/Users/ivanwakeup/Library/Caches/pypoetry/virtualenvs/nl-airflow-dags-VBs6ACil-py3.9/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 736, in do_execute
    cursor.execute(statement, parameters)
sqlite3.OperationalError: no such table: variable

The above exception was the direct cause of the following exception:

Anything else

https://airflow.apache.org/docs/apache-airflow/2.8.4/best-practices.html#mocking-variables-and-connections - guide page in question

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

@ivanwakeup ivanwakeup added kind:bug This is a clearly a bug kind:documentation needs-triage label for new issues that we didn't triage yet labels Apr 11, 2024
Copy link

boring-cyborg bot commented Apr 11, 2024

Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.

@ivanwakeup
Copy link
Author

I think i found the issue. lowercase my_test_var instead of MY_TEST_VAR in the variable definition causes airflow to not pickup the value. changing this fixes it.

@aritra24
Copy link
Collaborator

Closing this issue in that case

@aritra24 aritra24 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 12, 2024
@aritra24 aritra24 removed kind:bug This is a clearly a bug needs-triage label for new issues that we didn't triage yet labels Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants