This commit is contained in:
parent
fe6c39379a
commit
464d93a99f
4
db/db.go
4
db/db.go
|
@ -25,3 +25,7 @@ func InitDB() {
|
|||
func GetDB() *gorm.DB {
|
||||
return db
|
||||
}
|
||||
|
||||
func SetDB(database *gorm.DB) {
|
||||
db = database
|
||||
}
|
||||
|
|
3
go.mod
3
go.mod
|
@ -6,13 +6,16 @@ require (
|
|||
github.com/gin-contrib/cors v1.7.2
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/stretchr/testify v1.9.0
|
||||
gorm.io/gorm v1.25.10
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/glebarez/go-sqlite v1.21.2 // indirect
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
modernc.org/libc v1.22.5 // indirect
|
||||
modernc.org/mathutil v1.5.0 // indirect
|
||||
|
|
|
@ -34,7 +34,7 @@ func PostEmojiForm(c *gin.Context) {
|
|||
|
||||
// Check if parameters are missing
|
||||
if reactedPostId == "" || reaction == "" {
|
||||
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "missing parameters"})
|
||||
c.HTML(http.StatusOK, "emoji_form_error.tmpl", gin.H{"errorMessage": "missing parameters"})
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -43,14 +43,14 @@ func PostEmojiForm(c *gin.Context) {
|
|||
DoUpdates: clause.AssignmentColumns([]string{"emoji"}),
|
||||
}).Create(&models.EmojiReaction{UserAnonIp: c.Request.RemoteAddr, Emoji: reaction, PostId: reactedPostId})
|
||||
if result.Error != nil {
|
||||
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error writing to database"})
|
||||
c.HTML(http.StatusOK, "emoji_form_error.tmpl", gin.H{"errorMessage": "error writing to database"})
|
||||
return
|
||||
}
|
||||
|
||||
// get emoji counts for each emoji
|
||||
emojiCounter, err := CountEmojis(reactedPostId)
|
||||
if err != nil {
|
||||
c.HTML(http.StatusOK, "emoji_form.tmpl", gin.H{"error": "error getting the emoji counts"})
|
||||
c.HTML(http.StatusOK, "emoji_form_error.tmpl", gin.H{"errorMessage": "error getting the emoji counts"})
|
||||
return
|
||||
}
|
||||
|
103
handlers/emoji_form_test.go
Normal file
103
handlers/emoji_form_test.go
Normal file
|
@ -0,0 +1,103 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"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"
|
||||
)
|
||||
|
||||
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.SetDB(db)
|
||||
|
||||
router := gin.Default()
|
||||
router.LoadHTMLGlob("../templates/*") // Ensure you have your templates in the right path
|
||||
|
||||
router.GET("/emoji_form", GetEmojiForm)
|
||||
|
||||
req, _ := http.NewRequest("GET", "/emoji_form?postId=1", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Contains(t, w.Body.String(), "👍")
|
||||
assert.Contains(t, w.Body.String(), "😑")
|
||||
}
|
||||
|
||||
func TestPostEmojiForm(t *testing.T) {
|
||||
db := SetupTestDB()
|
||||
DB.SetDB(db) // Set the mock DB
|
||||
|
||||
router := gin.Default()
|
||||
router.LoadHTMLGlob("../templates/*")
|
||||
|
||||
router.POST("/emoji_form", PostEmojiForm)
|
||||
|
||||
form := url.Values{}
|
||||
form.Add("postId", "1")
|
||||
form.Add("emojiInput", "👍")
|
||||
|
||||
req, _ := http.NewRequest("POST", "/emoji_form", 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(), "👍 1")
|
||||
assert.Contains(t, w.Body.String(), "😑")
|
||||
}
|
||||
|
||||
func TestPostEmojiFormMissingParams(t *testing.T) {
|
||||
db := 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("/emoji_form", PostEmojiForm)
|
||||
|
||||
form := url.Values{}
|
||||
form.Add("postId", "1")
|
||||
|
||||
req, _ := http.NewRequest("POST", "/emoji_form", 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(), "missing parameters")
|
||||
}
|
||||
|
||||
func TestCountEmojis(t *testing.T) {
|
||||
db := SetupTestDB()
|
||||
DB.SetDB(db) // Set the mock DB
|
||||
|
||||
// Populate the test database with data
|
||||
db.Create(&models.EmojiReaction{UserAnonIp: "127.0.0.2", Emoji: "😊", PostId: "1"})
|
||||
db.Create(&models.EmojiReaction{UserAnonIp: "127.0.0.1", Emoji: "😂", PostId: "1"})
|
||||
|
||||
counts, err := CountEmojis("1")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, counts["😊"])
|
||||
assert.Equal(t, 1, counts["😂"])
|
||||
}
|
9
templates/emoji_form_error.tmpl
Normal file
9
templates/emoji_form_error.tmpl
Normal file
|
@ -0,0 +1,9 @@
|
|||
<div class="emoji-buttons-container">
|
||||
<button name="emojiInput" value="👍" type="submit" class="emoji-button">👍 </button>
|
||||
<button name="emojiInput" value="👎" type="submit" class="emoji-button">👎 </button>
|
||||
<button name="emojiInput" value="😀" type="submit" class="emoji-button">😀 </button>
|
||||
<button name="emojiInput" value="😑" type="submit" class="emoji-button">😑 </button>
|
||||
<button name="emojiInput" value="🤢" type="submit" class="emoji-button">🤢 </button>
|
||||
<button name="emojiInput" value="👀" type="submit" class="emoji-button">👀 </button>
|
||||
</div>
|
||||
<div id="emoji-form-error"><p>{{ .errorMessage }}</p></div>
|
Loading…
Reference in New Issue
Block a user