😜 Fix GetViews format and MORE TESTS!!

This commit is contained in:
Maxim Lebedev 2016-12-25 00:26:36 +05:00
parent ae5a43be2d
commit 2c076b69a6
No known key found for this signature in database
GPG Key ID: F8978F46FF0FFA4F
4 changed files with 299 additions and 266 deletions

145
invalid_test.go Normal file
View File

@ -0,0 +1,145 @@
package telegraph
import "testing"
func TestContentFormatByWTF(t *testing.T) {
_, err := ContentFormat(42)
if err == nil {
t.Error()
}
t.Log(err.Error())
}
func TestCreateInvalidAccount(t *testing.T) {
_, err := CreateAccount("", "", "")
if err == nil {
t.Error()
}
t.Log(err.Error())
}
func TestCreateInvalidPage(t *testing.T) {
newPage := &Page{
AuthorURL: "lolwat",
}
_, err := demoAccount.CreatePage(newPage, false)
if err == nil {
t.Error()
}
t.Log(err.Error())
}
func TestEditInvalidAccountInfo(t *testing.T) {
var update Account
_, err := demoAccount.EditAccountInfo(&update)
if 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 {
t.Error()
}
t.Log(err.Error())
}
func TestGetInvalidAccountInfo(t *testing.T) {
var account Account
_, err := account.GetAccountInfo([]string{"short_name", "page_count"})
if err == nil {
t.Error()
}
t.Log(err.Error())
}
func TestGetInvalidPageList(t *testing.T) {
var account Account
_, err := account.GetPageList(0, 3)
if err == nil {
t.Error()
}
t.Log(err.Error())
}
func TestGetInvalidPageListByOffset(t *testing.T) {
var account Account
_, err := account.GetPageList(-42, 3)
if err == nil {
t.Error()
}
t.Log(err.Error())
}
func TestGetInvalidPageListByLimit(t *testing.T) {
var account Account
_, err := account.GetPageList(0, 9000)
if err == nil {
t.Error()
}
t.Log(err.Error())
}
func TestGetInvalidPage(t *testing.T) {
_, err := GetPage("lolwat", true)
if err == nil {
t.Error()
}
t.Log(err.Error())
}
func TestGetInvalidViewsByPage(t *testing.T) {
_, err := GetViews("lolwat", 2016, 12, 0, -1)
if 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 {
t.Error()
}
t.Log(err.Error())
}
func TestGetInvalidViewsByDay(t *testing.T) {
_, err := GetViews("Sample-Page-12-15", 23, 42, 0, 0)
if 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 {
t.Error()
}
t.Log(err.Error())
}
func TestGetInvalidViewsByYear(t *testing.T) {
_, err := GetViews("Sample-Page-12-15", 23, 24, 12, 1980)
if err == nil {
t.Error()
}
t.Log(err.Error())
}
func TestRevokeInvalidAccessToken(t *testing.T) {
var account Account
_, err := account.RevokeAccessToken()
if err == nil {
t.Error()
}
t.Log(err.Error())
}

32
page.go
View File

@ -150,30 +150,30 @@ func (account *Account) GetPageList(offset int, limit int) (*PageList, error) {
// GetViews get the number of views for a Telegraph article. By default, the total number of page
// views will be returned. Returns a PageViews object on success.
func GetViews(path string, year int, month int, day int, hour int) (*PageViews, error) {
func GetViews(path string, hour int, day int, month int, year int) (*PageViews, error) {
var args fasthttp.Args
if hour > -1 {
// If passed, the number of page views for the requested hour will be returned.
args.Add("hour", strconv.Itoa(hour))
}
if day > 0 {
// Required if hour is passed. If passed, the number of page views for the requested day
// will be returned.
args.Add("day", strconv.Itoa(day))
if day > 0 {
// Required if hour is passed. If passed, the number of page views for the requested
// day will be returned.
args.Add("day", strconv.Itoa(day))
}
if month > 0 {
// Required if day is passed. If passed, the number of page views for the requested month
// will be returned.
args.Add("month", strconv.Itoa(month))
if month > 0 {
// Required if day is passed. If passed, the number of page views for the
// requested month will be returned.
args.Add("month", strconv.Itoa(month))
}
if year > 0 {
// Required if month is passed. If passed, the number of page views for the requested year
// will be returned.
args.Add("year", strconv.Itoa(year))
}
}
}
if year > 0 {
// Required if month is passed. If passed, the number of page views for the
// requested year will be returned.
args.Add("year", strconv.Itoa(year))
}
url := fmt.Sprintf(PathEndpoint, "getViews", path)

View File

@ -1,250 +0,0 @@
package telegraph
import (
"testing"
)
var (
demoAccount Account
demoPage Page
demoContent = `<p>Hello, World!</p>`
)
func TestContentFormatByString(t *testing.T) {
_, err := ContentFormat(demoContent)
if err != nil {
t.Error(err.Error())
}
}
func TestContentFormatByBytes(t *testing.T) {
_, err := ContentFormat([]byte(demoContent))
if err != nil {
t.Error(err.Error())
}
}
func TestContentFormatByWTF(t *testing.T) {
_, err := ContentFormat(42)
if err != nil {
t.Error(err.Error())
}
}
func TestCreateInvalidAccount(t *testing.T) {
_, err := CreateAccount("", "", "")
if err != nil {
t.Error(err.Error())
}
}
func TestCreateValidAccount(t *testing.T) {
acc, err := CreateAccount("Sandbox", "Anonymous", "")
if err != nil {
t.Error(err.Error())
}
demoAccount = *acc
t.Logf("New account created!\n%#v", *acc)
}
func TestCreateInvalidPage(t *testing.T) {
newPage := &Page{
AuthorURL: "lolwat",
}
_, err := demoAccount.CreatePage(newPage, false)
if err != nil {
t.Error(err.Error())
}
}
func TestCreateValidPage(t *testing.T) {
content, err := ContentFormat(demoContent)
if err != nil {
t.Error(err.Error())
}
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.Error())
}
demoPage = *page
t.Logf("%#v", *page)
}
func TestEditInvalidAccountInfo(t *testing.T) {
var update Account
_, err := demoAccount.EditAccountInfo(&update)
if err != nil {
t.Error(err.Error())
}
}
func TestEditValidAccountInfo(t *testing.T) {
update := &Account{
ShortName: "Sandbox",
AuthorName: "Anonymous",
AuthorURL: "https://telegram.me/telegraph",
}
info, err := demoAccount.EditAccountInfo(update)
if err != nil {
t.Error(err.Error())
}
t.Logf("Account updated!\n%#v", info)
}
func TestEditInvalidPage(t *testing.T) {
update := &Page{
AuthorURL: "lolwat",
}
_, err := demoAccount.EditPage(update, false)
if err != nil {
t.Error(err.Error())
}
}
func TestEditValidPage(t *testing.T) {
content, err := ContentFormat(demoContent)
if err != nil {
t.Error(err)
}
update := &Page{
Path: demoPage.Path,
Title: "Sample Page",
AuthorName: "Anonymous",
Content: content,
}
page, err := demoAccount.EditPage(update, true)
if err != nil {
t.Error(err.Error())
}
t.Logf("%#v", *page)
}
func TestGetInvalidAccountInfo(t *testing.T) {
var account Account
_, err := account.GetAccountInfo([]string{"short_name", "page_count"})
if err != nil {
t.Error(err.Error())
}
}
func TestGetValidAccountInfo(t *testing.T) {
account, err := demoAccount.GetAccountInfo([]string{"short_name", "page_count"})
if err != nil {
t.Error(err.Error())
}
t.Logf("Account info:\nShort Name: %s\nPage Count: %d", account.ShortName, account.PageCount)
}
func TestGetInvalidPageList(t *testing.T) {
var account Account
_, err := account.GetPageList(0, 3)
if err != nil {
t.Error(err.Error())
}
}
func TestGetValidPageList(t *testing.T) {
pages, err := demoAccount.GetPageList(0, 3)
if err != nil {
t.Error(err.Error())
}
t.Logf("Total %d pages\n%#v", pages.TotalCount, pages.Pages)
}
func TestGetInvalidPage(t *testing.T) {
_, err := GetPage("lolwat", true)
if err != nil {
t.Error(err.Error())
}
}
func TestGetValidPage(t *testing.T) {
page, err := GetPage(demoPage.Path, true)
if err != nil {
t.Error(err.Error())
}
t.Logf("%#v", page)
}
func TestGetInvalidViewsByPage(t *testing.T) {
_, err := GetViews("lolwat", 2016, 12, 0, -1)
if err != nil {
t.Error(err.Error())
}
}
func TestGetInvalidViewsByHour(t *testing.T) {
_, err := GetViews("Sample-Page-12-15", 0, 0, 0, 42)
if err != nil {
t.Error(err.Error())
}
}
func TestGetInvalidViewsByDay(t *testing.T) {
_, err := GetViews("Sample-Page-12-15", 0, 0, 42, -1)
if err != nil {
t.Error(err.Error())
}
}
func TestGetInvalidViewsByMonth(t *testing.T) {
_, err := GetViews("Sample-Page-12-15", 0, 22, 24, -1)
if err != nil {
t.Error(err.Error())
}
}
func TestGetInvalidViewsByYear(t *testing.T) {
_, err := GetViews("Sample-Page-12-15", 2100, 12, 24, 23)
if err != nil {
t.Error(err.Error())
}
}
func TestGetValidViews(t *testing.T) {
views, err := GetViews("Sample-Page-12-15", 2016, 12, 0, -1)
if err != nil {
t.Error(err.Error())
}
t.Logf("This page have %d views", views.Views)
}
func TestRevokeInvalidAccessToken(t *testing.T) {
var account Account
_, err := account.RevokeAccessToken()
if err != nil {
t.Error(err.Error())
}
}
func TestRevokeAccessToken(t *testing.T) {
t.Logf("Old Access Token: %s", demoAccount.AccessToken)
token, err := demoAccount.RevokeAccessToken()
if err != nil {
t.Error(err.Error())
}
t.Logf("New Access Token: %s", token.AccessToken)
}

138
valid_test.go Normal file
View File

@ -0,0 +1,138 @@
package telegraph
import "testing"
var (
demoAccount Account
demoPage Page
demoContent = `<p>Hello, World!</p>`
)
func TestContentFormatByString(t *testing.T) {
_, err := ContentFormat(demoContent)
if err != nil {
t.Error(err)
}
}
func TestContentFormatByBytes(t *testing.T) {
_, err := ContentFormat([]byte(demoContent))
if err != nil {
t.Error(err)
}
}
func TestCreateValidAccount(t *testing.T) {
account, err := CreateAccount("Sandbox", "Anonymous", "")
if err != nil {
t.Error(err)
}
demoAccount = *account
t.Logf("New account created!\n%#v", *account)
}
func TestCreateValidPage(t *testing.T) {
content, err := ContentFormat(demoContent)
if err != nil {
t.Error(err)
}
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)
}
func TestEditValidAccountInfo(t *testing.T) {
update := &Account{
ShortName: "Sandbox",
AuthorName: "Anonymous",
AuthorURL: "https://telegram.me/telegraph",
}
info, err := demoAccount.EditAccountInfo(update)
if err != nil {
t.Error(err)
}
t.Logf("Account updated!\n%#v", *info)
}
func TestEditValidPage(t *testing.T) {
content, err := ContentFormat(demoContent)
if err != nil {
t.Error(err)
}
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([]string{"short_name", "page_count"})
if err != nil {
t.Error(err)
}
t.Logf("Account info:\n%#v", *account)
}
func TestGetValidPageList(t *testing.T) {
pages, err := demoAccount.GetPageList(0, 3)
if err != nil {
t.Error(err)
}
t.Logf("Total %d pages\n%#v", pages.TotalCount, pages.Pages)
}
func TestGetValidPage(t *testing.T) {
page, err := GetPage(demoPage.Path, true)
if err != nil {
t.Error(err)
}
t.Logf("%#v", *page)
}
func TestGetValidViews(t *testing.T) {
views, err := GetViews("Sample-Page-12-15", -1, 0, 12, 2016)
if err != nil {
t.Error(err)
}
t.Logf("This page have %d views", views.Views)
}
func TestRevokeAccessToken(t *testing.T) {
t.Logf("Old Access Token: %s", demoAccount.AccessToken)
token, err := demoAccount.RevokeAccessToken()
if err != nil {
t.Error(err)
}
t.Logf("New Access Token: %s", token.AccessToken)
}