From ca521d51d6561aa125f2e8c6639becd570844e0a Mon Sep 17 00:00:00 2001 From: Carlo Strub Date: Thu, 16 Mar 2017 20:21:24 +0000 Subject: [PATCH] fix tests and remove filter.go --- filter.go | 17 ----------------- mail.go | 5 +++-- mail_test.go | 17 +++++++++++++++-- main.go | 12 ++++++------ 4 files changed, 24 insertions(+), 27 deletions(-) delete mode 100644 filter.go diff --git a/filter.go b/filter.go deleted file mode 100644 index 5d1f7a3..0000000 --- a/filter.go +++ /dev/null @@ -1,17 +0,0 @@ -package main - -import ( - "github.com/jbrukh/bayesian" -) - -const ( - // good is the class of good mails that are not supposed to be Spam - good bayesian.Class = "Good" - // junk is the class of Spam mails - junk bayesian.Class = "Junk" -) - -// Classifiers contains the classifiers for mail subjects and bodies -type Classifiers struct { - Subject, Body *bayesian.Classifier -} diff --git a/mail.go b/mail.go index 36d0f64..95f709c 100644 --- a/mail.go +++ b/mail.go @@ -22,10 +22,11 @@ type Mail struct { // Index loads all mail keys from the Maildir directory for processing. func Index(d string, junk bool) (m []*Mail, err error) { + var j []string if junk { - j, err := maildir.Dir(d + "/.Junk").Keys() + j, err = maildir.Dir(d + "/.Junk").Keys() } else { - j, err := maildir.Dir(d).Keys() + j, err = maildir.Dir(d).Keys() } if err != nil { return m, err diff --git a/mail_test.go b/mail_test.go index 7c74113..93ef2b9 100644 --- a/mail_test.go +++ b/mail_test.go @@ -40,7 +40,7 @@ var _ = Describe("Mail", func() { Context("Maildir", func() { It("Create a slice of mail keys", func() { - result, err := s.Index("test/Maildir") + result, err := s.Index("test/Maildir", true) Ω(err).ShouldNot(HaveOccurred()) name := func(m1, m2 *s.Mail) bool { @@ -86,6 +86,19 @@ var _ = Describe("Mail", func() { Body: nil, Junk: true, }, + })) + }) + It("Create a slice of mail keys", func() { + result, err := s.Index("test/Maildir", false) + Ω(err).ShouldNot(HaveOccurred()) + + name := func(m1, m2 *s.Mail) bool { + return m1.Key < m2.Key + } + mailBy(name).Sort(result) + Ω(result).Should(Equal( + []*s.Mail{ + { Key: "1488230510.M141612P8565.mail.carlostrub.ch,S=5978,W=6119", Subject: nil, @@ -95,7 +108,7 @@ var _ = Describe("Mail", func() { })) }) It("Fail if Maildir does not exist", func() { - _, err := s.Index("test/DOESNOTEXIST") + _, err := s.Index("test/DOESNOTEXIST", false) Ω(err).Should(HaveOccurred()) }) }) diff --git a/main.go b/main.go index 04db441..c1e4c7c 100644 --- a/main.go +++ b/main.go @@ -140,12 +140,12 @@ func main() { for i := range mailsGood { db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("Processed")) - v := b.Get([]byte(mails[i].Key)) + v := b.Get([]byte(mailsGood[i].Key)) if len(v) == 0 { - unprocessedGood = append(unprocessedGood, mails[i].Key) + unprocessedGood = append(unprocessedGood, mailsGood[i].Key) } if string(v) == junk { - unprocessedGood = append(unprocessedGood, mails[i].Key) + unprocessedGood = append(unprocessedGood, mailsGood[i].Key) } return nil }) @@ -153,12 +153,12 @@ func main() { for i := range mailsJunk { db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("Processed")) - v := b.Get([]byte(mails[i].Key)) + v := b.Get([]byte(mailsJunk[i].Key)) if len(v) == 0 { - unprocessedJunk = append(unprocessedJunk, mails[i].Key) + unprocessedJunk = append(unprocessedJunk, mailsJunk[i].Key) } if string(v) == good { - unprocessedJunk = append(unprocessedJunk, mails[i].Key) + unprocessedJunk = append(unprocessedJunk, mailsJunk[i].Key) } return nil })