|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tests for helper methods for processing strings with minimal dependencies. |
| 16 | +
|
| 17 | +Please keep the dependencies used in this subpackage to a minimum to avoid the |
| 18 | +risk of circular dependencies. |
| 19 | +""" |
| 20 | + |
| 21 | +import base64 |
| 22 | +import random |
| 23 | +import sys |
| 24 | +import uuid |
| 25 | + |
| 26 | +import pytest |
| 27 | + |
| 28 | +from bigframes._tools import strings |
| 29 | + |
| 30 | +# To stress test some unicode comparisons. |
| 31 | +# https://stackoverflow.com/a/39682429/101923 |
| 32 | +ALL_UNICODE_CHARS = "".join(chr(i) for i in range(32, 0x110000) if chr(i).isprintable()) |
| 33 | +RANDOM_STRINGS = ( |
| 34 | + pytest.param(str(uuid.uuid4()), id="uuid4"), |
| 35 | + pytest.param(hex(random.randint(0, sys.maxsize)), id="hex"), |
| 36 | + pytest.param( |
| 37 | + base64.b64encode( |
| 38 | + "".join(random.choice(ALL_UNICODE_CHARS) for _ in range(100)).encode( |
| 39 | + "utf-8" |
| 40 | + ) |
| 41 | + ).decode("utf-8"), |
| 42 | + id="base64", |
| 43 | + ), |
| 44 | + pytest.param( |
| 45 | + "".join(random.choice(ALL_UNICODE_CHARS) for _ in range(8)), id="unicode8" |
| 46 | + ), |
| 47 | + pytest.param( |
| 48 | + "".join(random.choice(ALL_UNICODE_CHARS) for _ in range(64)), id="unicode64" |
| 49 | + ), |
| 50 | +) |
| 51 | + |
| 52 | + |
| 53 | +def random_char_not_equal(avoid: str): |
| 54 | + random_char = avoid |
| 55 | + while random_char == avoid: |
| 56 | + random_char = random.choice(ALL_UNICODE_CHARS) |
| 57 | + return random_char |
| 58 | + |
| 59 | + |
| 60 | +def random_deletion(original: str): |
| 61 | + """original string with one character removed""" |
| 62 | + char_index = random.randrange(len(original)) |
| 63 | + return original[:char_index] + original[char_index + 1 :] |
| 64 | + |
| 65 | + |
| 66 | +def random_insertion(original: str): |
| 67 | + char_index = random.randrange(len(original)) |
| 68 | + random_char = random.choice(ALL_UNICODE_CHARS) |
| 69 | + return original[: char_index + 1] + random_char + original[char_index + 1 :] |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.parametrize( |
| 73 | + ("left", "right", "expected"), |
| 74 | + ( |
| 75 | + ("", "", 0), |
| 76 | + ("abc", "abc", 0), |
| 77 | + # Deletions |
| 78 | + ("abcxyz", "abc", 3), |
| 79 | + ("xyzabc", "abc", 3), |
| 80 | + ("AXYZBC", "ABC", 3), |
| 81 | + ("AXYZBC", "XYZ", 3), |
| 82 | + # Insertions |
| 83 | + ("abc", "abcxyz", 3), |
| 84 | + ("abc", "xyzabc", 3), |
| 85 | + # Substitutions |
| 86 | + ("abc", "aBc", 1), |
| 87 | + ("abcxyz", "aBcXyZ", 3), |
| 88 | + # Combinations |
| 89 | + ("abcdefxyz", "abcExyzα", 4), |
| 90 | + ), |
| 91 | +) |
| 92 | +def test_levenshtein_distance(left: str, right: str, expected: int): |
| 93 | + assert strings.levenshtein_distance(left, right) == expected |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.parametrize(("random_string",), RANDOM_STRINGS) |
| 97 | +def test_levenshtein_distance_equal_strings(random_string: str): |
| 98 | + """Mini fuzz test with different strings.""" |
| 99 | + assert strings.levenshtein_distance(random_string, random_string) == 0 |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.parametrize(("random_string",), RANDOM_STRINGS) |
| 103 | +def test_levenshtein_distance_random_deletion(random_string: str): |
| 104 | + """Mini fuzz test with different strings.""" |
| 105 | + |
| 106 | + num_deleted = random.randrange(1, min(10, len(random_string))) |
| 107 | + assert 1 <= num_deleted < len(random_string) |
| 108 | + |
| 109 | + deleted = random_string |
| 110 | + for _ in range(num_deleted): |
| 111 | + deleted = random_deletion(deleted) |
| 112 | + |
| 113 | + assert deleted != random_string |
| 114 | + assert len(deleted) == len(random_string) - num_deleted |
| 115 | + assert strings.levenshtein_distance(random_string, deleted) == num_deleted |
| 116 | + |
| 117 | + |
| 118 | +@pytest.mark.parametrize(("random_string",), RANDOM_STRINGS) |
| 119 | +def test_levenshtein_distance_random_insertion(random_string: str): |
| 120 | + """Mini fuzz test with different strings.""" |
| 121 | + |
| 122 | + num_inserted = random.randrange(1, min(10, len(random_string))) |
| 123 | + assert 1 <= num_inserted < len(random_string) |
| 124 | + |
| 125 | + inserted = random_string |
| 126 | + for _ in range(num_inserted): |
| 127 | + inserted = random_insertion(inserted) |
| 128 | + |
| 129 | + assert inserted != random_string |
| 130 | + assert len(inserted) == len(random_string) + num_inserted |
| 131 | + assert strings.levenshtein_distance(random_string, inserted) == num_inserted |
| 132 | + |
| 133 | + |
| 134 | +@pytest.mark.parametrize(("random_string",), RANDOM_STRINGS) |
| 135 | +def test_levenshtein_distance_random_substitution(random_string: str): |
| 136 | + """Mini fuzz test with different strings. |
| 137 | +
|
| 138 | + Note: we don't do multiple substitutions here to avoid accidentally |
| 139 | + substituting the same character twice. |
| 140 | + """ |
| 141 | + char_index = random.randrange(len(random_string)) |
| 142 | + replaced_char = random_string[char_index] |
| 143 | + random_char = random_char_not_equal(replaced_char) |
| 144 | + substituted = ( |
| 145 | + random_string[:char_index] + random_char + random_string[char_index + 1 :] |
| 146 | + ) |
| 147 | + assert substituted != random_string |
| 148 | + assert len(substituted) == len(random_string) |
| 149 | + assert strings.levenshtein_distance(random_string, substituted) == 1 |
0 commit comments