2
0
mirror of https://github.com/carlostrub/sisyphus synced 2024-10-31 09:20:15 +00:00
sisyphus/classify_test.go

128 lines
2.7 KiB
Go
Raw Permalink Normal View History

2017-05-13 21:22:23 +00:00
package sisyphus_test
import (
"math"
"os"
. "github.com/carlostrub/sisyphus"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
2017-05-15 22:26:44 +00:00
var _ = Describe("Classify Mails", func() {
Context("Classify one word from the mail that was ", func() {
2017-05-13 21:22:23 +00:00
BeforeEach(func() {
// check whether there exists a DB file
_, oserr := os.Stat("test/Maildir/sisyphus.db")
Ω(os.IsNotExist(oserr)).Should(BeTrue())
// Load db
2017-05-27 21:09:47 +00:00
dbs, err = LoadDatabases([]Maildir{
"test/Maildir",
})
2017-05-13 21:22:23 +00:00
Ω(err).ShouldNot(HaveOccurred())
m = new(Mail)
// Load junk mail
m = &Mail{
Key: "1488226337.M327833P8269.mail.carlostrub.ch,S=6960,W=7161:2,Sa",
Junk: true,
}
err = m.Learn(dbs["test/Maildir"], "test/Maildir")
2017-05-13 22:34:54 +00:00
Ω(err).ShouldNot(HaveOccurred())
2017-05-13 21:22:23 +00:00
// Load good mail
m = &Mail{
Key: "1488230510.M141612P8565.mail.carlostrub.ch,S=5978,W=6119",
}
err = m.Learn(dbs["test/Maildir"], "test/Maildir")
2017-05-13 22:34:54 +00:00
Ω(err).ShouldNot(HaveOccurred())
2017-05-13 21:22:23 +00:00
})
AfterEach(func() {
// Cleanup
CloseDatabases(dbs)
err = os.Remove("test/Maildir/sisyphus.db")
Ω(err).ShouldNot(HaveOccurred())
})
2017-05-15 22:26:44 +00:00
It("learned before and is junk", func() {
2017-05-13 21:22:23 +00:00
answer, prob, err := Junk(dbs["test/Maildir"], []string{"london"})
Ω(err).ShouldNot(HaveOccurred())
Ω(prob).Should(Equal(1.0))
Ω(answer).Should(BeTrue())
})
2017-05-15 22:26:44 +00:00
It("learned before and is good", func() {
2017-05-13 21:22:23 +00:00
answer, prob, err := Junk(dbs["test/Maildir"], []string{"localbase"})
Ω(err).ShouldNot(HaveOccurred())
Ω(prob).Should(Equal(0.0))
Ω(answer).Should(BeFalse())
})
2017-05-15 22:26:44 +00:00
It("never learned before", func() {
2017-05-13 21:22:23 +00:00
answer, prob, err := Junk(dbs["test/Maildir"], []string{"abcdefg"})
Ω(err).ShouldNot(HaveOccurred())
Ω(math.IsNaN(prob)).Should(BeTrue())
Ω(answer).Should(BeFalse())
})
2017-05-15 22:26:44 +00:00
It("learned both as good and junk, respectively", func() {
2017-05-13 21:22:23 +00:00
answer, prob, err := Junk(dbs["test/Maildir"], []string{"than"})
Ω(err).ShouldNot(HaveOccurred())
2017-05-15 22:26:44 +00:00
Ω(prob).Should(Equal(0.5))
Ω(answer).Should(BeFalse())
2017-05-13 21:22:23 +00:00
})
})
2017-05-27 21:09:47 +00:00
Context("Do not classify as junk if there is no information", func() {
BeforeEach(func() {
// Load empty Maildir2
2017-06-05 16:25:44 +00:00
err = LoadMaildirs([]Maildir{
2017-05-27 21:09:47 +00:00
"test/Maildir2",
})
Ω(err).ShouldNot(HaveOccurred())
// Load db
dbs, err = LoadDatabases([]Maildir{
"test/Maildir2",
})
Ω(err).ShouldNot(HaveOccurred())
})
AfterEach(func() {
// Cleanup
CloseDatabases(dbs)
err = os.RemoveAll("test/Maildir2")
Ω(err).ShouldNot(HaveOccurred())
})
It("learned nothing and thus return always good", func() {
answer, prob, err := Junk(dbs["test/Maildir2"], []string{"Carlo"})
2017-06-05 14:13:27 +00:00
Ω(err).ShouldNot(HaveOccurred())
Ω(math.IsNaN(prob)).Should(BeTrue())
2017-05-27 21:09:47 +00:00
Ω(answer).Should(BeFalse())
})
})
2017-05-13 21:22:23 +00:00
})