|
7 | 7 | "reflect"
|
8 | 8 | "testing"
|
9 | 9 |
|
| 10 | + "github.com/stretchr/testify/assert" |
10 | 11 | "github.com/stretchr/testify/require"
|
11 | 12 |
|
12 | 13 | statsigProvider "github.com/open-feature/go-sdk-contrib/providers/statsig/pkg"
|
@@ -88,6 +89,88 @@ func TestBoolLayerEvaluation(t *testing.T) {
|
88 | 89 | }
|
89 | 90 | }
|
90 | 91 |
|
| 92 | +func TestConvertsValidUserIDToString(t *testing.T) { |
| 93 | + evalCtx := of.FlattenedContext{ |
| 94 | + "UserID": "test_user", |
| 95 | + } |
| 96 | + |
| 97 | + user, err := statsigProvider.ToStatsigUser(evalCtx) |
| 98 | + assert.NoError(t, err) |
| 99 | + assert.Equal(t, "test_user", user.UserID) |
| 100 | +} |
| 101 | + |
| 102 | +// Converts valid EvaluationContext with all fields correctly to statsig.User |
| 103 | +func TestConvertsValidEvaluationContextToStatsigUser(t *testing.T) { |
| 104 | + evalCtx := of.FlattenedContext{ |
| 105 | + of.TargetingKey: "test-key", |
| 106 | + |
| 107 | + "IpAddress": "192.168.1.1", |
| 108 | + "UserAgent": "Mozilla/5.0", |
| 109 | + "Country": "US", |
| 110 | + "Locale": "en-US", |
| 111 | + "AppVersion": "1.0.0", |
| 112 | + "Custom": map[string]interface{}{"customKey": "customValue"}, |
| 113 | + "PrivateAttributes": map[string]interface{}{"privateKey": "privateValue"}, |
| 114 | + "StatsigEnvironment": map[string]string{"envKey": "envValue"}, |
| 115 | + "CustomIDs": map[string]string{"customIDKey": "customIDValue"}, |
| 116 | + "custom-key": "custom-value", |
| 117 | + } |
| 118 | + |
| 119 | + user, err := statsigProvider.ToStatsigUser(evalCtx) |
| 120 | + if err != nil { |
| 121 | + t.Fatalf("expected no error, got %v", err) |
| 122 | + } |
| 123 | + |
| 124 | + if user.UserID != "test-key" { |
| 125 | + t.Errorf("expected UserID to be 'test-key', got %v", user.UserID) |
| 126 | + } |
| 127 | + if user. Email != "[email protected]" { |
| 128 | + t. Errorf( "expected Email to be '[email protected]', got %v", user. Email) |
| 129 | + } |
| 130 | + if user.IpAddress != "192.168.1.1" { |
| 131 | + t.Errorf("expected IpAddress to be '192.168.1.1', got %v", user.IpAddress) |
| 132 | + } |
| 133 | + if user.UserAgent != "Mozilla/5.0" { |
| 134 | + t.Errorf("expected UserAgent to be 'Mozilla/5.0', got %v", user.UserAgent) |
| 135 | + } |
| 136 | + if user.Country != "US" { |
| 137 | + t.Errorf("expected Country to be 'US', got %v", user.Country) |
| 138 | + } |
| 139 | + if user.Locale != "en-US" { |
| 140 | + t.Errorf("expected Locale to be 'en-US', got %v", user.Locale) |
| 141 | + } |
| 142 | + if user.AppVersion != "1.0.0" { |
| 143 | + t.Errorf("expected AppVersion to be '1.0.0', got %v", user.AppVersion) |
| 144 | + } |
| 145 | + if user.Custom["customKey"] != "customValue" { |
| 146 | + t.Errorf("expected Custom['customKey'] to be 'customValue', got %v", user.Custom["customKey"]) |
| 147 | + } |
| 148 | + if user.PrivateAttributes["privateKey"] != "privateValue" { |
| 149 | + t.Errorf("expected PrivateAttributes['privateKey'] to be 'privateValue', got %v", user.PrivateAttributes["privateKey"]) |
| 150 | + } |
| 151 | + if user.StatsigEnvironment["envKey"] != "envValue" { |
| 152 | + t.Errorf("expected StatsigEnvironment['envKey'] to be 'envValue', got %v", user.StatsigEnvironment["envKey"]) |
| 153 | + } |
| 154 | + if user.CustomIDs["customIDKey"] != "customIDValue" { |
| 155 | + t.Errorf("expected CustomIDs['customIDKey'] to be 'customIDValue', got %v", user.CustomIDs["customIDKey"]) |
| 156 | + } |
| 157 | + if user.Custom["custom-key"] != "custom-value" { |
| 158 | + t.Errorf("expected CustomIDs['custom-key'] to be 'custom_value', got %v", user.Custom["custom-key"]) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +// Handles missing TargetingKey by checking for "key" in EvaluationContext |
| 163 | +func TestHandlesMissingTargetingKey(t *testing.T) { |
| 164 | + evalCtx := of.FlattenedContext{ |
| 165 | + "dummy-key": "test-key", |
| 166 | + } |
| 167 | + |
| 168 | + _, err := statsigProvider.ToStatsigUser(evalCtx) |
| 169 | + if err == nil { |
| 170 | + t.Fatalf("expected error") |
| 171 | + } |
| 172 | +} |
| 173 | + |
91 | 174 | func cleanup() {
|
92 | 175 | provider.Shutdown()
|
93 | 176 | }
|
|
0 commit comments