diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 5e1d48e..0000000 Binary files a/.DS_Store and /dev/null differ diff --git a/.gitignore b/.gitignore index a096101..555d98a 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,5 @@ coverage.xml *metadata/*.json *images/*.png + +.DS_Store diff --git a/src/common/validate.py b/src/common/validate.py index 8c758a8..87632eb 100644 --- a/src/common/validate.py +++ b/src/common/validate.py @@ -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"]) diff --git a/tests/test_floating_point_errors.py b/tests/test_floating_point_errors.py new file mode 100644 index 0000000..3661fed --- /dev/null +++ b/tests/test_floating_point_errors.py @@ -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