-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test new actions and apps #16
Conversation
Signed-off-by: Azanul <[email protected]>
Signed-off-by: Azanul <[email protected]>
Signed-off-by: Azanul <[email protected]>
Signed-off-by: Azanul <[email protected]>
Signed-off-by: Azanul <[email protected]>
Template literals are useful when you need: 1. [Interpolated strings](https://en.wikipedia.org/wiki/String_interpolation).
refactor: replace template strings with regular string literals
Signed-off-by: Azanul <[email protected]>
Please double check the following review of the pull request:Issues counts
Changes in the diff
Identified Issues
Issue 1: 💪Best PracticesDetails: The File Path: Lines of Code: rows := sqlmock.NewRows([]string{"id", "title", "genre", "year", "wiki", "plot", "director", "cast"}).
AddRow(uuid.New(), "Movie 1", "Action", 2021, "wiki1", "plot1", "director1", "cast1").
AddRow(uuid.New(), "Movie 2", "Comedy", 2022, "wiki2", "plot2", "director2", "cast2")
mock.ExpectQuery("^SELECT (.+) FROM movies").WillReturnRows(rows)
mock.ExpectQuery("^SELECT COUNT").WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(2)) Proposed Fix: func (r *MovieRepository) GetMovies(ctx context.Context, searchTerm string, page, pageSize int) (*MoviePage, error) {
offset := (page - 1) * pageSize
query := "SELECT id, title, genre, year, wiki, plot, director, cast FROM movies"
if searchTerm != "" {
query += " WHERE title ILIKE '%' || $1 || '%' OR genre ILIKE '%' || $1 || '%'"
}
query += " LIMIT $2 OFFSET $3"
rows, err := r.db.QueryContext(ctx, query, searchTerm, pageSize, offset)
if err != nil {
return nil, err
}
defer rows.Close()
var movies []*models.Movie
for rows.Next() {
var movie models.Movie
if err := rows.Scan(&movie.ID, &movie.Title, &movie.Genre, &movie.Year, &movie.Wiki, &movie.Plot, &movie.Director, &movie.Cast); err != nil {
return nil, err
}
movies = append(movies, &movie)
}
countQuery := "SELECT COUNT(*) FROM movies"
if searchTerm != "" {
countQuery += " WHERE title ILIKE '%' || $1 || '%' OR genre ILIKE '%' || $1 || '%'"
}
var totalCount int
err = r.db.QueryRowContext(ctx, countQuery, searchTerm).Scan(&totalCount)
if err != nil {
return nil, err
}
return &MoviePage{
Movies: movies,
TotalCount: totalCount,
HasNextPage: offset+pageSize < totalCount,
HasPreviousPage: offset > 0,
}, nil
} Explanation of the Fix: Missing TestsThe provided tests cover most of the new functionalities. However, additional tests can be added to ensure comprehensive coverage, especially for edge cases and error scenarios. Example Test for Pagination: func TestMovieRepository_GetMovies_Pagination(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
repo := NewMovieRepository(db)
tests := []struct {
name string
searchTerm string
page int
pageSize int
mockSetup func()
want *MoviePage
wantErr bool
}{
{
name: "Success - With Pagination",
searchTerm: "",
page: 2,
pageSize: 1,
mockSetup: func() {
rows := sqlmock.NewRows([]string{"id", "title", "genre", "year", "wiki", "plot", "director", "cast"}).
AddRow(uuid.New(), "Movie 2", "Comedy", 2022, "wiki2", "plot2", "director2", "cast2")
mock.ExpectQuery("^SELECT (.+) FROM movies LIMIT 1 OFFSET 1").WillReturnRows(rows)
mock.ExpectQuery("^SELECT COUNT").WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(2))
},
want: &MoviePage{
Movies: []*models.Movie{},
TotalCount: 2,
HasNextPage: false,
HasPreviousPage: true,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.mockSetup()
got, err := repo.GetMovies(context.Background(), tt.searchTerm, tt.page, tt.pageSize)
if (err != nil) != tt.wantErr {
t.Errorf("MovieRepository.GetMovies() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
assert.Equal(t, tt.want.TotalCount, got.TotalCount)
assert.Equal(t, tt.want.HasNextPage, got.HasNextPage)
assert.Equal(t, tt.want.HasPreviousPage, got.HasPreviousPage)
}
})
}
} Explanation: Summon me to re-review when updated! Yours, Gooroo.dev |
Signed-off-by: Azanul <[email protected]>
Signed-off-by: Azanul <[email protected]>
Signed-off-by: Azanul <[email protected]>
Signed-off-by: Azanul <[email protected]>
Signed-off-by: Azanul <[email protected]>
Signed-off-by: Azanul <[email protected]>
No description provided.