package endpoints import ( "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" "github.com/jarcoal/httpmock" "net/http" "testing" ) func TestGetPost(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() postResponse := model.PostResponse{ Post: model.Post{ ID: 658415636580, 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, }, Posts: nil, } jsonResponser, err := httpmock.NewJsonResponder(200, postResponse) if err != nil { t.Error(err) return } httpmock.RegisterResponder("GET", "https://e621.net/posts/658415636580.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, "658415636580") if err != nil { t.Error(err) return } if post.ID == postResponse.Post.ID && 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() postResponse := model.PostResponse{ Post: model.Post{}, Posts: []model.Post{ { ID: 658415636580, 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: 698418695, 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: 48976516894, 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.json", jsonResponser) requestContext := model.RequestContext{ Host: "https://e621.net", UserAgent: "Go-e621-SDK (@username)", Username: "memo", APIKey: "123456", } client := http.Client{} posts, err := GetPosts(client, requestContext, map[string]string{}) if err != nil { t.Error(err) return } if len(posts) == 3 && posts[0].ID == postResponse.Posts[0].ID && posts[1].File.Md5 == postResponse.Posts[1].File.Md5 && posts[2].File.EXT == postResponse.Posts[2].File.EXT { return } t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", posts, postResponse) }