Skip to content

Commit

Permalink
added an update note api
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Sep 14, 2023
1 parent 60ea49a commit 8b00def
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
34 changes: 14 additions & 20 deletions server/backend/updateNews_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package backend
import (
"context"
"database/sql"
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"regexp"
"testing"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/DATA-DOG/go-sqlmock"
pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -50,44 +50,38 @@ const ExpectedGetUpdateNoteQuery = "SELECT * FROM `update_note` WHERE `update_no

func (s *UpdateNoteSuite) Test_GetUpdateNoteOne() {
s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetUpdateNoteQuery)).
WillReturnRows(sqlmock.NewRows([]string{"source", "title", "url"}).
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))
WillReturnRows(sqlmock.NewRows([]string{"version_code", "version_name", "message"}).
AddRow(1, "1.0.0", "Test Message"))

meta := metadata.MD{}
server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
response, err := server.GetUpdateNote(metadata.NewIncomingContext(context.Background(), meta), &pb.GetUpdateNoteRequest{Version: 1})
require.NoError(s.T(), err)
expectedResp := &pb.NewsSourceReply{
Sources: []*pb.NewsSource{
{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},
},
expectedResp := &pb.GetUpdateNoteReply{
Message: "Test Message",
VersionName: "1.0.0",
}
require.Equal(s.T(), expectedResp, response)
}

func (s *UpdateNoteSuite) Test_GetUpdateNoteNone() {
s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetSourceQuery)).
WillReturnRows(sqlmock.NewRows([]string{"source", "title", "url"}))
s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetUpdateNoteQuery)).
WillReturnRows(sqlmock.NewRows([]string{"version_code", "version_name", "message"}))

meta := metadata.MD{}
server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
response, err := server.GetUpdateNote(metadata.NewIncomingContext(context.Background(), meta), &pb.GetUpdateNoteRequest{Version: 1})
require.NoError(s.T(), err)
expectedResp := &pb.NewsSourceReply{
Sources: []*pb.NewsSource(nil),
}
require.Equal(s.T(), expectedResp, response)
require.Equal(s.T(), status.Error(codes.NotFound, "No update note found"), err)
require.Nil(s.T(), response)
}

func (s *UpdateNoteSuite) Test_GetUpdateNoteError() {
s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetTopNewsQuery)).WillReturnError(gorm.ErrInvalidDB)
s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetUpdateNoteQuery)).WillReturnError(gorm.ErrInvalidDB)

meta := metadata.MD{}
server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
response, err := server.GetUpdateNote(metadata.NewIncomingContext(context.Background(), meta), &pb.GetUpdateNoteRequest{Version: 1})
require.Equal(s.T(), status.Error(codes.Internal, "could not GetTopNews"), err)
require.Equal(s.T(), status.Error(codes.Internal, "Internal server error"), err)
require.Nil(s.T(), response)
}

Expand Down
6 changes: 4 additions & 2 deletions server/backend/updateNote.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"errors"

"google.golang.org/grpc/codes"

pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev"
"github.com/TUM-Dev/Campus-Backend/server/model"
log "github.com/sirupsen/logrus"
Expand All @@ -18,10 +20,10 @@ func (s *CampusServer) GetUpdateNote(ctx context.Context, req *pb.GetUpdateNoteR

res := model.UpdateNote{VersionCode: req.Version}
if err := s.db.First(&res).Error; errors.Is(err, gorm.ErrRecordNotFound) {
return nil, status.Error(404, "No update note found")
return nil, status.Error(codes.NotFound, "No update note found")
} else if err != nil {
log.WithField("VersionCode", req.Version).WithError(err).Error("Failed to get update note")
return nil, status.Error(500, "Internal server error")
return nil, status.Error(codes.Internal, "Internal server error")
}

return &pb.GetUpdateNoteReply{Message: res.Message, VersionName: res.VersionName}, nil
Expand Down
5 changes: 5 additions & 0 deletions server/model/updateNote.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ type UpdateNote struct {
VersionName string `gorm:"column:version_name;type:text;"`
Message string `gorm:"column:message;type:text;"`
}

// TableName sets the insert table name for this struct type
func (n *UpdateNote) TableName() string {
return "update_note"
}

0 comments on commit 8b00def

Please sign in to comment.