forked from wgroeneveld/go-jamming
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
3.8 KiB
163 lines
3.8 KiB
package db
|
|
|
|
import (
|
|
"brainbaking.com/go-jamming/app/mf"
|
|
"fmt"
|
|
"github.com/stretchr/testify/assert"
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestSaveAndGetPicture(t *testing.T) {
|
|
data, err := ioutil.ReadFile("../mocks/picture.jpg")
|
|
assert.NoError(t, err)
|
|
|
|
db := newMentionRepoBunt(":memory:", []string{
|
|
"pussycat.com",
|
|
})
|
|
key, dberr := db.SavePicture(string(data), "bloeberig.be")
|
|
assert.NoError(t, dberr)
|
|
assert.Equal(t, "bloeberig.be:picture", key)
|
|
|
|
picDataAfterSave := db.GetPicture("bloeberig.be")
|
|
assert.Equal(t, data, picDataAfterSave)
|
|
}
|
|
|
|
func TestCleanupSpam(t *testing.T) {
|
|
db := newMentionRepoBunt(":memory:", []string{
|
|
"pussycat.com",
|
|
})
|
|
db.Save(mf.Mention{
|
|
Source: "https://naar.hier/jup",
|
|
Target: "https://pussycat.com/coolpussy.html",
|
|
}, &mf.IndiewebData{
|
|
Name: "lolz",
|
|
Target: "https://pussycat.com/coolpussy.html",
|
|
Source: "https://naar.hier/jup",
|
|
Url: "https://naar.hier",
|
|
})
|
|
db.Save(mf.Mention{
|
|
Source: "https://spam.be/malicious",
|
|
Target: "https://pussycat.com/dinges",
|
|
}, &mf.IndiewebData{
|
|
Name: "kapot",
|
|
Target: "https://pussycat.com/dinges",
|
|
Source: "https://spam.be/malicious",
|
|
Url: "https://spam.be",
|
|
})
|
|
|
|
db.CleanupSpam("pussycat.com", []string{"spam.be", "jaak.com"})
|
|
|
|
results := db.GetAll("pussycat.com")
|
|
assert.Equal(t, 1, len(results.Data))
|
|
assert.Equal(t, "lolz", results.Data[0].Name)
|
|
}
|
|
|
|
func TestDelete(t *testing.T) {
|
|
db := newMentionRepoBunt(":memory:", []string{
|
|
"pussycat.com",
|
|
})
|
|
wm := mf.Mention{
|
|
Target: "https://pussycat.com/coolpussy.html",
|
|
}
|
|
db.Save(wm, &mf.IndiewebData{
|
|
Name: "lolz",
|
|
})
|
|
db.Delete(wm)
|
|
|
|
results := db.GetAll("pussycat.com")
|
|
assert.Equal(t, 0, len(results.Data))
|
|
}
|
|
|
|
func TestUpdateLastSentMention(t *testing.T) {
|
|
db := newMentionRepoBunt(":memory:", []string{
|
|
"pussycat.com",
|
|
})
|
|
|
|
db.UpdateLastSentMention("pussycat.com", "https://last.sent")
|
|
last := db.LastSentMention("pussycat.com")
|
|
|
|
assert.Equal(t, "https://last.sent", last)
|
|
}
|
|
|
|
func TestGet(t *testing.T) {
|
|
db := newMentionRepoBunt(":memory:", []string{
|
|
"pussycat.com",
|
|
})
|
|
wm := mf.Mention{
|
|
Target: "https://pussycat.com/coolpussy.html",
|
|
}
|
|
db.Save(wm, &mf.IndiewebData{
|
|
Name: "lolz",
|
|
})
|
|
|
|
result := db.Get(wm)
|
|
assert.Equal(t, "lolz", result.Name)
|
|
}
|
|
|
|
func BenchmarkMentionRepoBunt_GetAll(b *testing.B) {
|
|
b.Cleanup(func() {
|
|
os.Remove("test.db")
|
|
})
|
|
db := newMentionRepoBunt("test.db", []string{
|
|
"pussycat.com",
|
|
})
|
|
|
|
items := 10000
|
|
fmt.Printf(" -- Saving %d items\n", items)
|
|
for n := 0; n < items; n++ {
|
|
db.Save(mf.Mention{
|
|
Source: fmt.Sprintf("https://blahsource.com/%d/ding.html", n),
|
|
Target: fmt.Sprintf("https://pussycat.com/%d/ding.html", n),
|
|
}, &mf.IndiewebData{
|
|
Name: fmt.Sprintf("benchmark %d", n),
|
|
Author: mf.IndiewebAuthor{
|
|
Name: fmt.Sprintf("author %d", n),
|
|
},
|
|
})
|
|
}
|
|
|
|
b.Run(fmt.Sprintf(" -- Benchmark Get All for #%d\n", b.N), func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
db.GetAll("pussycat.com")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestGetAllAndSaveSomeJson(t *testing.T) {
|
|
db := newMentionRepoBunt(":memory:", []string{
|
|
"pussycat.com",
|
|
})
|
|
db.Save(mf.Mention{
|
|
Target: "https://pussycat.com/coolpussy.html",
|
|
}, &mf.IndiewebData{
|
|
Name: "lolz",
|
|
Published: "2021-07-24T23:27:25+01:00",
|
|
})
|
|
|
|
results := db.GetAll("pussycat.com")
|
|
assert.Equal(t, 1, len(results.Data))
|
|
assert.Equal(t, "lolz", results.Data[0].Name)
|
|
}
|
|
|
|
func TestGetFiltersBasedOnDomain(t *testing.T) {
|
|
db := newMentionRepoBunt(":memory:", []string{
|
|
"pussycat.com",
|
|
})
|
|
db.Save(mf.Mention{
|
|
Target: "https://pussycat.com/coolpussy.html",
|
|
}, &mf.IndiewebData{
|
|
Name: "lolz",
|
|
})
|
|
db.Save(mf.Mention{
|
|
Target: "https://dingeling.com/dogshateus.html",
|
|
}, &mf.IndiewebData{
|
|
Name: "amaigat",
|
|
})
|
|
|
|
results := db.GetAll("pussycat.com")
|
|
assert.Equal(t, 1, len(results.Data))
|
|
assert.Equal(t, "lolz", results.Data[0].Name)
|
|
}
|