/
common.go
113 lines (100 loc) · 2.47 KB
/
common.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
112
113
package tests
import (
"bytes"
"fmt"
"github.com/bozaro/tech-db-forum/generated/client"
"github.com/bozaro/tech-db-forum/generated/models"
"github.com/go-openapi/strfmt"
"io"
"io/ioutil"
"math/rand"
"reflect"
"strings"
"time"
)
type UserByNickname models.Users
func (a UserByNickname) Len() int { return len(a) }
func (a UserByNickname) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a UserByNickname) Less(i, j int) bool {
return strings.ToLower(a[i].Nickname) < strings.ToLower(a[j].Nickname)
}
type ThreadByCreated models.Threads
func (a ThreadByCreated) Len() int { return len(a) }
func (a ThreadByCreated) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ThreadByCreated) Less(i, j int) bool {
null_or_default := func(t *strfmt.DateTime) time.Time {
if t == nil {
return time.Time{}
}
return time.Time(*t)
}
time_i := null_or_default(a[i].Created).UTC()
time_j := null_or_default(a[j].Created).UTC()
return time_i.Before(time_j)
}
func CheckNil(err interface{}) {
if err != nil {
panic(err)
}
}
func CheckIsType(expectedType interface{}, object interface{}) {
if !ObjectsAreEqual(reflect.TypeOf(object), reflect.TypeOf(expectedType)) {
panic(fmt.Sprintf("Object expected to be of type %v, but was %v", reflect.TypeOf(expectedType), reflect.TypeOf(object)))
}
}
func ObjectsAreEqual(expected, actual interface{}) bool {
if expected == nil || actual == nil {
return expected == actual
}
return reflect.DeepEqual(expected, actual)
}
func GetBody(stream *io.ReadCloser) ([]byte, error) {
if *stream == nil {
return []byte{}, nil
}
ibody := *stream
defer ibody.Close()
body, err := ioutil.ReadAll(ibody)
if err != nil {
return body, err
}
*stream = ioutil.NopCloser(bytes.NewReader(body))
return body, nil
}
func Modifications(checker func(c *client.Forum, f *Factory, modify *Modify)) func(c *client.Forum, f *Factory) {
return func(c *client.Forum, f *Factory) {
pass := 0
for true {
pass++
if !Checkpoint(c, fmt.Sprintf("Pass %d", pass)) {
break
}
modify := Modify(pass)
// Run check
checker(c, f, &modify)
// Done?
if modify != 0 {
break
}
}
}
}
func GetSlugOrId(slug string, id int64) string {
if (len(slug) != 0) && (rand.Intn(4) == 1) {
return GetRandomCase(slug)
}
return fmt.Sprintf("%d", id)
}
func GetRandomCase(s string) string {
r := rand.Intn(6)
switch r {
case 0:
return strings.ToLower(s)
case 1:
return strings.ToUpper(s)
case 2:
return InvertCase(s)
default:
return s
}
}