From e1e58591ec3d951f063596e5ce85494155f795d3 Mon Sep 17 00:00:00 2001 From: cws Date: Mon, 5 Jun 2017 20:51:29 -0400 Subject: [PATCH] stats: Add tests and clean a few things up * Make statFromDB return 0 if no stats are in the DB instead of error * Convert tests to use the assert library * Add tests for actual messages --- plugins/stats/stats.go | 4 + plugins/stats/stats_test.go | 248 ++++++++++++++++++++++++++---------- 2 files changed, 188 insertions(+), 64 deletions(-) diff --git a/plugins/stats/stats.go b/plugins/stats/stats.go index 33d37ec..2123f9b 100644 --- a/plugins/stats/stats.go +++ b/plugins/stats/stats.go @@ -107,6 +107,10 @@ func statFromDB(path, bucket, key string) (stat, error) { return stat{}, err } + if v == nil { + return stat{bucket, key, 0}, nil + } + return mkStat(buk, k, v) } diff --git a/plugins/stats/stats_test.go b/plugins/stats/stats_test.go index 5d801c5..2c95bf4 100644 --- a/plugins/stats/stats_test.go +++ b/plugins/stats/stats_test.go @@ -2,18 +2,22 @@ package stats import ( "encoding/json" - "fmt" "os" "strings" "testing" + + "github.com/stretchr/testify/assert" + "github.com/velour/catbase/bot" + "github.com/velour/catbase/bot/msg" + "github.com/velour/catbase/bot/user" ) +var dbPath = "test.db" + func TestJSON(t *testing.T) { expected := 5 b, err := json.Marshal(expected) - if err != nil { - t.Error(err) - } + assert.Nil(t, err) t.Logf("%+v", expected) t.Log(string(b)) } @@ -22,25 +26,17 @@ func TestValueConversion(t *testing.T) { expected := value(5) b, err := expected.Bytes() - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) t.Log(string(b)) actual, err := valueFromBytes(b) - if err != nil { - t.Error(err) - return - } + assert.Nil(t, err) - if fmt.Sprintf("%+v", actual) != fmt.Sprintf("%+v", expected) { - t.Errorf("Did not get equivalent objects: %+v != %+v", actual, expected) - } + assert.Equal(t, actual, expected) } -func rmDB(t *testing.T, dbPath string) { +func rmDB(t *testing.T) { err := os.Remove(dbPath) if err != nil && !strings.Contains(err.Error(), "no such file or directory") { t.Fatal(err) @@ -48,8 +44,7 @@ func rmDB(t *testing.T, dbPath string) { } func TestWithDB(t *testing.T) { - dbPath := "test.db" - rmDB(t, dbPath) + rmDB(t) t.Run("TestDBReadWrite", func(t *testing.T) { bucket := "testBucket" @@ -62,36 +57,22 @@ func TestWithDB(t *testing.T) { }} err := expected.toDB(dbPath) - if err != nil { - t.Fatalf("Error writing to DB: %s", err) - } + assert.Nil(t, err) actual, err := statFromDB(dbPath, bucket, key) - if err != nil { - t.Fatalf("Error reading DB: %s", err) - } + assert.Nil(t, err) - if actual != expected[0] { - t.Fatalf("%+v != %+v", actual, expected) - } + assert.Equal(t, actual, expected[0]) }) - rmDB(t, dbPath) + rmDB(t) t.Run("TestDBAddStatInLoop", func(t *testing.T) { bucket := "testBucket" key := "testKey" expected := value(25) - os.Remove(dbPath) - defer func() { - err := os.Remove(dbPath) - if err != nil { - t.Fatal(err) - } - }() - statPack := stats{stat{ bucket, key, @@ -100,36 +81,22 @@ func TestWithDB(t *testing.T) { for i := 0; i < 5; i++ { err := statPack.toDB(dbPath) - if err != nil { - t.Fatalf("Error writing to DB: %s", err) - } + assert.Nil(t, err) } actual, err := statFromDB(dbPath, bucket, key) - if err != nil { - t.Fatalf("Error reading DB: %s", err) - } + assert.Nil(t, err) - if actual.val != expected { - t.Fatalf("%+v != %+v", actual.val, expected) - } + assert.Equal(t, actual.val, expected) }) - rmDB(t, dbPath) + rmDB(t) t.Run("TestDBAddStats", func(t *testing.T) { bucket := "testBucket" key := "testKey" expected := value(5) - os.Remove(dbPath) - defer func() { - err := os.Remove(dbPath) - if err != nil { - t.Fatal(err) - } - }() - statPack := stats{ stat{ bucket, @@ -159,19 +126,172 @@ func TestWithDB(t *testing.T) { } err := statPack.toDB(dbPath) - if err != nil { - t.Fatalf("Error writing to DB: %s", err) - } + assert.Nil(t, err) actual, err := statFromDB(dbPath, bucket, key) - if err != nil { - t.Fatalf("Error reading DB: %s", err) - } + assert.Nil(t, err) - if actual.val != expected { - t.Fatalf("%+v != %+v", actual.val, expected) - } + assert.Equal(t, actual.val, expected) }) - rmDB(t, dbPath) + rmDB(t) +} + +func makeMessage(payload string) msg.Message { + isCmd := strings.HasPrefix(payload, "!") + if isCmd { + payload = payload[1:] + } + return msg.Message{ + User: &user.User{Name: "tester"}, + Channel: "test", + Body: payload, + Command: isCmd, + } +} + +func testUserCounter(t *testing.T, count int) { + expected := value(count) + mb := bot.NewMockBot() + mb.Cfg.Stats.DBPath = dbPath + s := New(mb) + assert.NotNil(t, s) + + for i := 0; i < count; i++ { + s.Message(makeMessage("test")) + } + + _, err := os.Stat(dbPath) + assert.Nil(t, err) + + stat, err := statFromDB(mb.Config().Stats.DBPath, "user", "tester") + assert.Nil(t, err) + actual := stat.val + assert.Equal(t, actual, expected) +} + +func TestMessages(t *testing.T) { + _, err := os.Stat(dbPath) + assert.NotNil(t, err) + + t.Run("TestOneUserCounter", func(t *testing.T) { + count := 5 + expected := value(count) + mb := bot.NewMockBot() + mb.Cfg.Stats.DBPath = dbPath + s := New(mb) + assert.NotNil(t, s) + + for i := 0; i < count; i++ { + s.Message(makeMessage("test")) + } + + _, err := os.Stat(dbPath) + assert.Nil(t, err) + + stat, err := statFromDB(mb.Config().Stats.DBPath, "user", "tester") + assert.Nil(t, err) + actual := stat.val + assert.Equal(t, actual, expected) + }) + + rmDB(t) + + t.Run("TestTenUserCounter", func(t *testing.T) { + count := 5 + expected := value(count) + mb := bot.NewMockBot() + mb.Cfg.Stats.DBPath = dbPath + s := New(mb) + assert.NotNil(t, s) + + for i := 0; i < count; i++ { + s.Message(makeMessage("test")) + } + + _, err := os.Stat(dbPath) + assert.Nil(t, err) + + stat, err := statFromDB(mb.Config().Stats.DBPath, "user", "tester") + assert.Nil(t, err) + actual := stat.val + assert.Equal(t, actual, expected) + }) + + rmDB(t) + + t.Run("TestChannelCounter", func(t *testing.T) { + count := 5 + expected := value(count) + mb := bot.NewMockBot() + mb.Cfg.Stats.DBPath = dbPath + s := New(mb) + assert.NotNil(t, s) + + for i := 0; i < count; i++ { + s.Message(makeMessage("test")) + } + + _, err := os.Stat(dbPath) + assert.Nil(t, err) + + stat, err := statFromDB(mb.Config().Stats.DBPath, "channel", "test") + assert.Nil(t, err) + actual := stat.val + assert.Equal(t, actual, expected) + }) + + rmDB(t) + + t.Run("TestSightingCounter", func(t *testing.T) { + count := 5 + expected := value(count) + mb := bot.NewMockBot() + + mb.Cfg.Stats.DBPath = dbPath + mb.Cfg.Stats.Sightings = []string{"user", "nobody"} + + s := New(mb) + assert.NotNil(t, s) + + for i := 0; i < count; i++ { + s.Message(makeMessage("user sighting")) + } + + _, err := os.Stat(dbPath) + assert.Nil(t, err) + + stat, err := statFromDB(mb.Config().Stats.DBPath, "sighting", "user") + assert.Nil(t, err) + actual := stat.val + assert.Equal(t, actual, expected) + }) + + rmDB(t) + + t.Run("TestSightingCounterNoResults", func(t *testing.T) { + count := 5 + expected := value(0) + mb := bot.NewMockBot() + + mb.Cfg.Stats.DBPath = dbPath + mb.Cfg.Stats.Sightings = []string{} + + s := New(mb) + assert.NotNil(t, s) + + for i := 0; i < count; i++ { + s.Message(makeMessage("user sighting")) + } + + _, err := os.Stat(dbPath) + assert.Nil(t, err) + + stat, err := statFromDB(mb.Config().Stats.DBPath, "sighting", "user") + assert.Nil(t, err) + actual := stat.val + assert.Equal(t, actual, expected) + }) + + rmDB(t) }