From 9ec3a3bb4c2c9bbd4cda6672c00232fca199d998 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Mon, 11 Sep 2023 23:56:32 +0200 Subject: [PATCH] added a testcase for GetTopNews --- server/backend/news.go | 25 +++++++++------- server/backend/news_test.go | 59 +++++++++++++++++++++++++++++++++++++ server/model/news_alert.go | 22 +++++--------- 3 files changed, 81 insertions(+), 25 deletions(-) diff --git a/server/backend/news.go b/server/backend/news.go index ce495dd5..3262e547 100644 --- a/server/backend/news.go +++ b/server/backend/news.go @@ -42,17 +42,20 @@ func (s *CampusServer) GetTopNews(ctx context.Context, _ *emptypb.Empty) (*pb.Ge return nil, err } - 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).Error("Failed to fetch top news") - } else if res != nil { - return &pb.GetTopNewsReply{ - //ImageUrl: res.Name, - Link: res.Link.String, - To: timestamppb.New(res.To), - }, nil + err := s.db.Joins("Files").Where("NOW() between `from` and `to`").First(&res).Error + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil, status.Error(codes.NotFound, "no currenty active top news") + } else if err != nil { + log.WithError(err).Error("could not GetTopNews") + return nil, status.Error(codes.Internal, "could not GetTopNews") } - return &pb.GetTopNewsReply{}, nil + + return &pb.GetTopNewsReply{ + ImageUrl: res.Files.URL.String, + Link: res.Link.String, + Created: timestamppb.New(res.Created), + From: timestamppb.New(res.From), + To: timestamppb.New(res.To), + }, nil } diff --git a/server/backend/news_test.go b/server/backend/news_test.go index 334e983a..b125e6a4 100644 --- a/server/backend/news_test.go +++ b/server/backend/news_test.go @@ -10,11 +10,15 @@ import ( "github.com/guregu/null" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" + "google.golang.org/grpc/codes" "google.golang.org/grpc/metadata" + "google.golang.org/grpc/status" + "google.golang.org/protobuf/types/known/timestamppb" "gorm.io/driver/mysql" "gorm.io/gorm" "regexp" "testing" + "time" ) type NewsSuite struct { @@ -116,6 +120,61 @@ func (s *NewsSuite) Test_GetNewsSourcesNone() { require.Equal(s.T(), expectedResp, response) } +const ExpectedGetTopNewsQuery = "SELECT `news_alert`.`news_alert`,`news_alert`.`file`,`news_alert`.`name`,`news_alert`.`link`,`news_alert`.`created`,`news_alert`.`from`,`news_alert`.`to`,`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 `news_alert` LEFT JOIN `files` `Files` ON `news_alert`.`file` = `Files`.`file` WHERE NOW() between `from` and `to` ORDER BY `news_alert`.`news_alert` LIMIT 1" + +func (s *NewsSuite) Test_GetTopNewsOne() { + expectedAlert := model.NewsAlert{ + NewsAlert: 1, + FilesID: 3001, + Files: model.Files{ + File: 3001, + Name: "Tournament_app_02-02.png", + Path: "newsalerts/", + Downloads: 0, + URL: sql.NullString{Valid: false}, + Downloaded: sql.NullBool{Bool: true, Valid: true}, + }, + Name: null.String{NullString: sql.NullString{String: "Exzellenzuniversität", Valid: true}}, + Link: null.String{NullString: sql.NullString{String: "https://tum.de", Valid: true}}, + Created: time.Time.Add(time.Now(), time.Hour*-4), + From: time.Time.Add(time.Now(), time.Hour*-2), + To: time.Time.Add(time.Now(), time.Hour*2), + } + s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetTopNewsQuery)). + WillReturnRows(sqlmock.NewRows([]string{"news_alert", "file", "name", "link", "created", "from", "to", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"}). + AddRow(expectedAlert.NewsAlert, expectedAlert.FilesID, expectedAlert.Name, expectedAlert.Link, expectedAlert.Created, expectedAlert.From, expectedAlert.To, expectedAlert.Files.File, expectedAlert.Files.Name, expectedAlert.Files.Path, expectedAlert.Files.Downloads, expectedAlert.Files.URL, expectedAlert.Files.Downloaded)) + + meta := metadata.MD{} + server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} + response, err := server.GetTopNews(metadata.NewIncomingContext(context.Background(), meta), nil) + require.NoError(s.T(), err) + require.Equal(s.T(), &pb.GetTopNewsReply{ + ImageUrl: expectedAlert.Files.URL.String, + Link: expectedAlert.Link.String, + Created: timestamppb.New(expectedAlert.Created), + From: timestamppb.New(expectedAlert.From), + To: timestamppb.New(expectedAlert.To), + }, response) +} +func (s *NewsSuite) Test_GetTopNewsNone() { + s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetTopNewsQuery)).WillReturnError(gorm.ErrRecordNotFound) + + meta := metadata.MD{} + server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} + response, err := server.GetTopNews(metadata.NewIncomingContext(context.Background(), meta), nil) + require.Equal(s.T(), status.Error(codes.NotFound, "no currenty active top news"), err) + require.Nil(s.T(), response) +} +func (s *NewsSuite) Test_GetTopNewsError() { + s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetTopNewsQuery)).WillReturnError(gorm.ErrInvalidDB) + + meta := metadata.MD{} + server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} + response, err := server.GetTopNews(metadata.NewIncomingContext(context.Background(), meta), nil) + require.Equal(s.T(), status.Error(codes.Internal, "could not GetTopNews"), err) + require.Nil(s.T(), response) +} + func (s *NewsSuite) AfterTest(_, _ string) { require.NoError(s.T(), s.mock.ExpectationsWereMet()) } diff --git a/server/model/news_alert.go b/server/model/news_alert.go index ad5f126a..0bf502a6 100644 --- a/server/model/news_alert.go +++ b/server/model/news_alert.go @@ -17,20 +17,14 @@ var ( // NewsAlert struct is a row record of the news_alert table in the tca database type NewsAlert struct { - //[ 0] news_alert int null: false primary: true isArray: false auto: true col: int len: -1 default: [] - NewsAlert int32 `gorm:"primary_key;AUTO_INCREMENT;column:news_alert;type:int;" json:"news_alert"` - //[ 1] file int null: true primary: false isArray: false auto: false col: int len: -1 default: [] - File null.Int `gorm:"column:file;type:int;" json:"file"` - //[ 2] name varchar(100) null: true primary: false isArray: false auto: false col: varchar len: 100 default: [] - Name null.String `gorm:"column:name;type:varchar(100);" json:"name"` - //[ 3] link text(65535) null: true primary: false isArray: false auto: false col: text len: 65535 default: [] - Link null.String `gorm:"column:link;type:text;size:65535;" json:"link"` - //[ 4] created timestamp null: false primary: false isArray: false auto: false col: timestamp len: -1 default: [CURRENT_TIMESTAMP] - Created time.Time `gorm:"column:created;type:timestamp;default:CURRENT_TIMESTAMP;" json:"created"` - //[ 5] from datetime null: false primary: false isArray: false auto: false col: datetime len: -1 default: [CURRENT_TIMESTAMP] - From time.Time `gorm:"column:from;type:datetime;default:CURRENT_TIMESTAMP;" json:"from"` - //[ 6] to datetime null: false primary: false isArray: false auto: false col: datetime len: -1 default: [CURRENT_TIMESTAMP] - To time.Time `gorm:"column:to;type:datetime;default:CURRENT_TIMESTAMP;" json:"to"` + NewsAlert int32 `gorm:"primary_key;AUTO_INCREMENT;column:news_alert;type:int;" json:"news_alert"` + FilesID int32 `gorm:"column:file;not null"` + Files Files `gorm:"foreignKey:FilesID;references:file;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` + Name null.String `gorm:"column:name;type:varchar(100);" json:"name"` + Link null.String `gorm:"column:link;type:text;size:65535;" json:"link"` + Created time.Time `gorm:"column:created;type:timestamp;default:CURRENT_TIMESTAMP;" json:"created"` + From time.Time `gorm:"column:from;type:datetime;default:CURRENT_TIMESTAMP;" json:"from"` + To time.Time `gorm:"column:to;type:datetime;default:CURRENT_TIMESTAMP;" json:"to"` } // TableName sets the insert table name for this struct type