payload_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. package api // import "miniflux.app/api"
  5. import (
  6. "testing"
  7. "miniflux.app/model"
  8. )
  9. func TestUpdateFeedURL(t *testing.T) {
  10. feedURL := "http://example.com/"
  11. changes := &feedModification{FeedURL: &feedURL}
  12. feed := &model.Feed{FeedURL: "http://example.org/"}
  13. changes.Update(feed)
  14. if feed.FeedURL != feedURL {
  15. t.Fatalf(`Unexpected value, got %q instead of %q`, feed.FeedURL, feedURL)
  16. }
  17. }
  18. func TestUpdateFeedURLWithEmptyString(t *testing.T) {
  19. feedURL := ""
  20. changes := &feedModification{FeedURL: &feedURL}
  21. feed := &model.Feed{FeedURL: "http://example.org/"}
  22. changes.Update(feed)
  23. if feed.FeedURL == feedURL {
  24. t.Fatal(`The FeedURL should not be modified`)
  25. }
  26. }
  27. func TestUpdateFeedURLWhenNotSet(t *testing.T) {
  28. changes := &feedModification{}
  29. feed := &model.Feed{FeedURL: "http://example.org/"}
  30. changes.Update(feed)
  31. if feed.FeedURL != "http://example.org/" {
  32. t.Fatal(`The FeedURL should not be modified`)
  33. }
  34. }
  35. func TestUpdateFeedSiteURL(t *testing.T) {
  36. siteURL := "http://example.com/"
  37. changes := &feedModification{SiteURL: &siteURL}
  38. feed := &model.Feed{SiteURL: "http://example.org/"}
  39. changes.Update(feed)
  40. if feed.SiteURL != siteURL {
  41. t.Fatalf(`Unexpected value, got %q instead of %q`, feed.SiteURL, siteURL)
  42. }
  43. }
  44. func TestUpdateFeedSiteURLWithEmptyString(t *testing.T) {
  45. siteURL := ""
  46. changes := &feedModification{FeedURL: &siteURL}
  47. feed := &model.Feed{SiteURL: "http://example.org/"}
  48. changes.Update(feed)
  49. if feed.SiteURL == siteURL {
  50. t.Fatal(`The FeedURL should not be modified`)
  51. }
  52. }
  53. func TestUpdateFeedSiteURLWhenNotSet(t *testing.T) {
  54. changes := &feedModification{}
  55. feed := &model.Feed{SiteURL: "http://example.org/"}
  56. changes.Update(feed)
  57. if feed.SiteURL != "http://example.org/" {
  58. t.Fatal(`The SiteURL should not be modified`)
  59. }
  60. }
  61. func TestUpdateFeedTitle(t *testing.T) {
  62. title := "Example 2"
  63. changes := &feedModification{Title: &title}
  64. feed := &model.Feed{Title: "Example"}
  65. changes.Update(feed)
  66. if feed.Title != title {
  67. t.Fatalf(`Unexpected value, got %q instead of %q`, feed.Title, title)
  68. }
  69. }
  70. func TestUpdateFeedTitleWithEmptyString(t *testing.T) {
  71. title := ""
  72. changes := &feedModification{Title: &title}
  73. feed := &model.Feed{Title: "Example"}
  74. changes.Update(feed)
  75. if feed.Title == title {
  76. t.Fatal(`The Title should not be modified`)
  77. }
  78. }
  79. func TestUpdateFeedTitleWhenNotSet(t *testing.T) {
  80. changes := &feedModification{}
  81. feed := &model.Feed{Title: "Example"}
  82. changes.Update(feed)
  83. if feed.Title != "Example" {
  84. t.Fatal(`The Title should not be modified`)
  85. }
  86. }
  87. func TestUpdateFeedUsername(t *testing.T) {
  88. username := "Alice"
  89. changes := &feedModification{Username: &username}
  90. feed := &model.Feed{Username: "Bob"}
  91. changes.Update(feed)
  92. if feed.Username != username {
  93. t.Fatalf(`Unexpected value, got %q instead of %q`, feed.Username, username)
  94. }
  95. }
  96. func TestUpdateFeedUsernameWithEmptyString(t *testing.T) {
  97. username := ""
  98. changes := &feedModification{Username: &username}
  99. feed := &model.Feed{Username: "Bob"}
  100. changes.Update(feed)
  101. if feed.Username != "" {
  102. t.Fatal(`The Username should be empty now`)
  103. }
  104. }
  105. func TestUpdateFeedUsernameWhenNotSet(t *testing.T) {
  106. changes := &feedModification{}
  107. feed := &model.Feed{Username: "Alice"}
  108. changes.Update(feed)
  109. if feed.Username != "Alice" {
  110. t.Fatal(`The Username should not be modified`)
  111. }
  112. }
  113. func TestUpdateFeedDisabled(t *testing.T) {
  114. valueTrue := true
  115. valueFalse := false
  116. scenarios := []struct {
  117. changes *feedModification
  118. feed *model.Feed
  119. expected bool
  120. }{
  121. {&feedModification{}, &model.Feed{Disabled: true}, true},
  122. {&feedModification{Disabled: &valueTrue}, &model.Feed{Disabled: true}, true},
  123. {&feedModification{Disabled: &valueFalse}, &model.Feed{Disabled: true}, false},
  124. {&feedModification{}, &model.Feed{Disabled: false}, false},
  125. {&feedModification{Disabled: &valueTrue}, &model.Feed{Disabled: false}, true},
  126. {&feedModification{Disabled: &valueFalse}, &model.Feed{Disabled: false}, false},
  127. }
  128. for _, scenario := range scenarios {
  129. scenario.changes.Update(scenario.feed)
  130. if scenario.feed.Disabled != scenario.expected {
  131. t.Errorf(`Unexpected result, got %v, want: %v`,
  132. scenario.feed.Disabled,
  133. scenario.expected,
  134. )
  135. }
  136. }
  137. }
  138. func TestUpdateFeedCategory(t *testing.T) {
  139. categoryID := int64(1)
  140. changes := &feedModification{CategoryID: &categoryID}
  141. feed := &model.Feed{Category: &model.Category{ID: 42}}
  142. changes.Update(feed)
  143. if feed.Category.ID != categoryID {
  144. t.Fatalf(`Unexpected value, got %q instead of %q`, feed.Username, categoryID)
  145. }
  146. }
  147. func TestUpdateFeedCategoryWithZero(t *testing.T) {
  148. categoryID := int64(0)
  149. changes := &feedModification{CategoryID: &categoryID}
  150. feed := &model.Feed{Category: &model.Category{ID: 42}}
  151. changes.Update(feed)
  152. if feed.Category.ID != 42 {
  153. t.Fatal(`The CategoryID should not be modified`)
  154. }
  155. }
  156. func TestUpdateFeedCategoryWhenNotSet(t *testing.T) {
  157. changes := &feedModification{}
  158. feed := &model.Feed{Category: &model.Category{ID: 42}}
  159. changes.Update(feed)
  160. if feed.Category.ID != 42 {
  161. t.Fatal(`The CategoryID should not be modified`)
  162. }
  163. }
  164. func TestUpdateUserTheme(t *testing.T) {
  165. theme := "Example 2"
  166. changes := &userModification{Theme: &theme}
  167. user := &model.User{Theme: "Example"}
  168. changes.Update(user)
  169. if user.Theme != theme {
  170. t.Fatalf(`Unexpected value, got %q instead of %q`, user.Theme, theme)
  171. }
  172. }
  173. func TestUserThemeWhenNotSet(t *testing.T) {
  174. changes := &userModification{}
  175. user := &model.User{Theme: "Example"}
  176. changes.Update(user)
  177. if user.Theme != "Example" {
  178. t.Fatal(`The user Theme should not be modified`)
  179. }
  180. }