package model import ( "testing" "time" "git.aiterp.net/AiteRP/aitestory/server" "git.aiterp.net/gisle/wrouter/generate" ) var testPageTags = []*Tag{ &Tag{"", "Event", "Skipping Work (Test)"}, &Tag{"", "Location", "Redrock's Office (Test)"}, &Tag{"", "Character", "Renala T'Iavay (Test)"}, &Tag{"", "Character", "Senva T'Vaoma (Test)"}, } var extraPageTag = &Tag{"", "Character", "Va'ynna Atana (Test)"} var fictionalDate, _ = time.Parse("2006-01-02", "2185-07-25") var postingDate, _ = time.Parse("2006-01-02", "2017-09-13") var editDate, _ = time.Parse("2006-01-02", "2017-09-15") var testPage = Page{ Name: "Returning Va'ynna's Omni-Tool (Test)", Author: "test:Gisle", Category: "Story", FictionalDate: fictionalDate, PublishDate: postingDate, EditDate: editDate, Dated: true, Published: true, Unlisted: false, Specific: false, Indexed: true, BackgroundURL: "", Type: "Markdown", Source: "# Returning Va'ynna's Omni-Tool", Tags: make([]Tag, 0, 4), } func TestPage(t *testing.T) { var assertEquals = func(t *testing.T, label string, a, b interface{}) { if a != b { t.Errorf("Assert Failed (%s): \"%+v\" == \"%+v\"", label, a, b) } } if server.Main.Config.DB.Password == "" { t.Skip("No database password") return } t.Run("SetupTags", func(t *testing.T) { for _, tag := range testPageTags { err := tag.Insert() if err != nil { t.Error(err) } testPage.Tags = append(testPage.Tags, *tag) } err := extraPageTag.Insert() if err != nil { t.Error(err) } }) t.Run("BasicConstants", func(t *testing.T) { if PageMinDate.Format(time.RFC3339) != "1753-01-01T00:00:00Z" { t.Error("Invalid date:", PageMinDate.Format(time.RFC3339)) } page := Page{} page.generateID() if len(page.ID) != 16 { t.Errorf("len(page.ID): %d != 16", len(page.ID)) } id1 := page.ID page.generateID() id2 := page.ID t.Logf("Page IDs: %s, %s (should not be the same)", id1, id2) if id1 == id2 { t.Fail() } }) t.Run("Insert", func(t *testing.T) { err := testPage.Insert() if err != nil { t.Error(err) } t.Logf("testPage.ID = \"%s\"", testPage.ID) if testPage.ID == "" { t.Fail() } }) t.Run("ListHeaders", func(t *testing.T) { headers, err := ListHeaders() if err != nil { t.Error(err) } t.Logf("Got %d headers", len(headers)) found := false for _, header := range headers { if header.ID == testPage.ID { found = true t.Logf("Found header: %+v", header) assertEquals(t, "Name", header.Name, testPage.Name) assertEquals(t, "Author", header.Author, testPage.Author) assertEquals(t, "Category", header.Category, testPage.Category) assertEquals(t, "PublishDate", header.PublishDate, testPage.PublishDate) assertEquals(t, "EditDate", header.EditDate, testPage.EditDate) assertEquals(t, "FictionalDate", header.FictionalDate, testPage.FictionalDate) assertEquals(t, "Dated", header.Dated, testPage.Dated) assertEquals(t, "PrimaryTag.ID", header.PrimaryTag.ID, testPageTags[0].ID) } } if !found { t.Error("Did not find the inserted page's header") } }) t.Run("ListHeadersByTag", func(t *testing.T) { headers, err := ListHeadersByTag("", testPageTags[1]) if err != nil { t.Error(err) } t.Logf("Got %d headers", len(headers)) found := false for _, header := range headers { if header.ID == testPage.ID { found = true t.Logf("Found header: %+v", header) assertEquals(t, "Name", header.Name, testPage.Name) assertEquals(t, "Author", header.Author, testPage.Author) assertEquals(t, "Category", header.Category, testPage.Category) assertEquals(t, "PublishDate", header.PublishDate, testPage.PublishDate) assertEquals(t, "EditDate", header.EditDate, testPage.EditDate) assertEquals(t, "FictionalDate", header.FictionalDate, testPage.FictionalDate) assertEquals(t, "Dated", header.Dated, testPage.Dated) assertEquals(t, "PrimaryTag.ID", header.PrimaryTag.ID, testPageTags[0].ID) } } if !found { t.Error("Did not find the inserted page's header") } // Make a fake tag and make sure that doesn't return stuff headers, err = ListHeadersByTag("", &Tag{ID: generate.ID()}) if err != nil { t.Error(err) } if len(headers) != 0 { t.Errorf("This shouldn't have been found: %+v", headers) } }) t.Run("Find", func(t *testing.T) { page, err := FindPage(testPage.ID) if err != nil { t.Errorf("FindPage: %s", err) return } assertEquals(t, "Name", page.Name, testPage.Name) assertEquals(t, "Author", page.Author, testPage.Author) assertEquals(t, "Category", page.Category, testPage.Category) assertEquals(t, "PublishDate", page.PublishDate, testPage.PublishDate) assertEquals(t, "EditDate", page.EditDate, testPage.EditDate) assertEquals(t, "FictionalDate", page.FictionalDate, testPage.FictionalDate) assertEquals(t, "Dated", page.Dated, testPage.Dated) assertEquals(t, "Specific", page.Specific, testPage.Specific) assertEquals(t, "Published", page.Published, testPage.Published) assertEquals(t, "Unlisted", page.Unlisted, testPage.Unlisted) assertEquals(t, "Indexed", page.Indexed, testPage.Indexed) assertEquals(t, "Source", page.Source, testPage.Source) assertEquals(t, "BackgroundURL", page.BackgroundURL, testPage.BackgroundURL) }) t.Run("Modify", func(t *testing.T) { page, err := FindPage(testPage.ID) if err != nil { t.Errorf("FindPage: %s", err) return } page.Name = "New Page name" page.Source += "\nAdditional Content is additional" page.Tags[3] = *extraPageTag // Correct "Senva T'Vaoma" to "Renala T'Iavay" page.Unlisted = true err = page.Update() if err != nil { t.Errorf("Update: %s", err) return } page2, err := FindPage(page.ID) if err != nil { t.Errorf("FindPage 2: %s", err) return } assertEquals(t, "Name", page2.Name, page.Name) assertEquals(t, "Author", page2.Author, page.Author) assertEquals(t, "Category", page2.Category, page.Category) assertEquals(t, "PublishDate", page2.PublishDate, page.PublishDate) assertEquals(t, "EditDate", page2.EditDate, page.EditDate) assertEquals(t, "FictionalDate", page2.FictionalDate, page.FictionalDate) assertEquals(t, "Dated", page2.Dated, page.Dated) assertEquals(t, "Specific", page2.Specific, page.Specific) assertEquals(t, "Published", page2.Published, page.Published) assertEquals(t, "Unlisted", page2.Unlisted, page.Unlisted) assertEquals(t, "Indexed", page2.Indexed, page.Indexed) assertEquals(t, "Source", page2.Source, page.Source) assertEquals(t, "BackgroundURL", page2.BackgroundURL, page.BackgroundURL) }) t.Run("VerifyUnlisted", func(t *testing.T) { headers, err := ListHeaders() if err != nil { t.Error(err) } t.Logf("Got %d headers", len(headers)) for _, header := range headers { if header.ID == testPage.ID { t.Errorf("Found header: %+v", header) break } } }) t.Run("Content", func(t *testing.T) { page, err := FindPage(testPage.ID) if err != nil { t.Errorf("FindPage: %s", err) return } content, err := page.Content() if err != nil { t.Errorf("page.Content: %s", err) } assertEquals(t, "page.Content()", content, "
Additional Content is additional
\n") }) t.Run("WikiURL", func(t *testing.T) { t.Skip("To be implemented") }) t.Run("Delete", func(t *testing.T) { err := testPage.Delete() if err != nil { t.Error(err) } }) t.Run("TeardownTags", func(t *testing.T) { for _, tag := range testPageTags { err := tag.Delete() if err != nil { t.Error(err) continue } t.Logf("Deleted %+v", tag) } err := extraPageTag.Delete() if err != nil { t.Error(err) } t.Logf("Deleted %+v", extraPageTag) }) }