Skip to content

Commit

Permalink
added a testcases for GetTopNews and GetNewsSources
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Sep 12, 2023
1 parent 8e503cb commit 81e2307
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 8 deletions.
9 changes: 9 additions & 0 deletions server/backend/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ type deviceBuffer struct {
interval time.Duration // flush interval
}

func newDeviceBuffer() *deviceBuffer {
return &deviceBuffer{
lock: sync.Mutex{},
devices: make(map[string]*model.Devices),
interval: time.Minute,
}

}

func (s *CampusServer) RunDeviceFlusher() error {
for {
time.Sleep(s.deviceBuf.interval)
Expand Down
186 changes: 186 additions & 0 deletions server/backend/news_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
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"
"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 {
suite.Suite
DB *gorm.DB
mock sqlmock.Sqlmock
deviceBuf *deviceBuffer
}

func (s *NewsSuite) SetupSuite() {
var (
db *sql.DB
err error
)

db, s.mock, err = sqlmock.New()
require.NoError(s.T(), err)

dialector := mysql.New(mysql.Config{
Conn: db,
DriverName: "mysql",
})
s.mock.ExpectQuery("SELECT VERSION()").
WillReturnRows(sqlmock.NewRows([]string{"VERSION()"}).AddRow("10.11.4-MariaDB"))
s.DB, err = gorm.Open(dialector, &gorm.Config{})
require.NoError(s.T(), err)

s.deviceBuf = newDeviceBuffer()
}

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}},
}
}

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}},
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(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}
response, err := server.GetNewsSources(metadata.NewIncomingContext(context.Background(), meta), nil)
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},
},
}
require.Equal(s.T(), expectedResp, response)
}

func (s *NewsSuite) Test_GetNewsSourcesNone() {
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)
expectedResp := &pb.NewsSourceReply{
Sources: []*pb.NewsSource(nil),
}
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())
}

// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestExampleTestSuite(t *testing.T) {
suite.Run(t, new(NewsSuite))
}
10 changes: 2 additions & 8 deletions server/backend/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"google.golang.org/grpc/status"
"gorm.io/gorm"
"net"
"sync"
"time"
)

func (s *CampusServer) GRPCServe(l net.Listener) error {
Expand All @@ -41,12 +39,8 @@ func New(db *gorm.DB) *CampusServer {
initTagRatingOptions(db)

return &CampusServer{
db: db,
deviceBuf: &deviceBuffer{
lock: sync.Mutex{},
devices: make(map[string]*model.Devices),
interval: time.Minute,
},
db: db,
deviceBuf: newDeviceBuffer(),
iOSNotificationsService: NewIOSNotificationsService(),
}
}
Expand Down
1 change: 1 addition & 0 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/TUM-Dev/Campus-Backend/server
go 1.21

require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/disintegration/imaging v1.6.2
github.com/gabriel-vasile/mimetype v1.4.2
github.com/getsentry/sentry-go v0.23.0
Expand Down
2 changes: 2 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbi
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53/go.mod h1:+3IMCy2vIlbG1XG/0ggNQv0SvxCAIpPM5b1nCz56Xno=
github.com/CloudyKit/jet/v6 v6.1.0/go.mod h1:d3ypHeIRNo2+XyqnGA8s+aphtcVpjP5hPwP/Lzo7Ro4=
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U=
Expand Down

0 comments on commit 81e2307

Please sign in to comment.