def test_model_query_common(self, _project_filter, _read_deleted_filter):
        _read_deleted_filter.return_value = self.session.query
        _project_filter.return_value = self.session.query
        utils.model_query(self.user_context, MyModel,
                          args=(MyModel.id,), session=self.session)

        self.session.query.assert_called_with(MyModel.id)
        _read_deleted_filter.assert_called_with(
            self.session.query, MyModel, self.user_context.read_deleted)
        _project_filter.assert_called_with(
            self.session.query, MyModel, self.user_context, False)
    def test_context_show_deleted(self, _project_filter, _read_deleted_filter):
        user_context = mock.MagicMock(is_admin=False, show_deleted='yes',
                                      user_id=42, project_id=43)
        delattr(user_context, 'read_deleted')
        _read_deleted_filter.return_value = self.session.query
        _project_filter.return_value = self.session.query
        utils.model_query(user_context, MyModel,
                          args=(MyModel.id,), session=self.session)

        self.session.query.assert_called_with(MyModel.id)
        _read_deleted_filter.assert_called_with(
            self.session.query, MyModel, user_context.show_deleted)
        _project_filter.assert_called_with(
            self.session.query, MyModel, user_context, False)
    def test_read_deleted_allow_none(self):
        mock_query = utils.model_query(
            self.user_context, MyModelSoftDeletedProjectId,
            session=self.session, project_only='allow_none')

        self.assertEqual(
            str(mock_query.filter.call_args[0][0]),
            'soft_deleted_project_id_test_model.project_id = :project_id_1 OR'
            ' soft_deleted_project_id_test_model.project_id IS NULL'
        )
    def test_read_deleted_no(self):
        mock_query = utils.model_query(
            self.user_context, MyModelSoftDeleted,
            session=self.session, read_deleted='no')

        deleted_filter = mock_query.filter.call_args[0][0]
        self.assertEqual(str(deleted_filter),
                         'soft_deleted_test_model.deleted = :deleted_1')
        self.assertEqual(deleted_filter.right.value,
                         MyModelSoftDeleted.__mapper__.c.deleted.default.arg)
    def test_project_only_true(self):
        mock_query = utils.model_query(
            self.user_context, MyModelSoftDeletedProjectId,
            session=self.session, project_only=True)

        deleted_filter = mock_query.filter.call_args[0][0]
        self.assertEqual(
            str(deleted_filter),
            'soft_deleted_project_id_test_model.project_id = :project_id_1')
        self.assertEqual(deleted_filter.right.value,
                         self.user_context.project_id)
    def test_read_deleted_yes(self):
        mock_query = utils.model_query(
            self.user_context, MyModelSoftDeleted,
            session=self.session, read_deleted='yes')

        self.assertEqual(mock_query.filter.call_count, 0)