From 736390c78d21dda71e19d929b38d5cfe68fe618f Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Tue, 5 Sep 2023 14:19:11 +0200 Subject: [PATCH] implemnted the api --- server/backend/movie.go | 42 +++++++++++++ server/backend/movie_test.go | 116 +++++++++++++++++++++++++++++++++++ server/go.mod | 1 + server/go.sum | 2 + 4 files changed, 161 insertions(+) create mode 100644 server/backend/movie.go create mode 100644 server/backend/movie_test.go diff --git a/server/backend/movie.go b/server/backend/movie.go new file mode 100644 index 00000000..660ed63b --- /dev/null +++ b/server/backend/movie.go @@ -0,0 +1,42 @@ +package backend + +import ( + "context" + 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/timestamppb" +) + +func (s *CampusServer) GetMovies(_ context.Context, req *pb.GetMoviesRequest) (*pb.GetMoviesReply, error) { + var movies []model.Kino + if err := s.db.Joins("Files").Find(&movies, "kino > ?", req.LastId).Error; err != nil { + log.WithError(err).Error("Error while fetching movies from database") + return nil, status.Error(codes.Internal, "Error while fetching movies from database") + } + var movieResponse []*pb.MovieMsgElement + for _, movie := range movies { + movieResponse = append(movieResponse, &pb.MovieMsgElement{ + Id: movie.Id, + Date: timestamppb.New(movie.Date), + Created: timestamppb.New(movie.Created), + Title: movie.Title, + Year: movie.Year, + Runtime: movie.Runtime, + Genre: movie.Genre, + Director: movie.Director, + Actors: movie.Actors, + ImdbRating: movie.ImdbRating, + Description: movie.Description, + CoverName: movie.Files.Name, + CoverPath: movie.Files.Path, + CoverID: movie.Files.File, + Link: movie.Link, + }) + } + return &pb.GetMoviesReply{ + Movies: movieResponse, + }, nil +} diff --git a/server/backend/movie_test.go b/server/backend/movie_test.go new file mode 100644 index 00000000..99ab8e9a --- /dev/null +++ b/server/backend/movie_test.go @@ -0,0 +1,116 @@ +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/protobuf/types/known/timestamppb" + "gorm.io/driver/mysql" + "gorm.io/gorm" + "testing" + "time" +) + +type MovieSuite struct { + suite.Suite + DB *gorm.DB + mock sqlmock.Sqlmock +} + +func (s *MovieSuite) 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) +} + +var ( + movie1 = pb.MovieMsgElement{ + Id: 1, + Date: timestamppb.New(time.Now()), + Created: timestamppb.New(time.Now()), + Title: "Mission Impossible 4 - Ghost Protocol", + Year: "2011", + Runtime: "133 min", + Genre: "Action, Adventure, Thriller", + Director: "Brad Bird", + Actors: "Tom Cruise, Jeremy Renner, Simon Pegg, Paula Patton", + ImdbRating: "7.4", + Description: "The IMF is shut down when it's implicated in the bombing of the Kremlin, causing Ethan Hunt and his new team to go rogue to clear their organization's name.", + CoverName: "mission_impossible_4.jpg", + CoverPath: "movie/mission_impossible_4.jpg", + CoverID: 1, + Link: "https://www.imdb.com/title/tt1229238/", + } + movie2 = pb.MovieMsgElement{ + Id: 2, + Date: timestamppb.New(time.Now()), + Created: timestamppb.New(time.Now()), + Title: "Mission Impossible 5 - Rogue Nation", + Year: "2015", + Runtime: "131 min", + Genre: "Action, Adventure, Thriller", + Director: "Christopher McQuarrie", + Actors: "Tom Cruise, Jeremy Renner, Simon Pegg, Rebecca Ferguson", + ImdbRating: "7.4", + Description: "Ethan and his team take on their most impossible mission yet when they have to eradicate an international rogue organization as highly skilled as they are and committed to destroying the IMF.", + CoverName: "mission_impossible_5.jpg", + CoverPath: "movie/mission_impossible_5.jpg", + CoverID: 2, + Link: "https://www.imdb.com/title/tt2381249/", + } +) + +func (s *MovieSuite) Test_GetMoviesAll() { + server := CampusServer{db: s.DB} + s.mock.ExpectQuery("SELECT `kino`.`kino`,`kino`.`date`,`kino`.`created`,`kino`.`title`,`kino`.`year`,`kino`.`runtime`,`kino`.`genre`,`kino`.`director`,`kino`.`actors`,`kino`.`rating`,`kino`.`description`,`kino`.`trailer`,`kino`.`cover`,`kino`.`link`,`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 `kino` LEFT JOIN `files` `Files` ON `kino`.`cover` = `Files`.`file` WHERE kino > ?"). + WithArgs(-1). + WillReturnRows(sqlmock.NewRows([]string{"kino", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "trailer", "cover", "link", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"}). + AddRow(movie2.Id, movie2.Date.AsTime(), movie2.Created.AsTime(), movie2.Title, movie2.Year, movie2.Runtime, movie2.Genre, movie2.Director, movie2.Actors, movie2.ImdbRating, movie2.Description, nil, movie2.CoverID, movie2.Link, movie2.CoverID, movie2.CoverName, movie2.CoverPath, 1, "", 1). + AddRow(movie1.Id, movie1.Date.AsTime(), movie1.Created.AsTime(), movie1.Title, movie1.Year, movie1.Runtime, movie1.Genre, movie1.Director, movie1.Actors, movie1.ImdbRating, movie1.Description, nil, movie1.CoverID, movie1.Link, movie1.CoverID, movie1.CoverName, movie1.CoverPath, 1, "", 1)) + response, err := server.GetMovies(context.Background(), &pb.GetMoviesRequest{LastId: -1}) + require.NoError(s.T(), err) + require.Equal(s.T(), &pb.GetMoviesReply{Movies: []*pb.MovieMsgElement{&movie2, &movie1}}, response) +} + +func (s *MovieSuite) Test_GetMoviesOne() { + server := CampusServer{db: s.DB} + s.mock.ExpectQuery("SELECT `kino`.`kino`,`kino`.`date`,`kino`.`created`,`kino`.`title`,`kino`.`year`,`kino`.`runtime`,`kino`.`genre`,`kino`.`director`,`kino`.`actors`,`kino`.`rating`,`kino`.`description`,`kino`.`trailer`,`kino`.`cover`,`kino`.`link`,`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 `kino` LEFT JOIN `files` `Files` ON `kino`.`cover` = `Files`.`file` WHERE kino > ?"). + WithArgs(1). + WillReturnRows(sqlmock.NewRows([]string{"kino", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "trailer", "cover", "link", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"}). + AddRow(movie1.Id, movie1.Date.AsTime(), movie1.Created.AsTime(), movie1.Title, movie1.Year, movie1.Runtime, movie1.Genre, movie1.Director, movie1.Actors, movie1.ImdbRating, movie1.Description, nil, movie1.CoverID, movie1.Link, movie1.CoverID, movie1.CoverName, movie1.CoverPath, 1, "", 1)) + response, err := server.GetMovies(context.Background(), &pb.GetMoviesRequest{LastId: 1}) + require.NoError(s.T(), err) + require.Equal(s.T(), &pb.GetMoviesReply{Movies: []*pb.MovieMsgElement{&movie1}}, response) +} + +func (s *MovieSuite) Test_GetMoviesNone() { + server := CampusServer{db: s.DB} + s.mock.ExpectQuery("SELECT `kino`.`kino`,`kino`.`date`,`kino`.`created`,`kino`.`title`,`kino`.`year`,`kino`.`runtime`,`kino`.`genre`,`kino`.`director`,`kino`.`actors`,`kino`.`rating`,`kino`.`description`,`kino`.`trailer`,`kino`.`cover`,`kino`.`link`,`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 `kino` LEFT JOIN `files` `Files` ON `kino`.`cover` = `Files`.`file` WHERE kino > ?"). + WithArgs(42). + WillReturnRows(sqlmock.NewRows([]string{"kino", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "trailer", "cover", "link", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"})) + response, err := server.GetMovies(context.Background(), &pb.GetMoviesRequest{LastId: 42}) + require.NoError(s.T(), err) + require.Equal(s.T(), &pb.GetMoviesReply{Movies: []*pb.MovieMsgElement(nil)}, response) +} + +// 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(MovieSuite)) +} diff --git a/server/go.mod b/server/go.mod index af09fc68..e827c76f 100644 --- a/server/go.mod +++ b/server/go.mod @@ -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 diff --git a/server/go.sum b/server/go.sum index 9a66ee21..6fbfc912 100644 --- a/server/go.sum +++ b/server/go.sum @@ -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=