Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mock flask-sqlalchemy query

I'm getting an error of Module not found when trying to create a test function to mock the get method of sqlalchemy query (with pytest)

example:

from mock import patch     
@patch('flask_sqlalchemy._QueryProperty.__get__')
def test_get_all(queryMock):
    assert True

When running pytest i get an error:

ModuleNotFoundError: No module named 'flask_sqlalchemy._QueryProperty'

I'm using the 3.0.2 version of Flask-SQLAlchemy. So i just changed to version 2.5.1 and it worked. However i think would be good to use the latest version. Is there any other way to mock the sql-alchemy query that works with the latest versions?

like image 487
Leonardo Avatar asked Apr 30 '26 12:04

Leonardo


1 Answers

Use flask_sqlalchemy.model._QueryProperty.__get__ instead of flask_sqlalchemy._QueryProperty.__get__. This will resolve your issue as _QueryProperty class has been moved into model.

from mock import patch     
@patch('flask_sqlalchemy.model._QueryProperty.__get__')
def test_get_all(queryMock):
    assert True
like image 117
Aadesh Baral Avatar answered May 02 '26 02:05

Aadesh Baral



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!