github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/web/submissions_internal_test.go (about) 1 package web 2 3 import ( 4 "sort" 5 "testing" 6 7 "github.com/quickfeed/quickfeed/qf" 8 ) 9 10 func TestOrderSubmissions(t *testing.T) { 11 assignments := []*qf.Assignment{ 12 { 13 ID: 1, 14 Order: 2, 15 }, 16 { 17 ID: 2, 18 Order: 3, 19 }, 20 { 21 ID: 3, 22 Order: 1, 23 }, 24 } 25 26 // Submissions in unsorted order 27 // We want to sort them by assignment order 28 submissions := []*qf.Submission{ 29 { 30 ID: 1, 31 AssignmentID: 1, 32 }, 33 { 34 ID: 2, 35 AssignmentID: 2, 36 }, 37 { 38 ID: 3, 39 AssignmentID: 3, 40 }, 41 } 42 43 // Create a map of assignment ID to order 44 orderMap := newOrderMap(assignments) 45 46 // Sort the submissions by assignment order 47 sort.Slice(submissions, func(i, j int) bool { 48 return orderMap.Less(submissions[i].AssignmentID, submissions[j].AssignmentID) 49 }) 50 51 // Check that the submissions are sorted correctly 52 if submissions[0].ID != 3 || submissions[1].ID != 1 || submissions[2].ID != 2 { 53 t.Error("Submissions not sorted correctly") 54 } 55 }