Skip to content

Commit f2648e9

Browse files
authored
Merge pull request #1 from capcom6/hotfix/backward-compatible-parsing
[domain] backward compatibility parsing
2 parents 3d0ba1d + d036cc3 commit f2648e9

File tree

6 files changed

+149
-33
lines changed

6 files changed

+149
-33
lines changed

.github/workflows/testing.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Python CI
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
test:
9+
runs-on: ubuntu-20.04
10+
strategy:
11+
matrix:
12+
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"]
13+
14+
steps:
15+
- uses: actions/checkout@v3
16+
17+
- name: Set up Python ${{ matrix.python-version }}
18+
uses: actions/setup-python@v4
19+
with:
20+
python-version: ${{ matrix.python-version }}
21+
cache: pipenv
22+
23+
- name: Install pipenv
24+
run: |
25+
python -m pip install --upgrade pipenv
26+
27+
- name: Install dependencies
28+
run: |
29+
pipenv install --dev
30+
31+
- name: Lint with flake8
32+
run: pipenv run flake8 android_sms_gateway tests
33+
34+
- name: Test with pytest
35+
run: pipenv run pytest tests

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test:
1414

1515
# Lint the project with flake8
1616
lint:
17-
pipenv run flake8 $(PACKAGE_NAME)
17+
pipenv run flake8 $(PACKAGE_NAME) tests
1818

1919
# Build the project
2020
build:

Pipfile.lock

Lines changed: 30 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

android_sms_gateway/domain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ def from_dict(cls, payload: t.Dict[str, t.Any]) -> "MessageState":
6060
RecipientState.from_dict(recipient)
6161
for recipient in payload["recipients"]
6262
],
63-
is_hashed=payload["isHashed"],
64-
is_encrypted=payload["isEncrypted"],
63+
is_hashed=payload.get("isHashed", False),
64+
is_encrypted=payload.get("isEncrypted", False),
6565
)

tests/__init__.py

Whitespace-only changes.

tests/test_domain.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import pytest
2+
3+
from android_sms_gateway.domain import MessageState, RecipientState
4+
5+
6+
# Test for successful instantiation from a dictionary
7+
def test_message_state_from_dict():
8+
payload = {
9+
"id": "123",
10+
"state": "Pending",
11+
"recipients": [
12+
{"phoneNumber": "123", "state": "Pending"},
13+
{"phoneNumber": "456", "state": "Pending"},
14+
],
15+
"isHashed": True,
16+
"isEncrypted": False,
17+
}
18+
19+
message_state = MessageState.from_dict(payload)
20+
assert message_state.id == payload["id"]
21+
assert message_state.state.name == payload["state"]
22+
assert all(
23+
isinstance(recipient, RecipientState) for recipient in message_state.recipients
24+
)
25+
assert len(message_state.recipients) == len(payload["recipients"])
26+
assert message_state.is_hashed == payload["isHashed"]
27+
assert message_state.is_encrypted == payload["isEncrypted"]
28+
29+
30+
# Test for backward compatibility
31+
def test_message_state_from_dict_backwards_compatibility():
32+
payload = {
33+
"id": "123",
34+
"state": "Pending",
35+
"recipients": [
36+
{"phoneNumber": "123", "state": "Pending"},
37+
{"phoneNumber": "456", "state": "Pending"},
38+
],
39+
}
40+
41+
message_state = MessageState.from_dict(payload)
42+
assert message_state.id == payload["id"]
43+
assert message_state.state.name == payload["state"]
44+
assert all(
45+
isinstance(recipient, RecipientState) for recipient in message_state.recipients
46+
)
47+
assert len(message_state.recipients) == len(payload["recipients"])
48+
assert message_state.is_hashed is False
49+
assert message_state.is_encrypted is False
50+
51+
52+
# Test for handling missing fields
53+
def test_message_state_from_dict_missing_fields():
54+
incomplete_payload = {
55+
"id": "123",
56+
# 'state' is missing
57+
"recipients": [
58+
{"phoneNumber": "123", "state": "Pending"}
59+
], # Assume one recipient is enough to test
60+
"isHashed": True,
61+
"isEncrypted": False,
62+
}
63+
64+
with pytest.raises(KeyError):
65+
MessageState.from_dict(incomplete_payload)
66+
67+
68+
# Test for handling incorrect types
69+
def test_message_state_from_dict_incorrect_types():
70+
incorrect_payload = {
71+
"id": 123, # Should be a string
72+
"state": 42, # Should be a string that can be converted to a ProcessState
73+
"recipients": "Alice, Bob", # Should be a list of dictionaries
74+
"isHashed": "yes", # Should be a boolean
75+
"isEncrypted": "no", # Should be a boolean
76+
}
77+
78+
with pytest.raises(
79+
Exception
80+
): # Replace Exception with the specific exception you expect
81+
MessageState.from_dict(incorrect_payload)

0 commit comments

Comments
 (0)