|
| 1 | +"""Tests for miscellaneous utility functions.""" |
| 2 | + |
| 3 | +import string |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from foca.utils.misc import generate_id |
| 8 | + |
| 9 | + |
| 10 | +class TestGenerateId: |
| 11 | + |
| 12 | + def test_default(self): |
| 13 | + """Use only default arguments.""" |
| 14 | + res = generate_id() |
| 15 | + assert isinstance(res, str) |
| 16 | + |
| 17 | + def test_charset_literal_string(self): |
| 18 | + """Argument to `charset` is non-default literal string.""" |
| 19 | + charset = string.digits |
| 20 | + res = generate_id(charset=charset) |
| 21 | + assert set(res) <= set(string.digits) |
| 22 | + |
| 23 | + def test_charset_literal_string_duplicates(self): |
| 24 | + """Argument to `charset` is non-default literal string with duplicates. |
| 25 | + """ |
| 26 | + charset = string.digits + string.digits |
| 27 | + res = generate_id(charset=charset) |
| 28 | + assert set(res) <= set(string.digits) |
| 29 | + |
| 30 | + def test_charset_evaluates_to_string(self): |
| 31 | + """Argument to `charset` evaluates to string.""" |
| 32 | + charset = "''.join([c for c in string.digits])" |
| 33 | + res = generate_id(charset=charset) |
| 34 | + assert set(res) <= set(string.digits) |
| 35 | + |
| 36 | + def test_charset_evaluates_to_empty_string(self): |
| 37 | + """Argument to `charset` evaluates to non-string.""" |
| 38 | + charset = "''.join([])" |
| 39 | + with pytest.raises(TypeError): |
| 40 | + generate_id(charset=charset) |
| 41 | + |
| 42 | + def test_charset_evaluates_to_non_string(self): |
| 43 | + """Argument to `charset` evaluates to non-string.""" |
| 44 | + charset = "1" |
| 45 | + with pytest.raises(TypeError): |
| 46 | + generate_id(charset=charset) |
| 47 | + |
| 48 | + def test_evaluation_error(self): |
| 49 | + """Evaulation of `length` raises an exception.""" |
| 50 | + charset = int |
| 51 | + with pytest.raises(TypeError): |
| 52 | + generate_id(charset=charset) # type: ignore |
| 53 | + |
| 54 | + def test_length(self): |
| 55 | + """Non-default argument to `length`.""" |
| 56 | + length = 1 |
| 57 | + res = generate_id(length=length) |
| 58 | + assert len(res) == length |
| 59 | + |
| 60 | + def test_length_not_int(self): |
| 61 | + """Argument to `length` is not an integer.""" |
| 62 | + length = "" |
| 63 | + with pytest.raises(TypeError): |
| 64 | + generate_id(length=length) # type: ignore |
| 65 | + |
| 66 | + def test_length_not_positive(self): |
| 67 | + """Argument to `length` is not a positive integer.""" |
| 68 | + length = -1 |
| 69 | + with pytest.raises(TypeError): |
| 70 | + generate_id(length=length) |
0 commit comments