Skip to content

Commit

Permalink
Cast to datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
rcy committed Oct 7, 2023
1 parent 7c1b93b commit 91d83f3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
4 changes: 2 additions & 2 deletions handlers/feed_me.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(&notes, query, t)
if err != nil {
if err == sql.ErrNoRows {
Expand Down
39 changes: 37 additions & 2 deletions handlers/feed_me_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 91d83f3

Please sign in to comment.