package db import ( "path/filepath" "testing" ) func TestTeamsSchemas(t *testing.T) { if !OnPostgres() { t.Skip("teams are Postgres's") } d, err := Open(filepath.Join(t.TempDir(), "app.db")) if err != nil { t.Fatal(err) } defer d.Close() teams, err := d.Teams() if err != nil { t.Fatal(err) } defer teams.Close() if _, err := teams.Open(1); err == nil { t.Error("opened a team that wasn't created") } for _, id := range []int64{1, 2} { if err := teams.Create(id); err != nil { t.Fatal(err) } } a, err := teams.Open(1) if err != nil { t.Fatal(err) } b, _ := teams.Open(2) if a2, _ := teams.Open(1); a2 != a { t.Error("a second pool") } if v, err := a.SchemaVersion(); err != nil || v != Schema() { t.Errorf("team schema at %d: %v", v, err) } // Each team's own tables. if err := a.PutJSON("company", "Acme"); err != nil { t.Fatal(err) } if err := b.PutJSON("company", "Bolt"); err != nil { t.Fatal(err) } var got string a.GetJSON("company", &got) if got != "Acme" { t.Errorf("team 1 sees %q", got) } b.GetJSON("company", &got) if got != "Bolt" { t.Errorf("team 2 sees %q", got) } var n int d.QueryRow(`SELECT count(*) FROM settings WHERE key = 'company'`).Scan(&n) if n != 0 { t.Error("a team's setting in the main schema") } // The shared tables: only in the main schema, reached from a team's. for _, tbl := range PlatformTables { var inTeam bool d.QueryRow(`SELECT to_regclass($1) IS NOT NULL`, teams.Schema(1)+"."+tbl).Scan(&inTeam) if inTeam { t.Errorf("%s is in the team's schema", tbl) } } if _, err := d.Exec(`INSERT INTO teams (name, created_at) VALUES ('Acme', 'now')`); err != nil { t.Fatal(err) } if err := a.QueryRow(`SELECT count(*) FROM teams`).Scan(&n); err != nil || n != 1 { t.Errorf("the shared teams table from a team: %d %v", n, err) } // Creating again only migrates; dropping removes it all. if err := teams.Create(1); err != nil { t.Fatal(err) } if err := teams.Drop(2); err != nil { t.Fatal(err) } if _, err := teams.Open(2); err == nil { t.Error("a dropped team opens") } }