-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot_test.go
61 lines (50 loc) · 1.09 KB
/
bot_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
package devicedetector
import (
"os"
"testing"
"gopkg.in/yaml.v3"
)
type TestBot struct {
Name string `yaml:"name"`
Category string `yaml:"category"`
Url string `yaml:"url"`
}
type BotFixture struct {
UserAgent string `yaml:"user_agent"`
Bot TestBot `yaml:"bot"`
}
var botFixtures []BotFixture
func TestBotWithFixtures(t *testing.T) {
cache, err := NewEmbeddedCache()
if err != nil {
t.Error("error creating cache:", err)
t.Fail()
return
}
if cache == nil {
t.Error("cache created but is nil?")
t.Fail()
return
}
data, err := os.ReadFile("fixtures/bots.yml")
if err != nil {
t.Error("error loading bot fixtures:", err)
t.Fail()
return
}
err = yaml.Unmarshal(data, &botFixtures)
if err != nil {
t.Error("error parsing fixtures:", err)
t.Fail()
return
}
for _, fixture := range botFixtures {
bot := NewBot(cache, fixture.UserAgent)
if !bot.IsBot() {
t.Errorf("expected bot '%s' to be a bot", bot.Name())
}
if bot.Name() != fixture.Bot.Name {
t.Errorf("expected bot name to be '%s' but was '%s'", fixture.Bot.Name, bot.Name())
}
}
}