コード例 #1
0
ファイル: test_utils.py プロジェクト: uktrade/data-hub-api
    def test_max_retries_reached(self):
        """
        Test that if there are n max collisions, the function raises RuntimeError.
        """
        model = mock.Mock()
        model.objects.filter().exists.side_effect = [True] * 10

        with pytest.raises(RuntimeError):
            generate_reference(model, lambda: 'something')
コード例 #2
0
 def generate_public_token(cls):
     """
     :returns: a random unused public token of form
         <50 uppercase/lowercase letters, digits and symbols>
     :raises RuntimeError: if no public_token can be generated
     """
     gen = partial(secrets.token_urlsafe, 37)
     return generate_reference(model=cls, gen=gen, field='public_token')
コード例 #3
0
ファイル: test_utils.py プロジェクト: uktrade/data-hub-api
    def test_defaults(self):
        """
        Test that it returns the value from its `gen` if the value
        hasn't been used before.
        """
        model = mock.Mock()
        model.objects.filter().exists.return_value = False

        reference = generate_reference(model, lambda: 'something')
        assert reference == 'something'
コード例 #4
0
ファイル: test_utils.py プロジェクト: uktrade/data-hub-api
    def test_with_prefix(self):
        """
        Test that if a prefix is specified, it will be used to generate the reference.
        """
        model = mock.Mock()
        model.objects.filter().exists.return_value = False

        reference = generate_reference(model,
                                       lambda: 'something',
                                       prefix='pref/')
        assert reference == 'pref/something'
コード例 #5
0
ファイル: test_utils.py プロジェクト: uktrade/data-hub-api
    def test_with_collision(self):
        """
        Test that if gen returns a collision, the next available generated value
        is used instead.
        """
        model = mock.Mock()
        model.objects.filter().exists.side_effect = [True, False]
        gen = mock.Mock()
        gen.side_effect = ['1st try', '2nd try']

        reference = generate_reference(model, gen)
        assert reference == '2nd try'
コード例 #6
0
    def generate_reference(cls):
        """
        :returns: a random unused reference of form:
            <(3) letters><(3) numbers>/<year> e.g. GEA962/16
        :raises RuntimeError: if no reference can be generated
        """
        def gen():
            year_suffix = now().strftime('%y')
            return '{letters}{numbers}/{year}'.format(
                letters=get_random_string(length=3, allowed_chars='ACEFHJKMNPRTUVWXY'),
                numbers=get_random_string(length=3, allowed_chars='123456789'),
                year=year_suffix,
            )

        return generate_reference(model=cls, gen=gen)
コード例 #7
0
def generate_quote_reference(order):
    """
    :returns: a random unused reference of form:
            <order.reference>/Q-<(2) lettes>/<(1) number> e.g. GEA962/16/Q-AB1
    :raises RuntimeError: if no reference can be generated.
    """
    from datahub.omis.quote.models import Quote

    def gen():
        return '{letters}{numbers}'.format(
            letters=get_random_string(length=2,
                                      allowed_chars='ACEFHJKMNPRTUVWXY'),
            numbers=get_random_string(length=1, allowed_chars='123456789'),
        )

    return generate_reference(model=Quote,
                              gen=gen,
                              prefix=f'{order.reference}/Q-')