♻️ Tests refactor

This commit is contained in:
Maxim Lebedev 2018-11-29 19:44:58 +05:00
parent dce3f313ed
commit 4bd004d955
No known key found for this signature in database
GPG Key ID: F8978F46FF0FFA4F
2 changed files with 58 additions and 160 deletions

View File

@ -4,6 +4,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
"gitlab.com/toby3d/telegraph" "gitlab.com/toby3d/telegraph"
) )
@ -16,15 +17,13 @@ const (
var invalidAccount = new(telegraph.Account) var invalidAccount = new(telegraph.Account)
func TestInvalidContentFormat(t *testing.T) { func TestInvalidContentFormat(t *testing.T) {
if _, err := telegraph.ContentFormat(invalidContent); err != telegraph.ErrInvalidDataType { _, err := telegraph.ContentFormat(invalidContent)
t.Error() assert.EqualError(t, telegraph.ErrInvalidDataType, err.Error())
}
} }
func TestInvalidCreateAccount(t *testing.T) { func TestInvalidCreateAccount(t *testing.T) {
if _, err := telegraph.CreateAccount(invalidAccount); err == nil { _, err := telegraph.CreateAccount(invalidAccount)
t.Error() assert.Error(t, err)
}
t.Run("invalidCreatePage", testInvalidCreatePage) t.Run("invalidCreatePage", testInvalidCreatePage)
t.Run("invalidEditAccountInfo", testInvalidEditAccountInfo) t.Run("invalidEditAccountInfo", testInvalidEditAccountInfo)
@ -37,109 +36,71 @@ func TestInvalidCreateAccount(t *testing.T) {
} }
func testInvalidCreatePage(t *testing.T) { func testInvalidCreatePage(t *testing.T) {
if _, err := invalidAccount.CreatePage(&telegraph.Page{ _, err := invalidAccount.CreatePage(&telegraph.Page{AuthorURL: invalidAuthorURL}, false)
AuthorURL: invalidAuthorURL, assert.Error(t, err)
}, false); err == nil {
t.Error()
}
} }
func testInvalidEditAccountInfo(t *testing.T) { func testInvalidEditAccountInfo(t *testing.T) {
if _, err := invalidAccount.EditAccountInfo(&telegraph.Account{ _, err := invalidAccount.EditAccountInfo(&telegraph.Account{AuthorURL: invalidAuthorURL})
AuthorURL: invalidAuthorURL, assert.Error(t, err)
}); err == nil {
t.Error()
}
} }
func testInvalidEditPage(t *testing.T) { func testInvalidEditPage(t *testing.T) {
if _, err := invalidAccount.EditPage(&telegraph.Page{ _, err := invalidAccount.EditPage(&telegraph.Page{AuthorURL: invalidAuthorURL}, false)
AuthorURL: invalidAuthorURL, assert.Error(t, err)
}, false); err == nil {
t.Error()
}
} }
func testInvalidGetAccountInfo(t *testing.T) { func testInvalidGetAccountInfo(t *testing.T) {
if _, err := invalidAccount.GetAccountInfo( _, err := invalidAccount.GetAccountInfo(telegraph.FieldShortName, telegraph.FieldPageCount)
telegraph.FieldShortName, assert.Error(t, err)
telegraph.FieldPageCount,
); err == nil {
t.Error()
}
} }
func testInvalidGetPageList(t *testing.T) { func testInvalidGetPageList(t *testing.T) {
if _, err := invalidAccount.GetPageList(0, 3); err == nil { _, err := invalidAccount.GetPageList(0, 3)
t.Error() assert.Error(t, err)
}
} }
func testInvalidGetPageListByOffset(t *testing.T) { func testInvalidGetPageListByOffset(t *testing.T) {
if _, err := invalidAccount.GetPageList(-42, 3); err == nil { _, err := invalidAccount.GetPageList(-42, 3)
t.Error() assert.Error(t, err)
}
} }
func testInvalidGetPageListByLimit(t *testing.T) { func testInvalidGetPageListByLimit(t *testing.T) {
if _, err := invalidAccount.GetPageList(0, 9000); err == nil { _, err := invalidAccount.GetPageList(0, 9000)
t.Error() assert.Error(t, err)
}
} }
func TestInvalidGetPage(t *testing.T) { func TestInvalidGetPage(t *testing.T) {
if _, err := telegraph.GetPage(invalidPageURL, true); err == nil { _, err := telegraph.GetPage(invalidPageURL, true)
t.Error() assert.Error(t, err)
}
} }
func TestInvalidGetViewsByPage(t *testing.T) { func TestInvalidGetViewsByPage(t *testing.T) {
if _, err := telegraph.GetViews( _, err := telegraph.GetViews(invalidPageURL, time.Date(2016, time.December, 0, 0, 0, 0, 0, time.UTC))
invalidPageURL, assert.Error(t, err)
time.Date(2016, time.December, 0, 0, 0, 0, 0, time.UTC),
); err == nil {
t.Error()
}
} }
func TestInvalidGetViewsByHour(t *testing.T) { func TestInvalidGetViewsByHour(t *testing.T) {
if _, err := telegraph.GetViews( _, err := telegraph.GetViews(validPageURL, time.Date(0, 0, 0, 42, 0, 0, 0, time.UTC))
validPageURL, assert.Error(t, err)
time.Date(0, 0, 0, 42, 0, 0, 0, time.UTC),
); err == nil {
t.Error()
}
} }
func TestInvalidGetViewsByDay(t *testing.T) { func TestInvalidGetViewsByDay(t *testing.T) {
if _, err := telegraph.GetViews( _, err := telegraph.GetViews(validPageURL, time.Date(0, 0, 42, 23, 0, 0, 0, time.UTC))
validPageURL, assert.Error(t, err)
time.Date(0, 0, 42, 23, 0, 0, 0, time.UTC),
); err == nil {
t.Error()
}
} }
func TestInvalidGetViewsByMonth(t *testing.T) { func TestInvalidGetViewsByMonth(t *testing.T) {
if _, err := telegraph.GetViews( _, err := telegraph.GetViews(validPageURL, time.Date(0, 22, 24, 23, 0, 0, 0, time.UTC))
validPageURL, assert.Error(t, err)
time.Date(0, 22, 24, 23, 0, 0, 0, time.UTC),
); err == nil {
t.Error()
}
} }
func TestInvalidGetViewsByYear(t *testing.T) { func TestInvalidGetViewsByYear(t *testing.T) {
if _, err := telegraph.GetViews( _, err := telegraph.GetViews(validPageURL, time.Date(1980, time.December, 24, 23, 0, 0, 0, time.UTC))
validPageURL, assert.Error(t, err)
time.Date(1980, time.December, 24, 23, 0, 0, 0, time.UTC),
); err == nil {
t.Error()
}
} }
func testInvalidRevokeAccessToken(t *testing.T) { func testInvalidRevokeAccessToken(t *testing.T) {
if _, err := invalidAccount.RevokeAccessToken(); err == nil { _, err := invalidAccount.RevokeAccessToken()
t.Error() assert.Error(t, err)
}
} }

View File

@ -4,6 +4,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
"gitlab.com/toby3d/telegraph" "gitlab.com/toby3d/telegraph"
) )
@ -18,34 +19,24 @@ const (
var ( var (
validContentDOM []telegraph.Node validContentDOM []telegraph.Node
validAccount *telegraph.Account
validPage *telegraph.Page
validAccount = new(telegraph.Account)
validPage = new(telegraph.Page)
validContent = `<p>Hello, World!</p>` validContent = `<p>Hello, World!</p>`
) )
func testValidContentFormatByString(t *testing.T) { func testValidContentFormatByString(t *testing.T) {
var err error var err error
validContentDOM, err = telegraph.ContentFormat(validContent) validContentDOM, err = telegraph.ContentFormat(validContent)
if err != nil { assert.NoError(t, err)
t.Error(err.Error()) assert.NotEmpty(t, validContentDOM)
t.FailNow()
}
if len(validContentDOM) <= 0 {
t.Error("DOM content is nil")
}
} }
func testValidContentFormatByBytes(t *testing.T) { func testValidContentFormatByBytes(t *testing.T) {
var err error var err error
validContentDOM, err = telegraph.ContentFormat([]byte(validContent)) validContentDOM, err = telegraph.ContentFormat([]byte(validContent))
if err != nil { assert.NoError(t, err)
t.Error(err.Error()) assert.NotEmpty(t, validContentDOM)
t.FailNow()
}
if len(validContentDOM) <= 0 {
t.Error("DOM content is nil")
}
} }
func TestValidCreateAccount(t *testing.T) { func TestValidCreateAccount(t *testing.T) {
@ -54,10 +45,7 @@ func TestValidCreateAccount(t *testing.T) {
ShortName: validShortName, ShortName: validShortName,
// AuthorName: validAuthorName, // AuthorName: validAuthorName,
}) })
if err != nil { assert.NoError(t, err)
t.Error(err.Error())
t.FailNow()
}
t.Run("validCreatePage", testValidCreatePage) t.Run("validCreatePage", testValidCreatePage)
t.Run("validEditAccountInfo", testValidEditAccountInfo) t.Run("validEditAccountInfo", testValidEditAccountInfo)
@ -77,14 +65,8 @@ func testValidCreatePage(t *testing.T) {
AuthorURL: validAuthorURL, AuthorURL: validAuthorURL,
Content: validContentDOM, Content: validContentDOM,
}, true) }, true)
if err != nil { assert.NoError(t, err)
t.Error(err.Error()) assert.NotEmpty(t, validPage.URL)
t.FailNow()
}
if validPage.URL == "" {
t.Error("new page not contain URL")
}
t.Run("validEditPage", testValidEditPage) t.Run("validEditPage", testValidEditPage)
} }
@ -94,13 +76,8 @@ func testValidEditAccountInfo(t *testing.T) {
AuthorName: validNewAuthorName, AuthorName: validNewAuthorName,
AuthorURL: validAuthorURL, AuthorURL: validAuthorURL,
}) })
if err != nil { assert.NoError(t, err)
t.Error(err.Error()) assert.NotEqual(t, validAccount.AuthorName, update.AuthorName)
t.FailNow()
}
if update.AuthorName == validAccount.AuthorName {
t.Error("account not updated")
}
} }
func testValidEditPage(t *testing.T) { func testValidEditPage(t *testing.T) {
@ -111,78 +88,38 @@ func testValidEditPage(t *testing.T) {
AuthorName: validAuthorName, AuthorName: validAuthorName,
Content: validContentDOM, Content: validContentDOM,
}, true) }, true)
if err != nil { assert.NoError(t, err)
t.Error(err.Error())
t.FailNow()
}
} }
func testValidGetAccountInfo(t *testing.T) { func testValidGetAccountInfo(t *testing.T) {
info, err := validAccount.GetAccountInfo( info, err := validAccount.GetAccountInfo(telegraph.FieldShortName, telegraph.FieldPageCount)
telegraph.FieldShortName, assert.NoError(t, err)
telegraph.FieldPageCount, assert.Equal(t, validAccount.ShortName, info.ShortName)
)
if err != nil {
t.Error(err.Error())
t.FailNow()
}
if info.ShortName != validAccount.ShortName {
t.Error("get wrong account info")
}
} }
func TestValidGetPage(t *testing.T) { func TestValidGetPage(t *testing.T) {
page, err := telegraph.GetPage(validPage.Path, true) page, err := telegraph.GetPage(validPage.Path, true)
if err != nil { assert.NoError(t, err)
t.Error(err.Error()) assert.Equal(t, validPage.Title, page.Title)
t.FailNow()
}
if page.Title != validPage.Title {
t.Error("get wrong page")
}
} }
func testValidGetPageList(t *testing.T) { func testValidGetPageList(t *testing.T) {
pages, err := validAccount.GetPageList(0, 3) pages, err := validAccount.GetPageList(0, 3)
if err != nil { assert.NoError(t, err)
t.Error(err.Error()) assert.NotZero(t, pages.TotalCount)
t.FailNow()
}
if pages.TotalCount <= 0 {
t.Error("no one page in page list")
}
} }
func TestValidGetViews(t *testing.T) { func TestValidGetViews(t *testing.T) {
stats, err := telegraph.GetViews( stats, err := telegraph.GetViews(validPageURL, time.Date(2016, time.December, 0, 0, 0, 0, 0, time.UTC))
validPageURL, assert.NoError(t, err)
time.Date(2016, time.December, 0, 0, 0, 0, 0, time.UTC),
)
if err != nil {
t.Error(err.Error())
t.FailNow()
}
t.Log("get", stats.Views, "views") t.Log("get", stats.Views, "views")
} }
func testValidRevokeAccessToken(t *testing.T) { func testValidRevokeAccessToken(t *testing.T) {
oldToken := validAccount.AccessToken oldToken := validAccount.AccessToken
var err error var err error
validAccount, err = validAccount.RevokeAccessToken() validAccount, err = validAccount.RevokeAccessToken()
if err != nil { assert.NoError(t, err)
t.Error(err.Error()) assert.NotEmpty(t, validAccount.AccessToken)
t.FailNow() assert.NotEqual(t, validAccount.AccessToken, oldToken)
}
if validAccount.AccessToken == "" {
t.Error("revokeAccessToken return nothing")
}
if oldToken == validAccount.AccessToken {
t.Error("old and new tokens are equal")
}
} }