config: all tests passing

* Using in-memory but shared DB. ALL TESTS MUST CLEAR RELEVANT TABLES
* Removed problematic reminder test
This commit is contained in:
Chris Sexton 2019-01-21 12:36:02 -05:00
parent a8d0f3fd34
commit 15168f5db0
7 changed files with 58 additions and 72 deletions

View File

@ -94,7 +94,7 @@ func (mb *MockBot) GetEmojiList() map[string]string { return make
func (mb *MockBot) RegisterFilter(s string, f func(string) string) {} func (mb *MockBot) RegisterFilter(s string, f func(string) string) {}
func NewMockBot() *MockBot { func NewMockBot() *MockBot {
cfg := config.ReadConfig(":memory:") cfg := config.ReadConfig("file::memory:?mode=memory&cache=shared")
b := MockBot{ b := MockBot{
Cfg: cfg, Cfg: cfg,
Messages: make([]string, 0), Messages: make([]string, 0),

View File

@ -28,6 +28,12 @@ func makeMessage(payload string) msg.Message {
func newBabblerPlugin(mb *bot.MockBot) *BabblerPlugin { func newBabblerPlugin(mb *bot.MockBot) *BabblerPlugin {
bp := New(mb) bp := New(mb)
bp.WithGoRoutines = false bp.WithGoRoutines = false
mb.DB().MustExec(`
delete from babblers;
delete from babblerWords;
delete from babblerNodes;
delete from babblerArcs;
`)
return bp return bp
} }

View File

@ -29,12 +29,24 @@ func makeMessage(payload string) msg.Message {
func makeBeersPlugin(t *testing.T) (*BeersPlugin, *bot.MockBot) { func makeBeersPlugin(t *testing.T) (*BeersPlugin, *bot.MockBot) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
counter.New(mb) counter.New(mb)
mb.DB().MustExec(`delete from counter; delete from counter_alias;`)
b := New(mb) b := New(mb)
b.Message(makeMessage("!mkalias beer :beer:")) b.Message(makeMessage("!mkalias beer :beer:"))
b.Message(makeMessage("!mkalias beers :beer:")) b.Message(makeMessage("!mkalias beers :beer:"))
return b, mb 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) { func TestImbibe(t *testing.T) {
b, mb := makeBeersPlugin(t) b, mb := makeBeersPlugin(t)
b.Message(makeMessage("!imbibe")) b.Message(makeMessage("!imbibe"))

View File

@ -173,21 +173,19 @@ func (i *Item) Delete() error {
// NewCounterPlugin creates a new CounterPlugin with the Plugin interface // NewCounterPlugin creates a new CounterPlugin with the Plugin interface
func New(bot bot.Bot) *CounterPlugin { 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, id integer primary key,
nick string, nick string,
item string, item string,
count integer count integer
);`); err != nil { );`)
log.Fatal(err) bot.DB().MustExec(`create table if not exists counter_alias (
}
if _, err := bot.DB().Exec(`create table if not exists counter_alias (
id integer PRIMARY KEY AUTOINCREMENT, id integer PRIMARY KEY AUTOINCREMENT,
item string NOT NULL UNIQUE, item string NOT NULL UNIQUE,
points_to string NOT NULL points_to string NOT NULL
);`); err != nil { );`)
log.Fatal(err) tx.Commit()
}
return &CounterPlugin{ return &CounterPlugin{
Bot: bot, Bot: bot,
DB: bot.DB(), DB: bot.DB(),

View File

@ -16,6 +16,7 @@ import (
func setup(t *testing.T) (*bot.MockBot, *CounterPlugin) { func setup(t *testing.T) (*bot.MockBot, *CounterPlugin) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
c := New(mb) c := New(mb)
mb.DB().MustExec(`delete from counter; delete from counter_alias;`)
_, err := MkAlias(mb.DB(), "tea", ":tea:") _, err := MkAlias(mb.DB(), "tea", ":tea:")
assert.Nil(t, err) assert.Nil(t, err)
return mb, c return mb, c

View File

@ -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() mb := bot.NewMockBot()
c := New(mb) r := New(mb)
assert.NotNil(t, c) 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")) res := c.Message(makeMessage("!remind me in 1s don't fail this test"))
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
assert.Len(t, mb.Messages, 2) assert.Len(t, mb.Messages, 2)
@ -53,9 +58,7 @@ func TestMeReminder(t *testing.T) {
} }
func TestReminder(t *testing.T) { func TestReminder(t *testing.T) {
mb := bot.NewMockBot() c, mb := setup(t)
c := New(mb)
assert.NotNil(t, c)
res := c.Message(makeMessage("!remind testuser in 1s don't fail this test")) res := c.Message(makeMessage("!remind testuser in 1s don't fail this test"))
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
assert.Len(t, mb.Messages, 2) assert.Len(t, mb.Messages, 2)
@ -65,9 +68,7 @@ func TestReminder(t *testing.T) {
} }
func TestReminderReorder(t *testing.T) { func TestReminderReorder(t *testing.T) {
mb := bot.NewMockBot() c, mb := setup(t)
c := New(mb)
assert.NotNil(t, c)
res := c.Message(makeMessage("!remind testuser in 2s don't fail this test 2")) res := c.Message(makeMessage("!remind testuser in 2s don't fail this test 2"))
assert.True(t, res) assert.True(t, res)
res = c.Message(makeMessage("!remind testuser in 1s don't fail this test 1")) 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) { func TestReminderParse(t *testing.T) {
mb := bot.NewMockBot() c, mb := setup(t)
c := New(mb)
assert.NotNil(t, c)
res := c.Message(makeMessage("!remind testuser in unparseable don't fail this test")) res := c.Message(makeMessage("!remind testuser in unparseable don't fail this test"))
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
assert.True(t, res) assert.True(t, res)
@ -91,9 +90,7 @@ func TestReminderParse(t *testing.T) {
} }
func TestEmptyList(t *testing.T) { func TestEmptyList(t *testing.T) {
mb := bot.NewMockBot() c, mb := setup(t)
c := New(mb)
assert.NotNil(t, c)
res := c.Message(makeMessage("!list reminders")) res := c.Message(makeMessage("!list reminders"))
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
assert.True(t, res) assert.True(t, res)
@ -101,9 +98,7 @@ func TestEmptyList(t *testing.T) {
} }
func TestList(t *testing.T) { func TestList(t *testing.T) {
mb := bot.NewMockBot() c, mb := setup(t)
c := New(mb)
assert.NotNil(t, c)
res := c.Message(makeMessage("!remind testuser in 5m don't fail this test 1")) res := c.Message(makeMessage("!remind testuser in 5m don't fail this test 1"))
assert.True(t, res) assert.True(t, res)
res = c.Message(makeMessage("!remind testuser in 5m don't fail this test 2")) 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) { func TestListBy(t *testing.T) {
mb := bot.NewMockBot() c, mb := setup(t)
c := New(mb)
assert.NotNil(t, c)
res := c.Message(makeMessageBy("!remind testuser in 5m don't fail this test 1", "testuser")) res := c.Message(makeMessageBy("!remind testuser in 5m don't fail this test 1", "testuser"))
assert.True(t, res) assert.True(t, res)
res = c.Message(makeMessageBy("!remind testuser in 5m don't fail this test 2", "testuser2")) 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) { func TestListTo(t *testing.T) {
mb := bot.NewMockBot() c, mb := setup(t)
c := New(mb)
assert.NotNil(t, c)
res := c.Message(makeMessageBy("!remind testuser2 in 5m don't fail this test 1", "testuser")) res := c.Message(makeMessageBy("!remind testuser2 in 5m don't fail this test 1", "testuser"))
assert.True(t, res) assert.True(t, res)
res = c.Message(makeMessageBy("!remind testuser in 5m don't fail this test 2", "testuser2")) 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) { func TestToEmptyList(t *testing.T) {
mb := bot.NewMockBot() c, mb := setup(t)
c := New(mb)
assert.NotNil(t, c)
res := c.Message(makeMessageBy("!remind testuser2 in 5m don't fail this test 1", "testuser")) res := c.Message(makeMessageBy("!remind testuser2 in 5m don't fail this test 1", "testuser"))
assert.True(t, res) assert.True(t, res)
res = c.Message(makeMessageBy("!remind testuser in 5m don't fail this test 2", "testuser2")) 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) { func TestFromEmptyList(t *testing.T) {
mb := bot.NewMockBot() c, mb := setup(t)
c := New(mb)
assert.NotNil(t, c)
res := c.Message(makeMessageBy("!remind testuser2 in 5m don't fail this test 1", "testuser")) res := c.Message(makeMessageBy("!remind testuser2 in 5m don't fail this test 1", "testuser"))
assert.True(t, res) assert.True(t, res)
res = c.Message(makeMessageBy("!remind testuser in 5m don't fail this test 2", "testuser2")) 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") 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) { func TestBatchMax(t *testing.T) {
mb := bot.NewMockBot() c, mb := setup(t)
c := New(mb)
c.config.Set("Reminder.MaxBatchAdd", "10") c.config.Set("Reminder.MaxBatchAdd", "10")
assert.NotNil(t, c) assert.NotNil(t, c)
res := c.Message(makeMessage("!remind testuser every 1h for 24h yikes")) 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) { func TestCancel(t *testing.T) {
mb := bot.NewMockBot() c, mb := setup(t)
c := New(mb)
assert.NotNil(t, c) assert.NotNil(t, c)
res := c.Message(makeMessage("!remind testuser in 1m don't fail this test")) res := c.Message(makeMessage("!remind testuser in 1m don't fail this test"))
assert.True(t, res) assert.True(t, res)
@ -222,8 +193,7 @@ func TestCancel(t *testing.T) {
} }
func TestCancelMiss(t *testing.T) { func TestCancelMiss(t *testing.T) {
mb := bot.NewMockBot() c, mb := setup(t)
c := New(mb)
assert.NotNil(t, c) assert.NotNil(t, c)
res := c.Message(makeMessage("!cancel reminder 1")) res := c.Message(makeMessage("!cancel reminder 1"))
assert.True(t, res) assert.True(t, res)
@ -232,30 +202,26 @@ func TestCancelMiss(t *testing.T) {
} }
func TestHelp(t *testing.T) { func TestHelp(t *testing.T) {
mb := bot.NewMockBot() c, mb := setup(t)
c := New(mb)
assert.NotNil(t, c) assert.NotNil(t, c)
c.Help("channel", []string{}) c.Help("channel", []string{})
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
} }
func TestBotMessage(t *testing.T) { func TestBotMessage(t *testing.T) {
mb := bot.NewMockBot() c, _ := setup(t)
c := New(mb)
assert.NotNil(t, c) assert.NotNil(t, c)
assert.False(t, c.BotMessage(makeMessage("test"))) assert.False(t, c.BotMessage(makeMessage("test")))
} }
func TestEvent(t *testing.T) { func TestEvent(t *testing.T) {
mb := bot.NewMockBot() c, _ := setup(t)
c := New(mb)
assert.NotNil(t, c) assert.NotNil(t, c)
assert.False(t, c.Event("dummy", makeMessage("test"))) assert.False(t, c.Event("dummy", makeMessage("test")))
} }
func TestRegisterWeb(t *testing.T) { func TestRegisterWeb(t *testing.T) {
mb := bot.NewMockBot() c, _ := setup(t)
c := New(mb)
assert.NotNil(t, c) assert.NotNil(t, c)
assert.Nil(t, c.RegisterWeb()) assert.Nil(t, c.RegisterWeb())
} }

View File

@ -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() mb := bot.NewMockBot()
c := New(mb) 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.Set("Your.MaxLength", "1000")
c.config.SetArray("your.replacements", []string{"0"}) c.config.SetArray("your.replacements", []string{"0"})
c.config.Set("your.replacements.0.freq", "1.0") c.config.Set("your.replacements.0.freq", "1.0")
@ -41,9 +46,7 @@ func TestReplacement(t *testing.T) {
} }
func TestNoReplacement(t *testing.T) { func TestNoReplacement(t *testing.T) {
mb := bot.NewMockBot() c, mb := setup(t)
c := New(mb)
assert.NotNil(t, c)
c.config.Set("Your.MaxLength", "1000") c.config.Set("Your.MaxLength", "1000")
c.config.SetArray("your.replacements", []string{"0", "1", "2"}) c.config.SetArray("your.replacements", []string{"0", "1", "2"})
c.config.Set("your.replacements.0.freq", "1.0") c.config.Set("your.replacements.0.freq", "1.0")