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

Best practice: Mock connections AttributeError: type object 'Connection' has no attribute 'get' #39475

Closed
2 tasks done
iamtodor opened this issue May 8, 2024 · 5 comments · Fixed by #39560
Closed
2 tasks done
Assignees

Comments

@iamtodor
Copy link
Contributor

iamtodor commented May 8, 2024

What do you see as an issue?

Per paragraph https://airflow.apache.org/docs/apache-airflow/2.8.2/best-practices.html#mocking-variables-and-connections
The given example

conn = Connection(
    conn_type="gcpssh",
    login="cat",
    host="conn-host",
)
conn_uri = conn.get_uri()
with mock.patch.dict("os.environ", AIRFLOW_CONN_MY_CONN=conn_uri):
    assert "cat" == Connection.get("my_conn").login

raises an error:

def test_foo():
        conn = Connection(
            conn_type="gcpssh",
            login="cat",
            host="conn-host",
        )
        conn_uri = conn.get_uri()
        with mock.patch.dict("os.environ", AIRFLOW_CONN_MY_CONN=conn_uri):
>           assert "cat" == Connection.get("my_conn").login
E           AttributeError: type object 'Connection' has no attribute 'get'

path/to/test/file.py:12: AttributeError

Solving the problem

Update the doc

Anything else

No response

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

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

boring-cyborg bot commented May 8, 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.

@iamtodor
Copy link
Contributor Author

iamtodor commented May 8, 2024

@rawwar rawwar removed the needs-triage label for new issues that we didn't triage yet label May 8, 2024
@rawwar
Copy link
Contributor

rawwar commented May 8, 2024

@iamtodor
Copy link
Contributor Author

iamtodor commented May 8, 2024

@rawwar yup, this approach could also be used. I think yours is better, is that right?

@rawwar
Copy link
Contributor

rawwar commented May 8, 2024

@rawwar yup, this approach could also be used. I think yours is better, is that right?

Yeah, I think get_connection_from_secrets is correct. But, You can raise a PR with get_connection_from_secrets and request a review.

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

Successfully merging a pull request may close this issue.

2 participants