Skip to content

Commit

Permalink
chore:news testing (#216)
Browse files Browse the repository at this point in the history
* renamed the response to response to be consisntent across devices and moved all news entries to a separate file for better testing

* added a testcases for `GetTopNews` and `GetNewsSources`
  • Loading branch information
CommanderStorm authored Sep 13, 2023
1 parent f67c9bb commit 9eccc47
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 105 deletions.
30 changes: 15 additions & 15 deletions server/api/CampusService.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/api/CampusService.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions server/api/CampusService.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ service Campus {
};
}

rpc GetNewsSources (google.protobuf.Empty) returns (NewsSourceArray) {
rpc GetNewsSources (google.protobuf.Empty) returns (NewsSourceReply) {
option (google.api.http) = {
get: "/news/sources",
response_body: "sources"
Expand Down Expand Up @@ -376,7 +376,7 @@ message Room {
string name = 9;
}

message NewsSourceArray {
message NewsSourceReply {
repeated NewsSource sources = 1;
}

Expand Down
10 changes: 5 additions & 5 deletions server/api/CampusService_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/api/gen/openapiv2/CampusService.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1925,7 +1925,7 @@
}
}
},
"apiNewsSourceArray": {
"apiNewsSourceReply": {
"type": "object",
"properties": {
"sources": {
Expand Down
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
61 changes: 61 additions & 0 deletions server/backend/news.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package backend

import (
"context"
"errors"
"fmt"
pb "github.com/TUM-Dev/Campus-Backend/server/api"
"github.com/TUM-Dev/Campus-Backend/server/model"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/timestamppb"
"gorm.io/gorm"
)

func (s *CampusServer) GetNewsSources(ctx context.Context, _ *emptypb.Empty) (newsSources *pb.NewsSourceReply, err error) {
if err = s.checkDevice(ctx); err != nil {
return
}

var sources []model.NewsSource
if err := s.db.Joins("Files").Find(&sources).Error; err != nil {
log.WithError(err).Error("could not find newsSources")
return nil, status.Error(codes.Internal, "could not GetNewsSources")
}

var resp []*pb.NewsSource
for _, source := range sources {
log.WithField("title", source.Title).Trace("sending news source")
resp = append(resp, &pb.NewsSource{
Source: fmt.Sprintf("%d", source.Source),
Title: source.Title,
Icon: source.Files.URL.String,
})
}
return &pb.NewsSourceReply{Sources: resp}, nil
}

func (s *CampusServer) GetTopNews(ctx context.Context, _ *emptypb.Empty) (*pb.GetTopNewsReply, error) {
if err := s.checkDevice(ctx); err != nil {
return nil, err
}

var res *model.NewsAlert
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{
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
}
Loading

0 comments on commit 9eccc47

Please sign in to comment.