Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(validation): floating point rounding error validation #60

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Binary file removed .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ coverage.xml

*metadata/*.json
*images/*.png

.DS_Store
4 changes: 3 additions & 1 deletion src/common/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ def validate_config(config: dict) -> bool:
)
)

if sum(layer["weights"]) != 100:
# ensure the sum of the weights is 100
# make sure to round the sum to 100 to account for floating point errors
if round(sum(layer["weights"]), 2) != 100:
raise ConfigValidationError(
'config["layers"][{}]["{}"]: The sum of the weights must be 100. Current sum: {}'.format(
i, required_key[0], sum(layer["weights"])
Expand Down
216 changes: 216 additions & 0 deletions tests/test_floating_point_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
from unittest.mock import patch

from src.common.validate import validate_config


@patch("os.path.isfile", return_value=True)
def test_floating_point_errors(mock_isfile):
valid_config = {
"layers": [
{
"name": "Background",
"values": [
"1.4.png",
"1.4.png",
"1.4.png",
"1.4.png",
"2.5.png",
"1.4.png",
"1.7.png",
"9.2.png",
"2.5.png",
"1.4.png",
"0.3.png",
"1.7.png",
"1.7.png",
"1.2.png",
"0.7.png",
"1.4.png",
"0.9.png",
"1.4.png",
"1.7.png",
"0.5.png",
"1.7.png",
"1.4.png",
"0.2.png",
"1.png",
"2.2.png",
"1.7.png",
"1.8.png",
"1.4.png",
"1.7.png",
"1.4.png",
"1.7.png",
"1.4.png",
"1.4.png",
"1.4.png",
"1.4.png",
"1.4.png",
"1.4.png",
"1.4.png",
"1.7.png",
"1.7.png",
"1.8.png",
"1.7.png",
"1.4.png",
"1.4.png",
"1.7.png",
"2.png",
"0.7.png",
"1.8.png",
"0.9.png",
"1.8.png",
"1.4.png",
"1.4.png",
"1.4.png",
"1.7.png",
"1.7.png",
"1.8.png",
"1.7.png",
"1.png",
"1.7.png",
"1.4.png",
"0.9.png",
"1.7.png",
"1.8.png",
],
"trait_path": "./trait-layers/foreground",
"filename": [
"1.4.png",
"1.4.png",
"1.4.png",
"1.4.png",
"2.5.png",
"1.4.png",
"1.7.png",
"9.2.png",
"2.5.png",
"1.4.png",
"0.3.png",
"1.7.png",
"1.7.png",
"1.2.png",
"0.7.png",
"1.4.png",
"0.9.png",
"1.4.png",
"1.7.png",
"0.5.png",
"1.7.png",
"1.4.png",
"0.2.png",
"1.png",
"2.2.png",
"1.7.png",
"1.8.png",
"1.4.png",
"1.7.png",
"1.4.png",
"1.7.png",
"1.4.png",
"1.4.png",
"1.4.png",
"1.4.png",
"1.4.png",
"1.4.png",
"1.4.png",
"1.7.png",
"1.7.png",
"1.8.png",
"1.7.png",
"1.4.png",
"1.4.png",
"1.7.png",
"2.png",
"0.7.png",
"1.8.png",
"0.9.png",
"1.8.png",
"1.4.png",
"1.4.png",
"1.4.png",
"1.7.png",
"1.7.png",
"1.8.png",
"1.7.png",
"1.png",
"1.7.png",
"1.4.png",
"0.9.png",
"1.7.png",
"1.8.png",
],
"weights": [
1.4,
1.4,
1.4,
1.4,
2.5,
1.4,
1.7,
9.2,
2.5,
1.4,
0.3,
1.7,
1.7,
1.2,
0.7,
1.4,
0.9,
1.4,
1.7,
0.5,
1.7,
1.4,
0.2,
1,
2.2,
1.7,
1.8,
1.4,
1.7,
1.4,
1.7,
1.4,
1.4,
1.4,
1.4,
1.4,
1.4,
1.4,
1.7,
1.7,
1.8,
1.7,
1.4,
1.4,
1.7,
2,
0.7,
1.8,
0.9,
1.8,
1.4,
1.4,
1.4,
1.7,
1.7,
1.8,
1.7,
1,
1.7,
1.4,
0.9,
1.7,
1.8,
],
}
],
"incompatibilities": [],
"baseURI": ".",
"name": "NFT #",
"description": "This is a description for this NFT series.",
}

assert validate_config(valid_config) is None # It should not raise an exception
Loading