|
| 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 | + tests := []struct { |
| 27 | + name string |
| 28 | + client *StorageClient |
| 29 | + doc *types.Document |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "nil document", |
| 33 | + client: &StorageClient{}, |
| 34 | + doc: nil, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "empty document", |
| 38 | + client: &StorageClient{}, |
| 39 | + doc: &types.Document{}, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "filled document", |
| 43 | + client: &StorageClient{}, |
| 44 | + doc: &types.Document{ |
| 45 | + Hostname: "host-1", |
| 46 | + Region: "cn", |
| 47 | + UploadedTime: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC), |
| 48 | + Time: "2026-01-01 00:00:00.000 +0000", |
| 49 | + TracerName: "cpu", |
| 50 | + TracerID: "task-1", |
| 51 | + TracerTime: "2026-01-01 00:00:00.000 +0000", |
| 52 | + TracerData: "payload", |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "nil receiver", |
| 57 | + client: nil, |
| 58 | + doc: &types.Document{ |
| 59 | + TracerName: "memory", |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + for _, tt := range tests { |
| 65 | + t.Run(tt.name, func(t *testing.T) { |
| 66 | + var before *types.Document |
| 67 | + if tt.doc != nil { |
| 68 | + snapshot := *tt.doc |
| 69 | + before = &snapshot |
| 70 | + } |
| 71 | + |
| 72 | + if err := tt.client.Write(tt.doc); err != nil { |
| 73 | + t.Errorf("Write() returned unexpected error: %v", err) |
| 74 | + } |
| 75 | + |
| 76 | + if before != nil && !reflect.DeepEqual(*before, *tt.doc) { |
| 77 | + t.Errorf("Write() should not mutate input document, before=%+v, after=%+v", *before, *tt.doc) |
| 78 | + } |
| 79 | + }) |
| 80 | + } |
| 81 | +} |
0 commit comments