telegraph/valid_test.go

180 lines
3.8 KiB
Go
Raw Normal View History

package telegraph
import "testing"
2017-10-03 13:39:23 +00:00
const (
validTitle = "Testing"
validShortName = "Sandbox"
validAuthorName = "Anonymous"
validNewAuthorName = "Gopher"
validAuthorURL = "https://t.me/telegraph"
validPageURL = "Sample-Page-12-15"
)
var (
2017-10-03 13:39:23 +00:00
validAccount *Account
validPage *Page
validContentDOM []Node
validContent = `<p>Hello, World!</p>`
)
2017-10-03 13:39:23 +00:00
func testValidContentFormatByString(t *testing.T) {
var err error
validContentDOM, err = ContentFormat(validContent)
if err != nil {
2017-10-03 13:39:23 +00:00
t.Error(err.Error())
t.FailNow()
}
if len(validContentDOM) <= 0 {
t.Error("DOM content is nil")
}
}
2017-10-03 13:39:23 +00:00
func testValidContentFormatByBytes(t *testing.T) {
var err error
validContentDOM, err = ContentFormat([]byte(validContent))
if err != nil {
2017-10-03 13:39:23 +00:00
t.Error(err.Error())
t.FailNow()
}
2017-10-03 13:39:23 +00:00
if len(validContentDOM) <= 0 {
t.Error("DOM content is nil")
}
2017-10-03 13:39:23 +00:00
}
2017-10-03 13:39:23 +00:00
func TestValidCreateAccount(t *testing.T) {
var err error
validAccount, err = CreateAccount(&Account{
ShortName: validShortName,
AuthorName: validAuthorName,
})
if err != nil {
2017-10-03 13:39:23 +00:00
t.Error(err.Error())
t.FailNow()
}
2017-10-03 13:39:23 +00:00
t.Run("validCreatePage", testValidCreatePage)
t.Run("validEditAccountInfo", testValidEditAccountInfo)
t.Run("validGetAccountInfo", testValidGetAccountInfo)
t.Run("validGetPageList", testValidGetPageList)
t.Run("validRevokeAccessToken", testValidRevokeAccessToken)
}
2017-10-03 13:39:23 +00:00
func testValidCreatePage(t *testing.T) {
t.Run("validContentFormatByString", testValidContentFormatByString)
t.Run("validContentFormatByBytes", testValidContentFormatByBytes)
var err error
validPage, err = validAccount.CreatePage(&Page{
Title: validTitle,
AuthorName: validAuthorName,
AuthorURL: validAuthorURL,
Content: validContentDOM,
}, true)
if err != nil {
2017-10-03 13:39:23 +00:00
t.Error(err.Error())
t.FailNow()
}
2017-10-03 13:39:23 +00:00
if validPage.URL == "" {
t.Error("new page not contain URL")
}
2017-10-03 13:39:23 +00:00
t.Run("validEditPage", testValidEditPage)
}
2017-10-03 13:39:23 +00:00
func testValidEditAccountInfo(t *testing.T) {
update, err := validAccount.EditAccountInfo(&Account{
ShortName: validShortName,
AuthorName: validNewAuthorName,
AuthorURL: validAuthorURL,
})
if err != nil {
2017-10-03 13:39:23 +00:00
t.Error(err.Error())
t.FailNow()
}
if update.AuthorName == validAccount.AuthorName {
t.Error("account not updated")
}
}
2017-10-03 13:39:23 +00:00
func testValidEditPage(t *testing.T) {
var err error
validPage, err = validAccount.EditPage(&Page{
Path: validPage.Path,
Title: validTitle,
AuthorName: validAuthorName,
Content: validContentDOM,
}, true)
if err != nil {
2017-10-03 13:39:23 +00:00
t.Error(err.Error())
t.FailNow()
}
2017-10-03 13:39:23 +00:00
}
2017-10-03 13:39:23 +00:00
func testValidGetAccountInfo(t *testing.T) {
info, err := validAccount.GetAccountInfo(FieldShortName, FieldPageCount)
if err != nil {
2017-10-03 13:39:23 +00:00
t.Error(err.Error())
t.FailNow()
}
if info.ShortName != validAccount.ShortName {
t.Error("get wrong account info")
}
}
2017-10-03 13:39:23 +00:00
func TestValidGetPage(t *testing.T) {
page, err := GetPage(validPage.Path, true)
if err != nil {
2017-10-03 13:39:23 +00:00
t.Error(err.Error())
t.FailNow()
}
2017-10-03 13:39:23 +00:00
if page.Title != validPage.Title {
t.Error("get wrong page")
}
}
2017-10-03 13:39:23 +00:00
func testValidGetPageList(t *testing.T) {
pages, err := validAccount.GetPageList(0, 3)
if err != nil {
2017-10-03 13:39:23 +00:00
t.Error(err.Error())
t.FailNow()
}
2017-10-03 13:39:23 +00:00
if pages.TotalCount <= 0 {
t.Error("no one page in page list")
}
}
2017-10-03 13:39:23 +00:00
func TestValidGetViews(t *testing.T) {
stats, err := GetViews(validPageURL, -1, 0, 12, 2016)
if err != nil {
2017-10-03 13:39:23 +00:00
t.Error(err.Error())
t.FailNow()
}
2017-10-03 13:39:23 +00:00
if stats.Views <= 0 {
t.Error("get 0 views")
}
}
2017-10-03 13:39:23 +00:00
func testValidRevokeAccessToken(t *testing.T) {
oldToken := validAccount.AccessToken
var err error
validAccount, err = validAccount.RevokeAccessToken()
if err != nil {
2017-10-03 13:39:23 +00:00
t.Error(err.Error())
t.FailNow()
}
2017-10-03 13:39:23 +00:00
if validAccount.AccessToken == "" {
t.Error("revokeAccessToken return nothing")
}
2017-10-03 13:39:23 +00:00
if oldToken == validAccount.AccessToken {
t.Error("old and new tokens are equal")
}
}