Skip to content

Commit

Permalink
Merge pull request #1 from capcom6/hotfix/backward-compatible-parsing
Browse files Browse the repository at this point in the history
[domain] backward compatibility parsing
  • Loading branch information
capcom6 committed Feb 14, 2024
2 parents 3d0ba1d + d036cc3 commit f2648e9
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 33 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Python CI

on:
push:
pull_request:

jobs:
test:
runs-on: ubuntu-20.04
strategy:
matrix:
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"]

steps:
- uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: pipenv

- name: Install pipenv
run: |
python -m pip install --upgrade pipenv
- name: Install dependencies
run: |
pipenv install --dev
- name: Lint with flake8
run: pipenv run flake8 android_sms_gateway tests

- name: Test with pytest
run: pipenv run pytest tests
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test:

# Lint the project with flake8
lint:
pipenv run flake8 $(PACKAGE_NAME)
pipenv run flake8 $(PACKAGE_NAME) tests

# Build the project
build:
Expand Down
60 changes: 30 additions & 30 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions android_sms_gateway/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ def from_dict(cls, payload: t.Dict[str, t.Any]) -> "MessageState":
RecipientState.from_dict(recipient)
for recipient in payload["recipients"]
],
is_hashed=payload["isHashed"],
is_encrypted=payload["isEncrypted"],
is_hashed=payload.get("isHashed", False),
is_encrypted=payload.get("isEncrypted", False),
)
Empty file added tests/__init__.py
Empty file.
81 changes: 81 additions & 0 deletions tests/test_domain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import pytest

from android_sms_gateway.domain import MessageState, RecipientState


# Test for successful instantiation from a dictionary
def test_message_state_from_dict():
payload = {
"id": "123",
"state": "Pending",
"recipients": [
{"phoneNumber": "123", "state": "Pending"},
{"phoneNumber": "456", "state": "Pending"},
],
"isHashed": True,
"isEncrypted": False,
}

message_state = MessageState.from_dict(payload)
assert message_state.id == payload["id"]
assert message_state.state.name == payload["state"]
assert all(
isinstance(recipient, RecipientState) for recipient in message_state.recipients
)
assert len(message_state.recipients) == len(payload["recipients"])
assert message_state.is_hashed == payload["isHashed"]
assert message_state.is_encrypted == payload["isEncrypted"]


# Test for backward compatibility
def test_message_state_from_dict_backwards_compatibility():
payload = {
"id": "123",
"state": "Pending",
"recipients": [
{"phoneNumber": "123", "state": "Pending"},
{"phoneNumber": "456", "state": "Pending"},
],
}

message_state = MessageState.from_dict(payload)
assert message_state.id == payload["id"]
assert message_state.state.name == payload["state"]
assert all(
isinstance(recipient, RecipientState) for recipient in message_state.recipients
)
assert len(message_state.recipients) == len(payload["recipients"])
assert message_state.is_hashed is False
assert message_state.is_encrypted is False


# Test for handling missing fields
def test_message_state_from_dict_missing_fields():
incomplete_payload = {
"id": "123",
# 'state' is missing
"recipients": [
{"phoneNumber": "123", "state": "Pending"}
], # Assume one recipient is enough to test
"isHashed": True,
"isEncrypted": False,
}

with pytest.raises(KeyError):
MessageState.from_dict(incomplete_payload)


# Test for handling incorrect types
def test_message_state_from_dict_incorrect_types():
incorrect_payload = {
"id": 123, # Should be a string
"state": 42, # Should be a string that can be converted to a ProcessState
"recipients": "Alice, Bob", # Should be a list of dictionaries
"isHashed": "yes", # Should be a boolean
"isEncrypted": "no", # Should be a boolean
}

with pytest.raises(
Exception
): # Replace Exception with the specific exception you expect
MessageState.from_dict(incorrect_payload)

0 comments on commit f2648e9

Please sign in to comment.