mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
clean: Article to Post in domain and related use cases
Renamed the 'Article' domain to 'Post' in order to better reflect the purpose and utility of the object. The change involved renaming all related use cases, classes, and other references from 'Article' to 'Post' and updating all functions and data types to work with Posts instead of Articles.
This commit is contained in:
@@ -1,144 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"GohCMS2/domain/article"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var TestCreateArticleSuccess = func(t *testing.T) {
|
||||
jsonBody, err := json.Marshal(map[string]string{
|
||||
"title": "Test Title",
|
||||
"body": "Test Body",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r, _ := ApiRequest("POST", "/article", bytes.NewBuffer(jsonBody))
|
||||
|
||||
var response article.Article
|
||||
bd, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(bd, &response)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, http.StatusOK, r.StatusCode)
|
||||
assert.Equal(t, "Test Title", response.Title)
|
||||
assert.Equal(t, "Test Body", response.Body)
|
||||
}
|
||||
|
||||
var TestCreateArticleFailTitleMissing = func(t *testing.T) {
|
||||
jsonBody, err := json.Marshal(map[string]string{
|
||||
"body": "Test Body",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r, _ := ApiRequest("POST", "/article", bytes.NewBuffer(jsonBody))
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, r.StatusCode)
|
||||
}
|
||||
|
||||
var TestCreateArticleTitleTooShort = func(t *testing.T) {
|
||||
jsonBody, err := json.Marshal(map[string]string{
|
||||
"title": "Te",
|
||||
"body": "Test Body",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r, _ := ApiRequest("POST", "/article", bytes.NewBuffer(jsonBody))
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, r.StatusCode)
|
||||
}
|
||||
|
||||
var TestGetArticleSuccess = func(t *testing.T) {
|
||||
var createdArticle article.Article
|
||||
var articleToCreate = article.Article{
|
||||
Title: "Test Title",
|
||||
Body: "Test Body",
|
||||
}
|
||||
db := GetDb()
|
||||
db.Create(&articleToCreate).Scan(&createdArticle)
|
||||
|
||||
r, _ := ApiRequest("GET", "/article/"+strconv.Itoa(int(createdArticle.ID)), nil)
|
||||
|
||||
var response article.Article
|
||||
bd, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(bd, &response)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, http.StatusOK, r.StatusCode)
|
||||
assert.Equal(t, createdArticle.ID, response.ID)
|
||||
assert.Equal(t, createdArticle.Title, response.Title)
|
||||
assert.Equal(t, createdArticle.Body, response.Body)
|
||||
}
|
||||
|
||||
var TestGetAllArticlesSuccess = func(t *testing.T) {
|
||||
var createdArticle article.Article
|
||||
var articleToCreate = article.Article{
|
||||
Title: "Test Title",
|
||||
Body: "Test Body",
|
||||
}
|
||||
db := GetDb()
|
||||
db.Create(&articleToCreate).Scan(&createdArticle)
|
||||
|
||||
r, _ := ApiRequest("GET", "/article", nil)
|
||||
|
||||
var response []article.Article
|
||||
bd, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(bd, &response)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, http.StatusOK, r.StatusCode)
|
||||
assert.Equal(t, createdArticle.Title, response[0].Title)
|
||||
assert.Equal(t, createdArticle.Body, response[0].Body)
|
||||
}
|
||||
|
||||
var TestArticleCreate = func(t *testing.T) {
|
||||
t.Run("Should return an article with the given title and body", TestCreateArticleSuccess)
|
||||
t.Run("Should return an error if the title is missing", TestCreateArticleFailTitleMissing)
|
||||
t.Run("Should return an error if the title is too short", TestCreateArticleTitleTooShort)
|
||||
}
|
||||
|
||||
var TestArticleGet = func(t *testing.T) {
|
||||
t.Run("Should return an article with the given id", TestGetArticleSuccess)
|
||||
}
|
||||
|
||||
var TestArticleGetAll = func(t *testing.T) {
|
||||
t.Run("Should return all articles", TestGetAllArticlesSuccess)
|
||||
}
|
||||
|
||||
func TestArticle(t *testing.T) {
|
||||
StartServerIfNotAlready()
|
||||
WaitForServer()
|
||||
|
||||
t.Run("Create", TestArticleCreate)
|
||||
t.Run("Get", TestArticleGet)
|
||||
t.Run("GetAll", TestArticleGetAll)
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"GohCMS2/domain/post"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var TestCreatePostSuccess = func(t *testing.T) {
|
||||
jsonBody, err := json.Marshal(map[string]string{
|
||||
"title": "Test Title",
|
||||
"body": "Test Body",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r, _ := ApiRequest("POST", "/post", bytes.NewBuffer(jsonBody))
|
||||
|
||||
var response post.Post
|
||||
bd, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(bd, &response)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, http.StatusOK, r.StatusCode)
|
||||
assert.Equal(t, "Test Title", response.Title)
|
||||
assert.Equal(t, "Test Body", response.Body)
|
||||
}
|
||||
|
||||
var TestCreatePostFailTitleMissing = func(t *testing.T) {
|
||||
jsonBody, err := json.Marshal(map[string]string{
|
||||
"body": "Test Body",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r, _ := ApiRequest("POST", "/post", bytes.NewBuffer(jsonBody))
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, r.StatusCode)
|
||||
}
|
||||
|
||||
var TestCreatePostTitleTooShort = func(t *testing.T) {
|
||||
jsonBody, err := json.Marshal(map[string]string{
|
||||
"title": "Te",
|
||||
"body": "Test Body",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r, _ := ApiRequest("POST", "/post", bytes.NewBuffer(jsonBody))
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, r.StatusCode)
|
||||
}
|
||||
|
||||
var TestGetPostSuccess = func(t *testing.T) {
|
||||
var createdPost post.Post
|
||||
var articleToCreate = post.Post{
|
||||
Title: "Test Title",
|
||||
Body: "Test Body",
|
||||
}
|
||||
db := GetDb()
|
||||
db.Create(&articleToCreate).Scan(&createdPost)
|
||||
|
||||
r, _ := ApiRequest("GET", "/post/"+strconv.Itoa(int(createdPost.ID)), nil)
|
||||
|
||||
var response post.Post
|
||||
bd, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(bd, &response)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, http.StatusOK, r.StatusCode)
|
||||
assert.Equal(t, createdPost.ID, response.ID)
|
||||
assert.Equal(t, createdPost.Title, response.Title)
|
||||
assert.Equal(t, createdPost.Body, response.Body)
|
||||
}
|
||||
|
||||
var TestGetAllPostsSuccess = func(t *testing.T) {
|
||||
var createdPost post.Post
|
||||
var articleToCreate = post.Post{
|
||||
Title: "Test Title",
|
||||
Body: "Test Body",
|
||||
}
|
||||
db := GetDb()
|
||||
db.Create(&articleToCreate).Scan(&createdPost)
|
||||
|
||||
r, _ := ApiRequest("GET", "/post", nil)
|
||||
|
||||
var response []post.Post
|
||||
bd, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(bd, &response)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Equal(t, http.StatusOK, r.StatusCode)
|
||||
assert.Equal(t, createdPost.Title, response[0].Title)
|
||||
assert.Equal(t, createdPost.Body, response[0].Body)
|
||||
}
|
||||
|
||||
var TestPostCreate = func(t *testing.T) {
|
||||
t.Run("Should return an post with the given title and body", TestCreatePostSuccess)
|
||||
t.Run("Should return an error if the title is missing", TestCreatePostFailTitleMissing)
|
||||
t.Run("Should return an error if the title is too short", TestCreatePostTitleTooShort)
|
||||
}
|
||||
|
||||
var TestPostGet = func(t *testing.T) {
|
||||
t.Run("Should return an post with the given id", TestGetPostSuccess)
|
||||
}
|
||||
|
||||
var TestPostGetAll = func(t *testing.T) {
|
||||
t.Run("Should return all articles", TestGetAllPostsSuccess)
|
||||
}
|
||||
|
||||
func TestPost(t *testing.T) {
|
||||
StartServerIfNotAlready()
|
||||
WaitForServer()
|
||||
|
||||
t.Run("Create", TestPostCreate)
|
||||
t.Run("Get", TestPostGet)
|
||||
t.Run("GetAll", TestPostGetAll)
|
||||
}
|
||||
+6
-4
@@ -12,16 +12,18 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const testDbFile = "test.db"
|
||||
|
||||
var ApiUrl string
|
||||
var AuthorizationCookie *http.Cookie
|
||||
var HttpClient = http.Client{}
|
||||
|
||||
func GetDb() *gorm.DB {
|
||||
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
|
||||
db, err := gorm.Open(sqlite.Open(testDbFile), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
_ = db.AutoMigrate(&models.Article{}, &models.User{})
|
||||
_ = db.AutoMigrate(&models.Post{}, &models.User{})
|
||||
return db
|
||||
}
|
||||
|
||||
@@ -30,8 +32,8 @@ func StartServerIfNotAlready() {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
_ = os.Remove("test.db")
|
||||
_ = os.Setenv("DB_FILE", "test.db")
|
||||
_ = os.Remove(testDbFile)
|
||||
_ = os.Setenv("DB_FILE", testDbFile)
|
||||
go func(url *string) {
|
||||
router := server.InitServer()
|
||||
*url = "http://localhost:" + os.Getenv("PORT") + "/v1"
|
||||
|
||||
Reference in New Issue
Block a user