-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain_test.go
156 lines (131 loc) · 3.55 KB
/
blockchain_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package blockchainlite
import (
"encoding/json"
"log"
"os"
"testing"
)
func removeFileIfExists(filePath string) {
if _, err := os.Stat(filePath); err == nil {
err := os.Remove(filePath)
if err != nil {
log.Fatalf("Failed to remove existing database file: %v", err)
}
} else if !os.IsNotExist(err) {
log.Fatalf("Error checking if file exists: %v", err)
}
}
func TestInsertBlock(t *testing.T) {
dbPath := "test_blockchain"
bc, err := NewBlockchain(dbPath)
if err != nil {
t.Fatalf("Failed to create blockchain: %v", err)
}
defer func() {
bc.Close()
removeFileIfExists(dbPath + ".db")
}()
data1 := map[string]interface{}{
"title": "First Block",
"value": 123,
}
err = bc.AddBlock(data1)
if err != nil {
t.Fatalf("Failed to insert block: %v", err)
}
data2 := []string{"Second Block", "Another piece of data"}
err = bc.AddBlock(data2)
if err != nil {
t.Fatalf("Failed to insert block: %v", err)
}
}
func TestGetBlockHistory(t *testing.T) {
dbPath := "test_blockchain"
bc, err := NewBlockchain(dbPath)
if err != nil {
t.Fatalf("Failed to create blockchain: %v", err)
}
defer func() {
bc.Close()
removeFileIfExists(dbPath + ".db")
}()
data1 := map[string]interface{}{
"title": "First Block",
"value": 123,
}
err = bc.AddBlock(data1)
if err != nil {
t.Fatalf("Failed to insert block: %v", err)
}
data2 := []string{"Second Block", "Another piece of data"}
err = bc.AddBlock(data2)
if err != nil {
t.Fatalf("Failed to insert block: %v", err)
}
// 获取区块历史记录
blocks, err := bc.GetBlockHistory()
if err != nil {
t.Fatalf("Failed to get block history: %v", err)
}
if len(blocks) != 2 {
t.Errorf("Expected 2 blocks, got %d", len(blocks))
}
// 检查区块内容
var dataMap1 map[string]interface{}
err = json.Unmarshal([]byte(blocks[0].Data), &dataMap1)
if err != nil {
t.Fatalf("Failed to unmarshal block data: %v", err)
}
title, ok1 := dataMap1["title"].(string)
value, ok2 := dataMap1["value"].(float64)
if !ok1 || !ok2 || title != "First Block" || value != 123.00 {
t.Errorf("Expected data for First Block, got %v", dataMap1)
}
var dataSlice []string
err = json.Unmarshal([]byte(blocks[1].Data), &dataSlice)
if err != nil {
t.Fatalf("Failed to unmarshal block data: %v", err)
}
if len(dataSlice) != 2 || dataSlice[0] != "Second Block" {
t.Errorf("Expected data for Second Block, got %v", dataSlice)
}
}
func TestGetLatestBlock(t *testing.T) {
dbPath := "test_blockchain"
bc, err := NewBlockchain(dbPath)
if err != nil {
t.Fatalf("Failed to create blockchain: %v", err)
}
defer func() {
bc.Close()
removeFileIfExists(dbPath + ".db")
}()
// 插入一些新区块
err = bc.AddBlock(map[string]interface{}{"title": "First Block", "value": 123})
if err != nil {
t.Fatalf("Failed to insert block: %v", err)
}
err = bc.AddBlock(map[string]interface{}{"title": "Second Block", "value": 456})
if err != nil {
t.Fatalf("Failed to insert block: %v", err)
}
// 获取最新的区块
latestBlock, err := bc.GetLatestBlock()
if err != nil {
t.Fatalf("Failed to get latest block: %v", err)
}
if latestBlock == nil {
t.Error("Expected latest block, got nil")
} else {
var dataMap map[string]interface{}
err = json.Unmarshal([]byte(latestBlock.Data), &dataMap)
if err != nil {
t.Fatalf("Failed to unmarshal latest block data: %v", err)
}
title, ok1 := dataMap["title"].(string)
value, ok2 := dataMap["value"].(float64)
if !ok1 || !ok2 || title != "Second Block" || value != 456 {
t.Errorf("Expected latest block data for Second Block, got %v", dataMap)
}
}
}