✅ Tests refactoring
This commit is contained in:
parent
08cb7e95ac
commit
d72ebe1309
132
invalid_test.go
132
invalid_test.go
|
@ -2,145 +2,119 @@ package telegraph
|
|||
|
||||
import "testing"
|
||||
|
||||
func TestContentFormatByWTF(t *testing.T) {
|
||||
_, err := ContentFormat(42)
|
||||
if err == nil {
|
||||
const (
|
||||
invalidAuthorURL = "lolwat"
|
||||
invalidPageURL = "sukablyat'"
|
||||
invalidContent = 42
|
||||
)
|
||||
|
||||
var invalidAccount = &Account{}
|
||||
|
||||
func TestInvalidContentFormat(t *testing.T) {
|
||||
if _, err := ContentFormat(invalidContent); err == nil {
|
||||
t.Error()
|
||||
}
|
||||
t.Log(err.Error())
|
||||
}
|
||||
|
||||
func TestCreateInvalidAccount(t *testing.T) {
|
||||
var account Account
|
||||
_, err := CreateAccount(&account)
|
||||
if err == nil {
|
||||
func TestInvalidCreateAccount(t *testing.T) {
|
||||
if _, err := CreateAccount(invalidAccount); err == nil {
|
||||
t.Error()
|
||||
}
|
||||
t.Log(err.Error())
|
||||
|
||||
t.Run("invalidCreatePage", testInvalidCreatePage)
|
||||
t.Run("invalidEditAccountInfo", testInvalidEditAccountInfo)
|
||||
t.Run("invalidEditPage", testInvalidEditPage)
|
||||
t.Run("invalidGetAccountInfo", testInvalidGetAccountInfo)
|
||||
t.Run("invalidGetPageList", testInvalidGetPageList)
|
||||
t.Run("invalidRevokeAccessToken", testInvalidRevokeAccessToken)
|
||||
}
|
||||
|
||||
func TestCreateInvalidPage(t *testing.T) {
|
||||
newPage := &Page{
|
||||
AuthorURL: "lolwat",
|
||||
}
|
||||
_, err := demoAccount.CreatePage(newPage, false)
|
||||
if err == nil {
|
||||
func testInvalidCreatePage(t *testing.T) {
|
||||
if _, err := invalidAccount.CreatePage(&Page{
|
||||
AuthorURL: invalidAuthorURL,
|
||||
}, false); err == nil {
|
||||
t.Error()
|
||||
}
|
||||
t.Log(err.Error())
|
||||
}
|
||||
|
||||
func TestEditInvalidAccountInfo(t *testing.T) {
|
||||
var update Account
|
||||
|
||||
_, err := demoAccount.EditAccountInfo(&update)
|
||||
if err == nil {
|
||||
func testInvalidEditAccountInfo(t *testing.T) {
|
||||
if _, err := invalidAccount.EditAccountInfo(&Account{
|
||||
AuthorURL: invalidAuthorURL,
|
||||
}); err == nil {
|
||||
t.Error()
|
||||
}
|
||||
t.Log(err.Error())
|
||||
}
|
||||
|
||||
func TestEditInvalidPage(t *testing.T) {
|
||||
update := &Page{
|
||||
AuthorURL: "lolwat",
|
||||
}
|
||||
|
||||
_, err := demoAccount.EditPage(update, false)
|
||||
if err == nil {
|
||||
func testInvalidEditPage(t *testing.T) {
|
||||
if _, err := invalidAccount.EditPage(&Page{
|
||||
AuthorURL: invalidAuthorURL,
|
||||
}, false); err == nil {
|
||||
t.Error()
|
||||
}
|
||||
t.Log(err.Error())
|
||||
}
|
||||
|
||||
func TestGetInvalidAccountInfo(t *testing.T) {
|
||||
var account Account
|
||||
_, err := account.GetAccountInfo("short_name", "page_count")
|
||||
if err == nil {
|
||||
func testInvalidGetAccountInfo(t *testing.T) {
|
||||
if _, err := invalidAccount.GetAccountInfo("short_name", "page_count"); err == nil {
|
||||
t.Error()
|
||||
}
|
||||
t.Log(err.Error())
|
||||
}
|
||||
|
||||
func TestGetInvalidPageList(t *testing.T) {
|
||||
var account Account
|
||||
_, err := account.GetPageList(0, 3)
|
||||
if err == nil {
|
||||
func testInvalidGetPageList(t *testing.T) {
|
||||
if _, err := invalidAccount.GetPageList(0, 3); err == nil {
|
||||
t.Error()
|
||||
}
|
||||
t.Log(err.Error())
|
||||
}
|
||||
|
||||
func TestGetInvalidPageListByOffset(t *testing.T) {
|
||||
var account Account
|
||||
_, err := account.GetPageList(-42, 3)
|
||||
if err == nil {
|
||||
func testInvalidGetPageListByOffset(t *testing.T) {
|
||||
if _, err := invalidAccount.GetPageList(-42, 3); err == nil {
|
||||
t.Error()
|
||||
}
|
||||
t.Log(err.Error())
|
||||
}
|
||||
|
||||
func TestGetInvalidPageListByLimit(t *testing.T) {
|
||||
var account Account
|
||||
_, err := account.GetPageList(0, 9000)
|
||||
if err == nil {
|
||||
func testInvalidGetPageListByLimit(t *testing.T) {
|
||||
if _, err := invalidAccount.GetPageList(0, 9000); err == nil {
|
||||
t.Error()
|
||||
}
|
||||
t.Log(err.Error())
|
||||
}
|
||||
|
||||
func TestGetInvalidPage(t *testing.T) {
|
||||
_, err := GetPage("lolwat", true)
|
||||
if err == nil {
|
||||
func TestInvalidGetPage(t *testing.T) {
|
||||
if _, err := GetPage(invalidPageURL, true); err == nil {
|
||||
t.Error()
|
||||
}
|
||||
t.Log(err.Error())
|
||||
}
|
||||
|
||||
func TestGetInvalidViewsByPage(t *testing.T) {
|
||||
_, err := GetViews("lolwat", 2016, 12, 0, -1)
|
||||
if err == nil {
|
||||
func TestInvalidGetViewsByPage(t *testing.T) {
|
||||
if _, err := GetViews(validPageURL, 2016, 12, 0, -1); err == nil {
|
||||
t.Error()
|
||||
}
|
||||
t.Log(err.Error())
|
||||
}
|
||||
|
||||
func TestGetInvalidViewsByHour(t *testing.T) {
|
||||
_, err := GetViews("Sample-Page-12-15", 42, 0, 0, 0)
|
||||
if err == nil {
|
||||
func TestInvalidGetViewsByHour(t *testing.T) {
|
||||
if _, err := GetViews(validPageURL, 42, 0, 0, 0); err == nil {
|
||||
t.Error()
|
||||
}
|
||||
t.Log(err.Error())
|
||||
}
|
||||
|
||||
func TestGetInvalidViewsByDay(t *testing.T) {
|
||||
_, err := GetViews("Sample-Page-12-15", 23, 42, 0, 0)
|
||||
if err == nil {
|
||||
func TestInvalidGetViewsByDay(t *testing.T) {
|
||||
if _, err := GetViews(validPageURL, 23, 42, 0, 0); err == nil {
|
||||
t.Error()
|
||||
}
|
||||
t.Log(err.Error())
|
||||
}
|
||||
|
||||
func TestGetInvalidViewsByMonth(t *testing.T) {
|
||||
_, err := GetViews("Sample-Page-12-15", 23, 24, 22, 0)
|
||||
if err == nil {
|
||||
func TestInvalidGetViewsByMonth(t *testing.T) {
|
||||
if _, err := GetViews(validPageURL, 23, 24, 22, 0); err == nil {
|
||||
t.Error()
|
||||
}
|
||||
t.Log(err.Error())
|
||||
}
|
||||
|
||||
func TestGetInvalidViewsByYear(t *testing.T) {
|
||||
_, err := GetViews("Sample-Page-12-15", 23, 24, 12, 1980)
|
||||
if err == nil {
|
||||
func TestInvalidGetViewsByYear(t *testing.T) {
|
||||
if _, err := GetViews(validPageURL, 23, 24, 12, 1980); err == nil {
|
||||
t.Error()
|
||||
}
|
||||
t.Log(err.Error())
|
||||
}
|
||||
|
||||
func TestRevokeInvalidAccessToken(t *testing.T) {
|
||||
var account Account
|
||||
_, err := account.RevokeAccessToken()
|
||||
if err == nil {
|
||||
func testInvalidRevokeAccessToken(t *testing.T) {
|
||||
if _, err := invalidAccount.RevokeAccessToken(); err == nil {
|
||||
t.Error()
|
||||
}
|
||||
t.Log(err.Error())
|
||||
}
|
||||
|
|
210
valid_test.go
210
valid_test.go
|
@ -2,140 +2,178 @@ package telegraph
|
|||
|
||||
import "testing"
|
||||
|
||||
var (
|
||||
demoAccount Account
|
||||
demoPage Page
|
||||
demoContent = `<p>Hello, World!</p>`
|
||||
const (
|
||||
validTitle = "Testing"
|
||||
validShortName = "Sandbox"
|
||||
validAuthorName = "Anonymous"
|
||||
validNewAuthorName = "Gopher"
|
||||
validAuthorURL = "https://t.me/telegraph"
|
||||
validPageURL = "Sample-Page-12-15"
|
||||
)
|
||||
|
||||
func TestContentFormatByString(t *testing.T) {
|
||||
_, err := ContentFormat(demoContent)
|
||||
var (
|
||||
validAccount *Account
|
||||
validPage *Page
|
||||
validContentDOM []Node
|
||||
|
||||
validContent = `<p>Hello, World!</p>`
|
||||
)
|
||||
|
||||
func testValidContentFormatByString(t *testing.T) {
|
||||
var err error
|
||||
validContentDOM, err = ContentFormat(validContent)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.Error(err.Error())
|
||||
t.FailNow()
|
||||
}
|
||||
if len(validContentDOM) <= 0 {
|
||||
t.Error("DOM content is nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestContentFormatByBytes(t *testing.T) {
|
||||
_, err := ContentFormat([]byte(demoContent))
|
||||
func testValidContentFormatByBytes(t *testing.T) {
|
||||
var err error
|
||||
validContentDOM, err = ContentFormat([]byte(validContent))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.Error(err.Error())
|
||||
t.FailNow()
|
||||
}
|
||||
if len(validContentDOM) <= 0 {
|
||||
t.Error("DOM content is nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateValidAccount(t *testing.T) {
|
||||
newAccount := &Account{
|
||||
ShortName: "Sandbox",
|
||||
AuthorName: "Anonymous",
|
||||
}
|
||||
|
||||
account, err := CreateAccount(newAccount)
|
||||
func TestValidCreateAccount(t *testing.T) {
|
||||
var err error
|
||||
validAccount, err = CreateAccount(&Account{
|
||||
ShortName: validShortName,
|
||||
AuthorName: validAuthorName,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.Error(err.Error())
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
demoAccount = *account
|
||||
t.Logf("New account created!")
|
||||
t.Run("validCreatePage", testValidCreatePage)
|
||||
t.Run("validEditAccountInfo", testValidEditAccountInfo)
|
||||
t.Run("validGetAccountInfo", testValidGetAccountInfo)
|
||||
t.Run("validGetPageList", testValidGetPageList)
|
||||
t.Run("validRevokeAccessToken", testValidRevokeAccessToken)
|
||||
}
|
||||
|
||||
func TestCreateValidPage(t *testing.T) {
|
||||
content, err := ContentFormat(demoContent)
|
||||
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 {
|
||||
t.Error(err)
|
||||
t.Error(err.Error())
|
||||
t.FailNow()
|
||||
}
|
||||
if validPage.URL == "" {
|
||||
t.Error("new page not contain URL")
|
||||
}
|
||||
|
||||
newPage := &Page{
|
||||
Title: "Sample Page",
|
||||
AuthorName: "Anonymous",
|
||||
AuthorURL: "https://telegram.me/telegraph",
|
||||
Content: content,
|
||||
}
|
||||
|
||||
page, err := demoAccount.CreatePage(newPage, true)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
demoPage = *page
|
||||
t.Logf("%#v", *page)
|
||||
t.Run("validEditPage", testValidEditPage)
|
||||
}
|
||||
|
||||
func TestEditValidAccountInfo(t *testing.T) {
|
||||
update := &Account{
|
||||
ShortName: "Sandbox",
|
||||
AuthorName: "Anonymous",
|
||||
AuthorURL: "https://telegram.me/telegraph",
|
||||
}
|
||||
|
||||
_, err := demoAccount.EditAccountInfo(update)
|
||||
func testValidEditAccountInfo(t *testing.T) {
|
||||
update, err := validAccount.EditAccountInfo(&Account{
|
||||
ShortName: validShortName,
|
||||
AuthorName: validNewAuthorName,
|
||||
AuthorURL: validAuthorURL,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.Error(err.Error())
|
||||
t.FailNow()
|
||||
}
|
||||
if update.AuthorName == validAccount.AuthorName {
|
||||
t.Error("account not updated")
|
||||
}
|
||||
|
||||
t.Logf("Account updated!")
|
||||
}
|
||||
|
||||
func TestEditValidPage(t *testing.T) {
|
||||
content, err := ContentFormat(demoContent)
|
||||
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 {
|
||||
t.Error(err)
|
||||
t.Error(err.Error())
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
update := &Page{
|
||||
Path: demoPage.Path,
|
||||
Title: "Sample Page",
|
||||
AuthorName: "Anonymous",
|
||||
Content: content,
|
||||
}
|
||||
|
||||
page, err := demoAccount.EditPage(update, true)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Logf("%#v", *page)
|
||||
}
|
||||
|
||||
func TestGetValidAccountInfo(t *testing.T) {
|
||||
account, err := demoAccount.GetAccountInfo("short_name", "page_count")
|
||||
func testValidGetAccountInfo(t *testing.T) {
|
||||
info, err := validAccount.GetAccountInfo("short_name", "page_count")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.Error(err.Error())
|
||||
t.FailNow()
|
||||
}
|
||||
if info.ShortName != validAccount.ShortName {
|
||||
t.Error("get wrong account info")
|
||||
}
|
||||
|
||||
t.Logf("%s create %d pages", account.ShortName, account.PageCount)
|
||||
}
|
||||
|
||||
func TestGetValidPageList(t *testing.T) {
|
||||
pages, err := demoAccount.GetPageList(0, 3)
|
||||
func TestValidGetPage(t *testing.T) {
|
||||
page, err := GetPage(validPage.Path, true)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.Error(err.Error())
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
t.Logf("Total %d pages\n%#v", pages.TotalCount, pages.Pages)
|
||||
if page.Title != validPage.Title {
|
||||
t.Error("get wrong page")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetValidPage(t *testing.T) {
|
||||
page, err := GetPage(demoPage.Path, true)
|
||||
func testValidGetPageList(t *testing.T) {
|
||||
pages, err := validAccount.GetPageList(0, 3)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.Error(err.Error())
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
t.Logf("%#v", *page)
|
||||
if pages.TotalCount <= 0 {
|
||||
t.Error("no one page in page list")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetValidViews(t *testing.T) {
|
||||
views, err := GetViews("Sample-Page-12-15", -1, 0, 12, 2016)
|
||||
func TestValidGetViews(t *testing.T) {
|
||||
stats, err := GetViews(validPageURL, -1, 0, 12, 2016)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.Error(err.Error())
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
t.Logf("This page have %d views", views.Views)
|
||||
if stats.Views <= 0 {
|
||||
t.Error("get 0 views")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRevokeAccessToken(t *testing.T) {
|
||||
_, err := demoAccount.RevokeAccessToken()
|
||||
func testValidRevokeAccessToken(t *testing.T) {
|
||||
oldToken := validAccount.AccessToken
|
||||
|
||||
var err error
|
||||
validAccount, err = validAccount.RevokeAccessToken()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.Error(err.Error())
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
t.Logf("New Access Token set!")
|
||||
if validAccount.AccessToken == "" {
|
||||
t.Error("revokeAccessToken return nothing")
|
||||
}
|
||||
|
||||
if oldToken == validAccount.AccessToken {
|
||||
t.Error("old and new tokens are equal")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue