def test_get_one_or_else_multiple_results(journalist_app, test_admin, test_journo): with journalist_app.app_context(): # precondition: there must be multiple journalists assert Journalist.query.count() == 2 mock_logger = MagicMock() mock_abort = MagicMock() # this is equivalent to "SELECT *" which we know returns 2 query = Journalist.query get_one_or_else(query, mock_logger, mock_abort) # Not specifying the very long log line in `logger.error` mock_logger.error.assert_called() mock_abort.assert_called_with(500)
def test_get_one_or_else_no_result_found(journalist_app, test_journo): with journalist_app.app_context(): # precondition: there must be one journalist assert Journalist.query.count() == 1 bad_name = test_journo["username"] + "aaaaaa" query = Journalist.query.filter_by(username=bad_name) mock_logger = MagicMock() mock_abort = MagicMock() get_one_or_else(query, mock_logger, mock_abort) log_line = "Found none when one was expected: " "No row was found for one()" mock_logger.error.assert_called_with(log_line) mock_abort.assert_called_with(404)
def get_source(filesystem_id): """Return a Source object, representing the database row, for the source with the `filesystem_id`""" source = None query = Source.query.filter(Source.filesystem_id == filesystem_id) source = get_one_or_else(query, current_app.logger, abort) return source
def test_get_one_or_else_returns_one(journalist_app, test_journo): with journalist_app.app_context(): # precondition: there must be one journalist assert Journalist.query.count() == 1 query = Journalist.query.filter_by(username=test_journo['username']) selected_journo = get_one_or_else(query, MagicMock(), MagicMock()) assert selected_journo.id == test_journo['id']
def get_source(filesystem_id: str, include_deleted: bool = False) -> Source: """ Return the Source object with `filesystem_id` If `include_deleted` is False, only sources with a null `deleted_at` will be returned. """ query = Source.query.filter(Source.filesystem_id == filesystem_id) if not include_deleted: query = query.filter_by(deleted_at=None) source = get_one_or_else(query, current_app.logger, abort) return source
def delete() -> werkzeug.Response: """This deletes the reply from the source's inbox, but preserves the history for journalists such that they can view conversation history. """ query = Reply.query.filter_by(filename=request.form['reply_filename'], source_id=g.source.id) reply = get_one_or_else(query, current_app.logger, abort) reply.deleted_by_source = True db.session.add(reply) db.session.commit() flash(gettext("Reply deleted"), "notification") return redirect(url_for('.lookup'))
def delete(logged_in_source: SourceUser) -> werkzeug.Response: """This deletes the reply from the source's inbox, but preserves the history for journalists such that they can view conversation history. """ query = Reply.query.filter_by( filename=request.form["reply_filename"], source_id=logged_in_source.db_record_id ) reply = get_one_or_else(query, current_app.logger, abort) reply.deleted_by_source = True db.session.add(reply) db.session.commit() flash_msg("success", gettext("Success!"), gettext("Reply deleted")) return redirect(url_for(".lookup"))