diff --git a/handlers/feed_me.go b/handlers/feed_me.go index c66358e..788fa2c 100644 --- a/handlers/feed_me.go +++ b/handlers/feed_me.go @@ -12,9 +12,9 @@ import ( func candidateLinks(age time.Duration) ([]notes.Note, error) { notes := []notes.Note{} - query := `select id, created_at, nick, text, kind from notes where created_at <= ? and nick = target order by random() limit 69` + query := `select id, created_at, nick, text, kind from notes where created_at <= datetime(?) and nick = target order by random() limit 69` - t := time.Now().Add(-age).UTC().Format("2006-01-02T15:04:05Z") + t := time.Now().UTC().Add(-age).Format(time.RFC3339) err := model.DB.Select(¬es, query, t) if err != nil { if err == sql.ErrNoRows { diff --git a/handlers/feed_me_test.go b/handlers/feed_me_test.go index 80ae3b8..b3c4620 100644 --- a/handlers/feed_me_test.go +++ b/handlers/feed_me_test.go @@ -92,7 +92,7 @@ func TestFeedMe(t *testing.T) { } for _, x := range tc.messages { - query := `insert into notes(target, nick, text, kind, created_at) values(?, ?, ?, ?, ?)` + query := `insert into notes(target, nick, text, kind, created_at) values(?, ?, ?, ?, datetime(?))` createdAt := x.createdAt.UTC().Format("2006-01-02T15:04:05Z") _, err := model.DB.Exec(query, "nick", "nick", "link", x.text, createdAt) if err != nil { @@ -212,7 +212,7 @@ func TestPipeHealth(t *testing.T) { } for _, x := range tc.messages { - query := `insert into notes(target, nick, text, kind, created_at) values(?, ?, ?, ?, ?)` + query := `insert into notes(target, nick, text, kind, created_at) values(?, ?, ?, ?, datetime(?))` createdAt := x.createdAt.UTC().Format("2006-01-02T15:04:05Z") _, err := model.DB.Exec(query, "nick", "nick", "link", x.text, createdAt) if err != nil { @@ -241,6 +241,41 @@ func TestPipeHealth(t *testing.T) { } } +func TestCandidateLinks(t *testing.T) { + _, err := model.DB.Exec(`delete from notes`) + if err != nil { + t.Fatalf("error deleting notes %s", err) + } + + err = Link(bot.HandlerParams{ + Privmsgf: dummyPrivmsgf, + Matches: []string{"", "http://www.example.com"}, + Target: "theguy", + Nick: "theguy", + }) + + if err != nil { + t.Fatal(err) + } + + var notes []notes.Note + notes, err = candidateLinks(0) + if err != nil { + t.Fatal(err) + } + if len(notes) != 1 { + t.Fatalf("candidateLinks 0: want 1 got %d", len(notes)) + } + + notes, err = candidateLinks(time.Hour) + if err != nil { + t.Fatal(err) + } + if len(notes) != 0 { + t.Fatalf("candidate links 1 hour: want 0 got %d", len(notes)) + } +} + func dummyPrivmsgf(x string, y string, z ...interface{}) { return }