Ejemplo n.º 1
0
 def test_get_or_create_stranger__stranger_not_found(self):
     from randtalkbot.stranger_service import Stranger as stranger_cls_mock
     stranger_cls_mock.get.side_effect = DoesNotExist()
     stranger_cls_mock.create.return_value = self.stranger_0
     self.stranger_service.get_cached_stranger = Mock()
     self.stranger_service.get_cached_stranger.return_value = 'cached_stranger'
     self.assertEqual(
         self.stranger_service.get_or_create_stranger(31416),
         'cached_stranger',
     )
     self.stranger_service.get_cached_stranger.assert_called_once_with(
         self.stranger_0)
Ejemplo n.º 2
0
def find_user(uid, is_admin=False):
    cond = (
        Users.disabled == False,
        Users.id == uid,
    )
    if is_admin:
        cond += (Users.is_admin == is_admin, )

    q = Users.select().where(*cond)

    if not q.count():
        raise DoesNotExist("User doesn't exists")

    return q.limit(1)[0]
Ejemplo n.º 3
0
 def validate(self, class_name, id):  # type: (str, Optional[int]) -> None
     if self._field is None:
         if not isinstance(self._limits, tuple):
             raise RuntimeError('Expected a fixed limit')
         limits = self._limits
     else:
         if not callable(self._limits):
             raise RuntimeError('Expected a limit generator')
         container = MemoryFieldContainer(name='id',
                                          memory_field=self._field,
                                          memory_address=self._field.get_address(None))
         limits = self._limits(container.decode())
     if id is None or not (limits[0] <= id <= limits[1]):
         if limits[0] > limits[1]:
             limit_info = 'No records available.'
         elif limits[0] == limits[1]:
             limit_info = 'Only one records available: {0}'.format(limits[0])
         else:
             limit_info = 'Available records: {0} <= id <= {1}'.format(limits[0], limits[1])
         raise DoesNotExist('Could not find {0}({1}). {2}'.format(class_name, '' if id is None else id, limit_info))
Ejemplo n.º 4
0
    async def get_next_step(self):
        with db.connection_context():
            try:
                # Get the next step
                step = ExperimentStep.select().where(
                    ExperimentStep.step_done == False).order_by(
                        ExperimentStep.id).first()

                # Check if the step is none, and skip to the catch clause if it is
                if step is None:
                    raise DoesNotExist('Step does not exist')

                # Check if the step has an associated datapoint
                if DataPoint.select().where(
                        ExperimentStep == step).count() < 1:
                    step.generate_datapoint()

                # Convert step to dict
                step_d = model_to_dict(step)

                # Set the experiment id (different from the step id)
                step_d['experiment_configuration_id'] = step_d[
                    'experiment_configuration']['id']

                # Remove datetime and experiment configuration from the dict
                # They are not needed in the client, and they are not directly serializable to json (due to missing datetime format)
                del (step_d['created'])
                del (step_d['experiment_configuration'])

                # Return the step if it exists
                return step_d
            # Check if the step even exists
            except DoesNotExist:
                # It is OK if it does not exist, we should just stop measuring
                print('No more steps ready')

                # Return None if no step exists
                return None
 def save_pulse_counters(self, pulse_counters):  # type: (List[PulseCounterDTO]) -> None
     pulse_counters_to_save = []
     for pulse_counter_dto in pulse_counters:
         pulse_counter = PulseCounter.get_or_none(number=pulse_counter_dto.id)  # type: PulseCounter
         if pulse_counter is None:
             raise DoesNotExist('A PulseCounter with id {0} could not be found'.format(pulse_counter_dto.id))
         if pulse_counter.source == 'master':
             # Only master pulse counters will be passed to the MasterController batch save
             pulse_counters_to_save.append(pulse_counter_dto)
             if 'name' in pulse_counter_dto.loaded_fields:
                 pulse_counter.name = pulse_counter_dto.name
         elif pulse_counter.source == 'gateway':
             pulse_counter = PulseCounterMapper.dto_to_orm(pulse_counter_dto)
         else:
             logger.warning('Trying to save a PulseCounter with unknown source {0}'.format(pulse_counter.source))
             continue
         if 'room' in pulse_counter_dto.loaded_fields:
             if pulse_counter_dto.room is None:
                 pulse_counter.room = None
             elif 0 <= pulse_counter_dto.room <= 100:
                 pulse_counter.room, _ = Room.get_or_create(number=pulse_counter_dto.room)
         pulse_counter.save()
     self._master_controller.save_pulse_counters(pulse_counters_to_save)
Ejemplo n.º 6
0
def test_create_reset_password_token_with_unknown_email(get_mock):
    get_mock.side_effect = DoesNotExist()

    result = CreateResetPasswordToken().execute(email='unknown_email')

    assert not result
Ejemplo n.º 7
0
def test_sign_in_with_unknown_token(get_mock):
    get_mock.side_effect = DoesNotExist()

    with pytest.raises(InvalidCredentialsError):
        SignIn().execute(email='*****@*****.**', password='******')
Ejemplo n.º 8
0
def mock_not_found_Account_get(*args, **kwargs):
    raise DoesNotExist()
Ejemplo n.º 9
0
 def mock_get(*args, **kwargs):
     raise DoesNotExist()
def test_reset_password_with_unknown_email(get_mock):
    get_mock.side_effect = DoesNotExist()

    with pytest.raises(DoesNotExist):
        ResetPassword().execute(reset_password_token='unknown',
                                password='******')
Ejemplo n.º 11
0
def test_activate_user_with_unknown_token(get_mock):
    get_mock.side_effect = DoesNotExist()

    with pytest.raises(DoesNotExist):
        ActivateUser().execute(activation_token='unknown_token')