paste-abroad/internel/storage/sqlite_test.go

78 lines
1.6 KiB
Go

package storage_test
import (
"github.com/leafee98/paste-abroad/internel/storage"
"github.com/leafee98/paste-abroad/internel/storage/sqlite"
"time"
"testing"
)
func compareBytes(a []byte, b []byte) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func TestSqlite(t * testing.T) {
s, err := sqlite.New("", 8)
if err != nil {
t.Fatalf("Fatal on New: %v", err)
}
defer s.Close()
testGetSave(s, t)
}
func testGetSave(s *sqlite.StorageSqlite, t * testing.T) {
var a = storage.Paste {
Content: []byte("abc"),
Encrypt: false,
Expire: time.Now().UnixMilli() + 15 * 1000,
}
var b = storage.Paste {
Content: []byte("def"),
Encrypt: false,
Expire: time.Now().UnixMilli() - 15 * 1000,
}
id_a, err := s.Save(&a)
if err != nil {
t.Fatalf("Fatal on Save a: %v", err)
}
id_b, err := s.Save(&b)
if err != nil {
t.Fatalf("Fatal on Save b: %v", err)
}
affected, err := s.Purge()
if err != nil {
t.Fatalf("Fatal on Purge: %v", err)
}
if affected != 1 {
t.Fatalf("Purged %v rows, expect 1 row", affected)
}
var p_a, p_b, _ *storage.Paste
if p_a, err = s.Get(id_a); err != nil {
t.Fatalf("Fatal on Get a: %v", err)
if !compareBytes(p_a.Content, []byte("abc")) {
t.Fatal("Content not match")
}
}
if p_b, err = s.Get(id_b); err == nil && p_b != nil {
t.Fatalf("Fatal on Get b, it shoud have been purged")
}
}