2023-10-19 13:00:17 +00:00
|
|
|
package endpoints
|
|
|
|
|
|
|
|
import (
|
2023-10-24 13:10:39 +00:00
|
|
|
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
|
2023-11-06 11:07:55 +00:00
|
|
|
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils"
|
2023-10-19 13:00:17 +00:00
|
|
|
"github.com/jarcoal/httpmock"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetPost(t *testing.T) {
|
|
|
|
httpmock.Activate()
|
|
|
|
defer httpmock.DeactivateAndReset()
|
|
|
|
|
2023-11-06 11:07:55 +00:00
|
|
|
data, err := utils.LoadJsonTestData[model.Post]("../../../tests/post.json")
|
2023-10-24 14:28:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response := model.PostResponse{
|
|
|
|
Post: data,
|
2023-10-19 13:00:17 +00:00
|
|
|
Posts: nil,
|
|
|
|
}
|
|
|
|
|
2023-10-24 14:28:24 +00:00
|
|
|
responder, err := httpmock.NewJsonResponder(200, response)
|
2023-10-19 13:00:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2023-10-24 14:28:24 +00:00
|
|
|
httpmock.RegisterResponder("GET", "https://e621.net/posts/1337.json", responder)
|
2023-10-19 13:00:17 +00:00
|
|
|
|
|
|
|
requestContext := model.RequestContext{
|
2023-10-23 14:06:52 +00:00
|
|
|
Client: http.Client{},
|
2023-10-19 13:00:17 +00:00
|
|
|
Host: "https://e621.net",
|
|
|
|
UserAgent: "Go-e621-SDK (@username)",
|
|
|
|
Username: "memo",
|
|
|
|
APIKey: "123456",
|
|
|
|
}
|
|
|
|
|
2023-10-24 14:28:24 +00:00
|
|
|
post, err := GetPost(requestContext, "1337")
|
2023-10-19 13:00:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-24 14:28:24 +00:00
|
|
|
if post.ID == response.Post.ID && post.File.URL == response.Post.File.URL {
|
2023-10-19 13:00:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-24 14:28:24 +00:00
|
|
|
t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", post, response)
|
2023-10-19 13:00:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPosts(t *testing.T) {
|
|
|
|
httpmock.Activate()
|
|
|
|
defer httpmock.DeactivateAndReset()
|
|
|
|
|
2023-11-06 11:07:55 +00:00
|
|
|
response, err := utils.LoadJsonTestData[model.PostResponse]("../../../tests/posts.json")
|
2023-10-24 14:28:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
2023-10-19 13:00:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-24 14:28:24 +00:00
|
|
|
responder, err := httpmock.NewJsonResponder(200, response)
|
2023-10-19 13:00:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2023-10-24 14:28:24 +00:00
|
|
|
httpmock.RegisterResponder("GET", "https://e621.net/posts.json", responder)
|
2023-10-19 13:00:17 +00:00
|
|
|
|
|
|
|
requestContext := model.RequestContext{
|
2023-10-23 14:06:52 +00:00
|
|
|
Client: http.Client{},
|
2023-10-19 13:00:17 +00:00
|
|
|
Host: "https://e621.net",
|
|
|
|
UserAgent: "Go-e621-SDK (@username)",
|
|
|
|
Username: "memo",
|
|
|
|
APIKey: "123456",
|
|
|
|
}
|
|
|
|
|
2023-10-23 14:06:52 +00:00
|
|
|
posts, err := GetPosts(requestContext, map[string]string{})
|
2023-10-19 13:00:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-24 14:28:24 +00:00
|
|
|
if posts[0].ID == response.Posts[0].ID && posts[1].File.Md5 == response.Posts[1].File.Md5 && posts[2].File.EXT == response.Posts[2].File.EXT {
|
2023-10-19 13:00:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-24 14:28:24 +00:00
|
|
|
t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", posts, response)
|
2023-10-19 13:00:17 +00:00
|
|
|
|
|
|
|
}
|