Skip to content

Commit

Permalink
Merge pull request #76 from Ajinx1/ayotee-dev
Browse files Browse the repository at this point in the history
fixed all tests and status_code
  • Loading branch information
Cyberguru1 authored Jul 21, 2024
2 parents 9af545d + d2c5820 commit 60b34b5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/hngprojects/hng_boilerplate_golang_web

go 1.19

toolchain go1.22.2
//toolchain go1.22.2

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
Expand Down
14 changes: 7 additions & 7 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ func Setup(logger *utility.Logger, validator *validator.Validate, db *storage.Da

r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"code": 200,
"message": "HNGi Golang Boilerplate",
"status": http.StatusOK,
"status_code": 200,
"message": "HNGi Golang Boilerplate",
"status": http.StatusOK,
})
})

r.NoRoute(func(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{
"name": "Not Found",
"message": "Page not found.",
"code": 404,
"status": http.StatusNotFound,
"name": "Not Found",
"message": "Page not found.",
"status_code": 404,
"status": http.StatusNotFound,
})
})

Expand Down
26 changes: 17 additions & 9 deletions tests/newsletter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/hngprojects/hng_boilerplate_golang_web/internal/models"
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/controller/newsletter"
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/repository/storage"
"github.com/hngprojects/hng_boilerplate_golang_web/utility"
)

func setupNewsLetterTestRouter() (*gin.Engine, *newsletter.Controller) {
Expand Down Expand Up @@ -40,8 +42,9 @@ func TestE2ENewsletterSubscription(t *testing.T) {
router, _ := setupNewsLetterTestRouter()

// Test POST /newsletter
currUUID := utility.GenerateUUID()
body := models.NewsLetter{
Email: "[email protected]",
Email: fmt.Sprintf("testuser%[email protected]", currUUID),
}
jsonBody, err := json.Marshal(body)
if err != nil {
Expand All @@ -66,8 +69,9 @@ func TestE2ENewsletterSubscription(t *testing.T) {
func TestPostNewsletter_ValidateEmail(t *testing.T) {
router, _ := setupNewsLetterTestRouter()

currUUID := utility.GenerateUUID()
body := models.NewsLetter{
Email: "invalid-email",
Email: fmt.Sprintf("testuser%v@qa", currUUID),
}
jsonBody, _ := json.Marshal(body)

Expand All @@ -85,11 +89,13 @@ func TestPostNewsletter_ValidateEmail(t *testing.T) {
func TestPostNewsletter_CheckDuplicateEmail(t *testing.T) {
router, newsController := setupNewsLetterTestRouter()

currUUID := utility.GenerateUUID()

db := newsController.Db.Postgresql
db.Create(&models.NewsLetter{Email: "[email protected]"})
db.Create(&models.NewsLetter{Email: fmt.Sprintf("testuser%[email protected]", currUUID)})

body := models.NewsLetter{
Email: "[email protected]",
Email: fmt.Sprintf("testuser%[email protected]", currUUID),
}
jsonBody, _ := json.Marshal(body)

Expand All @@ -107,8 +113,9 @@ func TestPostNewsletter_CheckDuplicateEmail(t *testing.T) {
func TestPostNewsletter_SaveData(t *testing.T) {
router, newsController := setupNewsLetterTestRouter()

currUUID := utility.GenerateUUID()
body := models.NewsLetter{
Email: "[email protected]",
Email: fmt.Sprintf("testuser%[email protected]", currUUID),
}
jsonBody, _ := json.Marshal(body)

Expand All @@ -123,17 +130,18 @@ func TestPostNewsletter_SaveData(t *testing.T) {
AssertResponseMessage(t, response["message"].(string), "subscribed successfully")

var newsletter models.NewsLetter
newsController.Db.Postgresql.First(&newsletter, "email = ?", "[email protected]")
if newsletter.Email != "[email protected]" {
t.Errorf("data not saved correctly to the database: expected email %s, got %s", "[email protected]", newsletter.Email)
newsController.Db.Postgresql.First(&newsletter, "email = ?", fmt.Sprintf("testuser%[email protected]", currUUID))
if newsletter.Email != fmt.Sprintf("testuser%[email protected]", currUUID) {
t.Errorf("data not saved correctly to the database: expected email %s, got %s", fmt.Sprintf("testuser%[email protected]", currUUID), newsletter.Email)
}
}

func TestPostNewsletter_ResponseAndStatusCode(t *testing.T) {
router, _ := setupNewsLetterTestRouter()

currUUID := utility.GenerateUUID()
body := models.NewsLetter{
Email: "test3@example.com",
Email: fmt.Sprintf("testuser%v@gmail.com", currUUID),
}
jsonBody, _ := json.Marshal(body)

Expand Down
2 changes: 1 addition & 1 deletion tests/organisation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func TestOrganizationCreate(t *testing.T) {

data := ParseResponse(rr)

code := int(data["code"].(float64))
code := int(data["status_code"].(float64))
AssertStatusCode(t, code, test.ExpectedCode)

if test.Message != "" {
Expand Down
4 changes: 2 additions & 2 deletions tests/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestSignup(t *testing.T) {

data := ParseResponse(rr)

code := int(data["code"].(float64))
code := int(data["status_code"].(float64))
AssertStatusCode(t, code, test.ExpectedCode)

if test.Message != "" {
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestLogin(t *testing.T) {

data := ParseResponse(rr)

code := int(data["code"].(float64))
code := int(data["status_code"].(float64))
AssertStatusCode(t, code, test.ExpectedCode)

if test.Message != "" {
Expand Down

0 comments on commit 60b34b5

Please sign in to comment.