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

     1  package database_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  	"github.com/quickfeed/quickfeed/internal/qtest"
     8  	"github.com/quickfeed/quickfeed/kit/score"
     9  	"github.com/quickfeed/quickfeed/qf"
    10  	"google.golang.org/protobuf/testing/protocmp"
    11  )
    12  
    13  func TestGormDBRemoveTest(t *testing.T) {
    14  	db, cleanup := qtest.TestDB(t)
    15  	defer cleanup()
    16  	user, course, assignment := setupCourseAssignment(t, db)
    17  
    18  	// create a new submission, ensure that build info and scores are saved as well
    19  	buildInfo := &score.BuildInfo{
    20  		BuildDate: qtest.Timestamp(t, "2022-11-10T13:00:00"),
    21  		BuildLog:  "Testing",
    22  		ExecTime:  33333,
    23  	}
    24  	scores := []*score.Score{
    25  		{TestName: "Test1", Score: 10, MaxScore: 15, Weight: 1},
    26  		{TestName: "Test2", Score: 0, MaxScore: 5, Weight: 1},
    27  		{TestName: "Test3", Score: 3, MaxScore: 5, Weight: 1},
    28  	}
    29  	if err := db.CreateSubmission(&qf.Submission{
    30  		AssignmentID: assignment.ID,
    31  		UserID:       user.ID,
    32  		BuildInfo:    buildInfo,
    33  		Scores:       scores,
    34  	}); err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	submissions, err := db.GetLastSubmissions(course.ID, &qf.Submission{UserID: user.ID})
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	if len(submissions) != 1 {
    42  		t.Fatalf("have %d submissions want %d", len(submissions), 1)
    43  	}
    44  
    45  	buildInfo.SubmissionID = submissions[0].ID
    46  	buildInfo.ID = 1
    47  	if diff := cmp.Diff(buildInfo, submissions[0].BuildInfo, protocmp.Transform()); diff != "" {
    48  		t.Errorf("Expected same build info, but got (-got +want):\n%s", diff)
    49  	}
    50  	if diff := cmp.Diff(
    51  		submissions[0].Scores,
    52  		scores,
    53  		protocmp.Transform(),
    54  		protocmp.IgnoreFields(&score.Score{}, "ID", "SubmissionID")); diff != "" {
    55  		t.Errorf("Incorrect scores after first save (-want, +got):\n%s", diff)
    56  	}
    57  
    58  	// buildInfo record must be updated (have the same ID as before) instead
    59  	// of saving a duplicate
    60  	oldSubmissionID := submissions[0].ID
    61  	updatedBuildInfo := &score.BuildInfo{
    62  		BuildDate: qtest.Timestamp(t, "2022-11-10T15:00:00"),
    63  		BuildLog:  "Updated",
    64  		ExecTime:  12345,
    65  	}
    66  	scores = []*score.Score{
    67  		{TestName: "Test1", Score: 10, MaxScore: 15, Weight: 1},
    68  		// Test2 is removed from the tests repository and should be removed in the output from GetLastSubmissions
    69  		{TestName: "Test3", Score: 3, MaxScore: 5, Weight: 1},
    70  	}
    71  
    72  	submissions[0].BuildInfo = updatedBuildInfo
    73  	submissions[0].Scores = scores
    74  	if err := db.CreateSubmission(submissions[0]); err != nil {
    75  		t.Fatal(err)
    76  	}
    77  	submissions, err = db.GetLastSubmissions(course.ID, &qf.Submission{UserID: user.ID})
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	if len(submissions) != 1 {
    82  		t.Fatalf("have %d submissions want %d", len(submissions), 1)
    83  	}
    84  
    85  	updatedBuildInfo.ID = submissions[0].BuildInfo.ID
    86  	updatedBuildInfo.SubmissionID = oldSubmissionID
    87  	if diff := cmp.Diff(submissions[0].BuildInfo, updatedBuildInfo, protocmp.Transform()); diff != "" {
    88  		t.Errorf("Expected updated build info, but got (-sub +want):\n%s", diff)
    89  	}
    90  	if diff := cmp.Diff(submissions[0].Scores, scores, protocmp.Transform()); diff != "" {
    91  		t.Errorf("Incorrect scores after update (-want, +got):\n%s", diff)
    92  	}
    93  
    94  	// attempting to update build info and scores with wrong submission ID must return an error
    95  	submissions[0].ID = 123
    96  	if err := db.CreateSubmission(submissions[0]); err == nil {
    97  		t.Fatal("expected error: record not found")
    98  	}
    99  }