Skip to content

Commit

Permalink
tested the methods and fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Sep 12, 2023
1 parent 100fd58 commit bbc0172
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 33 deletions.
13 changes: 6 additions & 7 deletions server/backend/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func (s *CampusServer) GetTopNewsAlert(ctx context.Context, _ *emptypb.Empty) (*
return nil, err
}

var res *model.NewsAlert
err := s.db.Joins("Files").Where("NOW() between `from` and `to`").First(&res).Error
var res model.NewsAlert
err := s.db.Joins("Files").First(&res, "NOW() between `from` and `to`").Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, status.Error(codes.NotFound, "no currenty active top news")
} else if err != nil {
Expand All @@ -65,10 +65,10 @@ func (s *CampusServer) GetNewsAlert(ctx context.Context, req *pb.GetNewsAlertReq
return nil, err
}

var res *model.NewsAlert
err := s.db.Joins("Files").Where("news_alert = ?", req.Id).First(&res).Error
res := model.NewsAlert{NewsAlert: req.Id}
err := s.db.Joins("Files").First(&res).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, status.Error(codes.NotFound, "no news alerts")
return nil, status.Error(codes.NotFound, "no news alert")
} else if err != nil {
log.WithError(err).Error("could not GetNewsAlert")
return nil, status.Error(codes.Internal, "could not GetNewsAlert")
Expand All @@ -89,7 +89,7 @@ func (s *CampusServer) GetNewsAlerts(ctx context.Context, _ *emptypb.Empty) (*pb
}

var res []*model.NewsAlert
err := s.db.Joins("Files").Scan(&res).Error
err := s.db.Joins("Files").Find(&res).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, status.Error(codes.NotFound, "no news alerts")
} else if err != nil {
Expand All @@ -107,6 +107,5 @@ func (s *CampusServer) GetNewsAlerts(ctx context.Context, _ *emptypb.Empty) (*pb
To: timestamppb.New(alert.To),
})
}

return &pb.GetNewsAlertsReply{Alerts: alerts}, nil
}
155 changes: 129 additions & 26 deletions server/backend/news_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,36 @@ func (s *NewsSuite) SetupSuite() {
s.deviceBuf = newDeviceBuffer()
}

func file1() *model.Files {
return &model.Files{
File: 1,
Name: "src_1.png",
Path: "news/sources",
Downloads: 1,
URL: sql.NullString{Valid: false},
Downloaded: sql.NullBool{Bool: true, Valid: true},
}
}

func file2() *model.Files {
return &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},
}
}

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}},
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}},
FilesID: file1().File,
Files: *file1(),
Hook: null.String{NullString: sql.NullString{String: "", Valid: true}},
}
}

Expand All @@ -72,16 +87,9 @@ func source2() *model.NewsSource {
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}},
FilesID: file2().File,
Files: *file2(),
Hook: null.String{NullString: sql.NullString{String: "hook", Valid: true}},
}
}

Expand Down Expand Up @@ -144,9 +152,8 @@ func (s *NewsSuite) Test_GetTopNewsAlertOne() {
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.GetTopNewsAlert(metadata.NewIncomingContext(context.Background(), meta), nil)
response, err := server.GetTopNewsAlert(metadata.NewIncomingContext(context.Background(), metadata.MD{}), nil)
require.NoError(s.T(), err)
require.Equal(s.T(), &pb.GetTopNewsAlertReply{Alert: &pb.NewsAlert{
ImageUrl: expectedAlert.Files.URL.String,
Expand All @@ -158,22 +165,118 @@ func (s *NewsSuite) Test_GetTopNewsAlertOne() {
func (s *NewsSuite) Test_GetTopNewsAlertNone() {
s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetTopNewsAlertQuery)).WillReturnError(gorm.ErrRecordNotFound)

meta := metadata.MD{}
server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
response, err := server.GetTopNewsAlert(metadata.NewIncomingContext(context.Background(), meta), nil)
response, err := server.GetTopNewsAlert(metadata.NewIncomingContext(context.Background(), metadata.MD{}), nil)
require.Equal(s.T(), status.Error(codes.NotFound, "no currenty active top news"), err)
require.Nil(s.T(), response)
}
func (s *NewsSuite) Test_GetTopNewsAlertError() {
s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetTopNewsAlertQuery)).WillReturnError(gorm.ErrInvalidDB)

meta := metadata.MD{}
server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
response, err := server.GetTopNewsAlert(metadata.NewIncomingContext(context.Background(), meta), nil)
response, err := server.GetTopNewsAlert(metadata.NewIncomingContext(context.Background(), metadata.MD{}), nil)
require.Equal(s.T(), status.Error(codes.Internal, "could not GetTopNewsAlert"), err)
require.Nil(s.T(), response)
}

func alert1() *model.NewsAlert {
return &model.NewsAlert{
NewsAlert: 1,
FilesID: file1().File,
Files: *file1(),
Name: null.String{},
Link: null.String{},
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),
}
}

func alert2() *model.NewsAlert {
return &model.NewsAlert{
NewsAlert: 2,
FilesID: file2().File,
Files: *file2(),
Name: null.String{},
Link: null.String{},
Created: time.Time.Add(time.Now(), time.Hour),
From: time.Time.Add(time.Now(), time.Hour*2),
To: time.Time.Add(time.Now(), time.Hour*3),
}
}

const ExpectedGetNewsAlertsQuery = "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`"

func (s *NewsSuite) Test_GetNewsAlertsError() {
s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetNewsAlertsQuery)).WillReturnError(gorm.ErrInvalidDB)

meta := metadata.MD{}
server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
response, err := server.GetNewsAlerts(metadata.NewIncomingContext(context.Background(), meta), nil)
require.Equal(s.T(), status.Error(codes.Internal, "could not GetNewsAlerts"), err)
require.Nil(s.T(), response)
}
func (s *NewsSuite) Test_GetNewsAlertsNone() {
s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetNewsAlertsQuery)).WillReturnError(gorm.ErrRecordNotFound)

server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
response, err := server.GetNewsAlerts(metadata.NewIncomingContext(context.Background(), metadata.MD{}), nil)
require.Equal(s.T(), status.Error(codes.NotFound, "no news alerts"), err)
require.Nil(s.T(), response)
}
func (s *NewsSuite) Test_GetNewsAlertsMultiple() {
a1 := alert1()
a2 := alert2()
s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetNewsAlertsQuery)).
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(a1.NewsAlert, a1.FilesID, a1.Name, a1.Link, a1.Created, a1.From, a1.To, a1.Files.File, a1.Files.Name, a1.Files.Path, a1.Files.Downloads, a1.Files.URL, a1.Files.Downloaded).
AddRow(a2.NewsAlert, a2.FilesID, a2.Name, a2.Link, a2.Created, a2.From, a2.To, a2.Files.File, a2.Files.Name, a2.Files.Path, a2.Files.Downloads, a2.Files.URL, a2.Files.Downloaded))

server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
response, err := server.GetNewsAlerts(metadata.NewIncomingContext(context.Background(), metadata.MD{}), nil)
require.NoError(s.T(), err)
expectedResp := &pb.GetNewsAlertsReply{
Alerts: []*pb.NewsAlert{
{ImageUrl: a1.Files.URL.String, Link: a1.Link.String, Created: timestamppb.New(a1.Created), From: timestamppb.New(a1.From), To: timestamppb.New(a1.To)},
{ImageUrl: a2.Files.URL.String, Link: a2.Link.String, Created: timestamppb.New(a2.Created), From: timestamppb.New(a2.From), To: timestamppb.New(a2.To)},
}}
require.Equal(s.T(), expectedResp, response)
}

const ExpectedGetNewsAlertQuery = "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 `news_alert`.`news_alert` = ? ORDER BY `news_alert`.`news_alert` LIMIT 1"

func (s *NewsSuite) Test_GetNewsAlertError() {
s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetNewsAlertQuery)).WillReturnError(gorm.ErrInvalidDB)

meta := metadata.MD{}
server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
response, err := server.GetNewsAlert(metadata.NewIncomingContext(context.Background(), meta), &pb.GetNewsAlertRequest{Id: 42})
require.Equal(s.T(), status.Error(codes.Internal, "could not GetNewsAlert"), err)
require.Nil(s.T(), response)
}
func (s *NewsSuite) Test_GetNewsAlertNone() {
s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetNewsAlertQuery)).WillReturnError(gorm.ErrRecordNotFound)

server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
response, err := server.GetNewsAlert(metadata.NewIncomingContext(context.Background(), metadata.MD{}), &pb.GetNewsAlertRequest{Id: 42})
require.Equal(s.T(), status.Error(codes.NotFound, "no news alert"), err)
require.Nil(s.T(), response)
}
func (s *NewsSuite) Test_GetNewsAlertOne() {
a1 := alert1()
s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetNewsAlertQuery)).
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(a1.NewsAlert, a1.FilesID, a1.Name, a1.Link, a1.Created, a1.From, a1.To, a1.Files.File, a1.Files.Name, a1.Files.Path, a1.Files.Downloads, a1.Files.URL, a1.Files.Downloaded))

server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
response, err := server.GetNewsAlert(metadata.NewIncomingContext(context.Background(), metadata.MD{}), &pb.GetNewsAlertRequest{Id: 1})
require.NoError(s.T(), err)
expectedResp := &pb.GetNewsAlertReply{
Alert: &pb.NewsAlert{ImageUrl: a1.Files.URL.String, Link: a1.Link.String, Created: timestamppb.New(a1.Created), From: timestamppb.New(a1.From), To: timestamppb.New(a1.To)},
}
require.Equal(s.T(), expectedResp, response)
}

func (s *NewsSuite) AfterTest(_, _ string) {
require.NoError(s.T(), s.mock.ExpectationsWereMet())
}
Expand Down

0 comments on commit bbc0172

Please sign in to comment.