diff --git a/zap/field_test.go b/zap/field_test.go index ead5af2386b5606a3861dc563ba6e49a6ed43f7b..5c107d7bc769c04a066b636e0f7a79ca892420f8 100644 --- a/zap/field_test.go +++ b/zap/field_test.go @@ -19,7 +19,7 @@ func TestCategory(t *testing.T) { want zap.Field }{ {name: "ok", field: Category("update"), want: zap.String("category", "update")}, - {name: "invalid", field: Category(""), want: zap.Skip()}, + {name: "invalid", field: Category(""), want: zap.String("category", "")}, } for _, tc := range tests { @@ -36,7 +36,7 @@ func TestComponent(t *testing.T) { want zap.Field }{ {name: "ok", field: Component("Items"), want: zap.String("component", "Items")}, - {name: "invalid", field: Component(""), want: zap.Skip()}, + {name: "invalid", field: Component(""), want: zap.String("component", "")}, } for _, tc := range tests { @@ -53,7 +53,7 @@ func TestEvent(t *testing.T) { want zap.Field }{ {name: "ok", field: Event("items.create"), want: zap.String("event", "items.create")}, - {name: "invalid", field: Event(""), want: zap.Skip()}, + {name: "invalid", field: Event(""), want: zap.String("event", "")}, } for _, tc := range tests { @@ -63,7 +63,7 @@ func TestEvent(t *testing.T) { } } -func TestObjectID(t *testing.T) { +func TestObject(t *testing.T) { item := &items.Item{ ID: "c4ca4238a0b923820dcc509a6f75849b", SpaceID: "c81e728d9d4c2f636f067f89cc14862c", @@ -82,21 +82,24 @@ func TestObjectID(t *testing.T) { {name: "system object", field: Object(item), want: zap.Reflect("object", oid)}, {name: "object id", field: Object(itemId), want: zap.Reflect("object", oid)}, {name: "string", field: Object(oid.String()), want: zap.Reflect("object", oid)}, - {name: "invalid", field: Object(nil), want: zap.Skip()}, + {name: "invalid", field: Object(nil), want: zap.Reflect("object", (*id.ObjectId)(nil))}, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - if tc.want.Equals(zap.Skip()) { - assert.True(t, tc.want.Equals(tc.field)) - return + wantObjectId, ok1 := tc.want.Interface.(*id.ObjectId) + fieldObjectId, ok2 := tc.field.Interface.(*id.ObjectId) + + if ok1 && ok2 && wantObjectId != nil && fieldObjectId != nil { + assert.Equal(t, wantObjectId.String(), fieldObjectId.String()) + } else { + assert.Equal(t, tc.want.Interface, tc.field.Interface) } - assert.Equal(t, tc.want.Interface.(id.Descriptor).String(), tc.field.Interface.(id.Descriptor).String()) }) } } -func TestCallerID(t *testing.T) { +func TestCaller(t *testing.T) { user := &users.User{ ID: "c4ca4238a0b923820dcc509a6f75849b", } @@ -112,21 +115,24 @@ func TestCallerID(t *testing.T) { {name: "system object", field: Caller(user), want: zap.Reflect("caller", oid)}, {name: "object id", field: Caller(userId), want: zap.Reflect("caller", oid)}, {name: "string", field: Caller(oid.String()), want: zap.Reflect("caller", oid)}, - {name: "invalid", field: Caller(nil), want: zap.Skip()}, + {name: "invalid", field: Caller(nil), want: zap.Reflect("caller", (*id.ObjectId)(nil))}, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - if tc.want.Equals(zap.Skip()) { - assert.True(t, tc.want.Equals(tc.field)) - return + wantObjectId, ok1 := tc.want.Interface.(*id.ObjectId) + fieldObjectId, ok2 := tc.field.Interface.(*id.ObjectId) + + if ok1 && ok2 && wantObjectId != nil && fieldObjectId != nil { + assert.Equal(t, wantObjectId.String(), fieldObjectId.String()) + } else { + assert.Equal(t, tc.want.Interface, tc.field.Interface) } - assert.Equal(t, tc.want.Interface.(id.Descriptor).String(), tc.field.Interface.(id.Descriptor).String()) }) } } -func TestCallerIDFromContext(t *testing.T) { +func TestCallerFromContext(t *testing.T) { ctx := auth.WithSystem(context.Background()) oid := id.MustObjectId(auth.GetPrincipal(ctx)) @@ -136,16 +142,19 @@ func TestCallerIDFromContext(t *testing.T) { want zap.Field }{ {name: "ok", field: CallerFromContext(ctx), want: zap.Reflect("caller", oid)}, - {name: "invalid", field: CallerFromContext(context.TODO()), want: zap.Skip()}, + {name: "invalid", field: CallerFromContext(context.TODO()), want: zap.Reflect("caller", (*id.ObjectId)(nil))}, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - if tc.want.Equals(zap.Skip()) { - assert.True(t, tc.want.Equals(tc.field)) - return + wantObjectId, ok1 := tc.want.Interface.(*id.ObjectId) + fieldObjectId, ok2 := tc.field.Interface.(*id.ObjectId) + + if ok1 && ok2 && wantObjectId != nil && fieldObjectId != nil { + assert.Equal(t, wantObjectId.String(), fieldObjectId.String()) + } else { + assert.Equal(t, tc.want.Interface, tc.field.Interface) } - assert.Equal(t, tc.want.Interface.(id.Descriptor).String(), tc.field.Interface.(id.Descriptor).String()) }) } } @@ -157,7 +166,7 @@ func TestAttr(t *testing.T) { want zap.Field }{ {name: "ok", field: Attr(map[string]string{"a": "b"}), want: zap.Reflect("attr", map[string]string{"a": "b"})}, - {name: "invalid", field: Attr(nil), want: zap.Skip()}, + {name: "invalid", field: Attr(nil), want: zap.Reflect("attr", nil)}, } for _, tc := range tests { @@ -174,7 +183,7 @@ func TestTags(t *testing.T) { want zap.Field }{ {name: "ok", field: Tags("a", "b", "c"), want: zap.Strings("tags", []string{"a", "b", "c"})}, - {name: "invalid", field: Tags(nil...), want: zap.Skip()}, + {name: "invalid", field: Tags(nil...), want: zap.Strings("tags", nil)}, } for _, tc := range tests {