-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
87 lines (68 loc) · 2.21 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
package main
import (
"database/sql"
"fmt"
"log"
"math"
"net/http"
"net/http/httptest"
"os"
"testing"
"gotest.tools/v3/assert"
)
const confName = "[email protected]"
var sqlDB *sql.DB
func TestMain(m *testing.M) {
var err error
if err := initDatabase(); err != nil {
log.Fatal("cannot initialize database")
}
sqlDB, err = sql.Open("sqlite3", *sqlitePath)
if err != nil {
log.Fatal("cannot open database")
}
exitCode := m.Run()
sqlDB.Close()
sqlDB = nil
os.Remove("conferencemapper.db")
os.Exit(exitCode)
}
func TestGetRandomConfID(t *testing.T) {
for i := 0; i < 300; i++ {
result := getConfId(sqlDB, fmt.Sprintf("TestGetRandomConfID%d", i))
assert.Check(t, result > int(math.Pow10(*xDigitIDs-1)))
assert.Check(t, result < int(math.Pow10(*xDigitIDs)))
}
}
func TestMapperJustConfName(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/conferenceMapper?conference="+confName, nil)
assert.NilError(t, err)
confNameSanizied := sanitizeConferenceName(confName)
// in this test confName has uppercase letters which will be changed to lowercase only
assert.Check(t, confName != confNameSanizied)
inserted := insertConference(sqlDB, confNameSanizied, 12553)
assert.Check(t, inserted)
rec := httptest.NewRecorder()
handler := http.HandlerFunc(mapper)
handler.ServeHTTP(rec, req)
assert.Assert(t, rec.Code == http.StatusOK)
assert.Equal(t, rec.Body.String(), `{"id":12553,"conference":"`+confNameSanizied+`"}`+"\n")
}
func TestMapperUnknownConfID(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/conferenceMapper?id=01234", nil)
assert.NilError(t, err)
rec := httptest.NewRecorder()
handler := http.HandlerFunc(mapper)
handler.ServeHTTP(rec, req)
assert.Assert(t, rec.Code == http.StatusOK)
assert.Equal(t, rec.Body.String(), `{"id":1234,"conference":"false"}`+"\n")
}
func TestMapperConfIDAndConfName(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/conferenceMapper?id=1234&[email protected]", nil)
assert.NilError(t, err)
rec := httptest.NewRecorder()
handler := http.HandlerFunc(mapper)
handler.ServeHTTP(rec, req)
assert.Assert(t, rec.Code == http.StatusOK)
assert.Equal(t, rec.Body.String(), `{"id":1234,"conference":"false"}`+"\n")
}