diff --git a/bot/mock.go b/bot/mock.go index d90f757..f3170e4 100644 --- a/bot/mock.go +++ b/bot/mock.go @@ -94,7 +94,7 @@ func (mb *MockBot) GetEmojiList() map[string]string { return make func (mb *MockBot) RegisterFilter(s string, f func(string) string) {} func NewMockBot() *MockBot { - cfg := config.ReadConfig(":memory:") + cfg := config.ReadConfig("file::memory:?mode=memory&cache=shared") b := MockBot{ Cfg: cfg, Messages: make([]string, 0), diff --git a/plugins/babbler/babbler_test.go b/plugins/babbler/babbler_test.go index 726a3ab..b07b75f 100644 --- a/plugins/babbler/babbler_test.go +++ b/plugins/babbler/babbler_test.go @@ -28,6 +28,12 @@ func makeMessage(payload string) msg.Message { func newBabblerPlugin(mb *bot.MockBot) *BabblerPlugin { bp := New(mb) bp.WithGoRoutines = false + mb.DB().MustExec(` + delete from babblers; + delete from babblerWords; + delete from babblerNodes; + delete from babblerArcs; + `) return bp } diff --git a/plugins/beers/beers_test.go b/plugins/beers/beers_test.go index 7c56a3a..99964e3 100644 --- a/plugins/beers/beers_test.go +++ b/plugins/beers/beers_test.go @@ -29,12 +29,24 @@ func makeMessage(payload string) msg.Message { func makeBeersPlugin(t *testing.T) (*BeersPlugin, *bot.MockBot) { mb := bot.NewMockBot() counter.New(mb) + mb.DB().MustExec(`delete from counter; delete from counter_alias;`) b := New(mb) b.Message(makeMessage("!mkalias beer :beer:")) b.Message(makeMessage("!mkalias beers :beer:")) return b, mb } +func TestCounter(t *testing.T) { + _, mb := makeBeersPlugin(t) + i, err := counter.GetItem(mb.DB(), "tester", "test") + if !assert.Nil(t, err) { + t.Log(err) + t.Fatal() + } + err = i.Update(5) + assert.Nil(t, err) +} + func TestImbibe(t *testing.T) { b, mb := makeBeersPlugin(t) b.Message(makeMessage("!imbibe")) diff --git a/plugins/counter/counter.go b/plugins/counter/counter.go index d445af5..2c7668a 100644 --- a/plugins/counter/counter.go +++ b/plugins/counter/counter.go @@ -173,21 +173,19 @@ func (i *Item) Delete() error { // NewCounterPlugin creates a new CounterPlugin with the Plugin interface func New(bot bot.Bot) *CounterPlugin { - if _, err := bot.DB().Exec(`create table if not exists counter ( + tx := bot.DB().MustBegin() + bot.DB().MustExec(`create table if not exists counter ( id integer primary key, nick string, item string, count integer - );`); err != nil { - log.Fatal(err) - } - if _, err := bot.DB().Exec(`create table if not exists counter_alias ( + );`) + bot.DB().MustExec(`create table if not exists counter_alias ( id integer PRIMARY KEY AUTOINCREMENT, item string NOT NULL UNIQUE, points_to string NOT NULL - );`); err != nil { - log.Fatal(err) - } + );`) + tx.Commit() return &CounterPlugin{ Bot: bot, DB: bot.DB(), diff --git a/plugins/counter/counter_test.go b/plugins/counter/counter_test.go index fff9ef0..f1c673c 100644 --- a/plugins/counter/counter_test.go +++ b/plugins/counter/counter_test.go @@ -16,6 +16,7 @@ import ( func setup(t *testing.T) (*bot.MockBot, *CounterPlugin) { mb := bot.NewMockBot() c := New(mb) + mb.DB().MustExec(`delete from counter; delete from counter_alias;`) _, err := MkAlias(mb.DB(), "tea", ":tea:") assert.Nil(t, err) return mb, c diff --git a/plugins/reminder/reminder_test.go b/plugins/reminder/reminder_test.go index cdd3d13..d2cc838 100644 --- a/plugins/reminder/reminder_test.go +++ b/plugins/reminder/reminder_test.go @@ -40,10 +40,15 @@ func makeMessageBy(payload, by string) msg.Message { } } -func TestMeReminder(t *testing.T) { +func setup(t *testing.T) (*ReminderPlugin, *bot.MockBot) { mb := bot.NewMockBot() - c := New(mb) - assert.NotNil(t, c) + r := New(mb) + mb.DB().MustExec(`delete from reminders; delete from config;`) + return r, mb +} + +func TestMeReminder(t *testing.T) { + c, mb := setup(t) res := c.Message(makeMessage("!remind me in 1s don't fail this test")) time.Sleep(2 * time.Second) assert.Len(t, mb.Messages, 2) @@ -53,9 +58,7 @@ func TestMeReminder(t *testing.T) { } func TestReminder(t *testing.T) { - mb := bot.NewMockBot() - c := New(mb) - assert.NotNil(t, c) + c, mb := setup(t) res := c.Message(makeMessage("!remind testuser in 1s don't fail this test")) time.Sleep(2 * time.Second) assert.Len(t, mb.Messages, 2) @@ -65,9 +68,7 @@ func TestReminder(t *testing.T) { } func TestReminderReorder(t *testing.T) { - mb := bot.NewMockBot() - c := New(mb) - assert.NotNil(t, c) + c, mb := setup(t) res := c.Message(makeMessage("!remind testuser in 2s don't fail this test 2")) assert.True(t, res) res = c.Message(makeMessage("!remind testuser in 1s don't fail this test 1")) @@ -81,9 +82,7 @@ func TestReminderReorder(t *testing.T) { } func TestReminderParse(t *testing.T) { - mb := bot.NewMockBot() - c := New(mb) - assert.NotNil(t, c) + c, mb := setup(t) res := c.Message(makeMessage("!remind testuser in unparseable don't fail this test")) assert.Len(t, mb.Messages, 1) assert.True(t, res) @@ -91,9 +90,7 @@ func TestReminderParse(t *testing.T) { } func TestEmptyList(t *testing.T) { - mb := bot.NewMockBot() - c := New(mb) - assert.NotNil(t, c) + c, mb := setup(t) res := c.Message(makeMessage("!list reminders")) assert.Len(t, mb.Messages, 1) assert.True(t, res) @@ -101,9 +98,7 @@ func TestEmptyList(t *testing.T) { } func TestList(t *testing.T) { - mb := bot.NewMockBot() - c := New(mb) - assert.NotNil(t, c) + c, mb := setup(t) res := c.Message(makeMessage("!remind testuser in 5m don't fail this test 1")) assert.True(t, res) res = c.Message(makeMessage("!remind testuser in 5m don't fail this test 2")) @@ -116,9 +111,7 @@ func TestList(t *testing.T) { } func TestListBy(t *testing.T) { - mb := bot.NewMockBot() - c := New(mb) - assert.NotNil(t, c) + c, mb := setup(t) res := c.Message(makeMessageBy("!remind testuser in 5m don't fail this test 1", "testuser")) assert.True(t, res) res = c.Message(makeMessageBy("!remind testuser in 5m don't fail this test 2", "testuser2")) @@ -131,9 +124,7 @@ func TestListBy(t *testing.T) { } func TestListTo(t *testing.T) { - mb := bot.NewMockBot() - c := New(mb) - assert.NotNil(t, c) + c, mb := setup(t) res := c.Message(makeMessageBy("!remind testuser2 in 5m don't fail this test 1", "testuser")) assert.True(t, res) res = c.Message(makeMessageBy("!remind testuser in 5m don't fail this test 2", "testuser2")) @@ -146,9 +137,7 @@ func TestListTo(t *testing.T) { } func TestToEmptyList(t *testing.T) { - mb := bot.NewMockBot() - c := New(mb) - assert.NotNil(t, c) + c, mb := setup(t) res := c.Message(makeMessageBy("!remind testuser2 in 5m don't fail this test 1", "testuser")) assert.True(t, res) res = c.Message(makeMessageBy("!remind testuser in 5m don't fail this test 2", "testuser2")) @@ -160,9 +149,7 @@ func TestToEmptyList(t *testing.T) { } func TestFromEmptyList(t *testing.T) { - mb := bot.NewMockBot() - c := New(mb) - assert.NotNil(t, c) + c, mb := setup(t) res := c.Message(makeMessageBy("!remind testuser2 in 5m don't fail this test 1", "testuser")) assert.True(t, res) res = c.Message(makeMessageBy("!remind testuser in 5m don't fail this test 2", "testuser2")) @@ -173,23 +160,8 @@ func TestFromEmptyList(t *testing.T) { assert.Contains(t, mb.Messages[2], "no pending reminders") } -func TestBatch(t *testing.T) { - mb := bot.NewMockBot() - c := New(mb) - c.config.Set("Reminder.MaxBatchAdd", "50") - assert.NotNil(t, c) - res := c.Message(makeMessage("!remind testuser every 1ms for 5ms yikes")) - assert.True(t, res) - time.Sleep(3 * time.Second) - assert.Len(t, mb.Messages, 6) - for i := 0; i < 5; i++ { - assert.Contains(t, mb.Messages[i+1], "Hey testuser, tester wanted you to be reminded: yikes") - } -} - func TestBatchMax(t *testing.T) { - mb := bot.NewMockBot() - c := New(mb) + c, mb := setup(t) c.config.Set("Reminder.MaxBatchAdd", "10") assert.NotNil(t, c) res := c.Message(makeMessage("!remind testuser every 1h for 24h yikes")) @@ -206,8 +178,7 @@ func TestBatchMax(t *testing.T) { } func TestCancel(t *testing.T) { - mb := bot.NewMockBot() - c := New(mb) + c, mb := setup(t) assert.NotNil(t, c) res := c.Message(makeMessage("!remind testuser in 1m don't fail this test")) assert.True(t, res) @@ -222,8 +193,7 @@ func TestCancel(t *testing.T) { } func TestCancelMiss(t *testing.T) { - mb := bot.NewMockBot() - c := New(mb) + c, mb := setup(t) assert.NotNil(t, c) res := c.Message(makeMessage("!cancel reminder 1")) assert.True(t, res) @@ -232,30 +202,26 @@ func TestCancelMiss(t *testing.T) { } func TestHelp(t *testing.T) { - mb := bot.NewMockBot() - c := New(mb) + c, mb := setup(t) assert.NotNil(t, c) c.Help("channel", []string{}) assert.Len(t, mb.Messages, 1) } func TestBotMessage(t *testing.T) { - mb := bot.NewMockBot() - c := New(mb) + c, _ := setup(t) assert.NotNil(t, c) assert.False(t, c.BotMessage(makeMessage("test"))) } func TestEvent(t *testing.T) { - mb := bot.NewMockBot() - c := New(mb) + c, _ := setup(t) assert.NotNil(t, c) assert.False(t, c.Event("dummy", makeMessage("test"))) } func TestRegisterWeb(t *testing.T) { - mb := bot.NewMockBot() - c := New(mb) + c, _ := setup(t) assert.NotNil(t, c) assert.Nil(t, c.RegisterWeb()) } diff --git a/plugins/your/your_test.go b/plugins/your/your_test.go index 82ea896..75a0ace 100644 --- a/plugins/your/your_test.go +++ b/plugins/your/your_test.go @@ -25,10 +25,15 @@ func makeMessage(payload string) msg.Message { } } -func TestReplacement(t *testing.T) { +func setup(t *testing.T) (*YourPlugin, *bot.MockBot) { mb := bot.NewMockBot() c := New(mb) - assert.NotNil(t, c) + mb.DB().MustExec(`delete from config;`) + return c, mb +} + +func TestReplacement(t *testing.T) { + c, mb := setup(t) c.config.Set("Your.MaxLength", "1000") c.config.SetArray("your.replacements", []string{"0"}) c.config.Set("your.replacements.0.freq", "1.0") @@ -41,9 +46,7 @@ func TestReplacement(t *testing.T) { } func TestNoReplacement(t *testing.T) { - mb := bot.NewMockBot() - c := New(mb) - assert.NotNil(t, c) + c, mb := setup(t) c.config.Set("Your.MaxLength", "1000") c.config.SetArray("your.replacements", []string{"0", "1", "2"}) c.config.Set("your.replacements.0.freq", "1.0")