Skip to content

Commit

Permalink
Merge pull request #60 from Jon-Becker/jon-becker/float-validation
Browse files Browse the repository at this point in the history
fix(validation): floating point rounding error validation
  • Loading branch information
Jon-Becker committed Mar 11, 2024
2 parents 43992b1 + 626639d commit f96e9df
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 1 deletion.
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

0 comments on commit f96e9df

Please sign in to comment.