Skip to content

Commit

Permalink
initialize cache for server and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joschahenningsen committed Nov 8, 2024
1 parent 1382046 commit 7b2adda
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
15 changes: 12 additions & 3 deletions server/backend/movie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package backend
import (
"context"
"database/sql"
"github.com/TUM-Dev/Campus-Backend/server/model"
"github.com/hashicorp/golang-lru/v2/expirable"
"regexp"
"testing"
"time"
Expand Down Expand Up @@ -81,7 +83,7 @@ var (
const ListMoviesQuery = "SELECT `movies`.`kino`,`movies`.`date`,`movies`.`created`,`movies`.`title`,`movies`.`year`,`movies`.`runtime`,`movies`.`genre`,`movies`.`director`,`movies`.`actors`,`movies`.`rating`,`movies`.`description`,`movies`.`trailer`,`movies`.`cover`,`movies`.`link`,`movies`.`location`,`File`.`file` AS `File__file`,`File`.`name` AS `File__name`,`File`.`path` AS `File__path`,`File`.`downloads` AS `File__downloads`,`File`.`url` AS `File__url`,`File`.`downloaded` AS `File__downloaded` FROM `movies` LEFT JOIN `files` `File` ON `movies`.`cover` = `File`.`file` WHERE kino > ? ORDER BY date ASC"

func (s *MovieSuite) Test_ListMoviesAll() {
server := CampusServer{db: s.DB}
server := s.getCampusTestServer()
s.mock.ExpectQuery(regexp.QuoteMeta(ListMoviesQuery)).
WithArgs(-1).
WillReturnRows(sqlmock.NewRows([]string{"kino", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "trailer", "cover", "link", "location", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded"}).
Expand All @@ -93,7 +95,7 @@ func (s *MovieSuite) Test_ListMoviesAll() {
}

func (s *MovieSuite) Test_ListMoviesOne() {
server := CampusServer{db: s.DB}
server := s.getCampusTestServer()
s.mock.ExpectQuery(regexp.QuoteMeta(ListMoviesQuery)).
WithArgs(1).
WillReturnRows(sqlmock.NewRows([]string{"kino", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "trailer", "cover", "link", "location", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded"}).
Expand All @@ -104,7 +106,7 @@ func (s *MovieSuite) Test_ListMoviesOne() {
}

func (s *MovieSuite) Test_ListMoviesNone() {
server := CampusServer{db: s.DB}
server := s.getCampusTestServer()
s.mock.ExpectQuery(regexp.QuoteMeta(ListMoviesQuery)).
WithArgs(42).
WillReturnRows(sqlmock.NewRows([]string{"kino", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "trailer", "cover", "link", "location", "File__file", "File__name", "File__path", "File__downloads", "File__url", "File__downloaded"}))
Expand All @@ -122,3 +124,10 @@ func (s *MovieSuite) AfterTest(_, _ string) {
func TestMovieSuite(t *testing.T) {
suite.Run(t, new(MovieSuite))
}

func (s *MovieSuite) getCampusTestServer() *CampusServer {
return &CampusServer{
db: s.DB,
moviesCache: expirable.NewLRU[string, []model.Movie](1024, nil, time.Minute*30),
}
}
1 change: 1 addition & 0 deletions server/backend/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ func New(db *gorm.DB) *CampusServer {
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),
moviesCache: expirable.NewLRU[string, []model.Movie](1024, nil, time.Minute*30),
}
}

0 comments on commit 7b2adda

Please sign in to comment.