MCPcopy Create free account
hub / github.com/KOSASIH/skybridge / TestNodeService

Function TestNodeService

src/api/node/node_test.go:17–116  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

15)
16
17func TestNodeService(t *testing.T) {
18 logger := logging.NewLogger("node_test")
19 nodeService := NewNodeService(logger)
20
21 t.Run("GetNode", func(t *testing.T) {
22 node := NewNode("node-1", "Node 1", "192.168.1.1", 8080)
23 nodeService.CreateNode(context.Background(), node)
24
25 req, err := http.NewRequest("GET", "/api/v1/nodes/node-1", nil)
26 if err != nil {
27 t.Fatal(err)
28 }
29 w := httptest.NewRecorder()
30 nodeService.ServeHTTP(w, req)
31
32 if w.Code != http.StatusOK {
33 t.Errorf("Expected status code %d, got %d", http.StatusOK, w.Code)
34 }
35
36 var gotNode Node
37 err = json.NewDecoder(w.Body).Decode(&gotNode)
38 if err != nil {
39 t.Fatal(err)
40 }
41
42 if gotNode.ID != node.ID {
43 t.Errorf("Expected node ID %s, got %s", node.ID, gotNode.ID)
44 }
45 })
46
47 t.Run("CreateNode", func(t *testing.T) {
48 node := NewNode("node-2", "Node 2", "192.168.1.2", 8081)
49
50 req, err := http.NewRequest("POST", "/api/v1/nodes", strings.NewReader(`{"name": "Node 2", "ip_address": "192.168.1.2", "port": 8081}`))
51 if err != nil {
52 t.Fatal(err)
53 }
54 w := httptest.NewRecorder()
55 nodeService.ServeHTTP(w, req)
56
57 if w.Code != http.StatusCreated {
58 t.Errorf("Expected status code %d, got %d", http.StatusCreated, w.Code)
59 }
60
61 gotNode, err := nodeService.GetNode(context.Background(), node.ID)
62 if err != nil {
63 t.Fatal(err)
64 }
65
66 if gotNode.ID != node.ID {
67 t.Errorf("Expected node ID %s, got %s", node.ID, gotNode.ID)
68 }
69 })
70
71 t.Run("UpdateNode", func(t *testing.T) {
72 node := NewNode("node-3", "Node 3", "192.168.1.3", 8082)
73 nodeService.CreateNode(context.Background(), node)
74

Callers

nothing calls this directly

Calls 6

CreateNodeMethod · 0.95
GetNodeMethod · 0.95
NewNodeServiceFunction · 0.85
FatalMethod · 0.80
ServeHTTPMethod · 0.80
NewNodeFunction · 0.70

Tested by

no test coverage detected