Skip to content

Commit

Permalink
added a test that the feedback is saved to the db as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Sep 3, 2023
1 parent 436221b commit b422ca5
Show file tree
Hide file tree
Showing 5 changed files with 94 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
80 changes: 80 additions & 0 deletions server/backend/feedback_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package backend

import (
"context"
"database/sql"
"github.com/DATA-DOG/go-sqlmock"
pb "github.com/TUM-Dev/Campus-Backend/server/api"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/types/known/emptypb"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"regexp"
"testing"
"time"
)

type FeedbackSuite struct {
suite.Suite
DB *gorm.DB
mock sqlmock.Sqlmock
deviceBuf *deviceBuffer
}

func (s *FeedbackSuite) 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 (s *FeedbackSuite) Test_SendFeedback() {
req := &pb.SendFeedbackRequest{
Topic: "tca",
Email: "[email protected]",
EmailId: "magic-id",
Message: "This is a Test",
ImageCount: 1,
Latitude: 0,
Longitude: 0,
OsVersion: "Android 10.0",
AppVersion: "TCA 10.2",
}
s.mock.ExpectBegin()
s.mock.ExpectQuery(regexp.QuoteMeta(
"INSERT INTO `feedback` (`image_count`,`email_id`,`receiver`,`reply_to`,`feedback`,`latitude`,`longitude`,`os_version`,`app_version`,`processed`) VALUES (?,?,?,?,?,?,?,?,?,?) RETURNING `timestamp`,`id`")).
WithArgs(req.ImageCount, req.EmailId, "[email protected]", req.Email, req.Message, req.Latitude, req.Longitude, req.OsVersion, req.AppVersion, false).
WillReturnRows(sqlmock.NewRows([]string{"timestamp", "id"}).AddRow(time.Time{}, 1))
s.mock.ExpectCommit()

meta := metadata.MD{}
server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf}
response, err := server.SendFeedback(metadata.NewIncomingContext(context.Background(), meta), req)
require.NoError(s.T(), err)
require.Equal(s.T(), &emptypb.Empty{}, response)
}
func (s *FeedbackSuite) 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(FeedbackSuite))
}
10 changes: 2 additions & 8 deletions server/backend/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"
"gorm.io/gorm"
"net"
"sync"
"time"
)

func (s *CampusServer) GRPCServe(l net.Listener) error {
Expand All @@ -44,12 +42,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.18

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
@@ -1,3 +1,5 @@
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/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U=
github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
Expand Down

0 comments on commit b422ca5

Please sign in to comment.