Skip to content

Commit

Permalink
fix: get_event_body for enum value from ampli (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
bohan-amplitude committed Jun 23, 2022
1 parent 828040e commit 2bbc1b0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/amplitude/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""

import copy
import enum
import json
import logging
from typing import Callable, Optional, Union
Expand Down Expand Up @@ -303,6 +304,11 @@ def get_event_body(self) -> dict:
event_body[value[0]] = self[key]
if "plan" in event_body:
event_body["plan"] = event_body["plan"].get_plan_body()
for properties in ["user_properties", "event_properties", "group_properties"]:
if properties in event_body:
for key, value in event_body[properties].items():
if isinstance(value, enum.Enum):
event_body[properties][key] = value.value
return utils.truncate(event_body)

def _verify_property(self, key, value) -> bool:
Expand Down Expand Up @@ -1187,7 +1193,7 @@ def is_validate_properties(key, value):
return result
if isinstance(value, dict):
return is_validate_object(value)
if not isinstance(value, (bool, float, int, str)):
if not isinstance(value, (bool, float, int, str, enum.Enum)):
return False
return True

Expand Down
10 changes: 8 additions & 2 deletions src/test/test_event.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import enum
import unittest
from unittest.mock import MagicMock

Expand Down Expand Up @@ -74,10 +75,15 @@ def test_callback_without_callback_function_success_callback(self):
callback_func.assert_not_called()

def test_base_event_get_event_body_success(self):
event = BaseEvent(event_type="test_event", user_id="test_user", user_properties={"email": "test@test"})
class TestEnum(enum.Enum):
ENUM1 = 'test'
ENUM2 = 'test2'
event = BaseEvent(event_type="test_event", user_id="test_user", user_properties={"email": "test@test"},
event_properties={'enum_properties': TestEnum.ENUM1})
expect_dict = {"event_type": "test_event",
"user_id": "test_user",
"user_properties": {"email": "test@test"}}
"user_properties": {"email": "test@test"},
"event_properties": {"enum_properties": 'test'}}
self.assertEqual(expect_dict, event.get_event_body())

def test_base_event_set_dict_event_attributes_with_invalid_value_failed(self):
Expand Down

0 comments on commit 2bbc1b0

Please sign in to comment.