-
Notifications
You must be signed in to change notification settings - Fork 0
/
isbot_test.go
46 lines (39 loc) · 875 Bytes
/
isbot_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
package isbot
import (
"bufio"
"fmt"
"os"
"strings"
"testing"
)
// Amount of bots that the regex should detect from the list
// If detection falls below this, the test fails
const threshold = 0.90
func TestCheckRegex(t *testing.T) {
f, err := os.Open("user-agents-bots.txt")
if err != nil {
t.Fatal(err)
}
defer f.Close()
var trueCount, falseCount float64
var falses []string
sc := bufio.NewScanner(f)
for sc.Scan() {
ua := sc.Text()
isBot := CheckRegex(ua)
if isBot {
trueCount++
continue
}
falseCount++
if falseCount < 20 {
falses = append(falses, ua)
}
}
detectionRate := (trueCount) / (trueCount + falseCount)
fmt.Printf("Detection rate: %.2f, Threshold: %.2f\n", detectionRate, threshold)
if detectionRate < threshold {
t.Fatalf("True: %.0f, False: %.0f\n%s", trueCount, falseCount,
strings.Join(falses, "\n"))
}
}