mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
Added tests for ArticlesBusiness
This commit is contained in:
@@ -1,119 +0,0 @@
|
|||||||
package internal
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Article struct {
|
|
||||||
IdName string `json:"id_name" bson:"id_name"`
|
|
||||||
Date int64 `json:"date" bson:"date"`
|
|
||||||
Content interface{} `json:"content" bson:"content"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type DelArticle struct {
|
|
||||||
IdName string `json:"id_name" bson:"id_name"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var articlesLocation = Location{Database: "gohcms", Collection: "articles"}
|
|
||||||
|
|
||||||
func GetAllArticles(c *gin.Context) {
|
|
||||||
// TODO Change to GetArticleList that will find filter json context
|
|
||||||
var articles []Article
|
|
||||||
documents, err := getDocuments(articlesLocation, bson.D{})
|
|
||||||
if err != nil {
|
|
||||||
SendBadRequest(c, err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < len(documents); i++ {
|
|
||||||
var newArticle Article
|
|
||||||
bson.Unmarshal(documents[i], &newArticle)
|
|
||||||
articles = append(articles, newArticle)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, articles)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetArticle(c *gin.Context) {
|
|
||||||
articleID := c.Params.ByName("id")
|
|
||||||
articles, err := getDocuments(articlesLocation,
|
|
||||||
bson.D{{Key: "id_name", Value: articleID}})
|
|
||||||
if err != nil {
|
|
||||||
SendBadRequest(c, err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(articles) == 0 {
|
|
||||||
SendBadRequest(c, "The ID provided doesn't match any article.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var parsedArticle Article
|
|
||||||
bson.Unmarshal(articles[0], &parsedArticle)
|
|
||||||
c.JSON(http.StatusOK, parsedArticle)
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsArticleIdAlreadyUsed(id string) bool {
|
|
||||||
documents, _ := getDocuments(articlesLocation, bson.D{})
|
|
||||||
for i := 0; i < len(documents); i++ {
|
|
||||||
var newArticle Article
|
|
||||||
bson.Unmarshal(documents[i], &newArticle)
|
|
||||||
if newArticle.IdName == id {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func AddArticle(c *gin.Context) {
|
|
||||||
var article Article
|
|
||||||
article.IdName = c.Params.ByName("id")
|
|
||||||
if c.BindJSON(&article) != nil {
|
|
||||||
SendBadRequest(c, "Could not correctly parse the article.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
document, err := bson.Marshal(article)
|
|
||||||
if err != nil {
|
|
||||||
SendBadRequest(c, "Could not correctly marshal the article.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if IsArticleIdAlreadyUsed(article.IdName) {
|
|
||||||
SendBadRequest(c, "Article ID already used.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = pushDocument(articlesLocation, document)
|
|
||||||
if err != nil {
|
|
||||||
SendBadRequest(c, "Could not insert document into DB.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
SendOk(c, "Article successfully added!")
|
|
||||||
}
|
|
||||||
|
|
||||||
func DeleteArticle(c *gin.Context) {
|
|
||||||
var delArticle DelArticle
|
|
||||||
delArticle.IdName = c.Params.ByName("id")
|
|
||||||
if c.BindJSON(&delArticle) != nil {
|
|
||||||
SendBadRequest(c, "Could not correctly parse the article ID.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
document, err := bson.Marshal(delArticle)
|
|
||||||
if err != nil {
|
|
||||||
SendBadRequest(c, "Could not correctly marshal the article ID.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
deleteCount, err := deleteDocument(articlesLocation, document)
|
|
||||||
if err != nil {
|
|
||||||
SendBadRequest(c, "Could not insert document into DB.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
SendOk(c, fmt.Sprintf("%d articles were successfully deleted!", deleteCount))
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Article1, _ = bson.Marshal(Article{
|
||||||
|
IdName: "test_article_1",
|
||||||
|
Date: 1671482492,
|
||||||
|
Content: gin.H{},
|
||||||
|
})
|
||||||
|
|
||||||
|
var Article2, _ = bson.Marshal(Article{
|
||||||
|
IdName: "test_article_2",
|
||||||
|
Date: 1671899112,
|
||||||
|
Content: gin.H{},
|
||||||
|
})
|
||||||
|
|
||||||
|
var BSONConvertedArticles = [][]byte{Article1, Article2}
|
||||||
|
|
||||||
|
func TestGetAllArticlesBusiness(t *testing.T) {
|
||||||
|
documents := GetAllArticlesBusiness(BSONConvertedArticles)
|
||||||
|
article := documents[0]
|
||||||
|
|
||||||
|
if article.IdName != "test_article_1" {
|
||||||
|
log.Fatalf(`Excepted "test_article_1" as IdName, found "%v"`, article.IdName)
|
||||||
|
} else if article.Date != 1671482492 {
|
||||||
|
log.Fatalf(`Excepted 1671482492 as Date, found "%v"`, article.Date)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsArticleIdAlreadyUsed(t *testing.T) {
|
||||||
|
if IsArticleIdAlreadyUsed("test_article_1", BSONConvertedArticles) == false {
|
||||||
|
log.Fatalf(`Excepted true, found false`)
|
||||||
|
} else if IsArticleIdAlreadyUsed("test_article_3", BSONConvertedArticles) == true {
|
||||||
|
log.Fatalf(`Excepted false, found true`)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
package internal
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/joho/godotenv"
|
|
||||||
)
|
|
||||||
|
|
||||||
type pingFormat struct {
|
|
||||||
Message string `json:"message"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPing(t *testing.T) {
|
|
||||||
godotenv.Load("../.env")
|
|
||||||
|
|
||||||
r := gin.Default()
|
|
||||||
r.GET("/ping/", Ping)
|
|
||||||
go r.Run(":" + os.Getenv("API_PORT"))
|
|
||||||
|
|
||||||
req, err := http.Get(fmt.Sprintf("http://localhost:%v/ping/", os.Getenv("API_PORT")))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err.Error())
|
|
||||||
}
|
|
||||||
defer req.Body.Close()
|
|
||||||
|
|
||||||
var exceptedResp = pingFormat{}
|
|
||||||
body, _ := io.ReadAll(req.Body)
|
|
||||||
json.Unmarshal(body, &exceptedResp)
|
|
||||||
|
|
||||||
if exceptedResp.Message != "pong" {
|
|
||||||
t.Fatalf(`/ping/ resulted in "%v" instead of "pong"`, exceptedResp.Message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user