From 7330659130cf8b172754da891ccb62fae0cc6203 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Mon, 11 Sep 2023 22:57:49 +0200 Subject: [PATCH] added tests for `GetNewsSources` --- server/backend/news.go | 18 +++++------ server/backend/news_test.go | 61 +++++++++++++++++++++++++------------ server/model/news_source.go | 16 ++++------ 3 files changed, 55 insertions(+), 40 deletions(-) diff --git a/server/backend/news.go b/server/backend/news.go index 12ce1dd1..ce495dd5 100644 --- a/server/backend/news.go +++ b/server/backend/news.go @@ -20,21 +20,18 @@ func (s *CampusServer) GetNewsSources(ctx context.Context, _ *emptypb.Empty) (ne } var sources []model.NewsSource - if err := s.db.Find(&sources).Error; err != nil { - return nil, status.Error(codes.Internal, err.Error()) + if err := s.db.Joins("Files").Find(&sources).Error; err != nil { + log.WithError(err).Error("could not find newsSources") + return nil, status.Error(codes.Internal, "could not GetNewsSources") } var resp []*pb.NewsSource for _, source := range sources { - var icon model.Files - if err := s.db.Where("file = ?", source.Icon).First(&icon).Error; err != nil { - icon = model.Files{File: 0} - } - log.Info("sending news source", source.Title) + log.WithField("title", source.Title).Trace("sending news source") resp = append(resp, &pb.NewsSource{ Source: fmt.Sprintf("%d", source.Source), Title: source.Title, - Icon: icon.URL.String, + Icon: source.Files.URL.String, }) } return &pb.NewsSourceReply{Sources: resp}, nil @@ -44,11 +41,12 @@ func (s *CampusServer) GetTopNews(ctx context.Context, _ *emptypb.Empty) (*pb.Ge if err := s.checkDevice(ctx); err != nil { return nil, err } - log.Printf("Received: get top news") + + log.Trace("Received: get top news") var res *model.NewsAlert err := s.db.Joins("Company").Where("NOW() between `from` and `to`").Limit(1).First(&res).Error if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { - log.WithError(err).Errorf("Failed to fetch top news") + log.WithError(err).Error("Failed to fetch top news") } else if res != nil { return &pb.GetTopNewsReply{ //ImageUrl: res.Name, diff --git a/server/backend/news_test.go b/server/backend/news_test.go index 5ec337da..334e983a 100644 --- a/server/backend/news_test.go +++ b/server/backend/news_test.go @@ -3,6 +3,7 @@ package backend import ( "context" "database/sql" + "fmt" "github.com/DATA-DOG/go-sqlmock" pb "github.com/TUM-Dev/Campus-Backend/server/api" "github.com/TUM-Dev/Campus-Backend/server/model" @@ -13,7 +14,6 @@ import ( "gorm.io/driver/mysql" "gorm.io/gorm" "regexp" - "strconv" "testing" ) @@ -47,29 +47,47 @@ func (s *NewsSuite) SetupSuite() { func source1() *model.NewsSource { return &model.NewsSource{ - Source: 1, - Title: "Amazing News 1", - URL: null.String{NullString: sql.NullString{String: "https://example.com/amazing1", Valid: true}}, - Icon: null.Int{NullInt64: sql.NullInt64{Int64: 1, Valid: true}}, - Hook: null.String{NullString: sql.NullString{String: "", Valid: true}}, + Source: 1, + Title: "Amazing News 1", + URL: null.String{NullString: sql.NullString{String: "https://example.com/amazing1", Valid: true}}, + FilesID: 2, + Files: model.Files{ + File: 2, + Name: "src_2.png", + Path: "news/sources", + Downloads: 1, + URL: sql.NullString{Valid: false}, + Downloaded: sql.NullBool{Bool: true, Valid: true}, + }, + Hook: null.String{NullString: sql.NullString{String: "", Valid: true}}, } } func source2() *model.NewsSource { return &model.NewsSource{ - Source: 2, - Title: "Amazing News 2", - URL: null.String{NullString: sql.NullString{String: "https://example.com/amazing2", Valid: true}}, - Icon: null.Int{NullInt64: sql.NullInt64{Int64: 2, Valid: true}}, - Hook: null.String{NullString: sql.NullString{String: "hook", Valid: true}}, + Source: 2, + Title: "Amazing News 2", + URL: null.String{NullString: sql.NullString{String: "https://example.com/amazing2", Valid: true}}, + FilesID: 2, + Files: model.Files{ + File: 2, + Name: "src_2.png", + Path: "news/sources", + Downloads: 1, + URL: sql.NullString{Valid: false}, + Downloaded: sql.NullBool{Bool: true, Valid: true}, + }, + Hook: null.String{NullString: sql.NullString{String: "hook", Valid: true}}, } } +const ExpectedGetSourceQuery = "SELECT `newsSource`.`source`,`newsSource`.`title`,`newsSource`.`url`,`newsSource`.`icon`,`newsSource`.`hook`,`Files`.`file` AS `Files__file`,`Files`.`name` AS `Files__name`,`Files`.`path` AS `Files__path`,`Files`.`downloads` AS `Files__downloads`,`Files`.`url` AS `Files__url`,`Files`.`downloaded` AS `Files__downloaded` FROM `newsSource` LEFT JOIN `files` `Files` ON `newsSource`.`icon` = `Files`.`file`" + func (s *NewsSuite) Test_GetNewsSourcesMultiple() { - s.mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `newsSource`")). - WillReturnRows(sqlmock.NewRows([]string{"source", "title", "url", "icon", "hook"}). - AddRow(source1().Source, source1().Title, source1().URL, source1().Icon, source1().Hook). - AddRow(source2().Source, source2().Title, source2().URL, source2().Icon, source2().Hook)) + s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetSourceQuery)). + WillReturnRows(sqlmock.NewRows([]string{"source", "title", "url", "icon", "hook", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"}). + AddRow(source1().Source, source1().Title, source1().URL, source1().FilesID, source1().Hook, source1().Files.File, source1().Files.Name, source1().Files.Path, source1().Files.Downloads, source1().Files.URL, source1().Files.Downloaded). + AddRow(source2().Source, source2().Title, source2().URL, source2().FilesID, source2().Hook, source2().Files.File, source2().Files.Name, source2().Files.Path, source2().Files.Downloads, source2().Files.URL, source2().Files.Downloaded)) meta := metadata.MD{} server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} @@ -77,22 +95,25 @@ func (s *NewsSuite) Test_GetNewsSourcesMultiple() { require.NoError(s.T(), err) expectedResp := &pb.NewsSourceReply{ Sources: []*pb.NewsSource{ - {Source: string(source1().Source), Title: source1().Title, Icon: strconv.FormatInt(source1().Icon.Int64, 10)}, - {Source: string(source2().Source), Title: source2().Title, Icon: strconv.FormatInt(source2().Icon.Int64, 10)}, + {Source: fmt.Sprintf("%d", source1().Source), Title: source1().Title, Icon: source1().Files.URL.String}, + {Source: fmt.Sprintf("%d", source2().Source), Title: source2().Title, Icon: source2().Files.URL.String}, }, } require.Equal(s.T(), expectedResp, response) } func (s *NewsSuite) Test_GetNewsSourcesNone() { - s.mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `newsSource`")). - WillReturnRows(sqlmock.NewRows([]string{"source", "title", "icon"})) + s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetSourceQuery)). + WillReturnRows(sqlmock.NewRows([]string{"source", "title", "url", "icon", "hook", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"})) meta := metadata.MD{} server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} response, err := server.GetNewsSources(metadata.NewIncomingContext(context.Background(), meta), nil) require.NoError(s.T(), err) - require.Equal(s.T(), &pb.NewsSourceReply{Sources: []*pb.NewsSource{nil}}, response) + expectedResp := &pb.NewsSourceReply{ + Sources: []*pb.NewsSource(nil), + } + require.Equal(s.T(), expectedResp, response) } func (s *NewsSuite) AfterTest(_, _ string) { diff --git a/server/model/news_source.go b/server/model/news_source.go index 8fe0f222..c1b3925d 100644 --- a/server/model/news_source.go +++ b/server/model/news_source.go @@ -17,16 +17,12 @@ var ( // NewsSource struct is a row record of the newsSource table in the tca database type NewsSource struct { - //[ 0] source int null: false primary: true isArray: false auto: true col: int len: -1 default: [] - Source int32 `gorm:"primary_key;AUTO_INCREMENT;column:source;type:int;" json:"source"` - //[ 1] title text(16777215) null: false primary: false isArray: false auto: false col: text len: 16777215 default: [] - Title string `gorm:"column:title;type:text;size:16777215;" json:"title"` - //[ 2] url text(16777215) null: true primary: false isArray: false auto: false col: text len: 16777215 default: [] - URL null.String `gorm:"column:url;type:text;size:16777215;" json:"url"` - //[ 3] icon int null: true primary: false isArray: false auto: false col: int len: -1 default: [] - Icon null.Int `gorm:"column:icon;type:int;" json:"icon"` - //[ 4] hook char(12) null: true primary: false isArray: false auto: false col: char len: 12 default: [] - Hook null.String `gorm:"column:hook;type:char;size:12;" json:"hook"` + Source int32 `gorm:"primary_key;AUTO_INCREMENT;column:source;type:int;"` + Title string `gorm:"column:title;type:text;size:16777215;"` + URL null.String `gorm:"column:url;type:text;size:16777215;"` + FilesID int32 `gorm:"column:icon;not null"` + Files Files `gorm:"foreignKey:FilesID;references:file;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` + Hook null.String `gorm:"column:hook;type:char;size:12;"` } // TableName sets the insert table name for this struct type