-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
executable file
·108 lines (98 loc) · 2.26 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
package main
import (
"bytes"
"encoding/json"
"github.com/gin-gonic/gin"
"net/http"
"net/http/httptest"
"os"
"sync"
"testing"
)
func setTestEnv() {
os.Setenv("HOST", "localhost")
os.Setenv("PORT", "8000")
}
func postReq(eng *gin.Engine, url string, buf *bytes.Buffer) []byte {
post, _ := http.NewRequest("POST", url, buf)
post.Header.Set("X-Custom-Header", "myvalue")
post.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
eng.ServeHTTP(response, post)
return response.Body.Bytes()
}
func TestRouter(t *testing.T) {
db, err := dbTest()
if err != nil {
t.Error("db err: ", err)
}
setTestEnv()
testRouter := router(db)
// post/get points
{
numRequest := 10
wg := &sync.WaitGroup{}
for count := 0; count < numRequest; count++ {
wg.Add(1)
go func() {
defer wg.Done()
jp, _ := json.Marshal(pointRnd())
postReq(testRouter, "/api/v1/locs", bytes.NewBuffer(jp))
}()
}
wg.Wait()
points, _ := http.NewRequest("GET", "/api/v1/locs/all", nil)
response := httptest.NewRecorder()
testRouter.ServeHTTP(response, points)
res := struct {
Msg string `json:"msg"`
Body []geoLocation `json:"body"`
}{}
err = json.Unmarshal(response.Body.Bytes(), &res)
if err != nil {
t.Error("err Unmarshal: ", err)
return
}
if len(res.Body) != numRequest {
t.Error("error, count post point don't coincides with get all point")
return
}
}
// near
{
loc := pointRnd()
req, _ := json.Marshal(reqNear{
Scope: 10000000, TGeos: "Point",
Lat: loc.Location.Coordinates[1],
Lng: loc.Location.Coordinates[0],
})
res := struct {
Msg string `json:"msg"`
Body []geoLocation `json:"body"`
}{}
wg := &sync.WaitGroup{}
for count := 0; count < 2; count++ {
wg.Add(1)
go func() {
defer wg.Done()
near, err := http.NewRequest(
"GET", "/api/v1/locs/near",
bytes.NewBuffer(req),
)
if err != nil {
t.Error("err near: ", err)
}
response := httptest.NewRecorder()
testRouter.ServeHTTP(response, near)
err = json.Unmarshal(response.Body.Bytes(), &res)
if len(res.Body) == 0 {
t.Error("error, empty near locations: ", res.Msg)
}
}()
}
if err != nil {
t.Errorf("error get near loc: %v", err)
}
wg.Wait()
}
}