-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipstore_test.go
123 lines (114 loc) · 2.19 KB
/
ipstore_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package ipstore
import (
"reflect"
"sort"
"testing"
"github.com/jaswdr/faker"
)
func TestRequestHandled(t *testing.T) {
type args struct {
ipAddress string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "test incorrect ip address input",
args: args{
ipAddress: "evisit.com",
},
wantErr: true,
},
{
name: "test correct ipv4 address input",
args: args{
ipAddress: "102.89.32.126",
},
wantErr: false,
},
{
name: "test correct ipv6 address input",
args: args{
ipAddress: "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := RequestHandled(tt.args.ipAddress); (err != nil) != tt.wantErr {
t.Errorf("RequestHandled() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestTop100(t *testing.T) {
isList1 := generateIPList(100)
isListLen50 := generateIPList(50)
isListLen70 := generateIPList(70)
tests := []struct {
name string
seed []string
want []string
}{
{
name: "output should be of length 100",
seed: isList1,
want: isList1,
},
{
name: "output should be of length 100",
seed: isListLen50,
want: isListLen50,
},
{
name: "output should be of length 100",
seed: isListLen70,
want: isListLen70,
},
}
for _, tt := range tests {
Clear()
insertIntoIPstore(tt.seed)
sort.Strings(tt.seed)
t.Run(tt.name, func(t *testing.T) {
got := Top100()
sort.Strings(got)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Top100() = %v, want %v", got, tt.want)
}
})
}
}
func generateIPList(limit int) []string {
newFaker := faker.New()
var outputIP []string
for i := 0; i < limit; i++ {
outputIP = append(outputIP, newFaker.Internet().Ipv4())
}
return outputIP
}
func insertIntoIPstore(ips []string) {
for _, ipAddress := range ips {
_ = RequestHandled(ipAddress)
}
}
func TestClear(t *testing.T) {
tests := []struct {
name string
}{
{
name: "should return empty list",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Clear()
if got := Top100(); len(got) != 0 {
t.Errorf("Top100() = %v, want []", got)
}
})
}
}