-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
111 lines (98 loc) · 2.76 KB
/
testing.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
package testing
import (
"bytes"
_ "embed"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/zitryss/aye-and-nay/domain/model"
. "github.com/zitryss/aye-and-nay/internal/generator"
)
const (
TOLERANCE = 0.000000000000001
)
var (
//go:embed small.png
png []byte
)
func Png() model.File {
buf := bytes.NewBuffer(png)
return model.File{Reader: buf, Size: int64(buf.Len())}
}
func AlbumFactory(id IdGenFunc, ids Ids) model.Album {
album := id()
img1 := model.Image{Id: id(), Src: "/aye-and-nay/albums/" + ids.Base64(0) + "/images/" + ids.Base64(1), Rating: 0.48954984}
img2 := model.Image{Id: id(), Src: "/aye-and-nay/albums/" + ids.Base64(0) + "/images/" + ids.Base64(2), Rating: 0.19186324}
img3 := model.Image{Id: id(), Src: "/aye-and-nay/albums/" + ids.Base64(0) + "/images/" + ids.Base64(3), Rating: 0.41218211}
img4 := model.Image{Id: id(), Src: "/aye-and-nay/albums/" + ids.Base64(0) + "/images/" + ids.Base64(4), Rating: 0.77920413}
img5 := model.Image{Id: id(), Src: "/aye-and-nay/albums/" + ids.Base64(0) + "/images/" + ids.Base64(5), Rating: 0.13278389}
imgs := []model.Image{img1, img2, img3, img4, img5}
edgs := map[uint64]map[uint64]int{}
edgs[ids.Uint64(1)] = map[uint64]int{}
edgs[ids.Uint64(2)] = map[uint64]int{}
edgs[ids.Uint64(3)] = map[uint64]int{}
edgs[ids.Uint64(4)] = map[uint64]int{}
edgs[ids.Uint64(5)] = map[uint64]int{}
expires := time.Time{}
alb := model.Album{album, imgs, edgs, expires}
return alb
}
func AssertStatusCode(t *testing.T, w *httptest.ResponseRecorder, code int) {
t.Helper()
got := w.Code
want := code
if got != want {
t.Errorf("Status Code = %v; want %v", got, want)
}
}
func AssertHeader(t *testing.T, w *httptest.ResponseRecorder, header string, value string) {
t.Helper()
got := w.Result().Header.Get(header)
want := value
if got != want {
t.Errorf("%v = %v; want %v", header, got, want)
}
}
func AssertBody(t *testing.T, w *httptest.ResponseRecorder, body string) {
t.Helper()
got := w.Body.String()
want := body
if got != want {
t.Errorf("Body = %v; want %v", got, want)
}
}
func AssertChannel(t *testing.T, heartbeat <-chan any) any {
t.Helper()
if heartbeat == nil {
return nil
}
v := any(nil)
select {
case v = <-heartbeat:
case <-time.After(1 * time.Second):
t.Error("<-time.After(1 * time.Second)")
}
return v
}
func AssertNotChannel(t *testing.T, heartbeat <-chan any) {
t.Helper()
if heartbeat == nil {
return
}
select {
case <-heartbeat:
t.Error("<-heartbeatDel")
case <-time.After(1 * time.Second):
}
}
func AssertEqualFile(t *testing.T, x, y model.File) {
t.Helper()
bx := make([]byte, x.Size)
_, err := x.Read(bx)
assert.NoError(t, err)
by := make([]byte, y.Size)
_, err = y.Read(by)
assert.NoError(t, err)
assert.Equal(t, bx, by)
}