Skip to content
quarry Artifact [b64f435760]
Login

Artifact [b64f435760]

Artifact b64f4357609b255f31dc8f9d34040e1d480bfa18c05436c981ebf26ec5fdf880:


package oltp

import (
	"path/filepath"
	"testing"
)

// The held-file table round-trips what the A card will need, finds a
// file by the object it names, counts sharers, and forgets on removal.
func TestLocalAttachmentsRoundTrip(t *testing.T) {
	db, err := Open(filepath.Join(t.TempDir(), "oltp.db"))
	if err != nil {
		t.Fatal(err)
	}
	defer db.Close()
	const sha = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
	if err := db.LocalAttachmentAdd(LocalAttachment{Repo: "r", Ticket: "t1", Name: "a.txt", Size: 5, Type: "text/plain", SHA256: sha, Who: "lou"}); err != nil {
		t.Fatal(err)
	}
	if err := db.LocalAttachmentAdd(LocalAttachment{Repo: "r", Ticket: "t1", Name: "b.txt", Size: 7, Type: "text/plain", SHA256: sha, Who: "lou"}); err != nil {
		t.Fatal(err)
	}
	if err := db.LocalAttachmentAdd(LocalAttachment{Ticket: "t1", Name: "x"}); err == nil {
		t.Error("a row without a repository or hash was accepted")
	}
	rows, err := db.LocalAttachments("r", "t1")
	if err != nil || len(rows) != 2 || rows[0].Name != "a.txt" || rows[0].Size != 5 || rows[0].Who != "lou" || rows[0].Created == "" {
		t.Fatalf("list = %+v, %v", rows, err)
	}
	if a, ok, _ := db.LocalAttachmentBySHA("r", sha); !ok || a.Ticket != "t1" {
		t.Errorf("by sha = %+v %v", a, ok)
	}
	if n, _ := db.LocalSHACount("r", sha); n != 2 {
		t.Errorf("sha count = %d, want 2", n)
	}
	if err := db.LocalAttachmentRemove("r", "t1", "a.txt"); err != nil {
		t.Fatal(err)
	}
	if rows, _ = db.LocalAttachments("r", "t1"); len(rows) != 1 || rows[0].Name != "b.txt" {
		t.Errorf("after removal = %+v", rows)
	}
	if n, _ := db.LocalSHACount("r", sha); n != 1 {
		t.Errorf("sha count after removal = %d, want 1", n)
	}
	if rows, _ = db.LocalAttachments("r", "other"); len(rows) != 0 {
		t.Errorf("another ticket sees the rows: %+v", rows)
	}
}