github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/database/gormdb_user_test.go (about)

     1  package database_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/quickfeed/quickfeed/internal/qtest"
     7  	"github.com/quickfeed/quickfeed/qf"
     8  )
     9  
    10  func TestGetUserByCourse(t *testing.T) {
    11  	const username = "meling"
    12  	db, cleanup := qtest.TestDB(t)
    13  	defer cleanup()
    14  
    15  	admin := qtest.CreateFakeUser(t, db)
    16  	course := &qf.Course{
    17  		ID:              1,
    18  		CourseCreatorID: admin.ID,
    19  		Code:            "DAT320",
    20  		Name:            "Operating Systems and Systems Programming",
    21  		Year:            2021,
    22  	}
    23  	qtest.CreateCourse(t, db, admin, course)
    24  
    25  	user := &qf.User{Login: username}
    26  	if err := db.CreateUser(user); err != nil {
    27  		t.Error(err)
    28  	}
    29  	qtest.EnrollStudent(t, db, user, course)
    30  
    31  	u, err := db.GetUserByCourse(course, username)
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	if u.ID != user.ID {
    36  		t.Errorf("expected user %d, got %d", user.ID, u.ID)
    37  	}
    38  	if u.Login != username {
    39  		t.Errorf("expected user %s, got %s", username, u.Login)
    40  	}
    41  }