Skip to content

Commit

Permalink
fix news tests (#461)
Browse files Browse the repository at this point in the history
* fix news tests

* fix the tests again

* lint
  • Loading branch information
joschahenningsen authored Oct 30, 2024
1 parent a8a948a commit 2932b80
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
12 changes: 4 additions & 8 deletions server/backend/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ import (

pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev"
"github.com/TUM-Dev/Campus-Backend/server/model"
"github.com/hashicorp/golang-lru/v2/expirable"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
"gorm.io/gorm"
)

var newsSourceCache = expirable.NewLRU[string, []model.NewsSource](1, nil, time.Hour*6)
var newsCache = expirable.NewLRU[string, []model.News](1024, nil, time.Minute*30)

func (s *CampusServer) ListNewsSources(ctx context.Context, _ *pb.ListNewsSourcesRequest) (*pb.ListNewsSourcesReply, error) {
if err := s.checkDevice(ctx); err != nil {
return nil, err
Expand All @@ -43,14 +39,14 @@ func (s *CampusServer) ListNewsSources(ctx context.Context, _ *pb.ListNewsSource
const CacheKeyAllNewsSources = "all_news_sources"

func (s *CampusServer) getNewsSources(ctx context.Context) ([]model.NewsSource, error) {
if newsSources, ok := newsSourceCache.Get(CacheKeyAllNewsSources); ok {
if newsSources, ok := s.newsSourceCache.Get(CacheKeyAllNewsSources); ok {
return newsSources, nil
}
var sources []model.NewsSource
if err := s.db.WithContext(ctx).Joins("File").Find(&sources).Error; err != nil {
return nil, err
}
newsSourceCache.Add(CacheKeyAllNewsSources, sources)
s.newsSourceCache.Add(CacheKeyAllNewsSources, sources)
return sources, nil
}

Expand Down Expand Up @@ -89,7 +85,7 @@ func (s *CampusServer) ListNews(ctx context.Context, req *pb.ListNewsRequest) (*
func (s *CampusServer) getNews(ctx context.Context, sourceID int32, lastNewsID int32, oldestDateAt time.Time) ([]model.News, error) {
cacheKey := fmt.Sprintf("%d_%d_%d", sourceID, oldestDateAt.Second(), lastNewsID)

if news, ok := newsCache.Get(cacheKey); ok {
if news, ok := s.newsCache.Get(cacheKey); ok {
return news, nil
}

Expand All @@ -110,7 +106,7 @@ func (s *CampusServer) getNews(ctx context.Context, sourceID int32, lastNewsID i
if err := tx.Find(&news).Error; err != nil {
return nil, err
}
newsCache.Add(cacheKey, news)
s.newsCache.Add(cacheKey, news)
return news, nil
}

Expand Down
36 changes: 22 additions & 14 deletions server/backend/news_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ import (
"testing"
"time"

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

"google.golang.org/protobuf/types/known/timestamppb"

"github.com/DATA-DOG/go-sqlmock"
pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev"
"github.com/TUM-Dev/Campus-Backend/server/model"
"github.com/guregu/null"
"github.com/hashicorp/golang-lru/v2/expirable"
"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"
)
Expand Down Expand Up @@ -95,7 +94,7 @@ func (s *NewsSuite) Test_ListNewsSourcesMultiple() {
AddRow(s2.Source, s2.Title, s2.URL, s2.FileID, s2.Hook, s2.File.File, s2.File.Name, s2.File.Path, s2.File.Downloads, s2.File.URL, s2.File.Downloaded))

meta := metadata.MD{}
server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
server := s.getCampusTestServer()
response, err := server.ListNewsSources(metadata.NewIncomingContext(context.Background(), meta), nil)
require.NoError(s.T(), err)
expectedResp := &pb.ListNewsSourcesReply{
Expand Down Expand Up @@ -132,7 +131,7 @@ func (s *NewsSuite) Test_ListNewsSourcesNone() {
WillReturnRows(sqlmock.NewRows([]string{"source", "title", "url", "icon", "hook", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded"}))

meta := metadata.MD{}
server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
server := s.getCampusTestServer()
response, err := server.ListNewsSources(metadata.NewIncomingContext(context.Background(), meta), nil)
require.NoError(s.T(), err)
expectedResp := &pb.ListNewsSourcesReply{
Expand All @@ -149,7 +148,7 @@ func (s *NewsSuite) Test_ListNewsNone_withFilters() {
WillReturnRows(sqlmock.NewRows([]string{"news", "date", "created", "title", "description", "src", "link", "image", "file", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded", "source", "NewsSource__source", "NewsSource__title", "NewsSource__url", "NewsSource__icon", "NewsSource__hook", "NewsSource__File__file", "NewsSource__File__name", "NewsSource__File__path", "NewsSource__File__downloads", "NewsSource__File__url", "NewsSource__File__downloaded"}))

meta := metadata.NewIncomingContext(context.Background(), metadata.MD{})
server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
server := s.getCampusTestServer()
response, err := server.ListNews(meta, &pb.ListNewsRequest{NewsSource: 1, LastNewsId: 2})
require.NoError(s.T(), err)
require.Equal(s.T(), &pb.ListNewsReply{News: nil}, response)
Expand All @@ -159,7 +158,7 @@ func (s *NewsSuite) Test_ListNewsNone() {
WillReturnRows(sqlmock.NewRows([]string{"news", "date", "created", "title", "description", "src", "link", "image", "file", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded", "source", "NewsSource__source", "NewsSource__title", "NewsSource__url", "NewsSource__icon", "NewsSource__hook", "NewsSource__File__file", "NewsSource__File__name", "NewsSource__File__path", "NewsSource__File__downloads", "NewsSource__File__url", "NewsSource__File__downloaded"}))

meta := metadata.NewIncomingContext(context.Background(), metadata.MD{})
server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
server := s.getCampusTestServer()
response, err := server.ListNews(meta, &pb.ListNewsRequest{})
require.NoError(s.T(), err)
require.Equal(s.T(), &pb.ListNewsReply{News: nil}, response)
Expand All @@ -173,7 +172,7 @@ func (s *NewsSuite) Test_ListNewsMultiple() {
AddRow(n2.News, n2.Date, n2.Created, n2.Title, n2.Description, n2.NewsSourceID, n2.Link, n2.Image, nil, nil, nil, nil, nil, nil, nil))

meta := metadata.NewIncomingContext(context.Background(), metadata.MD{})
server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
server := s.getCampusTestServer()
response, err := server.ListNews(meta, &pb.ListNewsRequest{})
require.NoError(s.T(), err)
expectedResp := &pb.ListNewsReply{
Expand Down Expand Up @@ -227,23 +226,23 @@ func (s *NewsSuite) Test_ListNewsAlertsError() {
s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedListNewsAlertsQuery)).WillReturnError(gorm.ErrInvalidDB)

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

server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
server := s.getCampusTestServer()
response, err := server.ListNewsAlerts(metadata.NewIncomingContext(context.Background(), metadata.MD{}), &pb.ListNewsAlertsRequest{})
require.Equal(s.T(), status.Error(codes.NotFound, "no news alerts"), err)
require.Nil(s.T(), response)
}
func (s *NewsSuite) Test_ListNewsAlertsNone_Filter() {
s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedListNewsAlertsQuery + " AND news_alerts.news_alert > ?")).WithArgs(42).WillReturnError(gorm.ErrRecordNotFound)

server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
server := s.getCampusTestServer()
response, err := server.ListNewsAlerts(metadata.NewIncomingContext(context.Background(), metadata.MD{}), &pb.ListNewsAlertsRequest{LastNewsAlertId: 42})
require.Equal(s.T(), status.Error(codes.NotFound, "no news alerts"), err)
require.Nil(s.T(), response)
Expand All @@ -255,7 +254,7 @@ func (s *NewsSuite) Test_ListNewsAlertsMultiple() {
AddRow(a1.NewsAlert, a1.FileID, a1.Name, a1.Link, a1.Created, a1.From, a1.To, a1.File.File, a1.File.Name, a1.File.Path, a1.File.Downloads, a1.File.URL, a1.File.Downloaded).
AddRow(a2.NewsAlert, a2.FileID, a2.Name, a2.Link, a2.Created, a2.From, a2.To, a2.File.File, a2.File.Name, a2.File.Path, a2.File.Downloads, a2.File.URL, a2.File.Downloaded))

server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
server := s.getCampusTestServer()
response, err := server.ListNewsAlerts(metadata.NewIncomingContext(context.Background(), metadata.MD{}), &pb.ListNewsAlertsRequest{})
require.NoError(s.T(), err)
expectedResp := &pb.ListNewsAlertsReply{
Expand All @@ -275,3 +274,12 @@ func (s *NewsSuite) AfterTest(_, _ string) {
func TestNewsSuite(t *testing.T) {
suite.Run(t, new(NewsSuite))
}

func (s *NewsSuite) getCampusTestServer() *CampusServer {
return &CampusServer{
db: s.DB,
deviceBuf: s.deviceBuf,
newsSourceCache: expirable.NewLRU[string, []model.NewsSource](1, nil, time.Hour*6),
newsCache: expirable.NewLRU[string, []model.News](1024, nil, time.Minute*30),
}
}
7 changes: 7 additions & 0 deletions server/backend/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package backend

import (
"sync"
"time"

pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev"
"github.com/TUM-Dev/Campus-Backend/server/model"
"github.com/hashicorp/golang-lru/v2/expirable"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
Expand All @@ -13,6 +16,8 @@ type CampusServer struct {
feedbackEmailLastReuestAt *sync.Map
db *gorm.DB
deviceBuf *deviceBuffer // deviceBuf stores all devices from recent request and flushes them to db
newsSourceCache *expirable.LRU[string, []model.NewsSource]
newsCache *expirable.LRU[string, []model.News]
}

// Verify that CampusServer implements the pb.CampusServer interface
Expand All @@ -24,5 +29,7 @@ func New(db *gorm.DB) *CampusServer {
db: db,
deviceBuf: newDeviceBuffer(),
feedbackEmailLastReuestAt: &sync.Map{},
newsSourceCache: expirable.NewLRU[string, []model.NewsSource](1, nil, time.Hour*6),
newsCache: expirable.NewLRU[string, []model.News](1024, nil, time.Minute*30),
}
}
2 changes: 1 addition & 1 deletion server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/gofrs/uuid/v5 v5.3.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0
github.com/guregu/null v4.0.0+incompatible
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/makasim/sentryhook v0.5.0
github.com/microcosm-cc/bluemonday v1.0.27
github.com/mmcdole/gofeed v1.3.0
Expand Down Expand Up @@ -58,7 +59,6 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/css v1.0.1 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down

0 comments on commit 2932b80

Please sign in to comment.