mirror of
https://github.com/hanzoai/insights.git
synced 2026-11-03 00:52:32 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers, and the ptr helper went once new(expr) could say it; omitzero is not applied, since it changes what the JSON says. Co-authored-by: Hanzo Dev <[email protected]>
126 lines
2.9 KiB
Go
126 lines
2.9 KiB
Go
package events
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestInsightsEventRoundTrip(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input InsightsEvent
|
|
}{
|
|
{
|
|
name: "all fields populated",
|
|
input: InsightsEvent{
|
|
Token: "tok_abc",
|
|
Event: "$pageview",
|
|
Properties: map[string]any{"url": "https://example.com", "$browser": "Chrome"},
|
|
Timestamp: "2026-01-01T00:00:00Z",
|
|
Uuid: "550e8400-e29b-41d4-a716-446655440000",
|
|
DistinctId: "user-42",
|
|
Lat: 40.712776,
|
|
Lng: -74.005974,
|
|
CountryCode: "US",
|
|
},
|
|
},
|
|
{
|
|
name: "numeric property values survive as float64",
|
|
input: InsightsEvent{
|
|
Token: "tok_abc",
|
|
Event: "$pageview",
|
|
Properties: map[string]any{"count": float64(42), "price": 9.99},
|
|
Uuid: "uuid-1",
|
|
DistinctId: "user-1",
|
|
},
|
|
},
|
|
{
|
|
name: "empty properties",
|
|
input: InsightsEvent{
|
|
Token: "tok_abc",
|
|
Event: "$identify",
|
|
Properties: map[string]any{},
|
|
Uuid: "uuid-2",
|
|
DistinctId: "user-2",
|
|
},
|
|
},
|
|
{
|
|
name: "nil properties",
|
|
input: InsightsEvent{
|
|
Token: "tok_abc",
|
|
Event: "$identify",
|
|
Properties: nil,
|
|
Uuid: "uuid-3",
|
|
DistinctId: "user-3",
|
|
},
|
|
},
|
|
{
|
|
name: "zero lat/lng",
|
|
input: InsightsEvent{
|
|
Token: "tok_abc",
|
|
Event: "$pageview",
|
|
Properties: map[string]any{},
|
|
Uuid: "uuid-4",
|
|
DistinctId: "user-4",
|
|
Lat: 0.0,
|
|
Lng: 0.0,
|
|
},
|
|
},
|
|
{
|
|
name: "timestamp as numeric",
|
|
input: InsightsEvent{
|
|
Token: "tok_abc",
|
|
Event: "$pageview",
|
|
Properties: map[string]any{},
|
|
Timestamp: float64(1704067200),
|
|
Uuid: "uuid-5",
|
|
DistinctId: "user-5",
|
|
},
|
|
},
|
|
{
|
|
name: "nested properties",
|
|
input: InsightsEvent{
|
|
Token: "tok_abc",
|
|
Event: "$pageview",
|
|
Properties: map[string]any{
|
|
"nested": map[string]any{"key": "value"},
|
|
"list": []any{"a", "b"},
|
|
},
|
|
Uuid: "uuid-6",
|
|
DistinctId: "user-6",
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
data, err := tt.input.MarshalJSON()
|
|
require.NoError(t, err)
|
|
|
|
var result InsightsEvent
|
|
err = result.UnmarshalJSON(data)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tt.input.Token, result.Token)
|
|
assert.Equal(t, tt.input.Event, result.Event)
|
|
assert.Equal(t, tt.input.Uuid, result.Uuid)
|
|
assert.Equal(t, tt.input.DistinctId, result.DistinctId)
|
|
assert.Equal(t, tt.input.Lat, result.Lat)
|
|
assert.Equal(t, tt.input.Lng, result.Lng)
|
|
assert.Equal(t, tt.input.CountryCode, result.CountryCode)
|
|
|
|
if tt.input.Properties == nil {
|
|
assert.True(t, len(result.Properties) == 0)
|
|
} else {
|
|
assert.Equal(t, tt.input.Properties, result.Properties)
|
|
}
|
|
|
|
if tt.input.Timestamp != nil {
|
|
assert.NotNil(t, result.Timestamp)
|
|
}
|
|
})
|
|
}
|
|
}
|