Skip to content

Commit f4e4322

Browse files
committed
add test
1 parent 7fc390b commit f4e4322

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package client
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gogo/protobuf/proto"
7+
)
8+
9+
func TestSerializeLogGroupToPB(t *testing.T) {
10+
// Prepare test data
11+
logGroupMap := map[string]interface{}{
12+
"Topic": "test-topic",
13+
"Source": "test-source",
14+
"LogTags": []interface{}{
15+
map[string]interface{}{
16+
"Key": "tag-key",
17+
"Value": "tag-value",
18+
},
19+
},
20+
"LogItems": []interface{}{
21+
map[string]interface{}{
22+
"Time": int(1678888888),
23+
"TimeNs": int(123456),
24+
"Contents": []interface{}{
25+
map[string]interface{}{
26+
"Key": "content-key",
27+
"Value": "content-value",
28+
},
29+
},
30+
},
31+
},
32+
}
33+
34+
// Execute
35+
data, err := serializeLogGroupToPB(logGroupMap)
36+
if err != nil {
37+
t.Fatalf("serializeLogGroupToPB failed: %v", err)
38+
}
39+
40+
// Validate
41+
if len(data) == 0 {
42+
t.Fatal("serialized data is empty")
43+
}
44+
45+
var lg LogGroup
46+
if err := proto.Unmarshal(data, &lg); err != nil {
47+
t.Fatalf("failed to unmarshal serialized data: %v", err)
48+
}
49+
50+
if lg.GetTopic() != "test-topic" {
51+
t.Errorf("expected topic 'test-topic', got %s", lg.GetTopic())
52+
}
53+
54+
if lg.GetSource() != "test-source" {
55+
t.Errorf("expected source 'test-source', got %s", lg.GetSource())
56+
}
57+
58+
if len(lg.LogTags) != 1 {
59+
t.Fatalf("expected 1 tag, got %d", len(lg.LogTags))
60+
}
61+
if lg.LogTags[0].GetKey() != "tag-key" || lg.LogTags[0].GetValue() != "tag-value" {
62+
t.Errorf("unexpected tag content: %v", lg.LogTags[0])
63+
}
64+
65+
if len(lg.Logs) != 1 {
66+
t.Fatalf("expected 1 log item, got %d", len(lg.Logs))
67+
}
68+
if lg.Logs[0].GetTime() != 1678888888 {
69+
t.Errorf("expected time 1678888888, got %d", lg.Logs[0].GetTime())
70+
}
71+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package client
2+
3+
import (
4+
"bytes"
5+
"io/ioutil"
6+
"strconv"
7+
"testing"
8+
9+
"github.com/alibabacloud-go/tea/tea"
10+
)
11+
12+
func TestZstdSimple(t *testing.T) {
13+
// 准备测试数据
14+
input := "Hello, Zstd!"
15+
originalBytes := []byte(input)
16+
compressType := "zstd"
17+
18+
// 1. 压缩
19+
compressedBytes, err := Compress(originalBytes, &compressType)
20+
if err != nil {
21+
t.Fatalf("Compression failed: %v", err)
22+
}
23+
24+
// 确保压缩后的数据不为空
25+
if len(compressedBytes) == 0 {
26+
t.Fatal("Compressed data is empty")
27+
}
28+
29+
// 2. 解压
30+
// ReadAndUncompressBlock 需要原始大小作为字符串参数
31+
rawSizeStr := strconv.Itoa(len(originalBytes))
32+
33+
resultReader, err := ReadAndUncompressBlock(
34+
bytes.NewReader(compressedBytes),
35+
&compressType,
36+
tea.String(rawSizeStr),
37+
)
38+
if err != nil {
39+
t.Fatalf("Decompression failed: %v", err)
40+
}
41+
42+
decodedBytes, err := ioutil.ReadAll(resultReader)
43+
if err != nil {
44+
t.Fatalf("Failed to read decompressed data: %v", err)
45+
}
46+
47+
// 3. 验证
48+
if string(decodedBytes) != input {
49+
t.Errorf("Content mismatch.\nExpected: %s\nGot: %s", input, string(decodedBytes))
50+
}
51+
}

0 commit comments

Comments
 (0)