|
1 | | -import pytest |
2 | | -import requests |
3 | 1 | from unittest.mock import Mock, patch |
4 | 2 |
|
5 | 3 | from openfeature.contrib.provider.unleash import UnleashProvider |
6 | 4 | from openfeature.evaluation_context import EvaluationContext |
7 | | -from openfeature.flag_evaluation import Reason |
8 | | -from openfeature.provider import ProviderStatus |
9 | 5 | from openfeature.event import ProviderEvent |
10 | 6 | from openfeature.exception import ( |
11 | 7 | FlagNotFoundError, |
12 | 8 | GeneralError, |
13 | 9 | ParseError, |
14 | 10 | TypeMismatchError, |
15 | 11 | ) |
| 12 | +from openfeature.flag_evaluation import Reason |
| 13 | +from openfeature.provider import ProviderStatus |
| 14 | +import pytest |
| 15 | +import requests |
16 | 16 |
|
17 | 17 |
|
18 | 18 | def test_unleash_provider_import(): |
@@ -800,215 +800,3 @@ def test_unleash_provider_type_validation(): |
800 | 800 | assert isinstance(result.value, str) |
801 | 801 |
|
802 | 802 | provider.shutdown() |
803 | | - |
804 | | - |
805 | | -def test_unleash_provider_track_basic(): |
806 | | - """Test basic tracking functionality.""" |
807 | | - mock_client = Mock() |
808 | | - mock_client.initialize_client.return_value = None |
809 | | - mock_event_callback = Mock() |
810 | | - |
811 | | - with patch( |
812 | | - "openfeature.contrib.provider.unleash.UnleashClient" |
813 | | - ) as mock_unleash_client: |
814 | | - mock_unleash_client.return_value = mock_client |
815 | | - |
816 | | - provider = UnleashProvider( |
817 | | - url="http://localhost:4242", app_name="test-app", api_token="test-token" |
818 | | - ) |
819 | | - provider.initialize() |
820 | | - |
821 | | - # Set the event callback |
822 | | - provider._unleash_event_callback = mock_event_callback |
823 | | - |
824 | | - # Track a basic event |
825 | | - provider.track("user_action") |
826 | | - |
827 | | - # Verify the tracking event was created and passed to callback |
828 | | - assert mock_event_callback.call_count == 1 |
829 | | - tracking_event = mock_event_callback.call_args[0][0] |
830 | | - assert tracking_event.feature_name == "user_action" |
831 | | - assert tracking_event.enabled is True |
832 | | - assert tracking_event.variant == "tracking_event" |
833 | | - |
834 | | - provider.shutdown() |
835 | | - |
836 | | - |
837 | | -def test_unleash_provider_track_with_context(): |
838 | | - """Test tracking with evaluation context.""" |
839 | | - mock_client = Mock() |
840 | | - mock_client.initialize_client.return_value = None |
841 | | - mock_event_callback = Mock() |
842 | | - |
843 | | - with patch( |
844 | | - "openfeature.contrib.provider.unleash.UnleashClient" |
845 | | - ) as mock_unleash_client: |
846 | | - mock_unleash_client.return_value = mock_client |
847 | | - |
848 | | - provider = UnleashProvider( |
849 | | - url="http://localhost:4242", app_name="test-app", api_token="test-token" |
850 | | - ) |
851 | | - provider.initialize() |
852 | | - |
853 | | - provider._unleash_event_callback = mock_event_callback |
854 | | - |
855 | | - context = EvaluationContext( |
856 | | - targeting_key="user123", |
857 | | - attributes={ "email": "[email protected]", "role": "admin"}, |
858 | | - ) |
859 | | - |
860 | | - provider.track("page_view", context) |
861 | | - |
862 | | - tracking_event = mock_event_callback.call_args[0][0] |
863 | | - assert tracking_event.context["userId"] == "user123" |
864 | | - assert tracking_event. context[ "email"] == "[email protected]" |
865 | | - assert tracking_event.context["role"] == "admin" |
866 | | - |
867 | | - provider.shutdown() |
868 | | - |
869 | | - |
870 | | -def test_unleash_provider_track_with_event_details(): |
871 | | - """Test tracking with event details.""" |
872 | | - mock_client = Mock() |
873 | | - mock_client.initialize_client.return_value = None |
874 | | - mock_event_callback = Mock() |
875 | | - |
876 | | - with patch( |
877 | | - "openfeature.contrib.provider.unleash.UnleashClient" |
878 | | - ) as mock_unleash_client: |
879 | | - mock_unleash_client.return_value = mock_client |
880 | | - |
881 | | - provider = UnleashProvider( |
882 | | - url="http://localhost:4242", app_name="test-app", api_token="test-token" |
883 | | - ) |
884 | | - provider.initialize() |
885 | | - |
886 | | - provider._unleash_event_callback = mock_event_callback |
887 | | - |
888 | | - event_details = {"value": 99.99, "currency": "USD", "category": "purchase"} |
889 | | - |
890 | | - provider.track("purchase_completed", event_details=event_details) |
891 | | - |
892 | | - tracking_event = mock_event_callback.call_args[0][0] |
893 | | - assert tracking_event.context["tracking_value"] == 99.99 |
894 | | - assert tracking_event.context["tracking_details"] == event_details |
895 | | - |
896 | | - provider.shutdown() |
897 | | - |
898 | | - |
899 | | -def test_unleash_provider_track_not_initialized(): |
900 | | - """Test tracking when provider is not initialized.""" |
901 | | - provider = UnleashProvider( |
902 | | - url="http://localhost:4242", app_name="test-app", api_token="test-token" |
903 | | - ) |
904 | | - |
905 | | - # Should not raise any exception, just return |
906 | | - provider.track("test_event") |
907 | | - |
908 | | - |
909 | | -def test_unleash_provider_track_empty_event_name(): |
910 | | - """Test tracking with empty event name.""" |
911 | | - mock_client = Mock() |
912 | | - mock_client.initialize_client.return_value = None |
913 | | - mock_event_callback = Mock() |
914 | | - |
915 | | - with patch( |
916 | | - "openfeature.contrib.provider.unleash.UnleashClient" |
917 | | - ) as mock_unleash_client: |
918 | | - mock_unleash_client.return_value = mock_client |
919 | | - |
920 | | - provider = UnleashProvider( |
921 | | - url="http://localhost:4242", app_name="test-app", api_token="test-token" |
922 | | - ) |
923 | | - provider.initialize() |
924 | | - |
925 | | - provider._unleash_event_callback = mock_event_callback |
926 | | - |
927 | | - provider.track("") |
928 | | - |
929 | | - tracking_event = mock_event_callback.call_args[0][0] |
930 | | - assert tracking_event.feature_name == "" |
931 | | - |
932 | | - provider.shutdown() |
933 | | - |
934 | | - |
935 | | -def test_unleash_provider_track_none_context(): |
936 | | - """Test tracking with None context.""" |
937 | | - mock_client = Mock() |
938 | | - mock_client.initialize_client.return_value = None |
939 | | - mock_event_callback = Mock() |
940 | | - |
941 | | - with patch( |
942 | | - "openfeature.contrib.provider.unleash.UnleashClient" |
943 | | - ) as mock_unleash_client: |
944 | | - mock_unleash_client.return_value = mock_client |
945 | | - |
946 | | - provider = UnleashProvider( |
947 | | - url="http://localhost:4242", app_name="test-app", api_token="test-token" |
948 | | - ) |
949 | | - provider.initialize() |
950 | | - |
951 | | - provider._unleash_event_callback = mock_event_callback |
952 | | - |
953 | | - provider.track("test_event", None) |
954 | | - |
955 | | - tracking_event = mock_event_callback.call_args[0][0] |
956 | | - assert tracking_event.context == {} |
957 | | - |
958 | | - provider.shutdown() |
959 | | - |
960 | | - |
961 | | -def test_unleash_provider_track_none_event_details(): |
962 | | - """Test tracking with None event details.""" |
963 | | - mock_client = Mock() |
964 | | - mock_client.initialize_client.return_value = None |
965 | | - mock_event_callback = Mock() |
966 | | - |
967 | | - with patch( |
968 | | - "openfeature.contrib.provider.unleash.UnleashClient" |
969 | | - ) as mock_unleash_client: |
970 | | - mock_unleash_client.return_value = mock_client |
971 | | - |
972 | | - provider = UnleashProvider( |
973 | | - url="http://localhost:4242", app_name="test-app", api_token="test-token" |
974 | | - ) |
975 | | - provider.initialize() |
976 | | - |
977 | | - provider._unleash_event_callback = mock_event_callback |
978 | | - |
979 | | - provider.track("test_event", event_details=None) |
980 | | - |
981 | | - tracking_event = mock_event_callback.call_args[0][0] |
982 | | - assert "tracking_value" not in tracking_event.context |
983 | | - assert "tracking_details" not in tracking_event.context |
984 | | - |
985 | | - provider.shutdown() |
986 | | - |
987 | | - |
988 | | -def test_unleash_provider_track_event_details_without_value(): |
989 | | - """Test tracking with event details that don't have a value field.""" |
990 | | - mock_client = Mock() |
991 | | - mock_client.initialize_client.return_value = None |
992 | | - mock_event_callback = Mock() |
993 | | - |
994 | | - with patch( |
995 | | - "openfeature.contrib.provider.unleash.UnleashClient" |
996 | | - ) as mock_unleash_client: |
997 | | - mock_unleash_client.return_value = mock_client |
998 | | - |
999 | | - provider = UnleashProvider( |
1000 | | - url="http://localhost:4242", app_name="test-app", api_token="test-token" |
1001 | | - ) |
1002 | | - provider.initialize() |
1003 | | - |
1004 | | - provider._unleash_event_callback = mock_event_callback |
1005 | | - |
1006 | | - event_details = {"category": "test", "action": "view"} |
1007 | | - |
1008 | | - provider.track("test_event", event_details=event_details) |
1009 | | - |
1010 | | - tracking_event = mock_event_callback.call_args[0][0] |
1011 | | - assert tracking_event.context["tracking_value"] is None |
1012 | | - assert tracking_event.context["tracking_details"] == event_details |
1013 | | - |
1014 | | - provider.shutdown() |
0 commit comments