218 lines
5.5 KiB
Go
218 lines
5.5 KiB
Go
package endpoints
|
|
|
|
import (
|
|
"git.dragse.it/anthrove/e621-to-graph/pkg/e621/model"
|
|
"github.com/jarcoal/httpmock"
|
|
"log"
|
|
"net/http"
|
|
"testing"
|
|
)
|
|
|
|
func TestGetPost(t *testing.T) {
|
|
httpmock.Activate()
|
|
defer httpmock.DeactivateAndReset()
|
|
|
|
postResponse := model.PostResponse{
|
|
Posts: nil,
|
|
Post: model.Post{
|
|
ID: 5316565561,
|
|
CreatedAt: "2023-10-15T03:58:27.272-04:00",
|
|
UpdatedAt: "2023-10-19T02:47:33.597-04:00",
|
|
File: model.File{
|
|
Width: 759,
|
|
Height: 980,
|
|
EXT: "jpg",
|
|
Size: 640942,
|
|
Md5: "36e229e910638c7aebe65a500f16f3ee",
|
|
URL: "https://static1.e621.net/data/36/e2/36e229e910638c7aebe65a500f16f3ee.jpg",
|
|
},
|
|
Preview: model.Preview{},
|
|
Sample: model.Sample{},
|
|
Score: model.Score{},
|
|
Tags: model.Tags{},
|
|
LockedTags: nil,
|
|
ChangeSeq: 0,
|
|
Flags: model.Flags{},
|
|
Rating: "",
|
|
FavCount: 0,
|
|
Sources: nil,
|
|
Pools: nil,
|
|
Relationships: model.Relationships{},
|
|
ApproverID: nil,
|
|
UploaderID: 0,
|
|
Description: "",
|
|
CommentCount: 0,
|
|
IsFavorited: false,
|
|
HasNotes: false,
|
|
Duration: nil,
|
|
},
|
|
}
|
|
jsonResponser, err := httpmock.NewJsonResponder(200, postResponse)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
httpmock.RegisterResponder("GET", "https://e621.net/posts/5316565561.json", jsonResponser)
|
|
|
|
requestContext := model.RequestContext{
|
|
Host: "https://e621.net",
|
|
UserAgent: "Go-e621-SDK (@username)",
|
|
Username: "memo",
|
|
APIKey: "123456",
|
|
}
|
|
|
|
client := http.Client{}
|
|
post, err := GetPost(client, requestContext, "5316565561")
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
if post.ID == postResponse.Post.ID && post.CreatedAt == postResponse.Post.CreatedAt && post.UpdatedAt == postResponse.Post.UpdatedAt && post.File.URL == postResponse.Post.File.URL {
|
|
return
|
|
}
|
|
|
|
t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", post, postResponse)
|
|
|
|
}
|
|
|
|
func TestGetPosts(t *testing.T) {
|
|
httpmock.Activate()
|
|
defer httpmock.DeactivateAndReset()
|
|
|
|
postList := []model.Post{
|
|
{
|
|
ID: 5316565561,
|
|
CreatedAt: "2023-10-15T03:58:27.272-04:00",
|
|
UpdatedAt: "2023-10-19T02:47:33.597-04:00",
|
|
File: model.File{
|
|
Width: 759,
|
|
Height: 980,
|
|
EXT: "jpg",
|
|
Size: 640942,
|
|
Md5: "36e229e910638c7aebe65a500f16f3ee",
|
|
URL: "https://static1.e621.net/data/36/e2/36e229e910638c7aebe65a500f16f3ee.jpg",
|
|
},
|
|
Preview: model.Preview{},
|
|
Sample: model.Sample{},
|
|
Score: model.Score{},
|
|
Tags: model.Tags{},
|
|
LockedTags: nil,
|
|
ChangeSeq: 0,
|
|
Flags: model.Flags{},
|
|
Rating: "",
|
|
FavCount: 0,
|
|
Sources: nil,
|
|
Pools: nil,
|
|
Relationships: model.Relationships{},
|
|
ApproverID: nil,
|
|
UploaderID: 0,
|
|
Description: "",
|
|
CommentCount: 0,
|
|
IsFavorited: false,
|
|
HasNotes: false,
|
|
Duration: nil,
|
|
},
|
|
{
|
|
ID: 5468564521685,
|
|
CreatedAt: "2023-10-15T03:58:27.272-04:00",
|
|
UpdatedAt: "2023-10-19T02:47:33.597-04:00",
|
|
File: model.File{
|
|
Width: 759,
|
|
Height: 980,
|
|
EXT: "jpg",
|
|
Size: 640942,
|
|
Md5: "36e229e910638c7aebe65a500f16f3ee",
|
|
URL: "https://static1.e621.net/data/36/e2/36e229e910638c7aebe65a500f16f3ee.jpg",
|
|
},
|
|
Preview: model.Preview{},
|
|
Sample: model.Sample{},
|
|
Score: model.Score{},
|
|
Tags: model.Tags{},
|
|
LockedTags: nil,
|
|
ChangeSeq: 0,
|
|
Flags: model.Flags{},
|
|
Rating: "",
|
|
FavCount: 0,
|
|
Sources: nil,
|
|
Pools: nil,
|
|
Relationships: model.Relationships{},
|
|
ApproverID: nil,
|
|
UploaderID: 0,
|
|
Description: "",
|
|
CommentCount: 0,
|
|
IsFavorited: false,
|
|
HasNotes: false,
|
|
Duration: nil,
|
|
},
|
|
{
|
|
ID: 68546984,
|
|
CreatedAt: "2023-10-15T03:58:27.272-04:00",
|
|
UpdatedAt: "2023-10-19T02:47:33.597-04:00",
|
|
File: model.File{
|
|
Width: 759,
|
|
Height: 980,
|
|
EXT: "jpg",
|
|
Size: 640942,
|
|
Md5: "36e229e910638c7aebe65a500f16f3ee",
|
|
URL: "https://static1.e621.net/data/36/e2/36e229e910638c7aebe65a500f16f3ee.jpg",
|
|
},
|
|
Preview: model.Preview{},
|
|
Sample: model.Sample{},
|
|
Score: model.Score{},
|
|
Tags: model.Tags{},
|
|
LockedTags: nil,
|
|
ChangeSeq: 0,
|
|
Flags: model.Flags{},
|
|
Rating: "",
|
|
FavCount: 0,
|
|
Sources: nil,
|
|
Pools: nil,
|
|
Relationships: model.Relationships{},
|
|
ApproverID: nil,
|
|
UploaderID: 0,
|
|
Description: "",
|
|
CommentCount: 0,
|
|
IsFavorited: false,
|
|
HasNotes: false,
|
|
Duration: nil,
|
|
},
|
|
}
|
|
|
|
postResponse := model.PostResponse{
|
|
Posts: postList,
|
|
Post: model.Post{},
|
|
}
|
|
|
|
jsonResponser, err := httpmock.NewJsonResponder(200, postResponse)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
httpmock.RegisterResponder("GET", "https://e621.net/posts.json", jsonResponser)
|
|
|
|
requestContext := model.RequestContext{
|
|
Host: "https://e621.net",
|
|
UserAgent: "Go-e621-SDK (@username)",
|
|
Username: "memo",
|
|
APIKey: "123456",
|
|
}
|
|
|
|
client := http.Client{}
|
|
post, err := GetPosts(client, requestContext, map[string]string{})
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
log.Println(len(post))
|
|
log.Println(len(postResponse.Posts))
|
|
|
|
if len(post) == len(postResponse.Posts) && post[0].ID == postResponse.Posts[0].ID && post[1].ID == postResponse.Posts[1].ID && post[2].File.URL == postResponse.Posts[2].File.URL {
|
|
return
|
|
}
|
|
|
|
t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", post, postResponse)
|
|
|
|
}
|