|
|
|
@ -8,28 +8,43 @@ import (
|
|
|
|
"log/slog"
|
|
|
|
"log/slog"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/http/httptest"
|
|
|
|
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"git.runcible.io/learning/pulley/internal/assert"
|
|
|
|
"git.runcible.io/learning/pulley/internal/assert"
|
|
|
|
"git.runcible.io/learning/pulley/internal/config"
|
|
|
|
"git.runcible.io/learning/pulley/internal/config"
|
|
|
|
|
|
|
|
"git.runcible.io/learning/pulley/internal/data"
|
|
|
|
|
|
|
|
"git.runcible.io/learning/pulley/internal/database"
|
|
|
|
|
|
|
|
"github.com/pashagolub/pgxmock/v4"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func newTestApplication() application {
|
|
|
|
func newTestApplication(pool database.PgxIface) application {
|
|
|
|
cfg := config.ServiceConfig{Env: "test"}
|
|
|
|
cfg := config.ServiceConfig{Env: "test"}
|
|
|
|
return application{config: cfg, logger: slog.New(slog.NewTextHandler(io.Discard, nil))}
|
|
|
|
mockModels := data.NewModels(pool)
|
|
|
|
|
|
|
|
// Discards log output from tests
|
|
|
|
|
|
|
|
// logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
|
|
|
|
|
|
|
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug, AddSource: true}))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return application{config: cfg, logger: logger, models: mockModels}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestHealthRoute(t *testing.T) {
|
|
|
|
func TestHealthRoute(t *testing.T) {
|
|
|
|
respRec := httptest.NewRecorder()
|
|
|
|
respRec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
mockPool, err := pgxmock.NewPool()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
defer mockPool.Close()
|
|
|
|
|
|
|
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "/v1/healthcheck", nil)
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "/v1/healthcheck", nil)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
app := newTestApplication()
|
|
|
|
app := newTestApplication(mockPool)
|
|
|
|
app.routes().ServeHTTP(respRec, r)
|
|
|
|
app.routes().ServeHTTP(respRec, r)
|
|
|
|
|
|
|
|
|
|
|
|
resp := respRec.Result()
|
|
|
|
resp := respRec.Result()
|
|
|
|
@ -49,15 +64,40 @@ func TestHealthRoute(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
|
|
func TestCreateMovieHandler(t *testing.T) {
|
|
|
|
func TestCreateMovieHandler(t *testing.T) {
|
|
|
|
respRec := httptest.NewRecorder()
|
|
|
|
respRec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
mockPool, err := pgxmock.NewPool()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
defer mockPool.Close()
|
|
|
|
|
|
|
|
|
|
|
|
requestBody := `{"title": "Moana", "year": 2019, "runtime": "120 mins", "genres": ["family", "Samoan"]}`
|
|
|
|
movie := struct {
|
|
|
|
|
|
|
|
Title string `json:"title"`
|
|
|
|
|
|
|
|
Year int32 `json:"year"`
|
|
|
|
|
|
|
|
Runtime string `json:"runtime"`
|
|
|
|
|
|
|
|
Genres []string `json:"genres"`
|
|
|
|
|
|
|
|
}{
|
|
|
|
|
|
|
|
Title: "Moana",
|
|
|
|
|
|
|
|
Year: 2019,
|
|
|
|
|
|
|
|
Runtime: "120 mins",
|
|
|
|
|
|
|
|
Genres: []string{"family", "Samoan"},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mockPool.ExpectQuery("INSERT INTO movies").
|
|
|
|
|
|
|
|
// must use Runtime explicitly for args
|
|
|
|
|
|
|
|
WithArgs(movie.Title, movie.Year, data.Runtime(120), movie.Genres).
|
|
|
|
|
|
|
|
WillReturnRows(
|
|
|
|
|
|
|
|
pgxmock.NewRows([]string{"id", "created_at", "version"}).
|
|
|
|
|
|
|
|
AddRow(1, time.Now(), 1), // These values will be scanned into the struct
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rquestBody, _ := json.Marshal(movie)
|
|
|
|
|
|
|
|
|
|
|
|
r, err := http.NewRequest(http.MethodPost, "/v1/movies", strings.NewReader(requestBody))
|
|
|
|
r, err := http.NewRequest(http.MethodPost, "/v1/movies", bytes.NewBuffer(rquestBody))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
app := newTestApplication()
|
|
|
|
app := newTestApplication(mockPool)
|
|
|
|
app.routes().ServeHTTP(respRec, r)
|
|
|
|
app.routes().ServeHTTP(respRec, r)
|
|
|
|
|
|
|
|
|
|
|
|
resp := respRec.Result()
|
|
|
|
resp := respRec.Result()
|
|
|
|
@ -72,10 +112,20 @@ func TestCreateMovieHandler(t *testing.T) {
|
|
|
|
body = bytes.TrimSpace(body)
|
|
|
|
body = bytes.TrimSpace(body)
|
|
|
|
|
|
|
|
|
|
|
|
assert.StringContains(t, string(body), "Moana")
|
|
|
|
assert.StringContains(t, string(body), "Moana")
|
|
|
|
|
|
|
|
err = mockPool.ExpectationsWereMet()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
t.Errorf("there were unfulfilled expectations: %s", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Consider simply testing app.jsonReader
|
|
|
|
// Consider simply testing app.jsonReader
|
|
|
|
func TestCreateMovieError(t *testing.T) {
|
|
|
|
func TestCreateMovieValidatorError(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mockPool, err := pgxmock.NewPool()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
defer mockPool.Close()
|
|
|
|
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
name string
|
|
|
|
@ -148,7 +198,7 @@ func TestCreateMovieError(t *testing.T) {
|
|
|
|
t.Fatal(err)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
app := newTestApplication()
|
|
|
|
app := newTestApplication(mockPool)
|
|
|
|
|
|
|
|
|
|
|
|
app.routes().ServeHTTP(respRec, r)
|
|
|
|
app.routes().ServeHTTP(respRec, r)
|
|
|
|
|
|
|
|
|
|
|
|
@ -166,8 +216,15 @@ func TestCreateMovieError(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestGetAllMoviesHandler(t *testing.T) {
|
|
|
|
func TestGetMovieHandler(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mockPool, err := pgxmock.NewPool()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
defer mockPool.Close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO since this isn't a table test anymore should be able to refactor it
|
|
|
|
testTable := []struct {
|
|
|
|
testTable := []struct {
|
|
|
|
name string
|
|
|
|
name string
|
|
|
|
id string
|
|
|
|
id string
|
|
|
|
@ -179,10 +236,53 @@ func TestGetAllMoviesHandler(t *testing.T) {
|
|
|
|
id: "1337",
|
|
|
|
id: "1337",
|
|
|
|
wantCode: 200,
|
|
|
|
wantCode: 200,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mockPool.ExpectQuery("SELECT id, created_at, title, year, runtime, genres, version FROM movies").
|
|
|
|
|
|
|
|
WithArgs(int64(1337)).WillReturnRows(
|
|
|
|
|
|
|
|
pgxmock.NewRows([]string{"id", "created_at", "title", "year", "runtime", "genres", "version"}).
|
|
|
|
|
|
|
|
AddRow(int64(1337), time.Now(), "a laura is born", 1990, 36, []string{"family", "wife"}, 1), // These values will be scanned into the struct
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, test := range testTable {
|
|
|
|
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
|
|
|
|
respRec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/v1/movies/%s", test.id), nil)
|
|
|
|
|
|
|
|
t.Logf("Path: %s, Method: %s", r.URL.Path, r.Method)
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app := newTestApplication(mockPool)
|
|
|
|
|
|
|
|
// want to test with httprouter since we use it to parse context
|
|
|
|
|
|
|
|
app.routes().ServeHTTP(respRec, r)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resp := respRec.Result()
|
|
|
|
|
|
|
|
t.Logf("Code: %d", resp.StatusCode)
|
|
|
|
|
|
|
|
assert.Equal(t, resp.StatusCode, test.wantCode)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestGetMovieHandlerErrors(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mockPool, err := pgxmock.NewPool()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
defer mockPool.Close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
testTable := []struct {
|
|
|
|
|
|
|
|
name string
|
|
|
|
|
|
|
|
id string
|
|
|
|
|
|
|
|
wantCode int
|
|
|
|
|
|
|
|
useID bool
|
|
|
|
|
|
|
|
}{
|
|
|
|
|
|
|
|
// will redirect to /v1/movies/
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: "No ID provided",
|
|
|
|
name: "No ID provided",
|
|
|
|
id: "",
|
|
|
|
id: "",
|
|
|
|
wantCode: 404,
|
|
|
|
wantCode: 301,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: "Negative ID",
|
|
|
|
name: "Negative ID",
|
|
|
|
@ -199,7 +299,7 @@ func TestGetAllMoviesHandler(t *testing.T) {
|
|
|
|
t.Logf("Path: %s, Method: %s", r.URL.Path, r.Method)
|
|
|
|
t.Logf("Path: %s, Method: %s", r.URL.Path, r.Method)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
|
|
app := newTestApplication()
|
|
|
|
app := newTestApplication(mockPool)
|
|
|
|
// want to test with httprouter since we use it to parse context
|
|
|
|
// want to test with httprouter since we use it to parse context
|
|
|
|
app.routes().ServeHTTP(respRec, r)
|
|
|
|
app.routes().ServeHTTP(respRec, r)
|
|
|
|
|
|
|
|
|
|
|
|
|