Skip to content

Commit ded4ecc

Browse files
committed
test(internal/storage/null): add unit testing for null
1 parent 866b008 commit ded4ecc

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

internal/storage/null/null_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2026 The HuaTuo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package null
16+
17+
import (
18+
"reflect"
19+
"testing"
20+
"time"
21+
22+
"huatuo-bamai/internal/storage/types"
23+
)
24+
25+
func TestStorageClientWrite(t *testing.T) {
26+
now := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)
27+
28+
tests := []struct {
29+
name string
30+
client *StorageClient
31+
doc *types.Document
32+
}{
33+
{
34+
name: "nil document",
35+
client: &StorageClient{},
36+
doc: nil,
37+
},
38+
{
39+
name: "empty document",
40+
client: &StorageClient{},
41+
doc: &types.Document{},
42+
},
43+
{
44+
name: "filled document",
45+
client: &StorageClient{},
46+
doc: &types.Document{
47+
Hostname: "host-1",
48+
Region: "cn",
49+
UploadedTime: now,
50+
Time: "2025-01-01 00:00:00.000 +0000",
51+
TracerName: "cpu",
52+
TracerID: "task-1",
53+
TracerTime: "2025-01-01 00:00:00.000 +0000",
54+
TracerData: "payload",
55+
},
56+
},
57+
{
58+
name: "nil receiver",
59+
client: nil,
60+
doc: &types.Document{
61+
TracerName: "memory",
62+
},
63+
},
64+
}
65+
66+
for _, tt := range tests {
67+
t.Run(tt.name, func(t *testing.T) {
68+
var before *types.Document
69+
if tt.doc != nil {
70+
snapshot := *tt.doc
71+
before = &snapshot
72+
}
73+
74+
if err := tt.client.Write(tt.doc); err != nil {
75+
t.Errorf("Write() returned unexpected error: %v", err)
76+
}
77+
78+
if before != nil && !reflect.DeepEqual(*before, *tt.doc) {
79+
t.Errorf("Write() should not mutate input document, before=%+v, after=%+v", *before, *tt.doc)
80+
}
81+
})
82+
}
83+
}

0 commit comments

Comments
 (0)