test: add tests for comments
This commit is contained in:
parent
8adfa22c9d
commit
84119e32c5
106
handlers/comment_test.go
Normal file
106
handlers/comment_test.go
Normal file
|
@ -0,0 +1,106 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
DB "log101-blog-services/db"
|
||||
"log101-blog-services/models"
|
||||
"log101-blog-services/utils"
|
||||
)
|
||||
|
||||
func TestGetComments(t *testing.T) {
|
||||
db := utils.SetupTestDB()
|
||||
DB.SetDB(db)
|
||||
|
||||
router := gin.Default()
|
||||
router.LoadHTMLGlob("../templates/*")
|
||||
|
||||
// Populate the test database with data
|
||||
// Create a comment with a username
|
||||
db.Create(&models.Comment{PostId: "1", Body: "sample body 1", Username: "username1"})
|
||||
db.Create(&models.Comment{PostId: "1", Body: "sample body 2", Username: "username2"})
|
||||
|
||||
router.GET("/comments", GetComments)
|
||||
|
||||
req, _ := http.NewRequest("GET", "/comments?postId=1", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Contains(t, w.Body.String(), "sample body 1")
|
||||
assert.Contains(t, w.Body.String(), "sample body 2")
|
||||
}
|
||||
|
||||
func TestPostComment(t *testing.T) {
|
||||
db := utils.SetupTestDB()
|
||||
DB.SetDB(db) // Set the mock DB
|
||||
|
||||
router := gin.Default()
|
||||
router.LoadHTMLGlob("../templates/*")
|
||||
|
||||
router.POST("/comments", PostComment)
|
||||
|
||||
form := url.Values{}
|
||||
form.Add("postId", "1")
|
||||
form.Add("commentBody", "sample comment 1")
|
||||
form.Add("username", "username1")
|
||||
|
||||
req, _ := http.NewRequest("POST", "/comments", strings.NewReader(form.Encode()))
|
||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Contains(t, w.Body.String(), "sample comment 1")
|
||||
}
|
||||
|
||||
func TestPostAnonymousComment(t *testing.T) {
|
||||
db := utils.SetupTestDB()
|
||||
DB.SetDB(db) // Set the mock DB
|
||||
|
||||
router := gin.Default()
|
||||
router.LoadHTMLGlob("../templates/*")
|
||||
|
||||
router.POST("/comments", PostComment)
|
||||
|
||||
form := url.Values{}
|
||||
form.Add("postId", "1")
|
||||
form.Add("commentBody", "sample comment 1")
|
||||
|
||||
req, _ := http.NewRequest("POST", "/comments", strings.NewReader(form.Encode()))
|
||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Contains(t, w.Body.String(), "sample comment 1")
|
||||
assert.Contains(t, w.Body.String(), "anonim")
|
||||
}
|
||||
|
||||
func TestPostCommentMissingParams(t *testing.T) {
|
||||
db := utils.SetupTestDB()
|
||||
DB.SetDB(db) // Set the mock DB
|
||||
|
||||
router := gin.Default()
|
||||
router.LoadHTMLGlob("../templates/*") // Ensure you have your templates in the right path
|
||||
|
||||
router.POST("/comments", PostComment)
|
||||
|
||||
form := url.Values{}
|
||||
form.Add("postId", "1")
|
||||
|
||||
req, _ := http.NewRequest("POST", "/comments", strings.NewReader(form.Encode()))
|
||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
}
|
|
@ -8,30 +8,19 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/glebarez/sqlite"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gorm.io/gorm"
|
||||
|
||||
DB "log101-blog-services/db"
|
||||
"log101-blog-services/models"
|
||||
"log101-blog-services/utils"
|
||||
)
|
||||
|
||||
func SetupTestDB() *gorm.DB {
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic("failed to connect to in-memory database")
|
||||
}
|
||||
|
||||
db.AutoMigrate(&models.EmojiReaction{})
|
||||
return db
|
||||
}
|
||||
|
||||
func TestGetEmojiForm(t *testing.T) {
|
||||
db := SetupTestDB()
|
||||
db := utils.SetupTestDB()
|
||||
DB.SetDB(db)
|
||||
|
||||
router := gin.Default()
|
||||
router.LoadHTMLGlob("../templates/*") // Ensure you have your templates in the right path
|
||||
router.LoadHTMLGlob("../templates/*")
|
||||
|
||||
router.GET("/emoji_form", GetEmojiForm)
|
||||
|
||||
|
@ -45,7 +34,7 @@ func TestGetEmojiForm(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestPostEmojiForm(t *testing.T) {
|
||||
db := SetupTestDB()
|
||||
db := utils.SetupTestDB()
|
||||
DB.SetDB(db) // Set the mock DB
|
||||
|
||||
router := gin.Default()
|
||||
|
@ -67,7 +56,7 @@ func TestPostEmojiForm(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestPostEmojiFormMissingParams(t *testing.T) {
|
||||
db := SetupTestDB()
|
||||
db := utils.SetupTestDB()
|
||||
DB.SetDB(db) // Set the mock DB
|
||||
|
||||
router := gin.Default()
|
||||
|
@ -88,7 +77,7 @@ func TestPostEmojiFormMissingParams(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestGetEmojis(t *testing.T) {
|
||||
db := SetupTestDB()
|
||||
db := utils.SetupTestDB()
|
||||
DB.SetDB(db) // Set the mock DB
|
||||
|
||||
// Populate the test database with data
|
||||
|
|
19
utils/main.go
Normal file
19
utils/main.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"log101-blog-services/models"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func SetupTestDB() *gorm.DB {
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic("failed to connect to in-memory database")
|
||||
}
|
||||
|
||||
db.AutoMigrate(&models.EmojiReaction{})
|
||||
db.AutoMigrate(&models.Comment{})
|
||||
return db
|
||||
}
|
Loading…
Reference in New Issue
Block a user