test_json.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from datetime import datetime
  2. from opendevin.core.utils import json
  3. from opendevin.events.action import MessageAction
  4. def test_event_serialization_deserialization():
  5. message = MessageAction(content='This is a test.', wait_for_response=False)
  6. message._id = 42
  7. message._timestamp = datetime(2020, 1, 1, 0, 0, 0)
  8. serialized = json.dumps(message)
  9. deserialized = json.loads(serialized)
  10. expected = {
  11. 'id': 42,
  12. 'timestamp': '2020-01-01T00:00:00',
  13. 'action': 'message',
  14. 'message': 'This is a test.',
  15. 'args': {
  16. 'content': 'This is a test.',
  17. 'wait_for_response': False,
  18. },
  19. }
  20. assert deserialized == expected
  21. def test_array_serialization_deserialization():
  22. message = MessageAction(content='This is a test.', wait_for_response=False)
  23. message._id = 42
  24. message._timestamp = datetime(2020, 1, 1, 0, 0, 0)
  25. serialized = json.dumps([message])
  26. deserialized = json.loads(serialized)
  27. expected = [
  28. {
  29. 'id': 42,
  30. 'timestamp': '2020-01-01T00:00:00',
  31. 'action': 'message',
  32. 'message': 'This is a test.',
  33. 'args': {
  34. 'content': 'This is a test.',
  35. 'wait_for_response': False,
  36. },
  37. }
  38. ]
  39. assert deserialized == expected