From 91a334b32bd656afd5dee0e14119beca86ac677e Mon Sep 17 00:00:00 2001 From: skkiesel Date: Sat, 19 Jan 2019 09:43:33 -0500 Subject: [PATCH] Fixed babbler tests by not running go routines during testing. (go fmt on nerdepedia because of course this should all happen in one commit) --- plugins/babbler/babbler.go | 14 +- plugins/babbler/babbler_test.go | 238 ++++++++++++++++--------------- plugins/babbler/commands.go | 6 +- plugins/nerdepedia/nerdepedia.go | 6 +- 4 files changed, 138 insertions(+), 126 deletions(-) diff --git a/plugins/babbler/babbler.go b/plugins/babbler/babbler.go index 0ae0b58..36f1521 100644 --- a/plugins/babbler/babbler.go +++ b/plugins/babbler/babbler.go @@ -23,9 +23,10 @@ var ( ) type BabblerPlugin struct { - Bot bot.Bot - db *sqlx.DB - config *config.Config + Bot bot.Bot + db *sqlx.DB + config *config.Config + WithGoRoutines bool } type Babbler struct { @@ -90,9 +91,10 @@ func New(bot bot.Bot) *BabblerPlugin { } plugin := &BabblerPlugin{ - Bot: bot, - db: bot.DB(), - config: bot.Config(), + Bot: bot, + db: bot.DB(), + config: bot.Config(), + WithGoRoutines: true, } plugin.createNewWord("") diff --git a/plugins/babbler/babbler_test.go b/plugins/babbler/babbler_test.go index 5031a19..d598359 100644 --- a/plugins/babbler/babbler_test.go +++ b/plugins/babbler/babbler_test.go @@ -25,12 +25,18 @@ func makeMessage(payload string) msg.Message { } } +func newBabblerPlugin(mb *bot.MockBot) *BabblerPlugin { + bp := New(mb) + bp.WithGoRoutines = false + return bp +} + func TestBabblerNoBabbler(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - c.config.Babbler.DefaultUsers = []string{"seabass"} - assert.NotNil(t, c) - c.Message(makeMessage("!seabass2 says")) + bp := newBabblerPlugin(mb) + bp.config.Babbler.DefaultUsers = []string{"seabass"} + assert.NotNil(t, bp) + bp.Message(makeMessage("!seabass2 says")) res := assert.Len(t, mb.Messages, 0) assert.True(t, res) // assert.Contains(t, mb.Messages[0], "seabass2 babbler not found") @@ -38,12 +44,12 @@ func TestBabblerNoBabbler(t *testing.T) { func TestBabblerNothingSaid(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - c.config.Babbler.DefaultUsers = []string{"seabass"} - assert.NotNil(t, c) - res := c.Message(makeMessage("initialize babbler for seabass")) + bp := newBabblerPlugin(mb) + bp.config.Babbler.DefaultUsers = []string{"seabass"} + assert.NotNil(t, bp) + res := bp.Message(makeMessage("initialize babbler for seabass")) assert.True(t, res) - res = c.Message(makeMessage("!seabass says")) + res = bp.Message(makeMessage("!seabass says")) assert.True(t, res) assert.Len(t, mb.Messages, 2) assert.Contains(t, mb.Messages[0], "okay.") @@ -52,17 +58,17 @@ func TestBabblerNothingSaid(t *testing.T) { func TestBabbler(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - c.config.Babbler.DefaultUsers = []string{"seabass"} - assert.NotNil(t, c) + bp := newBabblerPlugin(mb) + bp.config.Babbler.DefaultUsers = []string{"seabass"} + assert.NotNil(t, bp) seabass := makeMessage("This is a message") seabass.User = &user.User{Name: "seabass"} - res := c.Message(seabass) + res := bp.Message(seabass) seabass.Body = "This is another message" - res = c.Message(seabass) + res = bp.Message(seabass) seabass.Body = "This is a long message" - res = c.Message(seabass) - res = c.Message(makeMessage("!seabass says")) + res = bp.Message(seabass) + res = bp.Message(makeMessage("!seabass says")) assert.Len(t, mb.Messages, 1) assert.True(t, res) assert.Contains(t, mb.Messages[0], "this is") @@ -71,17 +77,17 @@ func TestBabbler(t *testing.T) { func TestBabblerSeed(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - c.config.Babbler.DefaultUsers = []string{"seabass"} - assert.NotNil(t, c) + bp := newBabblerPlugin(mb) + bp.config.Babbler.DefaultUsers = []string{"seabass"} + assert.NotNil(t, bp) seabass := makeMessage("This is a message") seabass.User = &user.User{Name: "seabass"} - res := c.Message(seabass) + res := bp.Message(seabass) seabass.Body = "This is another message" - res = c.Message(seabass) + res = bp.Message(seabass) seabass.Body = "This is a long message" - res = c.Message(seabass) - res = c.Message(makeMessage("!seabass says long")) + res = bp.Message(seabass) + res = bp.Message(makeMessage("!seabass says long")) assert.Len(t, mb.Messages, 1) assert.True(t, res) assert.Contains(t, mb.Messages[0], "long message") @@ -89,17 +95,17 @@ func TestBabblerSeed(t *testing.T) { func TestBabblerMultiSeed(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - c.config.Babbler.DefaultUsers = []string{"seabass"} - assert.NotNil(t, c) + bp := newBabblerPlugin(mb) + bp.config.Babbler.DefaultUsers = []string{"seabass"} + assert.NotNil(t, bp) seabass := makeMessage("This is a message") seabass.User = &user.User{Name: "seabass"} - res := c.Message(seabass) + res := bp.Message(seabass) seabass.Body = "This is another message" - res = c.Message(seabass) + res = bp.Message(seabass) seabass.Body = "This is a long message" - res = c.Message(seabass) - res = c.Message(makeMessage("!seabass says This is a long")) + res = bp.Message(seabass) + res = bp.Message(makeMessage("!seabass says This is a long")) assert.Len(t, mb.Messages, 1) assert.True(t, res) assert.Contains(t, mb.Messages[0], "this is a long message") @@ -107,17 +113,17 @@ func TestBabblerMultiSeed(t *testing.T) { func TestBabblerMultiSeed2(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - c.config.Babbler.DefaultUsers = []string{"seabass"} - assert.NotNil(t, c) + bp := newBabblerPlugin(mb) + bp.config.Babbler.DefaultUsers = []string{"seabass"} + assert.NotNil(t, bp) seabass := makeMessage("This is a message") seabass.User = &user.User{Name: "seabass"} - res := c.Message(seabass) + res := bp.Message(seabass) seabass.Body = "This is another message" - res = c.Message(seabass) + res = bp.Message(seabass) seabass.Body = "This is a long message" - res = c.Message(seabass) - res = c.Message(makeMessage("!seabass says is a long")) + res = bp.Message(seabass) + res = bp.Message(makeMessage("!seabass says is a long")) assert.Len(t, mb.Messages, 1) assert.True(t, res) assert.Contains(t, mb.Messages[0], "is a long message") @@ -125,52 +131,52 @@ func TestBabblerMultiSeed2(t *testing.T) { func TestBabblerBadSeed(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - c.config.Babbler.DefaultUsers = []string{"seabass"} - assert.NotNil(t, c) + bp := newBabblerPlugin(mb) + bp.config.Babbler.DefaultUsers = []string{"seabass"} + assert.NotNil(t, bp) seabass := makeMessage("This is a message") seabass.User = &user.User{Name: "seabass"} - c.Message(seabass) + bp.Message(seabass) seabass.Body = "This is another message" - c.Message(seabass) + bp.Message(seabass) seabass.Body = "This is a long message" - c.Message(seabass) - c.Message(makeMessage("!seabass says noooo this is bad")) + bp.Message(seabass) + bp.Message(makeMessage("!seabass says noooo this is bad")) assert.Len(t, mb.Messages, 1) assert.Contains(t, mb.Messages[0], "seabass never said 'noooo this is bad'") } func TestBabblerBadSeed2(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - c.config.Babbler.DefaultUsers = []string{"seabass"} - assert.NotNil(t, c) + bp := newBabblerPlugin(mb) + bp.config.Babbler.DefaultUsers = []string{"seabass"} + assert.NotNil(t, bp) seabass := makeMessage("This is a message") seabass.User = &user.User{Name: "seabass"} - c.Message(seabass) + bp.Message(seabass) seabass.Body = "This is another message" - c.Message(seabass) + bp.Message(seabass) seabass.Body = "This is a long message" - c.Message(seabass) - c.Message(makeMessage("!seabass says This is a really")) + bp.Message(seabass) + bp.Message(makeMessage("!seabass says This is a really")) assert.Len(t, mb.Messages, 1) assert.Contains(t, mb.Messages[0], "seabass never said 'this is a really'") } func TestBabblerSuffixSeed(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - c.config.Babbler.DefaultUsers = []string{"seabass"} - assert.NotNil(t, c) + bp := newBabblerPlugin(mb) + bp.config.Babbler.DefaultUsers = []string{"seabass"} + assert.NotNil(t, bp) seabass := makeMessage("This is message one") seabass.User = &user.User{Name: "seabass"} - res := c.Message(seabass) + res := bp.Message(seabass) seabass.Body = "It's easier to test with unique messages" - res = c.Message(seabass) + res = bp.Message(seabass) seabass.Body = "hi there" - res = c.Message(seabass) - res = c.Message(makeMessage("!seabass says-tail message one")) - res = c.Message(makeMessage("!seabass says-tail with unique")) + res = bp.Message(seabass) + res = bp.Message(makeMessage("!seabass says-tail message one")) + res = bp.Message(makeMessage("!seabass says-tail with unique")) assert.Len(t, mb.Messages, 2) assert.True(t, res) assert.Contains(t, mb.Messages[0], "this is message one") @@ -179,17 +185,17 @@ func TestBabblerSuffixSeed(t *testing.T) { func TestBabblerBadSuffixSeed(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - c.config.Babbler.DefaultUsers = []string{"seabass"} - assert.NotNil(t, c) + bp := newBabblerPlugin(mb) + bp.config.Babbler.DefaultUsers = []string{"seabass"} + assert.NotNil(t, bp) seabass := makeMessage("This is message one") seabass.User = &user.User{Name: "seabass"} - res := c.Message(seabass) + res := bp.Message(seabass) seabass.Body = "It's easier to test with unique messages" - res = c.Message(seabass) + res = bp.Message(seabass) seabass.Body = "hi there" - res = c.Message(seabass) - res = c.Message(makeMessage("!seabass says-tail anything true")) + res = bp.Message(seabass) + res = bp.Message(makeMessage("!seabass says-tail anything true")) assert.Len(t, mb.Messages, 1) assert.True(t, res) assert.Contains(t, mb.Messages[0], "seabass never said 'anything true'") @@ -197,13 +203,13 @@ func TestBabblerBadSuffixSeed(t *testing.T) { func TestBabblerBookendSeed(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - c.config.Babbler.DefaultUsers = []string{"seabass"} - assert.NotNil(t, c) + bp := newBabblerPlugin(mb) + bp.config.Babbler.DefaultUsers = []string{"seabass"} + assert.NotNil(t, bp) seabass := makeMessage("It's easier to test with unique messages") seabass.User = &user.User{Name: "seabass"} - res := c.Message(seabass) - res = c.Message(makeMessage("!seabass says-bridge It's easier | unique messages")) + res := bp.Message(seabass) + res = bp.Message(makeMessage("!seabass says-bridge It's easier | unique messages")) assert.Len(t, mb.Messages, 1) assert.True(t, res) assert.Contains(t, mb.Messages[0], "it's easier to test with unique messages") @@ -211,13 +217,13 @@ func TestBabblerBookendSeed(t *testing.T) { func TestBabblerBookendSeedShort(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - c.config.Babbler.DefaultUsers = []string{"seabass"} - assert.NotNil(t, c) + bp := newBabblerPlugin(mb) + bp.config.Babbler.DefaultUsers = []string{"seabass"} + assert.NotNil(t, bp) seabass := makeMessage("It's easier to test with unique messages") seabass.User = &user.User{Name: "seabass"} - res := c.Message(seabass) - res = c.Message(makeMessage("!seabass says-bridge It's easier to test with | unique messages")) + res := bp.Message(seabass) + res = bp.Message(makeMessage("!seabass says-bridge It's easier to test with | unique messages")) assert.Len(t, mb.Messages, 1) assert.True(t, res) assert.Contains(t, mb.Messages[0], "it's easier to test with unique messages") @@ -225,13 +231,13 @@ func TestBabblerBookendSeedShort(t *testing.T) { func TestBabblerBadBookendSeed(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - c.config.Babbler.DefaultUsers = []string{"seabass"} - assert.NotNil(t, c) + bp := newBabblerPlugin(mb) + bp.config.Babbler.DefaultUsers = []string{"seabass"} + assert.NotNil(t, bp) seabass := makeMessage("It's easier to test with unique messages") seabass.User = &user.User{Name: "seabass"} - res := c.Message(seabass) - res = c.Message(makeMessage("!seabass says-bridge It's easier | not unique messages")) + res := bp.Message(seabass) + res = bp.Message(makeMessage("!seabass says-bridge It's easier | not unique messages")) assert.Len(t, mb.Messages, 1) assert.True(t, res) assert.Contains(t, mb.Messages[0], "seabass never said 'it's easier ... not unique messages'") @@ -239,13 +245,13 @@ func TestBabblerBadBookendSeed(t *testing.T) { func TestBabblerMiddleOutSeed(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - c.config.Babbler.DefaultUsers = []string{"seabass"} - assert.NotNil(t, c) + bp := newBabblerPlugin(mb) + bp.config.Babbler.DefaultUsers = []string{"seabass"} + assert.NotNil(t, bp) seabass := makeMessage("It's easier to test with unique messages") seabass.User = &user.User{Name: "seabass"} - res := c.Message(seabass) - res = c.Message(makeMessage("!seabass says-middle-out test with")) + res := bp.Message(seabass) + res = bp.Message(makeMessage("!seabass says-middle-out test with")) assert.Len(t, mb.Messages, 1) assert.True(t, res) assert.Contains(t, mb.Messages[0], "it's easier to test with unique messages") @@ -253,13 +259,13 @@ func TestBabblerMiddleOutSeed(t *testing.T) { func TestBabblerBadMiddleOutSeed(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - c.config.Babbler.DefaultUsers = []string{"seabass"} - assert.NotNil(t, c) + bp := newBabblerPlugin(mb) + bp.config.Babbler.DefaultUsers = []string{"seabass"} + assert.NotNil(t, bp) seabass := makeMessage("It's easier to test with unique messages") seabass.User = &user.User{Name: "seabass"} - res := c.Message(seabass) - res = c.Message(makeMessage("!seabass says-middle-out anything true")) + res := bp.Message(seabass) + res = bp.Message(makeMessage("!seabass says-middle-out anything true")) assert.Len(t, mb.Messages, 1) assert.True(t, res) assert.Equal(t, mb.Messages[0], "seabass never said 'anything true'") @@ -267,13 +273,13 @@ func TestBabblerBadMiddleOutSeed(t *testing.T) { func TestBabblerBatch(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - c.config.Babbler.DefaultUsers = []string{"seabass"} - assert.NotNil(t, c) + bp := newBabblerPlugin(mb) + bp.config.Babbler.DefaultUsers = []string{"seabass"} + assert.NotNil(t, bp) seabass := makeMessage("batch learn for seabass This is a message! This is another message. This is not a long message? This is not a message! This is not another message. This is a long message?") - res := c.Message(seabass) + res := bp.Message(seabass) assert.Len(t, mb.Messages, 1) - res = c.Message(makeMessage("!seabass says")) + res = bp.Message(makeMessage("!seabass says")) assert.Len(t, mb.Messages, 2) assert.True(t, res) assert.Contains(t, mb.Messages[1], "this is") @@ -282,27 +288,27 @@ func TestBabblerBatch(t *testing.T) { func TestBabblerMerge(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - c.config.Babbler.DefaultUsers = []string{"seabass"} - assert.NotNil(t, c) + bp := newBabblerPlugin(mb) + bp.config.Babbler.DefaultUsers = []string{"seabass"} + assert.NotNil(t, bp) seabass := makeMessage(" This is a message") seabass.User = &user.User{Name: "seabass"} - res := c.Message(seabass) + res := bp.Message(seabass) assert.Len(t, mb.Messages, 0) seabass.Body = " This is another message" - res = c.Message(seabass) + res = bp.Message(seabass) seabass.Body = " This is a long message" - res = c.Message(seabass) + res = bp.Message(seabass) - res = c.Message(makeMessage("!merge babbler seabass into seabass2")) + res = bp.Message(makeMessage("!merge babbler seabass into seabass2")) assert.True(t, res) assert.Len(t, mb.Messages, 1) assert.Contains(t, mb.Messages[0], "mooooiggged") - res = c.Message(makeMessage("!seabass2 says")) + res = bp.Message(makeMessage("!seabass2 says")) assert.True(t, res) assert.Len(t, mb.Messages, 2) @@ -312,29 +318,29 @@ func TestBabblerMerge(t *testing.T) { func TestHelp(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - assert.NotNil(t, c) - c.Help("channel", []string{}) + bp := newBabblerPlugin(mb) + assert.NotNil(t, bp) + bp.Help("channel", []string{}) assert.Len(t, mb.Messages, 1) } func TestBotMessage(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - assert.NotNil(t, c) - assert.False(t, c.BotMessage(makeMessage("test"))) + bp := newBabblerPlugin(mb) + assert.NotNil(t, bp) + assert.False(t, bp.BotMessage(makeMessage("test"))) } func TestEvent(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - assert.NotNil(t, c) - assert.False(t, c.Event("dummy", makeMessage("test"))) + bp := newBabblerPlugin(mb) + assert.NotNil(t, bp) + assert.False(t, bp.Event("dummy", makeMessage("test"))) } func TestRegisterWeb(t *testing.T) { mb := bot.NewMockBot() - c := New(mb) - assert.NotNil(t, c) - assert.Nil(t, c.RegisterWeb()) + bp := newBabblerPlugin(mb) + assert.NotNil(t, bp) + assert.Nil(t, bp.RegisterWeb()) } diff --git a/plugins/babbler/commands.go b/plugins/babbler/commands.go index 38e50d9..52291bf 100644 --- a/plugins/babbler/commands.go +++ b/plugins/babbler/commands.go @@ -17,7 +17,11 @@ func (p *BabblerPlugin) initializeBabbler(tokens []string) (string, bool) { func (p *BabblerPlugin) addToBabbler(babblerName, whatWasSaid string) (string, bool) { babblerId, err := p.getOrCreateBabbler(babblerName) if err == nil { - go p.addToMarkovChain(babblerId, whatWasSaid) + if p.WithGoRoutines { + go p.addToMarkovChain(babblerId, whatWasSaid) + } else { + p.addToMarkovChain(babblerId, whatWasSaid) + } } return "", false } diff --git a/plugins/nerdepedia/nerdepedia.go b/plugins/nerdepedia/nerdepedia.go index 5c2373f..aa2665a 100644 --- a/plugins/nerdepedia/nerdepedia.go +++ b/plugins/nerdepedia/nerdepedia.go @@ -3,11 +3,11 @@ package nerdepedia import ( + "bufio" "fmt" - "strings" "html" "net/http" - "bufio" + "strings" "github.com/velour/catbase/bot" "github.com/velour/catbase/bot/msg" @@ -16,7 +16,7 @@ import ( const ( descriptionPrefix = "" )