Skip to content

Commit

Permalink
added tests for get movie by id endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronzi committed Jul 14, 2024
1 parent 25a2649 commit 684ce56
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/integration/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,29 @@ func TestGetMovies_Integration(t *testing.T) {

assert.Greater(t, len(movies), 0, "Expected at least one movie")
}

func TestGetMovie_Integration(t *testing.T) {
r := mux.NewRouter()
// Assuming api.GetMovie is the handler for getting a single movie by ID
r.HandleFunc("/movies/{id}", api.GetMovie).Methods("GET")

ts := httptest.NewServer(r)
defer ts.Close()

// Replace "someValidMovieID" with a valid movie ID that exists in your data source
resp, err := http.Get(ts.URL + "/movies/1")
if err != nil {
t.Fatal(err)
}

assert.Equal(t, http.StatusOK, resp.StatusCode, "Expected status code 200")

var movie api.Movie
err = json.NewDecoder(resp.Body).Decode(&movie)
if err != nil {
t.Fatal(err)
}

// Replace "someValidMovieID" with the same valid movie ID used in the request
assert.Equal(t, "1", movie.ID, "Expected the movie ID to match")
}
47 changes: 47 additions & 0 deletions tests/unit/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http/httptest"
"testing"

"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
)

Expand All @@ -34,3 +35,49 @@ func TestGetMovies_Unit(t *testing.T) {

assert.Greater(t, len(movies), 0, "Expected at least one movie")
}

func TestGetMovie_ValidID(t *testing.T) {
req, err := http.NewRequest("GET", "/movies/{id}", nil)
if err != nil {
t.Fatal(err)
}

vars := map[string]string{
"id": "1", // Replace with an actual valid ID
}
req = mux.SetURLVars(req, vars)

rr := httptest.NewRecorder()
handler := http.HandlerFunc(api.GetMovie)

handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code, "Expected status code 200")

var movie api.Movie
err = json.Unmarshal(rr.Body.Bytes(), &movie)
if err != nil {
t.Fatal(err)
}

assert.Equal(t, "1", movie.ID, "Expected the movie ID to match")
}

func TestGetMovie_InvalidID(t *testing.T) {
req, err := http.NewRequest("GET", "/movies/{id}", nil)
if err != nil {
t.Fatal(err)
}

vars := map[string]string{
"id": "nonExistentID",
}
req = mux.SetURLVars(req, vars)

rr := httptest.NewRecorder()
handler := http.HandlerFunc(api.GetMovie)

handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusNotFound, rr.Code, "Expected status code 404")
}

0 comments on commit 684ce56

Please sign in to comment.