Skip to content

Commit

Permalink
Abandon test suite for loops for pytest best practices
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtmckee committed Jul 30, 2024
1 parent 8ef7bbe commit 61f35ab
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 120 deletions.
110 changes: 58 additions & 52 deletions tests/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,61 +17,67 @@ def test_different_inputs():
assert sqids.decode(sqids.encode(numbers)) == numbers


def test_incremental_numbers():
@pytest.mark.parametrize(
"id_str, numbers",
(
("bM", [0]),
("Uk", [1]),
("gb", [2]),
("Ef", [3]),
("Vq", [4]),
("uw", [5]),
("OI", [6]),
("AX", [7]),
("p6", [8]),
("nJ", [9]),
)
)
def test_incremental_numbers(id_str, numbers):
sqids = Sqids()
ids = {
"bM": [0],
"Uk": [1],
"gb": [2],
"Ef": [3],
"Vq": [4],
"uw": [5],
"OI": [6],
"AX": [7],
"p6": [8],
"nJ": [9],
}
for id_str, numbers in ids.items():
assert sqids.encode(numbers) == id_str
assert sqids.decode(id_str) == numbers


def test_incremental_numbers_same_index_0():
assert sqids.encode(numbers) == id_str
assert sqids.decode(id_str) == numbers


@pytest.mark.parametrize(
"id_str, numbers",
(
("SvIz", [0, 0]),
("n3qa", [0, 1]),
("tryF", [0, 2]),
("eg6q", [0, 3]),
("rSCF", [0, 4]),
("sR8x", [0, 5]),
("uY2M", [0, 6]),
("74dI", [0, 7]),
("30WX", [0, 8]),
("moxr", [0, 9]),
)
)
def test_incremental_numbers_same_index_0(id_str, numbers):
sqids = Sqids()
ids = {
"SvIz": [0, 0],
"n3qa": [0, 1],
"tryF": [0, 2],
"eg6q": [0, 3],
"rSCF": [0, 4],
"sR8x": [0, 5],
"uY2M": [0, 6],
"74dI": [0, 7],
"30WX": [0, 8],
"moxr": [0, 9],
}
for id_str, numbers in ids.items():
assert sqids.encode(numbers) == id_str
assert sqids.decode(id_str) == numbers


def test_incremental_numbers_same_index_1():
assert sqids.encode(numbers) == id_str
assert sqids.decode(id_str) == numbers


@pytest.mark.parametrize(
"id_str, numbers",
(
("SvIz", [0, 0]),
("nWqP", [1, 0]),
("tSyw", [2, 0]),
("eX68", [3, 0]),
("rxCY", [4, 0]),
("sV8a", [5, 0]),
("uf2K", [6, 0]),
("7Cdk", [7, 0]),
("3aWP", [8, 0]),
("m2xn", [9, 0]),
)
)
def test_incremental_numbers_same_index_1(id_str, numbers):
sqids = Sqids()
ids = {
"SvIz": [0, 0],
"nWqP": [1, 0],
"tSyw": [2, 0],
"eX68": [3, 0],
"rxCY": [4, 0],
"sV8a": [5, 0],
"uf2K": [6, 0],
"7Cdk": [7, 0],
"3aWP": [8, 0],
"m2xn": [9, 0],
}
for id_str, numbers in ids.items():
assert sqids.encode(numbers) == id_str
assert sqids.decode(id_str) == numbers
assert sqids.encode(numbers) == id_str
assert sqids.decode(id_str) == numbers


def test_multi_input():
Expand Down
144 changes: 76 additions & 68 deletions tests/test_minlength.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,77 +14,85 @@ def test_simple():
assert sqids.decode(id_str) == numbers


def test_incremental():
@pytest.mark.parametrize(
"min_length, expected_id",
(
(6, "86Rf07"),
(7, "86Rf07x"),
(8, "86Rf07xd"),
(9, "86Rf07xd4"),
(10, "86Rf07xd4z"),
(11, "86Rf07xd4zB"),
(12, "86Rf07xd4zBm"),
(13, "86Rf07xd4zBmi"),
(
len(DEFAULT_ALPHABET) + 0,
"86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTM",
),
(
len(DEFAULT_ALPHABET) + 1,
"86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTMy",
),
(
len(DEFAULT_ALPHABET) + 2,
"86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTMyf",
),
(
len(DEFAULT_ALPHABET) + 3,
"86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTMyf1",
),
)
)
def test_incremental(min_length, expected_id):
numbers = [1, 2, 3]
map = {
6: "86Rf07",
7: "86Rf07x",
8: "86Rf07xd",
9: "86Rf07xd4",
10: "86Rf07xd4z",
11: "86Rf07xd4zB",
12: "86Rf07xd4zBm",
13: "86Rf07xd4zBmi",
}

map[
len(DEFAULT_ALPHABET) + 0
] = "86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTM"
map[
len(DEFAULT_ALPHABET) + 1
] = "86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTMy"
map[
len(DEFAULT_ALPHABET) + 2
] = "86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTMyf"
map[
len(DEFAULT_ALPHABET) + 3
] = "86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTMyf1"

for min_length, id in map.items():
sqids = Sqids(min_length=min_length)

assert sqids.encode(numbers) == id
assert len(sqids.encode(numbers)) == min_length
assert sqids.decode(id) == numbers


def test_incremental_numbers():
sqids = Sqids(min_length=min_length)

assert sqids.encode(numbers) == expected_id
assert len(sqids.encode(numbers)) == min_length
assert sqids.decode(expected_id) == numbers


@pytest.mark.parametrize(
"id_str, numbers",
(
("SvIzsqYMyQwI3GWgJAe17URxX8V924Co0DaTZLtFjHriEn5bPhcSkfmvOslpBu", [0, 0]),
("n3qafPOLKdfHpuNw3M61r95svbeJGk7aAEgYn4WlSjXURmF8IDqZBy0CT2VxQc", [0, 1]),
("tryFJbWcFMiYPg8sASm51uIV93GXTnvRzyfLleh06CpodJD42B7OraKtkQNxUZ", [0, 2]),
("eg6ql0A3XmvPoCzMlB6DraNGcWSIy5VR8iYup2Qk4tjZFKe1hbwfgHdUTsnLqE", [0, 3]),
("rSCFlp0rB2inEljaRdxKt7FkIbODSf8wYgTsZM1HL9JzN35cyoqueUvVWCm4hX", [0, 4]),
("sR8xjC8WQkOwo74PnglH1YFdTI0eaf56RGVSitzbjuZ3shNUXBrqLxEJyAmKv2", [0, 5]),
("uY2MYFqCLpgx5XQcjdtZK286AwWV7IBGEfuS9yTmbJvkzoUPeYRHr4iDs3naN0", [0, 6]),
("74dID7X28VLQhBlnGmjZrec5wTA1fqpWtK4YkaoEIM9SRNiC3gUJH0OFvsPDdy", [0, 7]),
("30WXpesPhgKiEI5RHTY7xbB1GnytJvXOl2p0AcUjdF6waZDo9Qk8VLzMuWrqCS", [0, 8]),
("moxr3HqLAK0GsTND6jowfZz3SUx7cQ8aC54Pl1RbIvFXmEJuBMYVeW9yrdOtin", [0, 9]),
)
)
def test_incremental_numbers(id_str, numbers):
sqids = Sqids(min_length=len(DEFAULT_ALPHABET))

ids = {
"SvIzsqYMyQwI3GWgJAe17URxX8V924Co0DaTZLtFjHriEn5bPhcSkfmvOslpBu": [0, 0],
"n3qafPOLKdfHpuNw3M61r95svbeJGk7aAEgYn4WlSjXURmF8IDqZBy0CT2VxQc": [0, 1],
"tryFJbWcFMiYPg8sASm51uIV93GXTnvRzyfLleh06CpodJD42B7OraKtkQNxUZ": [0, 2],
"eg6ql0A3XmvPoCzMlB6DraNGcWSIy5VR8iYup2Qk4tjZFKe1hbwfgHdUTsnLqE": [0, 3],
"rSCFlp0rB2inEljaRdxKt7FkIbODSf8wYgTsZM1HL9JzN35cyoqueUvVWCm4hX": [0, 4],
"sR8xjC8WQkOwo74PnglH1YFdTI0eaf56RGVSitzbjuZ3shNUXBrqLxEJyAmKv2": [0, 5],
"uY2MYFqCLpgx5XQcjdtZK286AwWV7IBGEfuS9yTmbJvkzoUPeYRHr4iDs3naN0": [0, 6],
"74dID7X28VLQhBlnGmjZrec5wTA1fqpWtK4YkaoEIM9SRNiC3gUJH0OFvsPDdy": [0, 7],
"30WXpesPhgKiEI5RHTY7xbB1GnytJvXOl2p0AcUjdF6waZDo9Qk8VLzMuWrqCS": [0, 8],
"moxr3HqLAK0GsTND6jowfZz3SUx7cQ8aC54Pl1RbIvFXmEJuBMYVeW9yrdOtin": [0, 9],
}

for id_str, numbers in ids.items():
assert sqids.encode(numbers) == id_str
assert sqids.decode(id_str) == numbers


def test_min_lengths():
for min_length in [0, 1, 5, 10, len(DEFAULT_ALPHABET)]:
for numbers in [
[0],
[0, 0, 0, 0, 0],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[100, 200, 300],
[1000, 2000, 3000],
[1000000],
[sys.maxsize],
]:
sqids = Sqids(min_length=min_length)

id_str = sqids.encode(numbers)
assert len(id_str) >= min_length
assert sqids.decode(id_str) == numbers
assert sqids.encode(numbers) == id_str
assert sqids.decode(id_str) == numbers


@pytest.mark.parametrize("min_length", (0, 1, 5, 10, len(DEFAULT_ALPHABET)))
@pytest.mark.parametrize(
"numbers",
(
[0],
[0, 0, 0, 0, 0],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[100, 200, 300],
[1000, 2000, 3000],
[1000000],
[sys.maxsize],
),
)
def test_min_lengths(min_length, numbers):
sqids = Sqids(min_length=min_length)

id_str = sqids.encode(numbers)
assert len(id_str) >= min_length
assert sqids.decode(id_str) == numbers


def test_out_of_range_invalid_min_length():
Expand Down

0 comments on commit 61f35ab

Please sign in to comment.