2023-02-06 07:33:05 +00:00
|
|
|
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"),
|
2023-02-15 04:09:09 +00:00
|
|
|
Encrypt: false,
|
2023-02-06 07:33:05 +00:00
|
|
|
Expire: time.Now().UnixMilli() + 15 * 1000,
|
|
|
|
}
|
|
|
|
var b = storage.Paste {
|
|
|
|
Content: []byte("def"),
|
2023-02-15 04:09:09 +00:00
|
|
|
Encrypt: false,
|
2023-02-06 07:33:05 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-02-14 10:56:03 +00:00
|
|
|
var p_a, p_b, _ *storage.Paste
|
2023-02-06 07:33:05 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
2023-02-14 10:56:03 +00:00
|
|
|
if p_b, err = s.Get(id_b); err == nil && p_b != nil {
|
2023-02-06 07:33:05 +00:00
|
|
|
t.Fatalf("Fatal on Get b, it shoud have been purged")
|
|
|
|
}
|
|
|
|
}
|