Skip to content

changes to iotshadow.py json None handling #297

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

Merged
merged 8 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ docs/.buildinfo
docs/.doctrees/
__pycache__/
*.egg-info
build/*
18 changes: 8 additions & 10 deletions awsiot/iotshadow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1327,16 +1327,14 @@ def __init__(self, *args, **kwargs):
def from_payload(cls, payload):
# type: (typing.Dict[str, typing.Any]) -> ShadowState
new = cls()
val = payload.get('desired')
if val is not None:
new.desired = val
val = payload.get('reported')
if val is not None:
new.reported = val
if new.desired == None:
new.desired_is_nullable = True
if new.reported == None:
new.reported_is_nullable = True
if 'desired' in payload:
new.desired = payload['desired']
new.desired_is_nullable = new.desired is None

if 'reported' in payload:
new.reported = payload['reported']
new.reported_is_nullable = new.reported is None

return new

def to_payload(self):
Expand Down
41 changes: 41 additions & 0 deletions test/test_shadow_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from pydoc import describe
from unittest import TestCase

from awsiot.iotshadow import ShadowState


class ShadowTest(TestCase):

def test_shadow_state_payload_filled(self):
test_state = ShadowState.from_payload({
"reported": {"Color": "Red"},
"desired": {"Color": "Blue"}
})
compareState = ShadowState(
reported={"Color": "Red"},
desired={"Color": "Blue"}
)
self.assertTrue(test_state.to_payload() == compareState.to_payload())

def test_shadow_state_payload_partial_filled(self):
test_state = ShadowState.from_payload({
"reported": {"Color": "Red"}
})
self.assertEqual(test_state.to_payload(), {"reported": {"Color": "Red"}})

def test_shadow_state_payload_can_send_null(self):
test_state = ShadowState(
reported={"Color": "Red"},
desired=None,
desired_is_nullable=True
)
self.assertEqual(test_state.to_payload(), {"reported": {"Color": "Red"}, "desired": None})

def test_shadow_state_payload_with_none(self):
test_state = ShadowState.from_payload({"reported": {"Color": "Red"}, "desired": None})
self.assertTrue(test_state.desired_is_nullable)
self.assertEqual(test_state.to_payload(), {"reported": {"Color": "Red"}, "desired": None})

def test_shadow_state_payload_without_none(self):
test_state = ShadowState.from_payload({"reported": {"deviceAgent": {"rebootRequired": True}}})
self.assertEqual(test_state.to_payload(), {"reported": {"deviceAgent": {"rebootRequired": True}}})