-
Notifications
You must be signed in to change notification settings - Fork 215
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
feat: add descriptions without modifying model and protocol digest #111
Closed
Closed
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
b4220cc
add descriptions without modifying model digest
zmezei fba70dc
rename function
zmezei 94ee30d
reformat, pass type instead of instance when refreshing schema cache
zmezei d5b85f2
avoid reimport, rename var
zmezei 9c9b9f4
hardcode model_schema args
zmezei 746146c
rename var
zmezei 0bf470f
rename var
zmezei 9147527
fix model_schema invocation
zmezei 59ea5ac
do not change protocol digest when setting descriptions
zmezei 55d523f
fix class var name
zmezei 1252445
add tests
zmezei f3e11f0
Merge branch 'main' into feature/upload-field-desc
zmezei 163c772
disable pylint rule
zmezei f942268
refine and add test cases
zmezei 9860db3
rename function
zmezei 83ffcec
improve tests
zmezei 7a3fb7d
Merge branch 'main' into feature/upload-field-desc
zmezei 1f96584
remove pylint rule disabling
zmezei 4c6db8e
test on_message instead of on_query
zmezei e93c2b3
convert static methods into class methods
zmezei 9bf84ac
fix based on review
zmezei 069d70c
fix compute_digest test
zmezei f552b91
add class variable as a cache
zmezei 0b2a1ff
refactor, avoid multiple json load and dump
zmezei ccbf531
change type annotation
zmezei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# pylint: disable=function-redefined | ||
import unittest | ||
from pydantic import Field | ||
from uagents import Model, Protocol | ||
|
||
|
||
protocol_no_descr = Protocol(name="test", version="1.1.1") | ||
protocol_with_descr = Protocol(name="test", version="1.1.1") | ||
|
||
|
||
def create_message_no_descr(): | ||
class Message(Model): | ||
message: str | ||
|
||
return Message | ||
|
||
|
||
def create_message_with_descr(): | ||
class Message(Model): | ||
message: str = Field(description="message field description") | ||
|
||
return Message | ||
|
||
|
||
def get_digests(protocol: Protocol): | ||
model_digest = next(iter(protocol.models)) | ||
proto_digest = protocol.digest | ||
return (model_digest, proto_digest) | ||
|
||
|
||
class TestFieldDescr(unittest.TestCase): | ||
def setUp(self) -> None: | ||
self.protocol_no_descr = protocol_no_descr | ||
self.protocol_with_descr = protocol_with_descr | ||
return super().setUp() | ||
|
||
def test_schema_json(self): | ||
class Message(Model): | ||
message: str | ||
id: str | ||
|
||
self.assertEqual( | ||
Message.schema_json(indent=None, sort_keys=True), | ||
Message.schema_json_no_descriptions(), | ||
) | ||
|
||
class Message(Model): | ||
message: str = Field(description="message field description") | ||
id: str = Field(description="id field description") | ||
|
||
self.assertNotEqual( | ||
Message.schema_json(indent=None, sort_keys=True), | ||
Message.schema_json_no_descriptions(), | ||
) | ||
|
||
class MessageArgs(Model): | ||
arg: str = Field(description="arg field description") | ||
|
||
class Message(Model): | ||
message: str = Field(description="message field description") | ||
id: str = Field(description="id field description") | ||
args: MessageArgs | ||
|
||
self.assertNotEqual( | ||
Message.schema_json(indent=None, sort_keys=True), | ||
Message.schema_json_no_descriptions(), | ||
) | ||
|
||
def test_model_digest(self): | ||
model_digest_no_descr = Model.build_schema_digest(create_message_no_descr()) | ||
model_digest_with_descr = Model.build_schema_digest(create_message_with_descr()) | ||
|
||
self.assertEqual(model_digest_no_descr, model_digest_with_descr) | ||
|
||
def test_protocol(self): | ||
@self.protocol_no_descr.on_message(create_message_no_descr()) | ||
def _(_ctx, _sender, _msg): | ||
pass | ||
|
||
self.assertEqual(len(self.protocol_no_descr.models), 1) | ||
self.assertNotIn( | ||
"description", | ||
self.protocol_no_descr.manifest()["models"][0]["schema"]["properties"][ | ||
"message" | ||
], | ||
) | ||
(model_digest_no_descr, proto_digest_no_descr) = get_digests( | ||
self.protocol_no_descr | ||
) | ||
|
||
@self.protocol_with_descr.on_message(create_message_with_descr()) | ||
def _(_ctx, _sender, _msg): | ||
pass | ||
|
||
self.assertEqual(len(self.protocol_with_descr.models), 1) | ||
self.assertEqual( | ||
self.protocol_with_descr.manifest()["models"][0]["schema"]["properties"][ | ||
"message" | ||
]["description"], | ||
"message field description", | ||
) | ||
(model_digest_with_descr, proto_digest_with_descr) = get_digests( | ||
self.protocol_with_descr | ||
) | ||
|
||
self.assertEqual(model_digest_no_descr, model_digest_with_descr) | ||
self.assertEqual(proto_digest_no_descr, proto_digest_with_descr) | ||
|
||
def test_compute_digest(self): | ||
protocol = Protocol(name="test", version="1.1.1") | ||
|
||
@protocol.on_message(create_message_with_descr()) | ||
def _(_ctx, _sender, _msg): | ||
pass | ||
|
||
computed_digest = Protocol.compute_digest(protocol.manifest()) | ||
self.assertEqual(protocol.digest, computed_digest) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
digest is also computed by the
compute_digest
static method.It takes as input the manifest which will contain the descriptions hence producing a different digest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I modified this method accordingly and also created test for it in test_fields_descr.py, please check it.