File size: 3,769 Bytes
a5784e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""
High-quality tests for api_utils/common_utils.py - Random ID generation.

Focus: Test random_id function with various lengths and verify output format.
Strategy: Test default/custom lengths, character set, uniqueness.
"""

import re

from api_utils.common_utils import random_id


def test_random_id_default_length():
    """
    Test scenario: Generate random ID with default length
    Expected: Return 24-character string (lines 5-6)
    """
    result = random_id()

    # Verify: Length is 24
    assert len(result) == 24

    # Verify: Only contains lowercase letters and numbers
    assert re.match(r"^[a-z0-9]+$", result)


def test_random_id_custom_length_short():
    """
    Test scenario: Generate random ID with short length (5)
    Expected: Return 5-character string
    """
    result = random_id(5)

    # Verify: Length is 5
    assert len(result) == 5

    # Verify: Only contains lowercase letters and numbers
    assert re.match(r"^[a-z0-9]+$", result)


def test_random_id_custom_length_long():
    """
    Test scenario: Generate random ID with long length (100)
    Expected: Return 100-character string
    """
    result = random_id(100)

    # Verify: Length is 100
    assert len(result) == 100

    # Verify: Only contains lowercase letters and numbers
    assert re.match(r"^[a-z0-9]+$", result)


def test_random_id_length_one():
    """
    Test scenario: Generate random ID with length 1
    Expected: Return 1-character string
    """
    result = random_id(1)

    # Verify: Length is 1
    assert len(result) == 1

    # Verify: Character is a lowercase letter or number
    assert result in "abcdefghijklmnopqrstuvwxyz0123456789"


def test_random_id_length_zero():
    """
    Test scenario: Generate random ID with length 0
    Expected: Return empty string
    """
    result = random_id(0)

    # Verify: Empty string
    assert result == ""
    assert len(result) == 0


def test_random_id_character_set():
    """
    Test scenario: Verify character set only contains lowercase letters and numbers
    Expected: Does not contain uppercase letters, special characters, or spaces (line 5)
    """
    result = random_id(50)

    # Verify: Each character is in the expected character set
    charset = "abcdefghijklmnopqrstuvwxyz0123456789"
    for char in result:
        assert char in charset


def test_random_id_uniqueness():
    """
    Test scenario: Multiple calls return different values
    Expected: Generated IDs have high uniqueness
    """
    results = [random_id() for _ in range(100)]

    # Verify: 100 calls have at least 95 different values (considering minimal probability of collision)
    unique_results = set(results)
    assert len(unique_results) >= 95


def test_random_id_no_uppercase():
    """
    Test scenario: Verify no uppercase letters included
    Expected: Output does not contain A-Z
    """
    result = random_id(50)

    # Verify: No uppercase letters
    assert not any(char.isupper() for char in result)


def test_random_id_no_special_characters():
    """
    Test scenario: Verify no special characters included
    Expected: Output only contains alphanumeric characters
    """
    result = random_id(50)

    # Verify: Is alphanumeric
    assert result.isalnum()

    # Verify: No spaces, punctuation, or other special characters
    assert not any(not char.isalnum() for char in result)


def test_random_id_multiple_calls_different_values():
    """
    Test scenario: Consecutive calls should return different values
    Expected: Two calls return different IDs (high probability)
    """
    id1 = random_id()
    id2 = random_id()

    # Verify: Extremely high probability of being different (theoretically could be same but probability is very low)
    assert id1 != id2