You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
5.3 KiB
Go

package dbi
import (
"testing"
)
func genScholarship(username string) Scholarship {
return Scholarship{
Citation: randString(40),
Hyperlink: "https://" + randString(12) + ".example.com",
DocumentLink: "https://" + randString(12) + ".example.com/doc.pdf",
Status: "reported",
Username: username,
}
}
func TestCreateScholarship(t *testing.T) {
conn := testConn(t, testDB(t))
s := genScholarship(randString(10))
if err := CreateScholarship(conn, &s); err != nil {
t.Fatalf("CreateScholarship: %v", err)
}
if s.UID == "" {
t.Error("expected UID to be set after create")
}
if s.Created == "" {
t.Error("expected Created to be set after create")
}
if s.Modified == "" {
t.Error("expected Modified to be set after create")
}
got, err := GetScholarship(conn, s.UID)
if err != nil {
t.Fatalf("GetScholarship after create: %v", err)
}
if got != s {
t.Errorf("got %+v, want %+v", got, s)
}
}
func TestCreateScholarshipPresetUID(t *testing.T) {
conn := testConn(t, testDB(t))
s := genScholarship(randString(10))
s.UID = "preset" + randString(10)
if err := CreateScholarship(conn, &s); err != nil {
t.Fatalf("CreateScholarship: %v", err)
}
if s.UID != "preset"+s.UID[6:] {
t.Error("CreateScholarship should not overwrite a non-empty UID")
}
got, err := GetScholarship(conn, s.UID)
if err != nil {
t.Fatalf("GetScholarship: %v", err)
}
if got.UID != s.UID {
t.Errorf("got UID %q, want %q", got.UID, s.UID)
}
}
func TestCreateScholarshipDuplicateUID(t *testing.T) {
conn := testConn(t, testDB(t))
s := genScholarship(randString(10))
if err := CreateScholarship(conn, &s); err != nil {
t.Fatalf("first CreateScholarship: %v", err)
}
s2 := s
if err := CreateScholarship(conn, &s2); err == nil {
t.Error("expected error on duplicate UID, got nil")
}
}
func TestGetScholarship(t *testing.T) {
conn := testConn(t, testDB(t))
s := genScholarship(randString(10))
if err := CreateScholarship(conn, &s); err != nil {
t.Fatalf("CreateScholarship: %v", err)
}
got, err := GetScholarship(conn, s.UID)
if err != nil {
t.Fatalf("GetScholarship: %v", err)
}
if got != s {
t.Errorf("got %+v, want %+v", got, s)
}
}
func TestGetScholarshipNotFound(t *testing.T) {
conn := testConn(t, testDB(t))
_, err := GetScholarship(conn, randString(32))
if err == nil {
t.Error("expected error for missing scholarship, got nil")
}
}
func TestUpdateScholarship(t *testing.T) {
conn := testConn(t, testDB(t))
s := genScholarship(randString(10))
if err := CreateScholarship(conn, &s); err != nil {
t.Fatalf("CreateScholarship: %v", err)
}
originalUID := s.UID
originalCreated := s.Created
s.Citation = randString(40)
s.Hyperlink = "https://" + randString(12) + ".example.com"
s.DocumentLink = "https://" + randString(12) + ".example.com/updated.pdf"
s.Status = "posted"
if err := UpdateScholarship(conn, &s); err != nil {
t.Fatalf("UpdateScholarship: %v", err)
}
got, err := GetScholarship(conn, s.UID)
if err != nil {
t.Fatalf("GetScholarship after update: %v", err)
}
if got.UID != originalUID {
t.Errorf("UID changed: got %q, want %q", got.UID, originalUID)
}
if got.Created != originalCreated {
t.Errorf("Created changed: got %q, want %q", got.Created, originalCreated)
}
if got.Citation != s.Citation {
t.Errorf("Citation not updated: got %q, want %q", got.Citation, s.Citation)
}
if got.DocumentLink != s.DocumentLink {
t.Errorf("DocumentLink not updated: got %q, want %q", got.DocumentLink, s.DocumentLink)
}
if got.Status != "posted" {
t.Errorf("Status not updated: got %q", got.Status)
}
if got.Modified == "" {
t.Error("Modified should be set after update")
}
}
func TestDeleteScholarship(t *testing.T) {
conn := testConn(t, testDB(t))
s := genScholarship(randString(10))
if err := CreateScholarship(conn, &s); err != nil {
t.Fatalf("CreateScholarship: %v", err)
}
if err := DeleteScholarship(conn, s.UID); err != nil {
t.Fatalf("DeleteScholarship: %v", err)
}
_, err := GetScholarship(conn, s.UID)
if err == nil {
t.Error("expected error after delete, got nil")
}
}
func TestGetScholarshipsForUsername(t *testing.T) {
conn := testConn(t, testDB(t))
username := randString(10)
items := make([]Scholarship, 3)
for i := range items {
items[i] = genScholarship(username)
if err := CreateScholarship(conn, &items[i]); err != nil {
t.Fatalf("CreateScholarship %d: %v", i, err)
}
}
// extra item belonging to a different user
other := genScholarship(randString(10))
if err := CreateScholarship(conn, &other); err != nil {
t.Fatalf("CreateScholarship other: %v", err)
}
got, err := GetScholarshipsForUsername(conn, username)
if err != nil {
t.Fatalf("GetScholarshipsForUsername: %v", err)
}
if len(got) != 3 {
t.Errorf("got %d scholarships, want 3", len(got))
}
byUID := make(map[string]Scholarship)
for _, s := range got {
byUID[s.UID] = s
}
for _, want := range items {
s, ok := byUID[want.UID]
if !ok {
t.Errorf("scholarship %q missing from results", want.UID)
continue
}
if s != want {
t.Errorf("scholarship %q: got %+v, want %+v", want.UID, s, want)
}
}
}
func TestGetScholarshipsForUsernameEmpty(t *testing.T) {
conn := testConn(t, testDB(t))
got, err := GetScholarshipsForUsername(conn, randString(10))
if err != nil {
t.Fatalf("GetScholarshipsForUsername: %v", err)
}
if len(got) != 0 {
t.Errorf("expected empty slice, got %d items", len(got))
}
}