category_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. // +build integration
  5. package tests
  6. import (
  7. "testing"
  8. )
  9. func TestCreateCategory(t *testing.T) {
  10. categoryName := "My category"
  11. client := createClient(t)
  12. category, err := client.CreateCategory(categoryName)
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. if category.ID == 0 {
  17. t.Fatalf(`Invalid categoryID, got "%v"`, category.ID)
  18. }
  19. if category.UserID <= 0 {
  20. t.Fatalf(`Invalid userID, got "%v"`, category.UserID)
  21. }
  22. if category.Title != categoryName {
  23. t.Fatalf(`Invalid title, got "%v" instead of "%v"`, category.Title, categoryName)
  24. }
  25. }
  26. func TestCreateCategoryWithEmptyTitle(t *testing.T) {
  27. client := createClient(t)
  28. _, err := client.CreateCategory("")
  29. if err == nil {
  30. t.Fatal(`The category title should be mandatory`)
  31. }
  32. }
  33. func TestCannotCreateDuplicatedCategory(t *testing.T) {
  34. client := createClient(t)
  35. categoryName := "My category"
  36. _, err := client.CreateCategory(categoryName)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. _, err = client.CreateCategory(categoryName)
  41. if err == nil {
  42. t.Fatal(`Duplicated categories should not be allowed`)
  43. }
  44. }
  45. func TestUpdateCategory(t *testing.T) {
  46. categoryName := "My category"
  47. client := createClient(t)
  48. category, err := client.CreateCategory(categoryName)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. categoryName = "Updated category"
  53. category, err = client.UpdateCategory(category.ID, categoryName)
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. if category.ID == 0 {
  58. t.Fatalf(`Invalid categoryID, got "%v"`, category.ID)
  59. }
  60. if category.UserID <= 0 {
  61. t.Fatalf(`Invalid userID, got "%v"`, category.UserID)
  62. }
  63. if category.Title != categoryName {
  64. t.Fatalf(`Invalid title, got "%v" instead of "%v"`, category.Title, categoryName)
  65. }
  66. }
  67. func TestListCategories(t *testing.T) {
  68. categoryName := "My category"
  69. client := createClient(t)
  70. _, err := client.CreateCategory(categoryName)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. categories, err := client.Categories()
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. if len(categories) != 2 {
  79. t.Fatalf(`Invalid number of categories, got "%v" instead of "%v"`, len(categories), 2)
  80. }
  81. if categories[0].ID == 0 {
  82. t.Fatalf(`Invalid categoryID, got "%v"`, categories[0].ID)
  83. }
  84. if categories[0].UserID <= 0 {
  85. t.Fatalf(`Invalid userID, got "%v"`, categories[0].UserID)
  86. }
  87. if categories[0].Title != "All" {
  88. t.Fatalf(`Invalid title, got "%v" instead of "%v"`, categories[0].Title, "All")
  89. }
  90. if categories[1].ID == 0 {
  91. t.Fatalf(`Invalid categoryID, got "%v"`, categories[0].ID)
  92. }
  93. if categories[1].UserID <= 0 {
  94. t.Fatalf(`Invalid userID, got "%v"`, categories[1].UserID)
  95. }
  96. if categories[1].Title != categoryName {
  97. t.Fatalf(`Invalid title, got "%v" instead of "%v"`, categories[1].Title, categoryName)
  98. }
  99. }
  100. func TestDeleteCategory(t *testing.T) {
  101. client := createClient(t)
  102. category, err := client.CreateCategory("My category")
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. err = client.DeleteCategory(category.ID)
  107. if err != nil {
  108. t.Fatal(`Removing a category should not raise any error`)
  109. }
  110. }
  111. func TestCannotDeleteCategoryOfAnotherUser(t *testing.T) {
  112. client := createClient(t)
  113. categories, err := client.Categories()
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. client = createClient(t)
  118. err = client.DeleteCategory(categories[0].ID)
  119. if err == nil {
  120. t.Fatal(`Removing a category that belongs to another user should be forbidden`)
  121. }
  122. }