From 58e5eea8a0234c5c0a0c89b44a4e958de37326f1 Mon Sep 17 00:00:00 2001 From: Tobias Jungmann Date: Mon, 16 Oct 2023 19:03:34 +0200 Subject: [PATCH 1/8] correct timestamp row mock --- server/backend/cafeteria_test.go | 145 +++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 server/backend/cafeteria_test.go diff --git a/server/backend/cafeteria_test.go b/server/backend/cafeteria_test.go new file mode 100644 index 00000000..ccc16d89 --- /dev/null +++ b/server/backend/cafeteria_test.go @@ -0,0 +1,145 @@ +package backend + +import ( + "context" + "database/sql" + pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev" + log "github.com/sirupsen/logrus" + "google.golang.org/grpc/metadata" + "regexp" + "testing" + "time" + + "github.com/DATA-DOG/go-sqlmock" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + "gorm.io/driver/mysql" + "gorm.io/gorm" +) + +type CafeteriaSuite struct { + suite.Suite + DB *gorm.DB + mock sqlmock.Sqlmock + deviceBuf *deviceBuffer +} + +func (s *CafeteriaSuite) 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() +} + +const ExpectedGetCafeteriaQuery = "SELECT * FROM `canteen_head_count` WHERE `canteen_head_count`.`canteen_id` = ? ORDER BY `canteen_head_count`.`canteen_id` LIMIT 1" + +//"SELECT * FROM `update_note` WHERE `update_note`.`version_code` = ? ORDER BY `update_note`.`version_code` LIMIT 1" + +func (s *CafeteriaSuite) Test_GetCafeteriaHeadCount() { + //inputString := "2023-10-09 11:45:22" + + // Define a custom layout that matches the input string format + //layout := "2006-01-02 15:04:05" + test := time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local) + + // Parse the input string into a time.Time object + //t, err := time.Parse(layout, inputString) + /*timeWrapper := timestamp.Timestamp{ + Seconds: t.Unix(), // The number of seconds since January 1, 1970 + Nanos: int32(t.Nanosecond()), // The nanoseconds part + }*/ + s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetCafeteriaQuery)). + WithArgs("mensa-garching"). + WillReturnRows(sqlmock.NewRows([]string{"canteen_id", "count", "max_count", "percent", "timestamp"}). + AddRow("mensa-garching", 0, 1000, 0, test)) + /* + all expectations were already fulfilled, call to Query 'SELECT * FROM `canteen_head_count` WHERE `canteen_head_count`.`canteen_id` = ? ORDER BY `canteen_head_count`.`canteen_id` LIMIT 1' + */ + /* + meta := metadata.MD{} + server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} + response, err := server.GetCafeteria(metadata.NewIncomingContext(context.Background(), meta), &pb.GetCafeteriaRequest{Version: 1}) + require.NoError(s.T(), err) + expectedResp := &pb.GetCafeteriaReply{ + Message: "Test Message", + VersionName: "1.0.0", + }*/ + meta := metadata.MD{} + server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} + response, err := server.GetCanteenHeadCount(metadata.NewIncomingContext(context.Background(), meta), + &pb.GetCanteenHeadCountRequest{ + CanteenId: "mensa-garching", + }, + ) + require.NoError(s.T(), err) + // require.NoError(s.T(), err) + if err != nil { + log.WithError(err).Error("Canteen HeadCount data request failed.") + } else { + log.WithField("res", response).Info("Canteen HeadCount data request successful.") + } + // require.Equal(s.T(), expectedResp, response) +} + +/* +meta := metadata.MD{} + + server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} + res, err := server.GetCanteenHeadCount(ctx, &pb.GetCanteenHeadCountRequest{ + CanteenId: "mensa-garching", + }) + + if err != nil { + log.WithError(err).Error("Canteen HeadCount data request failed.") + } else { + log.WithField("res", res).Info("Canteen HeadCount data request successful.") + } + require.Equal(s.T(), expectedResp, response) + } + +/* + + func (s *CafeteriaSuite) Test_GetCafeteriaNone() { + s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetCafeteriaQuery)). + WillReturnRows(sqlmock.NewRows([]string{"version_code", "version_name", "message"})) + + meta := metadata.MD{} + server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} + response, err := server.GetCafeteria(metadata.NewIncomingContext(context.Background(), meta), &pb.GetCafeteriaRequest{Version: 1}) + require.Equal(s.T(), status.Error(codes.NotFound, "No update note found"), err) + require.Nil(s.T(), response) + } + + func (s *CafeteriaSuite) Test_GetCafeteriaError() { + s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetCafeteriaQuery)).WillReturnError(gorm.ErrInvalidDB) + + meta := metadata.MD{} + server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} + response, err := server.GetCafeteria(metadata.NewIncomingContext(context.Background(), meta), &pb.GetCafeteriaRequest{Version: 1}) + require.Equal(s.T(), status.Error(codes.Internal, "Internal server error"), err) + require.Nil(s.T(), response) + } +*/ +func (s *CafeteriaSuite) 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 TestCafeteriaSuite(t *testing.T) { + suite.Run(t, new(CafeteriaSuite)) +} From d6e912553a1e41b3b76e3ff0a0c803bf9c0a64b6 Mon Sep 17 00:00:00 2001 From: Tobias Jungmann Date: Wed, 25 Oct 2023 17:32:32 +0200 Subject: [PATCH 2/8] cafeteria rating create test frame --- server/backend/cafeteria_test.go | 120 +++++++++--------- .../backend/test_data}/sampleimage.jpeg | Bin 2 files changed, 58 insertions(+), 62 deletions(-) rename {client/local/images => server/backend/test_data}/sampleimage.jpeg (100%) diff --git a/server/backend/cafeteria_test.go b/server/backend/cafeteria_test.go index ccc16d89..79816e65 100644 --- a/server/backend/cafeteria_test.go +++ b/server/backend/cafeteria_test.go @@ -17,6 +17,10 @@ import ( "gorm.io/gorm" ) +const ( + testImage = "./test_data/sampleimage.jpeg" +) + type CafeteriaSuite struct { suite.Suite DB *gorm.DB @@ -47,37 +51,12 @@ func (s *CafeteriaSuite) SetupSuite() { const ExpectedGetCafeteriaQuery = "SELECT * FROM `canteen_head_count` WHERE `canteen_head_count`.`canteen_id` = ? ORDER BY `canteen_head_count`.`canteen_id` LIMIT 1" -//"SELECT * FROM `update_note` WHERE `update_note`.`version_code` = ? ORDER BY `update_note`.`version_code` LIMIT 1" - func (s *CafeteriaSuite) Test_GetCafeteriaHeadCount() { - //inputString := "2023-10-09 11:45:22" - - // Define a custom layout that matches the input string format - //layout := "2006-01-02 15:04:05" - test := time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local) - - // Parse the input string into a time.Time object - //t, err := time.Parse(layout, inputString) - /*timeWrapper := timestamp.Timestamp{ - Seconds: t.Unix(), // The number of seconds since January 1, 1970 - Nanos: int32(t.Nanosecond()), // The nanoseconds part - }*/ s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetCafeteriaQuery)). WithArgs("mensa-garching"). WillReturnRows(sqlmock.NewRows([]string{"canteen_id", "count", "max_count", "percent", "timestamp"}). - AddRow("mensa-garching", 0, 1000, 0, test)) - /* - all expectations were already fulfilled, call to Query 'SELECT * FROM `canteen_head_count` WHERE `canteen_head_count`.`canteen_id` = ? ORDER BY `canteen_head_count`.`canteen_id` LIMIT 1' - */ - /* - meta := metadata.MD{} - server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} - response, err := server.GetCafeteria(metadata.NewIncomingContext(context.Background(), meta), &pb.GetCafeteriaRequest{Version: 1}) - require.NoError(s.T(), err) - expectedResp := &pb.GetCafeteriaReply{ - Message: "Test Message", - VersionName: "1.0.0", - }*/ + AddRow("mensa-garching", 0, 1000, 0, time.Date(2023, 10, 9, 11, 45, 22, 0, time.Local))) + meta := metadata.MD{} server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} response, err := server.GetCanteenHeadCount(metadata.NewIncomingContext(context.Background(), meta), @@ -86,54 +65,71 @@ func (s *CafeteriaSuite) Test_GetCafeteriaHeadCount() { }, ) require.NoError(s.T(), err) - // require.NoError(s.T(), err) if err != nil { log.WithError(err).Error("Canteen HeadCount data request failed.") + //todo compare results require.Equal(s.T(), status.Error(codes.NotFound, "No update note found"), err) } else { log.WithField("res", response).Info("Canteen HeadCount data request successful.") } - // require.Equal(s.T(), expectedResp, response) } -/* -meta := metadata.MD{} - - server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} - res, err := server.GetCanteenHeadCount(ctx, &pb.GetCanteenHeadCountRequest{ - CanteenId: "mensa-garching", - }) - - if err != nil { - log.WithError(err).Error("Canteen HeadCount data request failed.") - } else { - log.WithField("res", res).Info("Canteen HeadCount data request successful.") - } - require.Equal(s.T(), expectedResp, response) - } - -/* +// const ExpectedGetCafeteriaQuery = "SELECT * FROM `canteen_head_count` WHERE `canteen_head_count`.`canteen_id` = ? ORDER BY `canteen_head_count`.`canteen_id` LIMIT 1" +const ExpectedGetCafeteriaByName = "SELECT * FROM `cafeteria` WHERE name LIKE ? ORDER BY `cafeteria`.`cafeteria` LIMIT 1" - func (s *CafeteriaSuite) Test_GetCafeteriaNone() { - s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetCafeteriaQuery)). - WillReturnRows(sqlmock.NewRows([]string{"version_code", "version_name", "message"})) +func (s *CafeteriaSuite) Test_CreateCanteenRating() { + canteenName := "mensa-garching" + s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetCafeteriaByName)). + WithArgs(canteenName). + WillReturnRows(sqlmock.NewRows([]string{"cafeteria", "name", "address", "latitude", "longitude"}). + AddRow(2, "mensa-garching", "Boltzmannstraße 19, Garching", 48.2681, 11.6723)) - meta := metadata.MD{} - server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} - response, err := server.GetCafeteria(metadata.NewIncomingContext(context.Background(), meta), &pb.GetCafeteriaRequest{Version: 1}) - require.Equal(s.T(), status.Error(codes.NotFound, "No update note found"), err) - require.Nil(s.T(), response) + meta := metadata.MD{} + rating := generateCanteenRating(canteenName, 2, s) + server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} + response, err := server.CreateCanteenRating(metadata.NewIncomingContext(context.Background(), meta), + &rating, + ) + require.NoError(s.T(), err) + if err != nil { + log.WithError(err).Error("Canteen HeadCount data request failed.") + //todo compare results require.Equal(s.T(), status.Error(codes.NotFound, "No update note found"), err) + } else { + log.WithField("res", response).Info("Canteen HeadCount data request successful.") } +} - func (s *CafeteriaSuite) Test_GetCafeteriaError() { - s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetCafeteriaQuery)).WillReturnError(gorm.ErrInvalidDB) +func generateCanteenRating(canteen string, rating int32, s *CafeteriaSuite) pb.CreateCanteenRatingRequest { + canteen_id := 1 + image_name_path := "testimage.txt" + //dummyImage := createDummyImage(s.T(), 10, 10) + s.mock.ExpectBegin() + /*s.mock.ExpectQuery(regexp.QuoteMeta("INSERT INTO `cafeteria_rating` (`cafeteriaRating`,`points`,`comment`,`cafeteriaID`,`timestamp`,`image`) VALUES (?,?,?,?,?,?) RETURNING `cafeteriaRating`,`points`,`comment`,`cafeteriaID`,`timestamp`,`image`")). + WithArgs(1, 2, "custom comment", canteen_id,time.Date(2023, 10, 9, 11, 45, 22, 0, time.Local),image_name_path). + WillReturnRows(sqlmock.NewRows([]string{"url", "file"}).AddRow(nil, 1)) + */ + s.mock.ExpectQuery(regexp.QuoteMeta("INSERT INTO `cafeteria_rating` (`cafeteriaRating`,`points`,`comment`,`cafeteriaID`,`timestamp`,`image`) VALUES (?,?,?,?,?,?)")). + WithArgs(1, 2, "custom comment", canteen_id, time.Date(2023, 10, 9, 11, 45, 22, 0, time.Local), image_name_path) + s.mock.ExpectCommit() + + y := make([]*pb.RatingTag, 2) + y[0] = &pb.RatingTag{ + Points: float64(1 + rating), + TagId: 1, + } + y[1] = &pb.RatingTag{ + Points: float64(2 + rating), + TagId: 2, + } - meta := metadata.MD{} - server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} - response, err := server.GetCafeteria(metadata.NewIncomingContext(context.Background(), meta), &pb.GetCafeteriaRequest{Version: 1}) - require.Equal(s.T(), status.Error(codes.Internal, "Internal server error"), err) - require.Nil(s.T(), response) + return pb.CreateCanteenRatingRequest{ + Points: rating, + CanteenId: canteen, + Comment: "Everything perfect, 2 Stars", + RatingTags: y, + Image: getImageToBytes(testImage), } -*/ +} + func (s *CafeteriaSuite) AfterTest(_, _ string) { require.NoError(s.T(), s.mock.ExpectationsWereMet()) } diff --git a/client/local/images/sampleimage.jpeg b/server/backend/test_data/sampleimage.jpeg similarity index 100% rename from client/local/images/sampleimage.jpeg rename to server/backend/test_data/sampleimage.jpeg From b57b054accc5a8ab408f664c4dc52a201baae1a1 Mon Sep 17 00:00:00 2001 From: Tobias Jungmann Date: Thu, 26 Oct 2023 09:27:29 +0200 Subject: [PATCH 3/8] datatype test --- server/backend/cafeteria_test.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/server/backend/cafeteria_test.go b/server/backend/cafeteria_test.go index 79816e65..40734fdc 100644 --- a/server/backend/cafeteria_test.go +++ b/server/backend/cafeteria_test.go @@ -99,16 +99,21 @@ func (s *CafeteriaSuite) Test_CreateCanteenRating() { } func generateCanteenRating(canteen string, rating int32, s *CafeteriaSuite) pb.CreateCanteenRatingRequest { - canteen_id := 1 - image_name_path := "testimage.txt" + //canteen_id := 1 + image_name_path := "" // "testimage.txt" //dummyImage := createDummyImage(s.T(), 10, 10) s.mock.ExpectBegin() /*s.mock.ExpectQuery(regexp.QuoteMeta("INSERT INTO `cafeteria_rating` (`cafeteriaRating`,`points`,`comment`,`cafeteriaID`,`timestamp`,`image`) VALUES (?,?,?,?,?,?) RETURNING `cafeteriaRating`,`points`,`comment`,`cafeteriaID`,`timestamp`,`image`")). WithArgs(1, 2, "custom comment", canteen_id,time.Date(2023, 10, 9, 11, 45, 22, 0, time.Local),image_name_path). WillReturnRows(sqlmock.NewRows([]string{"url", "file"}).AddRow(nil, 1)) */ - s.mock.ExpectQuery(regexp.QuoteMeta("INSERT INTO `cafeteria_rating` (`cafeteriaRating`,`points`,`comment`,`cafeteriaID`,`timestamp`,`image`) VALUES (?,?,?,?,?,?)")). - WithArgs(1, 2, "custom comment", canteen_id, time.Date(2023, 10, 9, 11, 45, 22, 0, time.Local), image_name_path) + // var help int32 = 3 + //todo with timestamp s.mock.ExpectExec(regexp.QuoteMeta("INSERT INTO `cafeteria_rating` (`points`,`comment`,`cafeteriaID`,`timestamp`,`image`) VALUES (?,?,?,?,?) RETURNING `cafeteriaRating`")). + // WithArgs(rating, "Everything perfect, 2 Star", canteen, time.Now(), image_name_path) + s.mock.ExpectExec(regexp.QuoteMeta("INSERT INTO `cafeteria_rating` (`points`,`comment`,`cafeteriaID`,`image`) VALUES (?,?,?,?) RETURNING `cafeteriaRating`")). + WithArgs(rating, "Everything perfect, 2 Star", canteen, image_name_path) + //WillReturnRows(sqlmock.NewRows([]string{"cafeteria"}). + // AddRow(2)) s.mock.ExpectCommit() y := make([]*pb.RatingTag, 2) From 38869b5869c07e819c054012b6edd454414a2043 Mon Sep 17 00:00:00 2001 From: Tobias Jungmann Date: Thu, 26 Oct 2023 09:36:58 +0200 Subject: [PATCH 4/8] variable filepath for cafeteria images --- server/backend/cafeteria.go | 4 +++- server/backend/cafeteria_test.go | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/server/backend/cafeteria.go b/server/backend/cafeteria.go index 3fa7a355..09146621 100644 --- a/server/backend/cafeteria.go +++ b/server/backend/cafeteria.go @@ -34,6 +34,8 @@ const ( NAME ModelType = 3 ) +var StorageDir = "/Storage/rating/" + // GetCafeteriaRatings RPC Endpoint // Allows to query ratings for a specific cafeteria. // It returns the average rating, max/min rating as well as a number of actual ratings and the average ratings for @@ -389,7 +391,7 @@ func imageWrapper(image []byte, path string, id int64) string { var resPath = "" if len(image) > 0 { var resError error - path := fmt.Sprintf("%s%s%s%d%s", "/Storage/rating/", path, "/", id, "/") + path := fmt.Sprintf("%s%s%s%d%s", StorageDir, path, "/", id, "/") resPath, resError = storeImage(path, image) if resError != nil { diff --git a/server/backend/cafeteria_test.go b/server/backend/cafeteria_test.go index 40734fdc..bba09c73 100644 --- a/server/backend/cafeteria_test.go +++ b/server/backend/cafeteria_test.go @@ -77,6 +77,7 @@ func (s *CafeteriaSuite) Test_GetCafeteriaHeadCount() { const ExpectedGetCafeteriaByName = "SELECT * FROM `cafeteria` WHERE name LIKE ? ORDER BY `cafeteria`.`cafeteria` LIMIT 1" func (s *CafeteriaSuite) Test_CreateCanteenRating() { + StorageDir = "." canteenName := "mensa-garching" s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetCafeteriaByName)). WithArgs(canteenName). From 3f7b943e5b41c621e4f5137a7f102dae18fc0054 Mon Sep 17 00:00:00 2001 From: Tobias Jungmann Date: Thu, 26 Oct 2023 13:13:10 +0200 Subject: [PATCH 5/8] successfully insert cafeteris --- server/backend/cafeteria_test.go | 49 ++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/server/backend/cafeteria_test.go b/server/backend/cafeteria_test.go index bba09c73..b56dde06 100644 --- a/server/backend/cafeteria_test.go +++ b/server/backend/cafeteria_test.go @@ -73,19 +73,36 @@ func (s *CafeteriaSuite) Test_GetCafeteriaHeadCount() { } } -// const ExpectedGetCafeteriaQuery = "SELECT * FROM `canteen_head_count` WHERE `canteen_head_count`.`canteen_id` = ? ORDER BY `canteen_head_count`.`canteen_id` LIMIT 1" const ExpectedGetCafeteriaByName = "SELECT * FROM `cafeteria` WHERE name LIKE ? ORDER BY `cafeteria`.`cafeteria` LIMIT 1" func (s *CafeteriaSuite) Test_CreateCanteenRating() { StorageDir = "." canteenName := "mensa-garching" + cafeteriaID := 0 + comment := "Everything perfect, 2 Stars" + image_name_path := ".cafeterias/0/c73f6461a5ae03bb935b54a27f12e0a5.jpeg" // "testimage.txt" + var ratingValue int32 = 2 + + // s.mock.ExpectBegin() s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetCafeteriaByName)). WithArgs(canteenName). WillReturnRows(sqlmock.NewRows([]string{"cafeteria", "name", "address", "latitude", "longitude"}). - AddRow(2, "mensa-garching", "Boltzmannstraße 19, Garching", 48.2681, 11.6723)) + AddRow(cafeteriaID, "mensa-garching", "Boltzmannstraße 19, Garching", 48.2681, 11.6723)) + s.mock.ExpectBegin() + s.mock.ExpectQuery(regexp.QuoteMeta("INSERT INTO `cafeteria_rating` (`points`,`comment`,`cafeteriaID`,`timestamp`,`image`) VALUES (?,?,?,?,?) RETURNING `cafeteriaRating`")). + WithArgs(ratingValue, comment, cafeteriaID, sqlmock.AnyArg(), image_name_path). + WillReturnRows(sqlmock.NewRows([]string{"cafeteriaRating", "points", "comment", "cafeteriaID", "timestamp", "image"}). + AddRow(0, ratingValue, comment, cafeteriaID, time.Date(2023, 10, 9, 11, 45, 22, 0, time.Local), image_name_path)) + + /* todo current exec s.mock.ExpectExec(regexp.QuoteMeta("INSERT INTO `cafeteria_rating` (`points`,`comment`,`cafeteriaID`,`timestamp`,`image`) VALUES (?,?,?,?,?) RETURNING `cafeteriaRating`")). + WithArgs(ratingValue, "Everything perfect, 2 Star", canteenName, sqlmock.AnyArg(), image_name_path). + WillReturnResult(sqlmock.NewResult(1, 1)) + */ + s.mock.ExpectCommit() meta := metadata.MD{} - rating := generateCanteenRating(canteenName, 2, s) + rating := generateCanteenRating(canteenName, ratingValue, s, comment) + server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} response, err := server.CreateCanteenRating(metadata.NewIncomingContext(context.Background(), meta), &rating, @@ -99,23 +116,31 @@ func (s *CafeteriaSuite) Test_CreateCanteenRating() { } } -func generateCanteenRating(canteen string, rating int32, s *CafeteriaSuite) pb.CreateCanteenRatingRequest { +func generateCanteenRating(canteen string, rating int32, s *CafeteriaSuite, comment string) pb.CreateCanteenRatingRequest { //canteen_id := 1 - image_name_path := "" // "testimage.txt" + //dummyImage := createDummyImage(s.T(), 10, 10) - s.mock.ExpectBegin() + //s.mock.ExpectBegin() /*s.mock.ExpectQuery(regexp.QuoteMeta("INSERT INTO `cafeteria_rating` (`cafeteriaRating`,`points`,`comment`,`cafeteriaID`,`timestamp`,`image`) VALUES (?,?,?,?,?,?) RETURNING `cafeteriaRating`,`points`,`comment`,`cafeteriaID`,`timestamp`,`image`")). WithArgs(1, 2, "custom comment", canteen_id,time.Date(2023, 10, 9, 11, 45, 22, 0, time.Local),image_name_path). WillReturnRows(sqlmock.NewRows([]string{"url", "file"}).AddRow(nil, 1)) */ // var help int32 = 3 - //todo with timestamp s.mock.ExpectExec(regexp.QuoteMeta("INSERT INTO `cafeteria_rating` (`points`,`comment`,`cafeteriaID`,`timestamp`,`image`) VALUES (?,?,?,?,?) RETURNING `cafeteriaRating`")). - // WithArgs(rating, "Everything perfect, 2 Star", canteen, time.Now(), image_name_path) - s.mock.ExpectExec(regexp.QuoteMeta("INSERT INTO `cafeteria_rating` (`points`,`comment`,`cafeteriaID`,`image`) VALUES (?,?,?,?) RETURNING `cafeteriaRating`")). - WithArgs(rating, "Everything perfect, 2 Star", canteen, image_name_path) + /* s.mock.ExpectExec(regexp.QuoteMeta("INSERT INTO `cafeteria_rating` (`points`,`comment`,`cafeteriaID`,`timestamp`,`image`) VALUES (?,?,?,?,?) RETURNING `cafeteriaRating`")). + WithArgs(rating, "Everything perfect, 2 Star", canteen, sqlmock.AnyArg(), image_name_path). + WillReturnResult(sqlmock.NewResult(1, 1))*/ + + /* + s.mock.ExpectQuery(regexp.QuoteMeta("INSERT INTO `files` (`name`,`path`,`downloads`,`downloaded`) VALUES (?,?,?,?) RETURNING `url`,`file`")). + WithArgs("0.txt", sqlmock.AnyArg(), 1, true). + WillReturnRows(sqlmock.NewRows([]string{"url", "file"}).AddRow(nil, 1)) + */ + + /* s.mock.ExpectExec(regexp.QuoteMeta("INSERT INTO `cafeteria_rating` (`points`,`comment`,`cafeteriaID`,`image`) VALUES (?,?,?,?) RETURNING `cafeteriaRating`")). + WithArgs(rating, "Everything perfect, 2 Star", canteen, image_name_path)*/ //WillReturnRows(sqlmock.NewRows([]string{"cafeteria"}). // AddRow(2)) - s.mock.ExpectCommit() + //s.mock.ExpectCommit() y := make([]*pb.RatingTag, 2) y[0] = &pb.RatingTag{ @@ -130,7 +155,7 @@ func generateCanteenRating(canteen string, rating int32, s *CafeteriaSuite) pb.C return pb.CreateCanteenRatingRequest{ Points: rating, CanteenId: canteen, - Comment: "Everything perfect, 2 Stars", + Comment: comment, RatingTags: y, Image: getImageToBytes(testImage), } From 8d890978763c6ec90dab8c296db75a2efe930438 Mon Sep 17 00:00:00 2001 From: Tobias Jungmann Date: Thu, 26 Oct 2023 14:30:51 +0200 Subject: [PATCH 6/8] prepare tag rating insertion --- server/backend/cafeteria_test.go | 65 +++++++++++++------------------- 1 file changed, 26 insertions(+), 39 deletions(-) diff --git a/server/backend/cafeteria_test.go b/server/backend/cafeteria_test.go index b56dde06..c0fa6f07 100644 --- a/server/backend/cafeteria_test.go +++ b/server/backend/cafeteria_test.go @@ -80,6 +80,7 @@ func (s *CafeteriaSuite) Test_CreateCanteenRating() { canteenName := "mensa-garching" cafeteriaID := 0 comment := "Everything perfect, 2 Stars" + ratingID := 0 image_name_path := ".cafeterias/0/c73f6461a5ae03bb935b54a27f12e0a5.jpeg" // "testimage.txt" var ratingValue int32 = 2 @@ -88,16 +89,12 @@ func (s *CafeteriaSuite) Test_CreateCanteenRating() { WithArgs(canteenName). WillReturnRows(sqlmock.NewRows([]string{"cafeteria", "name", "address", "latitude", "longitude"}). AddRow(cafeteriaID, "mensa-garching", "Boltzmannstraße 19, Garching", 48.2681, 11.6723)) + s.mock.ExpectBegin() s.mock.ExpectQuery(regexp.QuoteMeta("INSERT INTO `cafeteria_rating` (`points`,`comment`,`cafeteriaID`,`timestamp`,`image`) VALUES (?,?,?,?,?) RETURNING `cafeteriaRating`")). WithArgs(ratingValue, comment, cafeteriaID, sqlmock.AnyArg(), image_name_path). WillReturnRows(sqlmock.NewRows([]string{"cafeteriaRating", "points", "comment", "cafeteriaID", "timestamp", "image"}). - AddRow(0, ratingValue, comment, cafeteriaID, time.Date(2023, 10, 9, 11, 45, 22, 0, time.Local), image_name_path)) - - /* todo current exec s.mock.ExpectExec(regexp.QuoteMeta("INSERT INTO `cafeteria_rating` (`points`,`comment`,`cafeteriaID`,`timestamp`,`image`) VALUES (?,?,?,?,?) RETURNING `cafeteriaRating`")). - WithArgs(ratingValue, "Everything perfect, 2 Star", canteenName, sqlmock.AnyArg(), image_name_path). - WillReturnResult(sqlmock.NewResult(1, 1)) - */ + AddRow(ratingID, ratingValue, comment, cafeteriaID, time.Date(2023, 10, 9, 11, 45, 22, 0, time.Local), image_name_path)) s.mock.ExpectCommit() meta := metadata.MD{} @@ -117,40 +114,11 @@ func (s *CafeteriaSuite) Test_CreateCanteenRating() { } func generateCanteenRating(canteen string, rating int32, s *CafeteriaSuite, comment string) pb.CreateCanteenRatingRequest { - //canteen_id := 1 - - //dummyImage := createDummyImage(s.T(), 10, 10) - //s.mock.ExpectBegin() - /*s.mock.ExpectQuery(regexp.QuoteMeta("INSERT INTO `cafeteria_rating` (`cafeteriaRating`,`points`,`comment`,`cafeteriaID`,`timestamp`,`image`) VALUES (?,?,?,?,?,?) RETURNING `cafeteriaRating`,`points`,`comment`,`cafeteriaID`,`timestamp`,`image`")). - WithArgs(1, 2, "custom comment", canteen_id,time.Date(2023, 10, 9, 11, 45, 22, 0, time.Local),image_name_path). - WillReturnRows(sqlmock.NewRows([]string{"url", "file"}).AddRow(nil, 1)) - */ - // var help int32 = 3 - /* s.mock.ExpectExec(regexp.QuoteMeta("INSERT INTO `cafeteria_rating` (`points`,`comment`,`cafeteriaID`,`timestamp`,`image`) VALUES (?,?,?,?,?) RETURNING `cafeteriaRating`")). - WithArgs(rating, "Everything perfect, 2 Star", canteen, sqlmock.AnyArg(), image_name_path). - WillReturnResult(sqlmock.NewResult(1, 1))*/ - - /* - s.mock.ExpectQuery(regexp.QuoteMeta("INSERT INTO `files` (`name`,`path`,`downloads`,`downloaded`) VALUES (?,?,?,?) RETURNING `url`,`file`")). - WithArgs("0.txt", sqlmock.AnyArg(), 1, true). - WillReturnRows(sqlmock.NewRows([]string{"url", "file"}).AddRow(nil, 1)) - */ - - /* s.mock.ExpectExec(regexp.QuoteMeta("INSERT INTO `cafeteria_rating` (`points`,`comment`,`cafeteriaID`,`image`) VALUES (?,?,?,?) RETURNING `cafeteriaRating`")). - WithArgs(rating, "Everything perfect, 2 Star", canteen, image_name_path)*/ - //WillReturnRows(sqlmock.NewRows([]string{"cafeteria"}). - // AddRow(2)) - //s.mock.ExpectCommit() - y := make([]*pb.RatingTag, 2) - y[0] = &pb.RatingTag{ - Points: float64(1 + rating), - TagId: 1, - } - y[1] = &pb.RatingTag{ - Points: float64(2 + rating), - TagId: 2, - } + var myRating = prepareTagRating(s, 1, 0, 1, 1) + y[0] = &myRating + myRating = prepareTagRating(s, 2, 0, 3, 2) + y[1] = &myRating return pb.CreateCanteenRatingRequest{ Points: rating, @@ -161,6 +129,25 @@ func generateCanteenRating(canteen string, rating int32, s *CafeteriaSuite, comm } } +func prepareTagRating(s *CafeteriaSuite, tagRatingID, ratingID int, points int, tagID int) pb.RatingTag { + // Query to check whether the tag exists + const ExpectedCafeteriaRatingTagOption = "SELECT count(*) FROM `cafeteria_rating_tag_option` WHERE cafeteriaRatingTagOption LIKE ?" + s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedCafeteriaRatingTagOption)). + WithArgs(tagID).WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1)) + + s.mock.ExpectBegin() + s.mock.ExpectQuery(regexp.QuoteMeta("INSERT INTO `cafeteria_rating_tag` (`correspondingRating`,`points`,`tagID`) VALUES (?,?,?) RETURNING `CafeteriaRatingTag`")). + WithArgs(ratingID, points, tagID). + WillReturnRows(sqlmock.NewRows([]string{"CafeteriaRatingTag", "correspondingRating", "points", "tagID"}). + AddRow(tagRatingID, ratingID, points, tagID)) + s.mock.ExpectCommit() + + return pb.RatingTag{ + Points: float64(points), + TagId: int64(tagID), + } +} + func (s *CafeteriaSuite) AfterTest(_, _ string) { require.NoError(s.T(), s.mock.ExpectationsWereMet()) } From 2d1baafda9e07562e6a029de37120bd5aef57ec6 Mon Sep 17 00:00:00 2001 From: Tobias Jungmann Date: Thu, 26 Oct 2023 16:05:16 +0200 Subject: [PATCH 7/8] prepare tag rating insertion --- server/backend/cafeteria_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/backend/cafeteria_test.go b/server/backend/cafeteria_test.go index c0fa6f07..7522bb5e 100644 --- a/server/backend/cafeteria_test.go +++ b/server/backend/cafeteria_test.go @@ -115,10 +115,10 @@ func (s *CafeteriaSuite) Test_CreateCanteenRating() { func generateCanteenRating(canteen string, rating int32, s *CafeteriaSuite, comment string) pb.CreateCanteenRatingRequest { y := make([]*pb.RatingTag, 2) - var myRating = prepareTagRating(s, 1, 0, 1, 1) + var myRating = prepareTagRating(s, 1, 0, 1, 5) y[0] = &myRating - myRating = prepareTagRating(s, 2, 0, 3, 2) - y[1] = &myRating + var myRatingSecond = prepareTagRating(s, 2, 0, 3, 7) + y[1] = &myRatingSecond return pb.CreateCanteenRatingRequest{ Points: rating, From acfce6b113cbb93f397d3dfc18c6ee35ecb7403c Mon Sep 17 00:00:00 2001 From: Tobias Jungmann Date: Sun, 4 Feb 2024 16:45:45 +0100 Subject: [PATCH 8/8] createdish rating --- server/backend/cafeteria_test.go | 69 +++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/server/backend/cafeteria_test.go b/server/backend/cafeteria_test.go index 7522bb5e..c8777546 100644 --- a/server/backend/cafeteria_test.go +++ b/server/backend/cafeteria_test.go @@ -76,12 +76,12 @@ func (s *CafeteriaSuite) Test_GetCafeteriaHeadCount() { const ExpectedGetCafeteriaByName = "SELECT * FROM `cafeteria` WHERE name LIKE ? ORDER BY `cafeteria`.`cafeteria` LIMIT 1" func (s *CafeteriaSuite) Test_CreateCanteenRating() { - StorageDir = "." + StorageDir = "." // Override directory used in the production environment canteenName := "mensa-garching" cafeteriaID := 0 comment := "Everything perfect, 2 Stars" ratingID := 0 - image_name_path := ".cafeterias/0/c73f6461a5ae03bb935b54a27f12e0a5.jpeg" // "testimage.txt" + image_name_path := ".cafeterias/0/c73f6461a5ae03bb935b54a27f12e0a5.jpeg" var ratingValue int32 = 2 // s.mock.ExpectBegin() @@ -104,6 +104,7 @@ func (s *CafeteriaSuite) Test_CreateCanteenRating() { response, err := server.CreateCanteenRating(metadata.NewIncomingContext(context.Background(), meta), &rating, ) + // todo test the response require.NoError(s.T(), err) if err != nil { log.WithError(err).Error("Canteen HeadCount data request failed.") @@ -148,6 +149,70 @@ func prepareTagRating(s *CafeteriaSuite, tagRatingID, ratingID int, points int, } } +const ExpectedGetDishByName = "SELECT * FROM `dish` WHERE name LIKE ? ORDER BY `dish`.`dish` LIMIT 1" + +// Test if dish ratings are correctly created +func (s *CafeteriaSuite) Test_CreateDishRating() { + StorageDir = "." // Override directory used in the production environment + canteenName := "MENSA_GARCHING" + dishName := "Vegane rote Grütze mit Soja-Vanillesauce" + dishId := 0 + dishType := "Pasta" + cafeteriaID := 0 + comment := "Everything perfect, 2 Stars" + dishRatingID := 0 + image_name_path := ".cafeterias/0/c73f6461a5ae03bb935b54a27f12e0a5.jpeg" + var ratingValue int32 = 2 + + // s.mock.ExpectBegin() + s.mock.ExpectQuery(regexp.QuoteMeta(ExpectedGetDishByName)). + WithArgs(dishName). + WillReturnRows(sqlmock.NewRows([]string{"dish", "name", "type", "cafeteriaID"}). + AddRow(dishId, dishName, dishType, cafeteriaID)) + + s.mock.ExpectBegin() + s.mock.ExpectQuery(regexp.QuoteMeta("INSERT INTO `dish_rating` (`dishRating`,`points`,`cafeteriaID`,`dishID`,`comment`,`timestamp`,`image`) VALUES (?,?,?,?,?,?) RETURNING `dishRating`")). + WithArgs(ratingValue, comment, cafeteriaID, sqlmock.AnyArg(), image_name_path). + WillReturnRows(sqlmock.NewRows([]string{"dishRating", "points", "cafeteriaID", "dishID", "comment", "timestamp", "image"}). + AddRow(dishRatingID, ratingValue, cafeteriaID, dishId, comment, time.Date(2023, 10, 9, 11, 45, 22, 0, time.Local), image_name_path)) + s.mock.ExpectCommit() + + meta := metadata.MD{} + rating := generateDishRating(canteenName, ratingValue, s, comment, dishName) + + server := CampusServer{db: s.DB, deviceBuf: s.deviceBuf} + response, err := server.CreateDishRating(metadata.NewIncomingContext(context.Background(), meta), + &rating, + ) + // todo test the response + require.NoError(s.T(), err) + if err != nil { + log.WithError(err).Error("Canteen HeadCount data request failed.") + //todo compare results require.Equal(s.T(), status.Error(codes.NotFound, "No update note found"), err) + } else { + log.WithField("res", response).Info("Canteen HeadCount data request successful.") + } +} + +func generateDishRating(canteen string, rating int32, s *CafeteriaSuite, comment string, dishName string) pb.CreateDishRatingRequest { + y := make([]*pb.RatingTag, 2) + // todo cahnge to dish tags + // todo add the dish specific tags + var myRating = prepareTagRating(s, 1, 0, 1, 5) + y[0] = &myRating + var myRatingSecond = prepareTagRating(s, 2, 0, 3, 7) + y[1] = &myRatingSecond + + return pb.CreateDishRatingRequest{ + Points: rating, + CanteenId: canteen, + Dish: dishName, + Comment: comment, + RatingTags: y, + Image: getImageToBytes(testImage), + } +} + func (s *CafeteriaSuite) AfterTest(_, _ string) { require.NoError(s.T(), s.mock.ExpectationsWereMet()) }