Skip to content

Commit

Permalink
Add Unit Tests for DeleteSubscriber Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Giurgiu committed Oct 3, 2024
1 parent 53de437 commit 3015a19
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 9 deletions.
18 changes: 9 additions & 9 deletions api/coverage.out
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,12 @@ mymodule/main.go:1241.2,1241.24 1 1
mymodule/main.go:1241.24,1243.17 2 1
mymodule/main.go:1243.17,1247.4 3 1
mymodule/main.go:1250.2,1250.46 1 1
mymodule/main.go:1254.74,1255.35 1 0
mymodule/main.go:1255.35,1258.3 2 0
mymodule/main.go:1260.2,1261.16 2 0
mymodule/main.go:1261.16,1264.3 2 0
mymodule/main.go:1266.2,1267.16 2 0
mymodule/main.go:1267.16,1270.3 2 0
mymodule/main.go:1272.2,1273.23 2 0
mymodule/main.go:1273.23,1276.3 2 0
mymodule/main.go:1278.2,1278.52 1 0
mymodule/main.go:1254.74,1255.35 1 1
mymodule/main.go:1255.35,1258.3 2 1
mymodule/main.go:1260.2,1261.16 2 1
mymodule/main.go:1261.16,1264.3 2 1
mymodule/main.go:1266.2,1267.16 2 1
mymodule/main.go:1267.16,1270.3 2 1
mymodule/main.go:1272.2,1273.23 2 1
mymodule/main.go:1273.23,1276.3 2 1
mymodule/main.go:1278.2,1278.52 1 1
101 changes: 101 additions & 0 deletions api/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3846,6 +3846,107 @@ func TestDeleteBook_FailedToDeleteAuthor(t *testing.T) {
assert.NoError(t, err)
}

// Tests for DeleteSubscriber handler
func TestDeleteSubscriber_Success(t *testing.T) {
app, mock := createTestApp(t)
defer app.DB.Close()

req := httptest.NewRequest("DELETE", "/subscribers/1", nil)
vars := map[string]string{"id": "1"}
req = mux.SetURLVars(req, vars)
rr := httptest.NewRecorder()

mock.ExpectExec(regexp.QuoteMeta(`DELETE FROM subscribers WHERE id = ?`)).
WithArgs(1).
WillReturnResult(sqlmock.NewResult(1, 1))

handler := http.HandlerFunc(app.DeleteSubscriber)
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
assert.Contains(t, rr.Body.String(), "Subscriber deleted successfully")

err := mock.ExpectationsWereMet()
assert.NoError(t, err)
}

func TestDeleteSubscriber_InvalidSubscriberID(t *testing.T) {
app, _ := createTestApp(t)
defer app.DB.Close()

req := httptest.NewRequest("DELETE", "/subscribers/invalid", nil)
vars := map[string]string{"id": "invalid"}
req = mux.SetURLVars(req, vars)
rr := httptest.NewRecorder()

handler := http.HandlerFunc(app.DeleteSubscriber)
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusBadRequest, rr.Code)
assert.Contains(t, rr.Body.String(), "Invalid subscriber ID")
}

func TestDeleteSubscriber_NotFound(t *testing.T) {
app, mock := createTestApp(t)
defer app.DB.Close()

req := httptest.NewRequest("DELETE", "/subscribers/1", nil)
vars := map[string]string{"id": "1"}
req = mux.SetURLVars(req, vars)
rr := httptest.NewRecorder()

mock.ExpectExec(regexp.QuoteMeta(`DELETE FROM subscribers WHERE id = ?`)).
WithArgs(1).
WillReturnResult(sqlmock.NewResult(1, 0))

handler := http.HandlerFunc(app.DeleteSubscriber)
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusNotFound, rr.Code)
assert.Contains(t, rr.Body.String(), "Subscriber not found")

err := mock.ExpectationsWereMet()
assert.NoError(t, err)
}

func TestDeleteSubscriber_DBError(t *testing.T) {
app, mock := createTestApp(t)
defer app.DB.Close()

req := httptest.NewRequest("DELETE", "/subscribers/1", nil)
vars := map[string]string{"id": "1"}
req = mux.SetURLVars(req, vars)
rr := httptest.NewRecorder()

mock.ExpectExec(regexp.QuoteMeta(`DELETE FROM subscribers WHERE id = ?`)).
WithArgs(1).
WillReturnError(fmt.Errorf("DB error"))

handler := http.HandlerFunc(app.DeleteSubscriber)
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusInternalServerError, rr.Code)
assert.Contains(t, rr.Body.String(), "Failed to delete subscriber")

err := mock.ExpectationsWereMet()
assert.NoError(t, err)
}

func TestDeleteSubscriber_MethodNotAllowed(t *testing.T) {
app, _ := createTestApp(t)
defer app.DB.Close()

req := httptest.NewRequest("GET", "/subscribers/1", nil)
vars := map[string]string{"id": "1"}
req = mux.SetURLVars(req, vars)
rr := httptest.NewRecorder()

handler := http.HandlerFunc(app.DeleteSubscriber)
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusMethodNotAllowed, rr.Code)
assert.Contains(t, rr.Body.String(), "Only DELETE method is supported")
}



0 comments on commit 3015a19

Please sign in to comment.