-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make SDK more backward and forward compatible with protocol
- Loading branch information
Showing
13 changed files
with
216 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
.DS_Store | ||
.idea | ||
.run | ||
*.iml | ||
.env | ||
|
||
|
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
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,12 @@ | ||
{ | ||
"uri": "at://did:plc:kvwvcn5iqfooopmyzvb4qzba/app.bsky.feed.post/3k2yinh52ne2x", | ||
"cid": "bafyreiaepzhlaf2hd4exhx5o74orvhtqloog4xwaptcrzsuyfkzr43va4e", | ||
"value": { | ||
"text": "foo", | ||
"$type": "app.bsky.feed.pythonSdkCustomRecordPost", | ||
"langs": [ | ||
"en" | ||
], | ||
"createdAt": "2023-07-21T01:33:51.481951" | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
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,94 @@ | ||
import pytest | ||
|
||
from atproto.exceptions import ModelError | ||
from atproto.xrpc_client import models | ||
from atproto.xrpc_client.models import get_or_create | ||
|
||
"""These tests are based on the following statements: | ||
Lexicons are allowed to change over time, within some bounds to ensure both forwards and backwards compatibility. | ||
The basic principle is that all old data must still be valid under the updated Lexicon, | ||
and new data must be valid under the old Lexicon. | ||
- Any new fields must be optional | ||
- Non-optional fields can not be removed | ||
- Types can not change | ||
- Fields can not be renamed | ||
Ref: https://atproto.com/specs/lexicon#lexicon-evolution | ||
""" | ||
|
||
|
||
def test_model_serialization() -> None: | ||
expected_signing_key = 'blabla' | ||
test_data = { | ||
'signingKey': expected_signing_key, | ||
} | ||
model = get_or_create(test_data, models.ComAtprotoServerReserveSigningKey.Response) | ||
|
||
assert isinstance(model, models.ComAtprotoServerReserveSigningKey.Response) | ||
assert isinstance(model.signing_key, str) | ||
|
||
assert model.signing_key == expected_signing_key | ||
assert model['signing_key'] == expected_signing_key | ||
|
||
|
||
def test_added_new_fields_as_optional() -> None: | ||
"""New fields must be optional. It means that model must be created without validation errors.""" | ||
|
||
expected_signing_key = 'blabla' | ||
test_data = { | ||
'signingKey': expected_signing_key, | ||
'brandNewBackendField': 'foo', | ||
} | ||
model = get_or_create(test_data, models.ComAtprotoServerReserveSigningKey.Response) | ||
|
||
assert isinstance(model, models.ComAtprotoServerReserveSigningKey.Response) | ||
assert isinstance(model.signing_key, str) | ||
|
||
assert model.signing_key == expected_signing_key | ||
assert model['signing_key'] == expected_signing_key | ||
|
||
assert model.model_extra is not None | ||
|
||
# also, we want to have access to new fields from SDK | ||
# the problem here is that we can't access them via snake_case, | ||
# could be fixed in the future | ||
assert model.brandNewBackendField == 'foo' | ||
assert model['brandNewBackendField'] == 'foo' | ||
|
||
|
||
def test_removed_non_optional_field() -> None: | ||
"""If protocol removed non-optional field, it breaks backward compatibility. We must throw an error.""" | ||
|
||
test_data = { | ||
# for example, signingKey was removed | ||
'brandNewBackendField': 'foo', | ||
} | ||
|
||
with pytest.raises(ModelError): | ||
get_or_create(test_data, models.ComAtprotoServerReserveSigningKey.Response) | ||
|
||
|
||
def test_changed_field_type() -> None: | ||
"""If protocol changed a field type, it breaks backward compatibility. We must throw an error.""" | ||
|
||
test_data = { | ||
# for example, signingKey now is an integer instead of string | ||
'signingKey': 123, | ||
} | ||
|
||
with pytest.raises(ModelError): | ||
get_or_create(test_data, models.ComAtprotoServerReserveSigningKey.Response) | ||
|
||
|
||
def test_renamed_field_type() -> None: | ||
"""If protocol changed a field type, it breaks backward compatibility. We must throw an error.""" | ||
|
||
expected_signing_key = 'blabla' | ||
test_data = { | ||
# for example, signingKey now signKey | ||
'signKey': expected_signing_key, | ||
} | ||
|
||
with pytest.raises(ModelError): | ||
get_or_create(test_data, models.ComAtprotoServerReserveSigningKey.Response) |
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,48 @@ | ||
from atproto.xrpc_client import models | ||
from atproto.xrpc_client.models import get_model_as_dict, get_or_create | ||
from atproto.xrpc_client.models.dot_dict import DotDict | ||
from tests.models.tests.utils import load_data_from_file | ||
|
||
|
||
def load_test_data() -> dict: | ||
return load_data_from_file('custom_record') | ||
|
||
|
||
def test_custom_record_deserialization() -> None: | ||
model = get_or_create(load_test_data(), models.ComAtprotoRepoGetRecord.Response) | ||
expected_custom_record_id = 'app.bsky.feed.pythonSdkCustomRecordPost' | ||
|
||
assert isinstance(model, models.ComAtprotoRepoGetRecord.Response) | ||
assert isinstance(model.value, DotDict) | ||
|
||
assert model.value['$type'] != models.ids.AppBskyFeedPost | ||
assert model.value['$type'] == expected_custom_record_id | ||
|
||
assert model.value.text == 'foo' | ||
assert model.value['text'] == 'foo' | ||
|
||
assert model.value.langs == ['en'] | ||
assert model.value['createdAt'] == '2023-07-21T01:33:51.481951' | ||
assert model.value.createdAt == '2023-07-21T01:33:51.481951' | ||
assert model.value['created_at'] == '2023-07-21T01:33:51.481951' | ||
assert model.value.created_at == '2023-07-21T01:33:51.481951' | ||
|
||
|
||
def test_custom_record_serialization() -> None: | ||
model = get_or_create(load_test_data(), models.ComAtprotoRepoGetRecord.Response) | ||
expected_custom_record_id = 'app.bsky.feed.pythonSdkCustomRecordPost' | ||
|
||
model_dict = get_model_as_dict(model) | ||
restored_model = get_or_create(model_dict, models.ComAtprotoRepoGetRecord.Response) | ||
|
||
assert isinstance(get_model_as_dict(model.value), dict) | ||
assert model_dict == get_model_as_dict(restored_model) | ||
|
||
assert restored_model.value['$type'] != models.ids.AppBskyFeedPost | ||
assert restored_model.value['$type'] == expected_custom_record_id | ||
|
||
assert restored_model.value.text == 'foo' | ||
assert restored_model.value['text'] == 'foo' | ||
|
||
assert model_dict['value']['$type'] == expected_custom_record_id | ||
assert model_dict['value']['text'] == 'foo' |
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