def test_should_raise_type_error_when_characters_is_not_a_string_or_is_empty(
            self, value):
        with pytest.raises(TypeError) as exc_info:
            random_values.rand_string(characters=value)

        assert (f'characters must be a non empty string'
                f' but you provided {value}') == str(exc_info.value)
Beispiel #2
0
    def random_value(self) -> Union[int, str]:
        """Returns a valid random value according to the field format."""
        if self._format == 'b':
            return rand_signed_bytes()

        elif self._format == 'B':
            return rand_bytes()

        elif self._format == 'h':
            return rand_signed_short()

        elif self._format == 'H':
            return rand_short()

        elif self._format == 'i':
            return rand_signed_int()

        elif self._format == 'I':
            return rand_int()

        elif self._format == 'q':
            return rand_signed_long()

        elif self._format == 'Q':
            return rand_long()
        else:
            return rand_string(int(self._format[:-1]))
    def test_should_return_compliant_string(self):
        characters = 'ABCDEFGH'
        value = random_values.rand_string(10, characters)

        assert isinstance(value, str)
        assert len(value) == 10
        assert any([character in value for character in characters])
Beispiel #4
0
 def random_value(self) -> AnyStr:
     """
     Returns a random string or bytes. The length of the string is either the max length if given or the length
     of the default attribute.
     """
     length = self._max_length if self._max_length is not None else len(
         self._default)
     random_string = rand_string(length)
     return random_string if self._decode else random_string.encode()
    def test_should_compute_length_if_not_given(self, mocker):
        randint_mock = mocker.patch('random.randint', return_value=30)
        value = random_values.rand_string()

        assert isinstance(value, str)
        randint_mock.assert_called_once_with(20, 150)
    def test_should_raise_value_error_when_length_is_less_than_0(self, value):
        with pytest.raises(ValueError) as exc_info:
            random_values.rand_string(value)

        assert f'length must be greater than 0 but you provided {value}' == str(
            exc_info.value)
    def test_should_raise_type_error_when_length_is_not_an_integer(self):
        with pytest.raises(TypeError) as exc_info:
            random_values.rand_string(4.5)

        assert 'length must be a positive integer but you provided 4.5' == str(
            exc_info.value)