github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/database/gormdb_review_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/database"
     8  	"github.com/quickfeed/quickfeed/internal/qtest"
     9  	"github.com/quickfeed/quickfeed/qf"
    10  	"google.golang.org/protobuf/testing/protocmp"
    11  	"google.golang.org/protobuf/types/known/timestamppb"
    12  )
    13  
    14  func TestCreateUpdateReview(t *testing.T) {
    15  	db, cleanup := qtest.TestDB(t)
    16  	defer cleanup()
    17  
    18  	user, course, assignment := setupCourseAssignment(t, db)
    19  
    20  	if err := db.CreateSubmission(&qf.Submission{
    21  		AssignmentID: assignment.ID,
    22  		UserID:       user.ID,
    23  	}); err != nil {
    24  		t.Fatal(err)
    25  	}
    26  	// confirm that the submission is in the database
    27  	submissions, err := db.GetLastSubmissions(course.ID, &qf.Submission{UserID: user.ID})
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	if len(submissions) != 1 {
    32  		t.Fatalf("have %d submissions want %d", len(submissions), 1)
    33  	}
    34  
    35  	review := &qf.Review{
    36  		SubmissionID: 2,
    37  		ReviewerID:   1,
    38  		Feedback:     "my very good feedback",
    39  		Ready:        false,
    40  		Score:        95,
    41  		Edited:       &timestamppb.Timestamp{},
    42  		GradingBenchmarks: []*qf.GradingBenchmark{
    43  			{
    44  				ID:           1,
    45  				AssignmentID: 1,
    46  				ReviewID:     1,
    47  				Heading:      "Major league baseball",
    48  				Comment:      "wonders of the world",
    49  				Criteria: []*qf.GradingCriterion{
    50  					{
    51  						ID:          1,
    52  						BenchmarkID: 1,
    53  						Points:      30,
    54  						Description: "my description",
    55  						Grade:       qf.GradingCriterion_PASSED,
    56  						Comment:     "another comment",
    57  					},
    58  				},
    59  			},
    60  		},
    61  	}
    62  
    63  	submission := &qf.Submission{
    64  		AssignmentID: 1,
    65  		UserID:       1,
    66  		Reviews:      []*qf.Review{review},
    67  	}
    68  	if err := db.CreateSubmission(submission); err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	updateSubmission(t, db, review)
    72  
    73  	review.Edited = qtest.Timestamp(t, "2022-03-03T19:00:00")
    74  	review.Score = 90
    75  	review.Ready = true
    76  	updateSubmission(t, db, review)
    77  
    78  	review.Edited = timestamppb.Now()
    79  	review.Ready = false
    80  	updateSubmission(t, db, review)
    81  }
    82  
    83  func updateSubmission(t *testing.T, db database.Database, wantReview *qf.Review) {
    84  	if err := db.UpdateReview(wantReview); err != nil {
    85  		t.Errorf("failed to update review: %v", err)
    86  	}
    87  	sub, err := db.GetSubmission(&qf.Submission{ID: 2})
    88  	if err != nil {
    89  		t.Fatal(err)
    90  	}
    91  	if len(sub.Reviews) != 1 {
    92  		t.Fatalf("have %d reviews want %d", len(sub.Reviews), 1)
    93  	}
    94  	var gotReview *qf.Review
    95  	for _, r := range sub.GetReviews() {
    96  		gotReview = r
    97  		// fmt.Printf("sub %d: %+v, score: %d\n", sub.GetID(), r.GetReady(), r.GetScore())
    98  	}
    99  	if diff := cmp.Diff(gotReview, wantReview, protocmp.Transform()); diff != "" {
   100  		t.Errorf("Expected same review, but got (-got +want):\n%s", diff)
   101  	}
   102  }