package oltp
import (
"path/filepath"
"testing"
)
// A binding answers both ways, and the table refuses the two shapes
// RFC 0015 rules out: a second forum for one repository, and a store
// bound to two parents.
func TestCompanionBindingAnswersBothWays(t *testing.T) {
d, err := Open(filepath.Join(t.TempDir(), "oltp.db"))
if err != nil {
t.Fatal(err)
}
defer d.Close()
if _, ok, _ := d.Companion("fossil", "forum"); ok {
t.Fatal("FAIL: an unbound repository reported a companion")
}
if err := d.BindCompanion("fossil", "forum", "fossil-forum"); err != nil {
t.Fatal(err)
}
store, ok, err := d.Companion("fossil", "forum")
if err != nil || !ok || store != "fossil-forum" {
t.Fatalf("FAIL: companion = %q %v %v", store, ok, err)
}
repo, feature, ok, err := d.CompanionParent("fossil-forum")
if err != nil || !ok || repo != "fossil" || feature != "forum" {
t.Fatalf("FAIL: parent = %q %q %v %v", repo, feature, ok, err)
}
if _, _, ok, _ := d.CompanionParent("fossil"); ok {
t.Error("FAIL: the parent was reported as somebody's companion")
}
if err := d.BindCompanion("fossil", "forum", "other"); err == nil {
t.Error("FAIL: a second forum for one repository was accepted")
}
if err := d.BindCompanion("sqlite", "forum", "fossil-forum"); err == nil {
t.Error("FAIL: one store was bound to two parents")
}
if list, _ := d.Companions(); len(list) != 1 || list[0].Store != "fossil-forum" {
t.Errorf("FAIL: listing = %+v", list)
}
if err := d.UnbindCompanion("fossil", "forum"); err != nil {
t.Fatal(err)
}
if _, ok, _ := d.Companion("fossil", "forum"); ok {
t.Error("FAIL: the binding survived unbind")
}
t.Log("ok: one companion per feature, one parent per store")
}