meowlib/client/peerstorage_test.go

61 lines
1.2 KiB
Go
Raw Permalink Normal View History

2024-05-28 18:39:32 +02:00
package client
import (
"testing"
"github.com/google/uuid"
)
func TestOpen(t *testing.T) {
ps := &PeerStorage{
DbFile: "peerdb.test",
cache: nil,
db: nil,
}
err := ps.open()
if err != nil {
t.Errorf("Failed to open database: %v", err)
}
}
func TestStorePeer(t *testing.T) {
id := createId()
GetConfig().SetMemPass("test")
GetConfig().SetIdentity(id)
ps := &PeerStorage{
DbFile: "peerdb.test",
cache: nil,
db: nil,
}
peer := &Peer{
Uid: uuid.New().String(),
Name: "testName",
InvitationId: "testInvitationId",
MyName: "testMyName",
ContactPublicKey: "testContactPublicKey",
2024-05-28 18:39:32 +02:00
}
err := ps.StorePeer(peer)
if err != nil {
t.Errorf("Failed to store peer: %v", err)
}
// load the peer from the database
peers, err := ps.LoadPeers(GetConfig().GetMemPass())
if err != nil {
t.Errorf("Failed to load peers: %v", err)
}
if len(peers) != 1 {
t.Errorf("Expected 1 peer, got %d", len(peers))
}
if peers[0].Uid != peer.Uid {
t.Errorf("Expected peer to have uid %s, got %s", peer.Uid, peers[0].Uid)
}
if peers[0].MyName != peer.MyName {
t.Errorf("Expected peer to have MyName %s, got %s", peer.MyName, peers[0].MyName)
}
2024-05-28 18:39:32 +02:00
}