mirror of
https://blitiri.com.ar/repos/chasquid
synced 2025-12-17 14:37:02 +00:00
We have many places in our tests where we create temporary directories, which we later remove (most of the time). We have at least 3 helpers to do this, and various places where it's done ad-hoc (and the cleanup is not always present). To try to reduce the clutter, and make the tests more uniform and readable, this patch introduces two helpers in a new "testutil" package: one for creating and one for removing temporary directories. These new functions are safer, better tested, and make the tests more consistent. All the tests are updated to use them.
80 lines
1.8 KiB
Go
80 lines
1.8 KiB
Go
package protoio
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"blitiri.com.ar/go/chasquid/internal/protoio/testpb"
|
|
"blitiri.com.ar/go/chasquid/internal/testlib"
|
|
)
|
|
|
|
func TestBin(t *testing.T) {
|
|
dir := testlib.MustTempDir(t)
|
|
defer testlib.RemoveIfOk(t, dir)
|
|
pb := &testpb.M{"hola"}
|
|
|
|
if err := WriteMessage("f", pb, 0600); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
pb2 := &testpb.M{}
|
|
if err := ReadMessage("f", pb2); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if pb.Content != pb2.Content {
|
|
t.Errorf("content mismatch, got %q, expected %q", pb2.Content, pb.Content)
|
|
}
|
|
}
|
|
|
|
func TestText(t *testing.T) {
|
|
dir := testlib.MustTempDir(t)
|
|
defer testlib.RemoveIfOk(t, dir)
|
|
pb := &testpb.M{"hola"}
|
|
|
|
if err := WriteTextMessage("f", pb, 0600); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
pb2 := &testpb.M{}
|
|
if err := ReadTextMessage("f", pb2); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if pb.Content != pb2.Content {
|
|
t.Errorf("content mismatch, got %q, expected %q", pb2.Content, pb.Content)
|
|
}
|
|
}
|
|
|
|
func TestStore(t *testing.T) {
|
|
dir := testlib.MustTempDir(t)
|
|
defer testlib.RemoveIfOk(t, dir)
|
|
st, err := NewStore(dir + "/store")
|
|
if err != nil {
|
|
t.Fatalf("failed to create store: %v", err)
|
|
}
|
|
|
|
if ids, err := st.ListIDs(); len(ids) != 0 || err != nil {
|
|
t.Errorf("expected no ids, got %v - %v", ids, err)
|
|
}
|
|
|
|
pb := &testpb.M{"hola"}
|
|
|
|
if err := st.Put("f", pb); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
pb2 := &testpb.M{}
|
|
if ok, err := st.Get("f", pb2); err != nil || !ok {
|
|
t.Errorf("Get(f): %v - %v", ok, err)
|
|
}
|
|
if pb.Content != pb2.Content {
|
|
t.Errorf("content mismatch, got %q, expected %q", pb2.Content, pb.Content)
|
|
}
|
|
|
|
if ok, err := st.Get("notexists", pb2); err != nil || ok {
|
|
t.Errorf("Get(notexists): %v - %v", ok, err)
|
|
}
|
|
|
|
if ids, err := st.ListIDs(); len(ids) != 1 || ids[0] != "f" || err != nil {
|
|
t.Errorf("expected [f], got %v - %v", ids, err)
|
|
}
|
|
}
|