-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb_test.go
135 lines (118 loc) · 3.98 KB
/
db_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
124
125
126
127
128
129
130
131
132
133
134
135
package bsearch
import (
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
// Test DB.Get() using testdata/rdns1.csv
func TestDBGet(t *testing.T) {
t.Parallel()
var tests = []struct {
key string
expect string
}{
{"001.000.128.000", "node-0.pool-1-0.dynamic.totinternet.net,202003,totinternet.net"},
{"001.034.164.000", "1-34-164-0.HINET-IP.hinet.net,202003,hinet.net"},
{"003.122.207.000", "ec2-3-122-207-0.eu-central-1.compute.amazonaws.com,202003,amazonaws.com"},
{"003.126.183.000", "ec2-3-126-183-0.eu-central-1.compute.amazonaws.com,202003,amazonaws.com"},
{"024.066.017.000", "S0106905851b9f0e0.rd.shawcable.net,202003,shawcable.net"},
{"032.176.184.000", "mobile000.mycingular.net,202003,mycingular.net"},
{"223.252.003.000", "223-252-3-0.as45671.net,202003,as45671.net"},
}
ensureIndex(t, "rdns1.csv")
db, err := NewDB("testdata/rdns1.csv")
if err != nil {
t.Fatal(err)
}
defer db.Close()
for _, tc := range tests {
val, err := db.Get([]byte(tc.key))
if err != nil {
t.Fatalf("%s: %s\n", tc.key, err.Error())
}
if string(val) != tc.expect {
t.Errorf("%q => %q\n expected %q\n", tc.key, val, tc.expect)
}
}
// Lookup a missing key
key := "foobar"
val, err := db.Get([]byte(key))
if err == nil || err != ErrNotFound {
t.Errorf("%q => %q, %q\n expected ErrNotFound\n", key, err, val)
}
}
// Test DB.GetString() using testdata/rdns1.csv
func TestDBGetString(t *testing.T) {
t.Parallel()
var tests = []struct {
key string
expect string
}{
{"001.000.128.000", "node-0.pool-1-0.dynamic.totinternet.net,202003,totinternet.net"},
{"001.034.164.000", "1-34-164-0.HINET-IP.hinet.net,202003,hinet.net"},
{"003.122.207.000", "ec2-3-122-207-0.eu-central-1.compute.amazonaws.com,202003,amazonaws.com"},
{"003.126.183.000", "ec2-3-126-183-0.eu-central-1.compute.amazonaws.com,202003,amazonaws.com"},
{"024.066.017.000", "S0106905851b9f0e0.rd.shawcable.net,202003,shawcable.net"},
{"032.176.184.000", "mobile000.mycingular.net,202003,mycingular.net"},
{"223.252.003.000", "223-252-3-0.as45671.net,202003,as45671.net"},
}
ensureIndex(t, "rdns1.csv")
db, err := NewDB("testdata/rdns1.csv")
if err != nil {
t.Fatal(err)
}
defer db.Close()
for _, tc := range tests {
val, err := db.GetString(tc.key)
if err != nil {
t.Fatalf("%s: %s\n", tc.key, err.Error())
}
if val != tc.expect {
t.Errorf("%q => %q\n expected %q\n", tc.key, val, tc.expect)
}
}
// Lookup a missing key
key := "foobar"
val, err := db.GetString(key)
if err == nil || err != ErrNotFound {
t.Errorf("%q => %q, %q\n expected ErrNotFound\n", key, err, val)
}
}
// Test DB.GetSlice() using testdata/rdns1.csv
func TestDBGetSlice(t *testing.T) {
t.Parallel()
var tests = []struct {
key string
expect []string
}{
{"001.000.128.000", []string{"node-0.pool-1-0.dynamic.totinternet.net", "202003", "totinternet.net"}},
{"001.034.164.000", []string{"1-34-164-0.HINET-IP.hinet.net", "202003", "hinet.net"}},
{"003.122.207.000", []string{"ec2-3-122-207-0.eu-central-1.compute.amazonaws.com", "202003", "amazonaws.com"}},
{"003.126.183.000", []string{"ec2-3-126-183-0.eu-central-1.compute.amazonaws.com", "202003", "amazonaws.com"}},
{"024.066.017.000", []string{"S0106905851b9f0e0.rd.shawcable.net", "202003", "shawcable.net"}},
{"032.176.184.000", []string{"mobile000.mycingular.net", "202003", "mycingular.net"}},
{"223.252.003.000", []string{"223-252-3-0.as45671.net", "202003", "as45671.net"}},
}
ensureIndex(t, "rdns1.csv")
db, err := NewDB("testdata/rdns1.csv")
if err != nil {
t.Fatal(err)
}
defer db.Close()
for _, tc := range tests {
s, err := db.GetSlice(tc.key)
if err != nil {
t.Fatalf("%s: %s\n", tc.key, err.Error())
}
if diff := cmp.Diff(tc.expect, s); diff != "" {
t.Errorf("%q mismatch (-want +got):\n%s", tc.key, diff)
}
}
// Lookup a missing key
key := "foobar"
s, err := db.GetSlice(key)
if err == nil || err != ErrNotFound {
t.Errorf("%q => %q, %q\n expected ErrNotFound\n",
key, err, strings.Join(s, ","))
}
}