Relative Content

Tag Archive for pythonpostgresqlunit-testingsqlalchemypytest

How can I test a SQLAlchemy engine connection to an external database in pytest, for the purpose of testing connection verification?

I am attempting to test a function which contains a try-except block for ‘engine.connect()’, and catches either a SQLAlchemy exc.OperationalError or an AttributeError. The former exception is to catch incorrect credentials used to build the URL object, which in turn is used to build the engine (wrong username, host name, database name, etc.). The latter is to catch when an object other than an engine object is passed to the function. I’m unfamiliar with pytest and can’t find a way to monkeypatch engine.connect() in a way that will still return the appropriate exc.OperationalError for an incorrect URL, if this is even the preferred approach. As far as I’m aware I don’t want to actually connect to the cloud postgres database in testing, but unsure how to replicate an external connection in a way that will still raise the correct exceptions given alternative inputs. What’s the standard here?

How can I test a SQLAlchemy engine connection to an external database in pytest, for the purpose of testing connection verification?

I am attempting to test a function which contains a try-except block for ‘engine.connect()’, and catches either a SQLAlchemy exc.OperationalError or an AttributeError. The former exception is to catch incorrect credentials used to build the URL object, which in turn is used to build the engine (wrong username, host name, database name, etc.). The latter is to catch when an object other than an engine object is passed to the function. I’m unfamiliar with pytest and can’t find a way to monkeypatch engine.connect() in a way that will still return the appropriate exc.OperationalError for an incorrect URL, if this is even the preferred approach. As far as I’m aware I don’t want to actually connect to the cloud postgres database in testing, but unsure how to replicate an external connection in a way that will still raise the correct exceptions given alternative inputs. What’s the standard here?

How can I test a SQLAlchemy engine connection to an external database in pytest, for the purpose of testing connection verification?

I am attempting to test a function which contains a try-except block for ‘engine.connect()’, and catches either a SQLAlchemy exc.OperationalError or an AttributeError. The former exception is to catch incorrect credentials used to build the URL object, which in turn is used to build the engine (wrong username, host name, database name, etc.). The latter is to catch when an object other than an engine object is passed to the function. I’m unfamiliar with pytest and can’t find a way to monkeypatch engine.connect() in a way that will still return the appropriate exc.OperationalError for an incorrect URL, if this is even the preferred approach. As far as I’m aware I don’t want to actually connect to the cloud postgres database in testing, but unsure how to replicate an external connection in a way that will still raise the correct exceptions given alternative inputs. What’s the standard here?