Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/commands/test_analyze_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_analyze_wheel_variant_custom_label(
assert (
capsys.readouterr().out
== """\
############################## Variant: `60567bd9` #############################
############################## Variant: `foo` #############################
installable_plugin :: feat1 :: val1c
################################################################################
"""
Expand Down
89 changes: 27 additions & 62 deletions tests/models/test_variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import pytest
from hypothesis import given
from hypothesis import strategies as st
from variantlib.constants import NULL_VARIANT_LABEL
from variantlib.constants import VALIDATION_FEATURE_NAME_REGEX
from variantlib.constants import VALIDATION_NAMESPACE_REGEX
from variantlib.constants import VALIDATION_VALUE_REGEX
from variantlib.constants import VALIDATION_VARIANT_LABEL_REGEX
from variantlib.errors import ValidationError
from variantlib.models.variant import VARIANT_HASH_LENGTH
from variantlib.models.variant import VariantDescription
Expand Down Expand Up @@ -177,29 +179,6 @@ def test_from_str_invalid_format() -> None:
VariantProperty.from_str(input_str)


def test_variantprop_serialization() -> None:
vprop = VariantProperty(namespace="provider", feature="feature", value="value")
assert vprop.serialize() == {
"namespace": "provider",
"feature": "feature",
"value": "value",
}


def test_variantprop_deserialization() -> None:
data = {
"namespace": "provider",
"feature": "feature",
"value": "value",
}

vprop = VariantProperty.deserialize(data)

assert vprop.namespace == data["namespace"]
assert vprop.feature == data["feature"]
assert vprop.value == data["value"]


def test_variantprop_sorting() -> None:
data = [
VariantProperty("z", "a", "a"),
Expand Down Expand Up @@ -237,6 +216,7 @@ def test_null_variant() -> None:
vdesc = VariantDescription()
assert vdesc.properties == []
assert vdesc.hexdigest == hashlib.sha256(b"").hexdigest()[:VARIANT_HASH_LENGTH]
assert vdesc.label == NULL_VARIANT_LABEL


def test_variantdescription_initialization() -> None:
Expand All @@ -247,7 +227,8 @@ def test_variantdescription_initialization() -> None:
vprop2 = VariantProperty(
namespace="tyrell_corporation", feature="client_id", value="secret_pass"
)
vdesc = VariantDescription([vprop1, vprop2])
vdesc = VariantDescription([vprop1, vprop2], label="test")
assert vdesc.label == "test"

# Check that the _data property is a list
assert isinstance(vdesc.properties, list)
Expand Down Expand Up @@ -280,14 +261,13 @@ def test_variantdescription_duplicate_data() -> None:


def test_variantdescription_partial_duplicate_data() -> None:
# Test that duplicate VariantProperty instances are removed
vprop1 = VariantProperty(
namespace="omnicorp", feature="custom_feat", value="secret_value"
)
vprop2 = VariantProperty(
namespace="omnicorp", feature="custom_feat", value="another_value"
)
VariantDescription([vprop1, vprop2])
VariantDescription([vprop1, vprop2], label="test")


def test_variantdescription_sorted_data() -> None:
Expand All @@ -301,7 +281,7 @@ def test_variantdescription_sorted_data() -> None:
vprop3 = VariantProperty(
namespace="omnicorp", feature="secret_pass", value="client_value"
)
vdesc = VariantDescription([vprop1, vprop2, vprop3])
vdesc = VariantDescription([vprop1, vprop2, vprop3], label="test")

# Check that data is sorted by namespace, feature, and value
sorted_vprops = sorted(
Expand All @@ -319,7 +299,7 @@ def test_variantdescription_hexdigest() -> None:
namespace="tyrell_corporation", feature="client_id", value="secret_pass"
)
vprops = [vprop1, vprop2]
vdesc = VariantDescription(vprops)
vdesc = VariantDescription(vprops, label="test")

# Compute the expected hash using shake_128 (mock the hash output for testing)
hash_object = hashlib.sha256(
Expand All @@ -337,48 +317,19 @@ def test_variantdescription_hexdigest_adjacent_strings() -> None:
[
VariantProperty("a", "b", "cx"),
VariantProperty("d", "e", "f"),
]
],
label="test",
).hexdigest
!= VariantDescription(
[
VariantProperty("a", "b", "c"),
VariantProperty("xd", "e", "f"),
]
],
label="another",
).hexdigest
)


def test_variantdescription_serialization() -> None:
vprop = VariantProperty(namespace="provider", feature="feature", value="value")
vdesc = VariantDescription(properties=[vprop])

assert vdesc.serialize() == [
{
"namespace": "provider",
"feature": "feature",
"value": "value",
}
]


def test_variantdescription_deserialization() -> None:
data = [
{
"namespace": "provider",
"feature": "feature",
"value": "value",
}
]

vdesc = VariantDescription.deserialize(data)

assert len(vdesc.properties) == 1
assert vdesc.properties[0].namespace == "provider"
assert vdesc.properties[0].feature == "feature"
assert vdesc.properties[0].value == "value"
assert vdesc.hexdigest == "c44d3adf"


# -----------------------------------------------
# Fuzzy Testing
# -----------------------------------------------
Expand Down Expand Up @@ -444,7 +395,7 @@ def test_fuzzy_variantprop(namespace: str, feature: str, value: str) -> None:
)
def test_fuzzy_variantdescription(vprop: list[VariantProperty]) -> None:
# Fuzzy test for random combinations of VariantDescription
vdesc = VariantDescription(vprop)
vdesc = VariantDescription(vprop, label="test")
assert isinstance(vdesc.properties, list)
assert len(vdesc.properties) >= 1

Expand All @@ -468,8 +419,22 @@ def test_fuzzy_variantdescription(vprop: list[VariantProperty]) -> None:
value=st.from_regex(VALIDATION_NAMESPACE_REGEX, fullmatch=True),
),
),
label=st.from_regex(VALIDATION_VARIANT_LABEL_REGEX, fullmatch=True),
)
)
def test_random_hexdigest(vdesc: VariantDescription) -> None:
assert isinstance(vdesc.hexdigest, str)
assert len(vdesc.hexdigest) == VARIANT_HASH_LENGTH


def test_null_variant_label():
with pytest.raises(
ValidationError,
match=rf"{NULL_VARIANT_LABEL!r} label can be used only for the null variant",
):
VariantDescription([VariantProperty("a", "b", "c")], label=NULL_VARIANT_LABEL)
with pytest.raises(
ValidationError,
match=rf"Null variant must always use {NULL_VARIANT_LABEL!r} label",
):
VariantDescription(label="zuul")
54 changes: 11 additions & 43 deletions tests/resolver/test_filtering.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

import copy
import random
from collections import deque

import pytest
Expand All @@ -12,7 +10,6 @@
from variantlib.resolver.filtering import filter_variants_by_features
from variantlib.resolver.filtering import filter_variants_by_namespaces
from variantlib.resolver.filtering import filter_variants_by_property
from variantlib.resolver.filtering import remove_duplicates


@pytest.fixture(scope="session")
Expand All @@ -32,41 +29,12 @@ def vdescs(vprops: list[VariantProperty]) -> list[VariantDescription]:
vprop1, vprop2 = vprops

return [
VariantDescription([vprop1]),
VariantDescription([vprop2]),
VariantDescription([vprop1, vprop2]),
VariantDescription([vprop1], label="a"),
VariantDescription([vprop2], label="b"),
VariantDescription([vprop1, vprop2], label="c"),
]


# =========================== `remove_duplicates` =========================== #


def test_remove_duplicates(vdescs: list[VariantDescription]) -> None:
assert len(vdescs) == 3

# using `copy.deepcopy` to ensure that all objects are actually unique
input_vdescs = [copy.deepcopy(random.choice(vdescs)) for _ in range(100)]
filtered_vdescs = list(remove_duplicates(input_vdescs))

assert len(filtered_vdescs) == 3

for vdesc in vdescs:
assert vdesc in filtered_vdescs


def test_remove_duplicates_empty() -> None:
assert list(remove_duplicates([])) == []


@pytest.mark.parametrize(
"vdescs",
["not a list", ["not a VariantDescription"]],
)
def test_remove_duplicates_validation_error(vdescs: list[VariantDescription]) -> None:
with pytest.raises(ValidationError):
deque(remove_duplicates(vdescs=vdescs), maxlen=0)


# ===================== `filter_variants_by_namespaces` ===================== #


Expand Down Expand Up @@ -139,11 +107,11 @@ def test_filter_variants_by_namespaces(vdescs: list[VariantDescription]) -> None
("vdescs", "forbidden_namespaces"),
[
(
[VariantDescription([VariantProperty("a", "b", "c")])],
[VariantDescription([VariantProperty("a", "b", "c")], label="test")],
"not a list",
),
(
[VariantDescription([VariantProperty("a", "b", "c")])],
[VariantDescription([VariantProperty("a", "b", "c")], label="test")],
[VariantProperty("not", "a", "str")],
),
("not a list", ["omnicorp"]),
Expand Down Expand Up @@ -245,11 +213,11 @@ def test_filter_variants_by_features(
("vdescs", "forbidden_features"),
[
(
[VariantDescription([VariantProperty("a", "b", "c")])],
[VariantDescription([VariantProperty("a", "b", "c")], label="test")],
"not a list",
),
(
[VariantDescription([VariantProperty("a", "b", "c")])],
[VariantDescription([VariantProperty("a", "b", "c")], label="test")],
["not a `VariantFeature`"],
),
("not a list", VariantFeature("a", "b")),
Expand Down Expand Up @@ -453,22 +421,22 @@ def test_filter_variants_by_property(
[VariantProperty("a", "b", "c")],
),
(
[VariantDescription([VariantProperty("a", "b", "c")])],
[VariantDescription([VariantProperty("a", "b", "c")], label="test")],
"not a list",
[VariantProperty("a", "b", "c")],
),
(
[VariantDescription([VariantProperty("a", "b", "c")])],
[VariantDescription([VariantProperty("a", "b", "c")], label="test")],
["not a `VariantFeature`"],
[VariantProperty("a", "b", "c")],
),
(
[VariantDescription([VariantProperty("a", "b", "c")])],
[VariantDescription([VariantProperty("a", "b", "c")], label="test")],
[VariantProperty("a", "b", "c")],
"not a list",
),
(
[VariantDescription([VariantProperty("a", "b", "c")])],
[VariantDescription([VariantProperty("a", "b", "c")], label="test")],
[VariantProperty("a", "b", "c")],
["not a `VariantFeature`"],
),
Expand Down
Loading
Loading