title: How to mock multiple query.filter_by in Flaks-SqlAlchemy
body: <h2>In brief</h2>
  <p>When testing in Flask-SqlAlchemy how can I mock multiple differents query.filter_by().first ?</p>
  <h2>More in detail</h2>
  <p>I followed <a href="https://stackoverflow.com/questions/42482021/how-to-mock-modelclass-query-filter-by-in-flask-sqlalchemy">this link</a> to do some unit testing and it works very well when I have only one query.filter_by.</p>
  <p>The problem is in one function I have:</p>
  <pre><code>user = User.query.filter_by(username=user[&quot;username&quot;]).first()
  organization = Organization.query.filter_by(organization_name=user.get(&quot;organization&quot;)).first()
  </code></pre>
  <p>If I follow the previous link when I do:</p>
  <pre><code>@mock.patch('flask_sqlalchemy._QueryProperty.__get__')
  def test(self, queryMOCK):
      queryMOCK \
          .return_value.filter_by \
          .return_value.first \
          .return_value = user
  </code></pre>
  <p>I mock all the query.filter_by() and not only one of the two.</p>
  <p>They say to do:</p>
  <pre><code>@patch('app.model.some_model.SomeModel')
  def test_some_case(self, some_model_mock):
      filter_by_mock = some_model_mock.query.filter_by
  </code></pre>
  <p>but I didn't manage to do it.</p>