From ddb5688f8be4510d6f570418cee7adc919deb12f Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Tue, 24 Sep 2024 18:44:14 +0800 Subject: [PATCH] made sure that the testcase is more happy --- server/backend/student_club.go | 5 ++--- server/backend/student_club_test.go | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/server/backend/student_club.go b/server/backend/student_club.go index 2e4caff4..15b4363b 100644 --- a/server/backend/student_club.go +++ b/server/backend/student_club.go @@ -24,7 +24,7 @@ func (s *CampusServer) ListStudentClub(ctx context.Context, req *pb.ListStudentC var dbClubCollections []model.StudentClubCollection if err := s.db.WithContext(ctx). - Where("language = ?", req.GetLanguage().String()). + Where(&model.StudentClubCollection{Language: req.GetLanguage().String()}). Find(&dbClubCollections).Error; err != nil { log.WithError(err).Error("Error while querying student club collections") return nil, status.Error(codes.Internal, "could not query the student club collections. Please retry later") @@ -36,7 +36,7 @@ func (s *CampusServer) ListStudentClub(ctx context.Context, req *pb.ListStudentC Title: dbCollection.Name, Description: dbCollection.Description, Clubs: make([]*pb.StudentClub, 0), - UnstableCollectionId: 0, + UnstableCollectionId: uint64(dbCollection.ID), }) } for _, dbClub := range dbClubs { @@ -53,7 +53,6 @@ func (s *CampusServer) ListStudentClub(ctx context.Context, req *pb.ListStudentC for _, collection := range collections { if collection.UnstableCollectionId == uint64(dbClub.StudentClubCollectionID) { collection.Clubs = append(collection.Clubs, resClub) - resClub = nil break } } diff --git a/server/backend/student_club_test.go b/server/backend/student_club_test.go index c4852d5f..38913381 100644 --- a/server/backend/student_club_test.go +++ b/server/backend/student_club_test.go @@ -19,14 +19,16 @@ func TestCampusServer_ListStudentClub(t *testing.T) { db := utils.SetupTestContainer(ctx, t) exampleClubs := genExampleClubData(db, t) server := CampusServer{db: db} - response, err := server.ListStudentClub(ctx, &pb.ListStudentClubRequest{}) + language := pb.Language_German + response, err := server.ListStudentClub(ctx, &pb.ListStudentClubRequest{Language: &language}) require.NoError(t, err) url0 := exampleClubs[0].Image.FullExternalUrl() expectedResp := &pb.ListStudentClubReply{ Collections: []*pb.StudentClubCollection{ { - Title: exampleClubs[0].StudentClubCollection.Name, - Description: exampleClubs[0].StudentClubCollection.Description, + UnstableCollectionId: uint64(exampleClubs[0].StudentClubCollection.ID), + Title: exampleClubs[0].StudentClubCollection.Name, + Description: exampleClubs[0].StudentClubCollection.Description, Clubs: []*pb.StudentClub{ { Name: exampleClubs[0].Name, @@ -40,8 +42,9 @@ func TestCampusServer_ListStudentClub(t *testing.T) { }, }, { - Title: exampleClubs[2].StudentClubCollection.Name, - Description: exampleClubs[2].StudentClubCollection.Description, + UnstableCollectionId: uint64(exampleClubs[2].StudentClubCollection.ID), + Title: exampleClubs[2].StudentClubCollection.Name, + Description: exampleClubs[2].StudentClubCollection.Description, Clubs: []*pb.StudentClub{ { Name: exampleClubs[2].Name, @@ -56,6 +59,7 @@ func TestCampusServer_ListStudentClub(t *testing.T) { func genExampleClubData(db *gorm.DB, t *testing.T) []*model.StudentClub { col1 := model.StudentClubCollection{ Name: "col1", + Language: pb.Language_German.String(), Description: "Awesome collection", } err := db.Create(&col1).Error @@ -63,6 +67,7 @@ func genExampleClubData(db *gorm.DB, t *testing.T) []*model.StudentClub { col2 := model.StudentClubCollection{ Name: "col2", Description: "Terrible collection", + Language: pb.Language_German.String(), } err = db.Create(&col2).Error require.NoError(t, err) @@ -78,6 +83,7 @@ func genExampleClubData(db *gorm.DB, t *testing.T) []*model.StudentClub { require.NoError(t, err) club1 := &model.StudentClub{ Name: "Student Club 1", + Language: pb.Language_German.String(), Description: null.StringFrom("With Description"), LinkUrl: null.StringFrom("https://example.com"), ImageID: null.IntFrom(file1.File), @@ -90,6 +96,7 @@ func genExampleClubData(db *gorm.DB, t *testing.T) []*model.StudentClub { require.NoError(t, err) club2 := &model.StudentClub{ Name: "Student Club 2", + Language: pb.Language_German.String(), StudentClubCollectionID: col1.ID, StudentClubCollection: col1, } @@ -97,6 +104,7 @@ func genExampleClubData(db *gorm.DB, t *testing.T) []*model.StudentClub { require.NoError(t, err) club3 := &model.StudentClub{ Name: "Student Club 3", + Language: pb.Language_German.String(), StudentClubCollectionID: col2.ID, StudentClubCollection: col2, }