The backend for the AiteStory website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

292 lines
7.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. package model
  2. import (
  3. "testing"
  4. "time"
  5. "git.aiterp.net/AiteRP/aitestory/server"
  6. "git.aiterp.net/gisle/wrouter/generate"
  7. )
  8. var testPageTags = []*Tag{
  9. &Tag{"", "Event", "Skipping Work (Test)"},
  10. &Tag{"", "Location", "Redrock's Office (Test)"},
  11. &Tag{"", "Character", "Renala T'Iavay (Test)"},
  12. &Tag{"", "Character", "Senva T'Vaoma (Test)"},
  13. }
  14. var extraPageTag = &Tag{"", "Character", "Va'ynna Atana (Test)"}
  15. var fictionalDate, _ = time.Parse("2006-01-02", "2185-07-25")
  16. var postingDate, _ = time.Parse("2006-01-02", "2017-09-13")
  17. var editDate, _ = time.Parse("2006-01-02", "2017-09-15")
  18. var testPage = Page{
  19. Name: "Returning Va'ynna's Omni-Tool (Test)",
  20. Author: "test:Gisle",
  21. Category: "Story",
  22. FictionalDate: fictionalDate,
  23. PublishDate: postingDate,
  24. EditDate: editDate,
  25. Dated: true,
  26. Published: true,
  27. Unlisted: false,
  28. Specific: false,
  29. Indexed: true,
  30. BackgroundURL: "",
  31. Type: "Markdown",
  32. Source: "# Returning Va'ynna's Omni-Tool",
  33. Tags: make([]Tag, 0, 4),
  34. }
  35. func TestPage(t *testing.T) {
  36. var assertEquals = func(t *testing.T, label string, a, b interface{}) {
  37. if a != b {
  38. t.Errorf("Assert Failed (%s): \"%+v\" == \"%+v\"", label, a, b)
  39. }
  40. }
  41. if server.Main.Config.DB.Password == "" {
  42. t.Skip("No database password")
  43. return
  44. }
  45. t.Run("SetupTags", func(t *testing.T) {
  46. for _, tag := range testPageTags {
  47. err := tag.Insert()
  48. if err != nil {
  49. t.Error(err)
  50. }
  51. testPage.Tags = append(testPage.Tags, *tag)
  52. }
  53. err := extraPageTag.Insert()
  54. if err != nil {
  55. t.Error(err)
  56. }
  57. })
  58. t.Run("BasicConstants", func(t *testing.T) {
  59. if PageMinDate.Format(time.RFC3339) != "1753-01-01T00:00:00Z" {
  60. t.Error("Invalid date:", PageMinDate.Format(time.RFC3339))
  61. }
  62. page := Page{}
  63. page.generateID()
  64. if len(page.ID) != 16 {
  65. t.Errorf("len(page.ID): %d != 16", len(page.ID))
  66. }
  67. id1 := page.ID
  68. page.generateID()
  69. id2 := page.ID
  70. t.Logf("Page IDs: %s, %s (should not be the same)", id1, id2)
  71. if id1 == id2 {
  72. t.Fail()
  73. }
  74. })
  75. t.Run("Insert", func(t *testing.T) {
  76. err := testPage.Insert()
  77. if err != nil {
  78. t.Error(err)
  79. }
  80. t.Logf("testPage.ID = \"%s\"", testPage.ID)
  81. if testPage.ID == "" {
  82. t.Fail()
  83. }
  84. })
  85. t.Run("ListHeaders", func(t *testing.T) {
  86. headers, err := ListHeaders()
  87. if err != nil {
  88. t.Error(err)
  89. }
  90. t.Logf("Got %d headers", len(headers))
  91. found := false
  92. for _, header := range headers {
  93. if header.ID == testPage.ID {
  94. found = true
  95. t.Logf("Found header: %+v", header)
  96. assertEquals(t, "Name", header.Name, testPage.Name)
  97. assertEquals(t, "Author", header.Author, testPage.Author)
  98. assertEquals(t, "Category", header.Category, testPage.Category)
  99. assertEquals(t, "PublishDate", header.PublishDate, testPage.PublishDate)
  100. assertEquals(t, "EditDate", header.EditDate, testPage.EditDate)
  101. assertEquals(t, "FictionalDate", header.FictionalDate, testPage.FictionalDate)
  102. assertEquals(t, "Dated", header.Dated, testPage.Dated)
  103. assertEquals(t, "PrimaryTag.ID", header.PrimaryTag.ID, testPageTags[0].ID)
  104. }
  105. }
  106. if !found {
  107. t.Error("Did not find the inserted page's header")
  108. }
  109. })
  110. t.Run("ListHeadersByTag", func(t *testing.T) {
  111. headers, err := ListHeadersByTag("", testPageTags[1])
  112. if err != nil {
  113. t.Error(err)
  114. }
  115. t.Logf("Got %d headers", len(headers))
  116. found := false
  117. for _, header := range headers {
  118. if header.ID == testPage.ID {
  119. found = true
  120. t.Logf("Found header: %+v", header)
  121. assertEquals(t, "Name", header.Name, testPage.Name)
  122. assertEquals(t, "Author", header.Author, testPage.Author)
  123. assertEquals(t, "Category", header.Category, testPage.Category)
  124. assertEquals(t, "PublishDate", header.PublishDate, testPage.PublishDate)
  125. assertEquals(t, "EditDate", header.EditDate, testPage.EditDate)
  126. assertEquals(t, "FictionalDate", header.FictionalDate, testPage.FictionalDate)
  127. assertEquals(t, "Dated", header.Dated, testPage.Dated)
  128. assertEquals(t, "PrimaryTag.ID", header.PrimaryTag.ID, testPageTags[0].ID)
  129. }
  130. }
  131. if !found {
  132. t.Error("Did not find the inserted page's header")
  133. }
  134. // Make a fake tag and make sure that doesn't return stuff
  135. headers, err = ListHeadersByTag("", &Tag{ID: generate.ID()})
  136. if err != nil {
  137. t.Error(err)
  138. }
  139. if len(headers) != 0 {
  140. t.Errorf("This shouldn't have been found: %+v", headers)
  141. }
  142. })
  143. t.Run("Find", func(t *testing.T) {
  144. page, err := FindPage(testPage.ID)
  145. if err != nil {
  146. t.Errorf("FindPage: %s", err)
  147. return
  148. }
  149. assertEquals(t, "Name", page.Name, testPage.Name)
  150. assertEquals(t, "Author", page.Author, testPage.Author)
  151. assertEquals(t, "Category", page.Category, testPage.Category)
  152. assertEquals(t, "PublishDate", page.PublishDate, testPage.PublishDate)
  153. assertEquals(t, "EditDate", page.EditDate, testPage.EditDate)
  154. assertEquals(t, "FictionalDate", page.FictionalDate, testPage.FictionalDate)
  155. assertEquals(t, "Dated", page.Dated, testPage.Dated)
  156. assertEquals(t, "Specific", page.Specific, testPage.Specific)
  157. assertEquals(t, "Published", page.Published, testPage.Published)
  158. assertEquals(t, "Unlisted", page.Unlisted, testPage.Unlisted)
  159. assertEquals(t, "Indexed", page.Indexed, testPage.Indexed)
  160. assertEquals(t, "Source", page.Source, testPage.Source)
  161. assertEquals(t, "BackgroundURL", page.BackgroundURL, testPage.BackgroundURL)
  162. })
  163. t.Run("Modify", func(t *testing.T) {
  164. page, err := FindPage(testPage.ID)
  165. if err != nil {
  166. t.Errorf("FindPage: %s", err)
  167. return
  168. }
  169. page.Name = "New Page name"
  170. page.Source += "\nAdditional Content is additional"
  171. page.Tags[3] = *extraPageTag // Correct "Senva T'Vaoma" to "Renala T'Iavay"
  172. page.Unlisted = true
  173. err = page.Update()
  174. if err != nil {
  175. t.Errorf("Update: %s", err)
  176. return
  177. }
  178. page2, err := FindPage(page.ID)
  179. if err != nil {
  180. t.Errorf("FindPage 2: %s", err)
  181. return
  182. }
  183. assertEquals(t, "Name", page2.Name, page.Name)
  184. assertEquals(t, "Author", page2.Author, page.Author)
  185. assertEquals(t, "Category", page2.Category, page.Category)
  186. assertEquals(t, "PublishDate", page2.PublishDate, page.PublishDate)
  187. assertEquals(t, "EditDate", page2.EditDate, page.EditDate)
  188. assertEquals(t, "FictionalDate", page2.FictionalDate, page.FictionalDate)
  189. assertEquals(t, "Dated", page2.Dated, page.Dated)
  190. assertEquals(t, "Specific", page2.Specific, page.Specific)
  191. assertEquals(t, "Published", page2.Published, page.Published)
  192. assertEquals(t, "Unlisted", page2.Unlisted, page.Unlisted)
  193. assertEquals(t, "Indexed", page2.Indexed, page.Indexed)
  194. assertEquals(t, "Source", page2.Source, page.Source)
  195. assertEquals(t, "BackgroundURL", page2.BackgroundURL, page.BackgroundURL)
  196. })
  197. t.Run("VerifyUnlisted", func(t *testing.T) {
  198. headers, err := ListHeaders()
  199. if err != nil {
  200. t.Error(err)
  201. }
  202. t.Logf("Got %d headers", len(headers))
  203. for _, header := range headers {
  204. if header.ID == testPage.ID {
  205. t.Errorf("Found header: %+v", header)
  206. break
  207. }
  208. }
  209. })
  210. t.Run("Content", func(t *testing.T) {
  211. page, err := FindPage(testPage.ID)
  212. if err != nil {
  213. t.Errorf("FindPage: %s", err)
  214. return
  215. }
  216. content, err := page.Content()
  217. if err != nil {
  218. t.Errorf("page.Content: %s", err)
  219. }
  220. assertEquals(t, "page.Content()", content, "<h1>Returning Va’ynna’s Omni-Tool</h1>\n\n<p>Additional Content is additional</p>\n")
  221. })
  222. t.Run("WikiURL", func(t *testing.T) {
  223. t.Skip("To be implemented")
  224. })
  225. t.Run("Delete", func(t *testing.T) {
  226. err := testPage.Delete()
  227. if err != nil {
  228. t.Error(err)
  229. }
  230. })
  231. t.Run("TeardownTags", func(t *testing.T) {
  232. for _, tag := range testPageTags {
  233. err := tag.Delete()
  234. if err != nil {
  235. t.Error(err)
  236. continue
  237. }
  238. t.Logf("Deleted %+v", tag)
  239. }
  240. err := extraPageTag.Delete()
  241. if err != nil {
  242. t.Error(err)
  243. }
  244. t.Logf("Deleted %+v", extraPageTag)
  245. })
  246. }