-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
54 lines (43 loc) · 1.37 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
package main
import (
"context"
"net/http"
"net/http/httptest"
"testing"
api "agnostic-web-api/api"
db "agnostic-web-api/db"
config "agnostic-web-api/config"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func TestAPI(t *testing.T) {
// Create a request to pass to the API handler
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
// Create a response recorder to record the response
rr := httptest.NewRecorder()
// Create a handler for the API endpoint
handler := http.HandlerFunc(api.Router)
// Call the API endpoint handler with the request and response recorder
handler.ServeHTTP(rr, req)
// Check the status code is what we expect
if status := rr.Code; status != http.StatusOK {
t.Errorf("API endpoint returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the response body is what we expect
expected := `{"data":"ok"}`
if rr.Body.String() != expected {
t.Errorf("API endpoint returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
}
func TestDBConnection(t *testing.T) {
config.LoadEnv(".env")
db.Connect()
err := db.DB.Client().Ping(context.TODO(), readpref.Primary())
if err !=nil {
t.Errorf("Unable to connect to db %v", err)
}
}