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.

326 lines
8.7 KiB

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