-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
51 lines (47 loc) · 1.24 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
package main_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/adisuper94/siesta/generated"
"github.com/jackc/pgx/v5/pgxpool"
)
func createDBConnection(connectionCount int32) *pgxpool.Pool {
pgxConfig, err := pgxpool.ParseConfig("postgres://adisuper:adisuper@localhost:5432/turbo?sslmode=disable")
if err != nil {
panic(err)
}
pgxConfig.MaxConns = connectionCount
conn, err := pgxpool.NewWithConfig(context.TODO(), pgxConfig)
if err != nil {
panic(err)
}
return conn
}
func Test1(t *testing.T) {
db := createDBConnection(2)
router := generated.GetRouter(db)
defer db.Close()
server := httptest.NewServer(router)
defer server.Close()
var url string
url = fmt.Sprintf("%s/messages", server.URL)
res, err := http.Get(url)
if err != nil {
fmt.Fprintf(os.Stderr, "error while call GET(all) endpoint for messages : \nurl: %s\nerror: %v\n", url, err)
t.Fail()
}
body, err := io.ReadAll(res.Body)
var prettyJSON bytes.Buffer
err = json.Indent(&prettyJSON, body,"", " ")
if res.StatusCode != http.StatusOK {
fmt.Fprintf(os.Stderr, "response status is not OK.\nresponse status:%d\n body:%s", res.StatusCode, body)
}
fmt.Println(string(prettyJSON.Bytes()))
}