Back to Repositories

Testing JSON Serialization Implementation in OpenHands

This test suite validates JSON serialization and deserialization functionality for the OpenHands messaging system. It ensures proper handling of MessageAction objects and their array representations, with specific focus on timestamp formatting and message content preservation.

Test Coverage Overview

The test suite provides comprehensive coverage of JSON serialization/deserialization operations.

Key areas tested include:
  • Single message object serialization
  • Array-based message serialization
  • Timestamp formatting verification
  • Message content preservation
  • Optional parameters handling

Implementation Analysis

The testing approach utilizes pytest’s assertion framework to validate JSON transformations. The implementation follows a systematic pattern of creating MessageAction objects, serializing them, then comparing deserialized results against expected data structures. Key technical aspects include datetime handling and nested dictionary validation.

The tests leverage custom JSON utilities from openhands.core.utils.

Technical Details

Testing components include:
  • pytest framework
  • datetime module for timestamp handling
  • Custom JSON serialization utilities
  • MessageAction class from openhands.events.action
Configuration focuses on proper datetime formatting (ISO 8601) and nested dictionary structure validation.

Best Practices Demonstrated

The test suite exemplifies several testing best practices including isolated test cases, explicit expected values, and comprehensive attribute validation. Notable practices include:
  • Separate tests for single and array serialization
  • Complete object state verification
  • Explicit timestamp formatting checks
  • Structured assertion comparisons

all-hands-ai/openhands

tests/unit/test_json.py

            
from datetime import datetime

from openhands.core.utils import json
from openhands.events.action import MessageAction


def test_event_serialization_deserialization():
    message = MessageAction(content='This is a test.', wait_for_response=False)
    message._id = 42
    message._timestamp = datetime(2020, 1, 1, 23, 59, 58)
    serialized = json.dumps(message)
    deserialized = json.loads(serialized)
    expected = {
        'id': 42,
        'timestamp': '2020-01-01T23:59:58',
        'action': 'message',
        'message': 'This is a test.',
        'args': {
            'content': 'This is a test.',
            'image_urls': None,
            'wait_for_response': False,
        },
    }
    assert deserialized == expected


def test_array_serialization_deserialization():
    message = MessageAction(content='This is a test.', wait_for_response=False)
    message._id = 42
    message._timestamp = datetime(2020, 1, 1, 0, 0, 0)
    serialized = json.dumps([message])
    deserialized = json.loads(serialized)
    expected = [
        {
            'id': 42,
            'timestamp': '2020-01-01T00:00:00',
            'action': 'message',
            'message': 'This is a test.',
            'args': {
                'content': 'This is a test.',
                'image_urls': None,
                'wait_for_response': False,
            },
        }
    ]
    assert deserialized == expected