|
| 1 | +package eppoclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +// TestAssignmentEventSerialization tests the JSON serialization and deserialization of AssignmentEvent |
| 10 | +func TestAssignmentEventSerialization(t *testing.T) { |
| 11 | + // Create a test case with each type |
| 12 | + testCases := []AssignmentEvent{ |
| 13 | + // empty subject attributes |
| 14 | + { |
| 15 | + Experiment: "testExperiment", |
| 16 | + FeatureFlag: "testFeatureFlag", |
| 17 | + Allocation: "testAllocation", |
| 18 | + Variation: String("testVariation"), |
| 19 | + Subject: "testSubject", |
| 20 | + Timestamp: "testTimestamp", |
| 21 | + }, |
| 22 | + { |
| 23 | + Experiment: "testExperiment", |
| 24 | + FeatureFlag: "testFeatureFlag", |
| 25 | + Allocation: "testAllocation", |
| 26 | + Variation: Bool(true), |
| 27 | + Subject: "testSubject", |
| 28 | + Timestamp: "testTimestamp", |
| 29 | + SubjectAttributes: dictionary{"testKey": "testValue"}, |
| 30 | + }, |
| 31 | + { |
| 32 | + Experiment: "testExperiment", |
| 33 | + FeatureFlag: "testFeatureFlag", |
| 34 | + Allocation: "testAllocation", |
| 35 | + Variation: Numeric(123.45), |
| 36 | + Subject: "testSubject", |
| 37 | + Timestamp: "testTimestamp", |
| 38 | + SubjectAttributes: dictionary{"testKey": "testValue"}, |
| 39 | + }, |
| 40 | + { |
| 41 | + Experiment: "testExperiment", |
| 42 | + FeatureFlag: "testFeatureFlag", |
| 43 | + Allocation: "testAllocation", |
| 44 | + Variation: String("testVariation"), |
| 45 | + Subject: "testSubject", |
| 46 | + Timestamp: "testTimestamp", |
| 47 | + SubjectAttributes: dictionary{"testKey": "testValue"}, |
| 48 | + }, |
| 49 | + { |
| 50 | + Experiment: "testExperiment", |
| 51 | + FeatureFlag: "testFeatureFlag", |
| 52 | + Allocation: "testAllocation", |
| 53 | + Variation: String("{\"foo\":\"bar\",\"car\":\"far\"}"), |
| 54 | + Subject: "testSubject", |
| 55 | + Timestamp: "testTimestamp", |
| 56 | + SubjectAttributes: dictionary{"testKey": "testValue"}, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, original := range testCases { |
| 61 | + // Marshal to JSON |
| 62 | + marshaled, err := json.Marshal(original) |
| 63 | + if err != nil { |
| 64 | + t.Errorf("Failed to marshal: %v", err) |
| 65 | + } |
| 66 | + |
| 67 | + // Unmarshal from JSON |
| 68 | + var unmarshaled AssignmentEvent |
| 69 | + err = json.Unmarshal(marshaled, &unmarshaled) |
| 70 | + if err != nil { |
| 71 | + t.Errorf("Failed to unmarshal: %v", err) |
| 72 | + } |
| 73 | + |
| 74 | + // Compare the original and unmarshaled |
| 75 | + if !reflect.DeepEqual(original, unmarshaled) { |
| 76 | + t.Errorf("Original and unmarshaled Value are not equal. Original: %+v, Unmarshaled: %+v", original, unmarshaled) |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments