-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
132 lines (102 loc) · 3.1 KB
/
main_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
// main_test.go
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"goroutine/handler"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var testClient *mongo.Client
var testCollection *mongo.Collection
func setup() {
err := godotenv.Load()
if err != nil {
panic("Error loading .env file")
}
mongoUser := os.Getenv("MONGO_USER")
mongoPass := os.Getenv("MONGO_PASS")
mongoHost := os.Getenv("MONGO_HOST")
mongoPort := os.Getenv("MONGO_PORT")
mongoDB := os.Getenv("MONGO_DB")
var mongoURI = "mongodb://" + mongoHost + ":" + mongoPort
clientOptions := options.Client().ApplyURI(mongoURI).SetAuth(options.Credential{
Username: mongoUser,
Password: mongoPass,
})
testClient, err = mongo.Connect(context.Background(), clientOptions)
if err != nil {
panic(err)
}
testCollection = testClient.Database(mongoDB).Collection("items")
}
func teardown() {
testClient.Disconnect(context.Background())
}
func TestGetItems(t *testing.T) {
setup()
defer teardown()
r := gin.Default()
r.GET("/items", handler.GetItems)
req, _ := http.NewRequest("GET", "/items", nil)
fmt.Println(req)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Errorf("Expected status code %d, but got %d. Response body: %s", http.StatusOK, resp.Code, resp.Body.String())
} else {
assert.Equal(t, http.StatusOK, resp.Code)
}
}
func TestCreateItem(t *testing.T) {
setup()
defer teardown()
r := gin.Default()
r.POST("/items", handler.CreateItem)
item := handler.Item{Name: "Test Item", Price: 100}
jsonData, _ := json.Marshal(item)
req, _ := http.NewRequest("POST", "/items", bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
assert.Equal(t, http.StatusCreated, resp.Code)
}
func TestUpdateItem(t *testing.T) {
setup()
defer teardown()
r := gin.Default()
r.PUT("/items/:id", handler.UpdateItem)
item := handler.Item{Name: "Test Item", Price: 100}
insertResult, _ := testCollection.InsertOne(context.Background(), item)
id := insertResult.InsertedID.(primitive.ObjectID).Hex()
updatedItem := handler.Item{Name: "Updated Item", Price: 150}
jsonData, _ := json.Marshal(updatedItem)
req, _ := http.NewRequest("PUT", "/items/"+id, bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code)
}
func TestDeleteItem(t *testing.T) {
setup()
defer teardown()
r := gin.Default()
r.DELETE("/items/:id", handler.DeleteItem)
item := handler.Item{Name: "Test Item", Price: 100}
insertResult, _ := testCollection.InsertOne(context.Background(), item)
id := insertResult.InsertedID.(primitive.ObjectID).Hex()
req, _ := http.NewRequest("DELETE", "/items/"+id, nil)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code)
}