예제 #1
0
 async def one(self) -> T:
     try:
         ret = await self.one_or_none()
     except orm_exc.MultipleResultsFound:
         raise orm_exc.MultipleResultsFound(
             "Multiple rows were found for one()")
     else:
         if ret is None:
             raise orm_exc.NoResultFound("No row was found for one()")
     return ret
예제 #2
0
 async def one_or_none(self) -> Optional[T]:
     ret = await self.all()
     length = len(ret)
     if length == 1:
         return ret[0]
     elif length == 0:
         return None
     else:
         raise orm_exc.MultipleResultsFound(
             "Multiple rows were found for one_or_none()")
예제 #3
0
 def one_or_none(self):
     ret = list(self)
     l = len(ret)
     if l == 1:
         return ret[0]
     elif l == 0:
         return None
     else:
         raise orm_exc.MultipleResultsFound(
             "Multiple rows were found for one_or_none()")
예제 #4
0
    def one(self):
        ret = list(self)
        l = len(ret)

        if l == 1:
            return ret[0]
        elif l == 0:
            raise sqla_exc.NoResultFound("No row was found for one()")
        else:
            raise sqla_exc.MultipleResultsFound(
                "Multiple rows were found for one()")
예제 #5
0
    def get(cls, **kwargs):
        result = SESSION.query(cls).filter_by(**kwargs)
        quantity = result.count()

        if not quantity:
            raise orm_exc.NoResultFound('{}: {}'.format(cls.__name__, kwargs))

        if quantity > 1:
            raise orm_exc.MultipleResultsFound('{}: {}'.format(
                cls.__name__, kwargs))

        return result.first()
예제 #6
0
 def test_get_one(self, mock_warning):
     mock_one = mock.Mock()
     mock_one.one.return_value = True
     mock_filter = mock.Mock()
     mock_filter.filter.return_value = mock_one
     mock_session = mock.Mock()
     mock_session.query.return_value = mock_filter
     self.o.session = mock_session
     self.assertTrue(self.o._get_one())
     mock_one.one.assert_called_with()
     self.assertTrue(mock_filter.filter.called)
     mock_session.query.assert_called_with(OngBD)
     mock_session.query.side_effect = UnboundLocalError()
     self.assertFalse(self.o._get_one())
     mock_warning.assert_called_with('Try search in database without ong name or id.')
     mock_session.query.side_effect = exc.NoResultFound()
     self.assertFalse(self.o._get_one())
     mock_warning.assert_called_with('Ong [None, anyOng] not found.')
     mock_session.query.side_effect = exc.MultipleResultsFound()
     self.assertFalse(self.o._get_one())
     mock_warning.assert_called_with('Multiple results found for ong [None, anyOng].')