-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from OpenRarity/strip_token_attributes
Strip whitespace for attribute names and string attribute values
- Loading branch information
Showing
7 changed files
with
210 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
def normalize_attribute_string(value: str) -> str: | ||
"""Normalizes either attribute names or string attribute values. | ||
This is a helper function to ensure we are consistently normalizing | ||
by always lower casing and stripping input string. | ||
Parameters | ||
---------- | ||
value : str | ||
The string to normalize | ||
(this should be either attribute name or a string attribute value) | ||
Returns | ||
------- | ||
str | ||
normalized string | ||
""" | ||
return value.lower().strip() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[tool.poetry] | ||
name = "open-rarity" | ||
version = "0.4.0-beta" | ||
description = "Open-Rarity library is an open standard that provides an easy , explanable and reproducible computation for NFT rarity" | ||
version = "0.4.1-beta" | ||
description = "Open-Rarity library is an open standard that provides an easy, explanable and reproducible computation for NFT rarity" | ||
authors = ["Dan Meshkov <[email protected]>", "Vicky Gong <[email protected]>"] | ||
license = "Apache-2.0" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
from open_rarity.models.token_metadata import ( | ||
NumericAttribute, | ||
StringAttribute, | ||
TokenMetadata, | ||
) | ||
|
||
from tests.helpers import create_evm_token | ||
|
||
|
||
class TestToken: | ||
def test_token_init_metadata_non_matching_attribute_names(self): | ||
token = create_evm_token( | ||
token_id=1, | ||
metadata=TokenMetadata( | ||
string_attributes={ | ||
"hat": StringAttribute(name="big hat", value="blue"), | ||
"shirt": StringAttribute(name=" shirt", value="red"), | ||
} | ||
), | ||
) | ||
assert token.metadata.string_attributes == { | ||
"hat": StringAttribute(name="hat", value="blue"), | ||
"shirt": StringAttribute(name="shirt", value="red"), | ||
} | ||
|
||
def test_token_attribute_normalization(self): | ||
expected_equal_metadata_tokens = [ | ||
create_evm_token( | ||
token_id=1, | ||
metadata=TokenMetadata( | ||
string_attributes={ | ||
"hat ": StringAttribute(name="hat", value="blue"), | ||
"Shirt ": StringAttribute(name="shirt", value="red"), | ||
}, | ||
numeric_attributes={ | ||
"level": NumericAttribute(name="level", value=1), | ||
}, | ||
), | ||
), | ||
create_evm_token( | ||
token_id=1, | ||
metadata=TokenMetadata( | ||
string_attributes={ | ||
"hat": StringAttribute(name="hat", value="blue"), | ||
"Shirt ": StringAttribute(name=" shirt", value="red"), | ||
}, | ||
numeric_attributes={ | ||
"Level": NumericAttribute(name="level", value=1), | ||
}, | ||
), | ||
), | ||
create_evm_token( | ||
token_id=1, | ||
metadata=TokenMetadata( | ||
string_attributes={ | ||
"Hat": StringAttribute(name=" hat ", value="blue"), | ||
"shirt": StringAttribute(name="shirt", value="red"), | ||
}, | ||
numeric_attributes={ | ||
"Level": NumericAttribute(name=" level ", value=1), | ||
}, | ||
), | ||
), | ||
create_evm_token( | ||
token_id=1, | ||
metadata=TokenMetadata( | ||
string_attributes={ | ||
" hat ": StringAttribute(name=" hat ", value="blue"), | ||
" shirt": StringAttribute(name="shirt", value="red"), | ||
}, | ||
numeric_attributes={ | ||
"level": NumericAttribute(name="level ", value=1), | ||
}, | ||
), | ||
), | ||
] | ||
|
||
assert all( | ||
t.metadata == expected_equal_metadata_tokens[0].metadata | ||
for t in expected_equal_metadata_tokens | ||
) | ||
|
||
expected_not_equal = [ | ||
create_evm_token( | ||
token_id=1, | ||
metadata=TokenMetadata( | ||
string_attributes={ | ||
" big hat ": StringAttribute( | ||
name=" hat ", value="blue" | ||
), | ||
" shirt": StringAttribute(name="shirt", value="red"), | ||
}, | ||
numeric_attributes={ | ||
"level": NumericAttribute(name="level", value=1), | ||
}, | ||
), | ||
), | ||
create_evm_token( | ||
token_id=1, | ||
metadata=TokenMetadata( | ||
string_attributes={ | ||
"hat": StringAttribute(name="hat", value="blue"), | ||
"shirt": StringAttribute(name="shirt", value="red"), | ||
}, | ||
numeric_attributes={ | ||
"big level": NumericAttribute(name="level", value=1), | ||
}, | ||
), | ||
), | ||
] | ||
|
||
assert all( | ||
t.metadata != expected_equal_metadata_tokens[0].metadata | ||
for t in expected_not_equal | ||
) |