Browse Source

added category test

master
Lars Hoogestraat 3 years ago
parent
commit
b41f66bfe5
  1. 1
      controllers/account_test.go
  2. 4
      controllers/article.go
  3. 2
      controllers/article_test.go
  4. 32
      controllers/category.go
  5. 175
      controllers/category_test.go
  6. 13
      controllers/file_test.go
  7. 8
      controllers/login_test.go
  8. 25
      controllers/site_test.go
  9. 13
      controllers/user_invite_test.go
  10. 7
      controllers/user_test.go
  11. 7
      controllers/utils_test.go
  12. 16
      models/category.go
  13. 33
      models/category_sqlite.go
  14. 1
      routers/router.go

1
controllers/account_test.go

@ -0,0 +1 @@
package controllers_test

4
controllers/article.go

@ -146,7 +146,7 @@ func ListArticlesCategoryHandler(ctx *middleware.AppContext, w http.ResponseWrit
category := getVar(r, "categorySlug")
c, err := ctx.CategoryService.GetBySlug(category)
c, err := ctx.CategoryService.GetBySlug(category, models.CategoriesWithPublishedArticles)
if err != nil {
return &middleware.Template{
@ -219,7 +219,7 @@ func IndexArticlesCategoryHandler(ctx *middleware.AppContext, w http.ResponseWri
category := getVar(r, "categorySlug")
c, err := ctx.CategoryService.GetBySlug(category)
c, err := ctx.CategoryService.GetBySlug(category, models.CategoriesWithPublishedArticles)
if err != nil {
return &middleware.Template{

2
controllers/article_test.go

@ -20,6 +20,8 @@ import (
func TestArticleWorkflow(t *testing.T) {
setup(t)
defer teardown()
artID, err := doAdminCreateArticleRequest(rAdminUser, getSampleArticle())
if err != nil {

32
controllers/category.go

@ -31,6 +31,34 @@ func AdminListCategoriesHandler(ctx *middleware.AppContext, w http.ResponseWrite
}}
}
func AdminGetCategoryHandler(ctx *middleware.AppContext, w http.ResponseWriter, r *http.Request) *middleware.Template {
reqVar := getVar(r, "categoryID")
id, err := parseInt(reqVar)
if err != nil {
return &middleware.Template{
Name: tplAdminCategories,
Err: err,
}
}
c, err := ctx.CategoryService.GetByID(id, models.AllCategories)
if err != nil {
return &middleware.Template{
Name: tplAdminCategories,
Err: err,
}
}
return &middleware.Template{
Name: tplAdminCategories,
Active: "categories",
Data: map[string]interface{}{
"category": c,
}}
}
// AdminCategoryNewHandler returns the template which shows the form to create a new article
func AdminCategoryNewHandler(ctx *middleware.AppContext, w http.ResponseWriter, r *http.Request) *middleware.Template {
return &middleware.Template{
@ -82,7 +110,7 @@ func AdminCategoryEditHandler(ctx *middleware.AppContext, w http.ResponseWriter,
}
}
c, err := ctx.CategoryService.GetByID(id)
c, err := ctx.CategoryService.GetByID(id, models.AllCategories)
if err != nil {
return &middleware.Template{
@ -154,7 +182,7 @@ func AdminCategoryDeleteHandler(ctx *middleware.AppContext, w http.ResponseWrite
}
}
c, err := ctx.CategoryService.GetByID(id)
c, err := ctx.CategoryService.GetByID(id, models.AllCategories)
if err != nil {
return &middleware.Template{

175
controllers/category_test.go

@ -0,0 +1,175 @@
package controllers_test
import (
"net/http/httptest"
"net/url"
"strconv"
"testing"
"git.hoogi.eu/go-blog/controllers"
"git.hoogi.eu/go-blog/models"
)
func TestCategoryWorkflow(t *testing.T) {
setup(t)
defer teardown()
c := &models.Category{
Name: "My Category",
}
id, err := doAdminCategoryNewRequest(rAdminUser, c)
if err != nil {
t.Fatal(err)
}
rcvCategory, err := doAdminGetCategoryRequest(rAdminUser, id)
if err != nil {
t.Fatal(err)
}
if rcvCategory.Name != c.Name {
t.Fatalf("the category name is wrong. expected: %s, actual: %s", c.Name, rcvCategory.Name)
}
c.ID = id
c.Name = "Updated Category"
err = doAdminCategoryEditRequest(rAdminUser, c)
if err != nil {
t.Fatal(err)
}
rcvCategory, err = doAdminGetCategoryRequest(rAdminUser, id)
if err != nil {
t.Fatal(err)
}
if rcvCategory.Name != c.Name {
t.Fatalf("the category name is wrong. expected: %s, actual: %s", c.Name, rcvCategory.Name)
}
err = doAdminDeleteCategoryRequest(rAdminUser, id)
if err != nil {
t.Fatal(err)
}
rcvCategory, err = doAdminGetCategoryRequest(rAdminUser, id)
if err == nil {
t.Fatalf("removed cateogry, but got a category %v", rcvCategory)
}
}
func doAdminGetCategoryRequest(user reqUser, categoryID int) (*models.Category, error) {
r := request{
url: "/admin/category/" + strconv.Itoa(categoryID),
user: user,
method: "GET",
pathVar: []pathVar{
pathVar{
key: "categoryID",
value: strconv.Itoa(categoryID),
},
},
}
rw := httptest.NewRecorder()
tpl := controllers.AdminGetCategoryHandler(ctx, rw, r.buildRequest())
if tpl.Err != nil {
return nil, tpl.Err
}
return tpl.Data["category"].(*models.Category), nil
}
func doAdminListCategoriesRequest(user reqUser) ([]models.Category, error) {
r := request{
url: "/admin/categories/",
user: user,
method: "GET",
}
rw := httptest.NewRecorder()
tpl := controllers.AdminListCategoriesHandler(ctx, rw, r.buildRequest())
if tpl.Err != nil {
return nil, tpl.Err
}
return tpl.Data["categories"].([]models.Category), nil
}
func doAdminCategoryNewRequest(user reqUser, c *models.Category) (int, error) {
values := url.Values{}
addValue(values, "name", c.Name)
r := request{
url: "/admin/category/new",
user: user,
method: "POST",
values: values,
}
rw := httptest.NewRecorder()
tpl := controllers.AdminCategoryNewPostHandler(ctx, rw, r.buildRequest())
if tpl.Err != nil {
return -1, tpl.Err
}
return tpl.Data["categoryID"].(int), nil
}
func doAdminCategoryEditRequest(user reqUser, c *models.Category) error {
values := url.Values{}
addValue(values, "name", c.Name)
r := request{
url: "/admin/category/edit/" + strconv.Itoa(c.ID),
user: user,
method: "POST",
values: values,
pathVar: []pathVar{
pathVar{
key: "categoryID",
value: strconv.Itoa(c.ID),
},
},
}
rw := httptest.NewRecorder()
tpl := controllers.AdminCategoryEditPostHandler(ctx, rw, r.buildRequest())
if tpl.Err != nil {
return tpl.Err
}
return nil
}
func doAdminDeleteCategoryRequest(user reqUser, categoryID int) error {
r := request{
url: "/admin/category/" + strconv.Itoa(categoryID),
user: user,
method: "GET",
pathVar: []pathVar{
pathVar{
key: "categoryID",
value: strconv.Itoa(categoryID),
},
},
}
rw := httptest.NewRecorder()
tpl := controllers.AdminCategoryDeletePostHandler(ctx, rw, r.buildRequest())
if tpl.Err != nil {
return tpl.Err
}
return nil
}

13
controllers/file_test.go

@ -0,0 +1,13 @@
package controllers_test
func doAdminListFilesRequest(user reqUser) {
}
func doAdminUploadFileRequest(user reqUser) {
}
func doAdminUploadDeleteRequest(user reqUser) {
}

8
controllers/login_test.go

@ -10,15 +10,17 @@ import (
)
func TestLogin(t *testing.T) {
setup(t)
defer teardown()
err := login("alice", "123456789012")
if err != nil {
t.Error(err)
}
}
func TestFailLogin(t *testing.T) {
err := login("alice", "test2")
err = login("alice", "test2")
if err == nil {
t.Error("expected a failed login, but error is nil")

25
controllers/site_test.go

@ -0,0 +1,25 @@
package controllers_test
func doAdminListSitesRequest(user reqUser) {
}
func doAdminSiteCreateRequest(user reqUser) {
}
func doAdminSitePublishRequest(user reqUser) {
}
func doAdminSiteEditRequest(user reqUser) {
}
func doAdminSiteDeleteRequest(user reqUser) {
}
func doAdminSiteOrderRequest(user reqUser) {
}

13
controllers/user_invite_test.go

@ -0,0 +1,13 @@
package controllers_test
func doAdminCreateUserInviteRequest(user reqUser) {
}
func doAdminResendUserInviteRequest(user reqUser) {
}
func doAdminRemoveUserInviteRequest(user reqUser) {
}

7
controllers/user_test.go

@ -16,6 +16,10 @@ import (
)
func TestUserWorklfow(t *testing.T) {
setup(t)
defer teardown()
expectedUser := &models.User{
DisplayName: "Homer Simpson",
Email: "homer@example.com",
@ -83,9 +87,6 @@ func checkUser(user, expectedUser *models.User) error {
if user.Email != expectedUser.Email {
return fmt.Errorf("got an unexpected email. expected: %s, actual: %s", expectedUser.Email, user.Email)
}
if user.ID != expectedUser.ID {
return fmt.Errorf("got an unexpected id. expected: %d, actual: %d", expectedUser.ID, user.ID)
}
if user.Active != expectedUser.Active {
return fmt.Errorf("got an unexpected active. expected: %t, actual: %t", expectedUser.Active, user.Active)
}

7
controllers/utils_test.go

@ -27,6 +27,7 @@ import (
)
var ctx *middleware.AppContext
var db *sql.DB
func setup(t *testing.T) {
logger.InitLogger(ioutil.Discard, "Debug")
@ -122,6 +123,12 @@ func setup(t *testing.T) {
}
}
func teardown() {
if db != nil {
db.Close()
}
}
func fillSeeds(db *sql.DB) error {
salt := utils.GenerateSalt()
saltedPassword := utils.AppendBytes([]byte("123456789012"), salt)

16
models/category.go

@ -46,8 +46,8 @@ type CategoryDatasourceService interface {
Create(c *Category) (int, error)
List(fc FilterCriteria) ([]Category, error)
Count(fc FilterCriteria) (int, error)
Get(categoryID int) (*Category, error)
GetBySlug(slug string) (*Category, error)
Get(categoryID int, fc FilterCriteria) (*Category, error)
GetBySlug(slug string, fc FilterCriteria) (*Category, error)
Update(c *Category) error
Delete(categoryID int) error
}
@ -62,8 +62,8 @@ func (c Category) SlugEscape() string {
return url.PathEscape(c.Slug)
}
func (cs CategoryService) GetBySlug(s string) (*Category, error) {
c, err := cs.Datasource.GetBySlug(s)
func (cs CategoryService) GetBySlug(s string, fc FilterCriteria) (*Category, error) {
c, err := cs.Datasource.GetBySlug(s, fc)
if err != nil {
if err == sql.ErrNoRows {
@ -75,8 +75,8 @@ func (cs CategoryService) GetBySlug(s string) (*Category, error) {
return c, nil
}
func (cs CategoryService) GetByID(id int) (*Category, error) {
c, err := cs.Datasource.Get(id)
func (cs CategoryService) GetByID(id int, fc FilterCriteria) (*Category, error) {
c, err := cs.Datasource.Get(id, fc)
if err != nil {
if err == sql.ErrNoRows {
@ -100,7 +100,7 @@ func (cs CategoryService) List(fc FilterCriteria) ([]Category, error) {
func (cs CategoryService) Create(c *Category) (int, error) {
for i := 0; i < 10; i++ {
c.Slug = utils.CreateURLSafeSlug(c.Name, i)
_, err := cs.Datasource.GetBySlug(c.Slug)
_, err := cs.Datasource.GetBySlug(c.Slug, AllCategories)
if err != nil {
if err == sql.ErrNoRows {
@ -133,7 +133,7 @@ func (cs CategoryService) Update(c *Category) error {
//Delete removes a category
func (cs CategoryService) Delete(id int) error {
c, err := cs.Datasource.Get(id)
c, err := cs.Datasource.Get(id, AllCategories)
if err != nil {
return err

33
models/category_sqlite.go

@ -95,7 +95,7 @@ func (rdb SQLiteCategoryDatasource) Count(fc FilterCriteria) (int, error) {
return total, nil
}
func (rdb SQLiteCategoryDatasource) Get(categoryID int) (*Category, error) {
func (rdb SQLiteCategoryDatasource) Get(categoryID int, fc FilterCriteria) (*Category, error) {
var stmt bytes.Buffer
stmt.WriteString("SELECT c.id, c.name, c.slug, c.last_modified, ")
@ -103,7 +103,20 @@ func (rdb SQLiteCategoryDatasource) Get(categoryID int) (*Category, error) {
stmt.WriteString("FROM category as c ")
stmt.WriteString("INNER JOIN user as u ")
stmt.WriteString("ON u.id = c.user_id ")
stmt.WriteString("WHERE c.id=? ")
if fc == CategoriesWithPublishedArticles {
stmt.WriteString("INNER JOIN article as a ")
stmt.WriteString("ON c.id = a.category_id ")
stmt.WriteString("WHERE a.published = true ")
stmt.WriteString("AND c.id=? ")
} else if fc == CategoriesWithoutArticles {
stmt.WriteString("LEFT JOIN article as a ")
stmt.WriteString("ON c.id = a.category_id ")
stmt.WriteString("WHERE a.categorie_id IS NULL ")
stmt.WriteString("AND c.id=? ")
} else {
stmt.WriteString("WHERE c.id=? ")
}
var c Category
var ru User
@ -118,7 +131,7 @@ func (rdb SQLiteCategoryDatasource) Get(categoryID int) (*Category, error) {
return &c, nil
}
func (rdb SQLiteCategoryDatasource) GetBySlug(slug string) (*Category, error) {
func (rdb SQLiteCategoryDatasource) GetBySlug(slug string, fc FilterCriteria) (*Category, error) {
var stmt bytes.Buffer
stmt.WriteString("SELECT c.id, c.name, c.slug, c.last_modified, ")
@ -126,7 +139,19 @@ func (rdb SQLiteCategoryDatasource) GetBySlug(slug string) (*Category, error) {
stmt.WriteString("FROM category as c ")
stmt.WriteString("INNER JOIN user as u ")
stmt.WriteString("ON u.id = c.user_id ")
stmt.WriteString("WHERE c.slug=? ")
if fc == CategoriesWithPublishedArticles {
stmt.WriteString("INNER JOIN article as a ")
stmt.WriteString("ON c.id = a.category_id ")
stmt.WriteString("WHERE a.published = true ")
stmt.WriteString("WHERE c.slug=? ")
} else if fc == CategoriesWithoutArticles {
stmt.WriteString("LEFT JOIN article as a ")
stmt.WriteString("ON c.id = a.category_id ")
stmt.WriteString("WHERE a.categorie_id IS NULL ")
stmt.WriteString("WHERE c.slug=? ")
} else {
stmt.WriteString("WHERE c.slug=? ")
}
var c Category
var ru User

1
routers/router.go

@ -124,6 +124,7 @@ func restrictedRoutes(ctx *m.AppContext, router *mux.Router, chain alice.Chain)
//article
router.Handle("/categories", chain.Then(useTemplateHandler(ctx, c.AdminListCategoriesHandler))).Methods("GET")
router.Handle("/category/{categoryID}", chain.Then(useTemplateHandler(ctx, c.AdminGetCategoryHandler))).Methods("POST")
router.Handle("/category/new", chain.Then(useTemplateHandler(ctx, c.AdminCategoryNewHandler))).Methods("GET")
router.Handle("/category/new", chain.Then(useTemplateHandler(ctx, c.AdminCategoryNewPostHandler))).Methods("POST")
router.Handle("/category/edit/{categoryID}", chain.Then(useTemplateHandler(ctx, c.AdminCategoryEditHandler))).Methods("GET")

Loading…
Cancel
Save