From 517c7f595e38d67e2d5d38a467aa780a49511b2f Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 6 Nov 2023 12:07:55 +0100 Subject: [PATCH] refactor: tests use premade json files to test against. Signed-off-by: SoXX --- pkg/e621/builder/favorite_test.go | 111 ++--------------- pkg/e621/builder/note_test.go | 31 ++--- pkg/e621/builder/notes_test.go | 63 ++-------- pkg/e621/builder/pool_test.go | 29 ++--- pkg/e621/builder/pools_test.go | 69 ++--------- pkg/e621/builder/post_test.go | 56 +++------ pkg/e621/builder/posts_test.go | 117 ++---------------- pkg/e621/builder/tag_test.go | 27 ++-- pkg/e621/builder/user_test.go | 42 ++----- pkg/e621/builder/users_test.go | 71 ++--------- pkg/e621/endpoints/favorite_test.go | 3 +- pkg/e621/endpoints/note_test.go | 5 +- pkg/e621/endpoints/pool_test.go | 5 +- pkg/e621/endpoints/post_test.go | 5 +- pkg/e621/endpoints/tag_test.go | 3 +- pkg/e621/endpoints/user_test.go | 5 +- .../utils_test.go => utils/test.go} | 4 +- 17 files changed, 129 insertions(+), 517 deletions(-) rename pkg/e621/{endpoints/utils_test.go => utils/test.go} (84%) diff --git a/pkg/e621/builder/favorite_test.go b/pkg/e621/builder/favorite_test.go index 1c2609f..f028643 100644 --- a/pkg/e621/builder/favorite_test.go +++ b/pkg/e621/builder/favorite_test.go @@ -2,6 +2,7 @@ package builder import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "github.com/jarcoal/httpmock" "net/http" "testing" @@ -11,109 +12,13 @@ func TestGetFavorites(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, - }, - }, + response, err := utils.LoadJsonTestData[model.PostResponse]("../../../tests/posts.json") + if err != nil { + t.Error(err) + return } - jsonResponser, err := httpmock.NewJsonResponder(200, postResponse) + jsonResponser, err := httpmock.NewJsonResponder(200, response) if err != nil { t.Error(err) return @@ -134,10 +39,10 @@ func TestGetFavorites(t *testing.T) { 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 { + 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 { return } - t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", posts, postResponse) + t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", posts, response) } diff --git a/pkg/e621/builder/note_test.go b/pkg/e621/builder/note_test.go index 3040b21..e1de9b9 100644 --- a/pkg/e621/builder/note_test.go +++ b/pkg/e621/builder/note_test.go @@ -2,6 +2,7 @@ package builder import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "github.com/jarcoal/httpmock" "net/http" "testing" @@ -11,28 +12,18 @@ func TestGetNote(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - noteResponse := model.Note{ - ID: 385777, - CreatedAt: "2023-10-19T06:42:20.171-04:00", - UpdatedAt: "2023-10-19T06:42:20.171-04:00", - CreatorID: 1472475, - X: 277, - Y: 841, - Width: 101, - Height: 176, - Version: 2, - IsActive: true, - PostID: 2624329, - Body: "Well, isn’t it just right to use them?", - CreatorName: "ponypussypounder", - } - - jsonResponser, err := httpmock.NewJsonResponder(200, noteResponse) + response, err := utils.LoadJsonTestData[model.Note]("../../../tests/note.json") if err != nil { t.Error(err) return } - httpmock.RegisterResponder("GET", "https://e621.net/notes/36957.json", jsonResponser) + + responser, err := httpmock.NewJsonResponder(200, response) + if err != nil { + t.Error(err) + return + } + httpmock.RegisterResponder("GET", "https://e621.net/notes/36957.json", responser) requestContext := model.RequestContext{ Client: http.Client{}, @@ -49,10 +40,10 @@ func TestGetNote(t *testing.T) { return } - if pool.ID == noteResponse.ID && pool.Body == noteResponse.Body { + if pool.ID == response.ID && pool.Body == response.Body { return } - t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", pool, noteResponse) + t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", pool, response) } diff --git a/pkg/e621/builder/notes_test.go b/pkg/e621/builder/notes_test.go index 5e649be..e7b61c0 100644 --- a/pkg/e621/builder/notes_test.go +++ b/pkg/e621/builder/notes_test.go @@ -2,6 +2,7 @@ package builder import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "github.com/jarcoal/httpmock" "net/http" "testing" @@ -11,60 +12,18 @@ func TestGetNotes(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - noteResponse := []model.Note{ - { - ID: 385777, - CreatedAt: "2023-10-19T06:42:20.171-04:00", - UpdatedAt: "2023-10-19T06:42:20.171-04:00", - CreatorID: 1472475, - X: 277, - Y: 841, - Width: 101, - Height: 176, - Version: 2, - IsActive: true, - PostID: 2624329, - Body: "Well, isn’t it just right to use them?", - CreatorName: "ponypussypounder", - }, - { - ID: 56782, - CreatedAt: "2023-10-19T06:42:20.171-04:00", - UpdatedAt: "2023-10-19T06:42:20.171-04:00", - CreatorID: 1472475, - X: 277, - Y: 841, - Width: 101, - Height: 176, - Version: 2, - IsActive: true, - PostID: 2624329, - Body: "Well, isn’t it just right to use them?", - CreatorName: "ponypussypounder", - }, - { - ID: 43847234, - CreatedAt: "2023-10-19T06:42:20.171-04:00", - UpdatedAt: "2023-10-19T06:42:20.171-04:00", - CreatorID: 1472475, - X: 277, - Y: 841, - Width: 101, - Height: 176, - Version: 2, - IsActive: true, - PostID: 2624329, - Body: "Well, isn’t it just right to use them?", - CreatorName: "ponypussypounder", - }, - } - - jsonResponser, err := httpmock.NewJsonResponder(200, noteResponse) + response, err := utils.LoadJsonTestData[[]model.Note]("../../../tests/notes.json") if err != nil { t.Error(err) return } - httpmock.RegisterResponder("GET", "https://e621.net/notes.json", jsonResponser) + + responser, err := httpmock.NewJsonResponder(200, response) + if err != nil { + t.Error(err) + return + } + httpmock.RegisterResponder("GET", "https://e621.net/notes.json", responser) requestContext := model.RequestContext{ Client: http.Client{}, @@ -81,10 +40,10 @@ func TestGetNotes(t *testing.T) { return } - if len(pools) == 3 && pools[0].ID == noteResponse[0].ID && pools[1].Body == noteResponse[1].Body && pools[2].UpdatedAt == noteResponse[2].UpdatedAt { + if pools[0].ID == response[0].ID && pools[1].Body == response[1].Body && pools[2].UpdatedAt == response[2].UpdatedAt { return } - t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", pools, noteResponse) + t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", pools, response) } diff --git a/pkg/e621/builder/pool_test.go b/pkg/e621/builder/pool_test.go index 2b2eff6..157a424 100644 --- a/pkg/e621/builder/pool_test.go +++ b/pkg/e621/builder/pool_test.go @@ -2,6 +2,7 @@ package builder import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "github.com/jarcoal/httpmock" "net/http" "testing" @@ -11,26 +12,18 @@ func TestGetPool(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - poolResponse := model.Pool{ - ID: 36957, - Name: "MLP:His_Nocturnal_Excellency", - CreatedAt: "2023-10-13T19:24:00.576-04:00", - UpdatedAt: "2023-10-19T07:46:00.267-04:00", - CreatorID: 16883, - Description: "Two (un)lucky royal guards who saw something they didn’t have to~", - IsActive: true, - Category: "series", - PostIDS: nil, - CreatorName: "2DUK", - PostCount: 11, - } - - jsonResponser, err := httpmock.NewJsonResponder(200, poolResponse) + response, err := utils.LoadJsonTestData[model.Pool]("../../../tests/pool.json") if err != nil { t.Error(err) return } - httpmock.RegisterResponder("GET", "https://e621.net/pools/36957.json", jsonResponser) + + responser, err := httpmock.NewJsonResponder(200, response) + if err != nil { + t.Error(err) + return + } + httpmock.RegisterResponder("GET", "https://e621.net/pools/36957.json", responser) requestContext := model.RequestContext{ Client: http.Client{}, @@ -47,10 +40,10 @@ func TestGetPool(t *testing.T) { return } - if pool.ID == poolResponse.ID && pool.Name == poolResponse.Name { + if pool.ID == response.ID && pool.Name == response.Name { return } - t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", pool, poolResponse) + t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", pool, response) } diff --git a/pkg/e621/builder/pools_test.go b/pkg/e621/builder/pools_test.go index 4a971e9..11ed09c 100644 --- a/pkg/e621/builder/pools_test.go +++ b/pkg/e621/builder/pools_test.go @@ -2,6 +2,7 @@ package builder import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "github.com/jarcoal/httpmock" "net/http" "testing" @@ -11,67 +12,17 @@ func TestGetPools(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - poolResponse := []model.Pool{ - { - ID: 36957, - Name: "MLP:His_Nocturnal_Excellency", - CreatedAt: "2023-10-13T19:24:00.576-04:00", - UpdatedAt: "2023-10-19T07:46:00.267-04:00", - CreatorID: 16883, - Description: "Two (un)lucky royal guards who saw something they didn’t have to~", - IsActive: true, - Category: "series", - PostIDS: nil, - CreatorName: "2DUK", - PostCount: 11, - }, - { - ID: 5772, - Name: "MLP:His_Nocturnal_Excellency", - CreatedAt: "2023-10-13T19:24:00.576-04:00", - UpdatedAt: "2023-10-19T07:46:00.267-04:00", - CreatorID: 16883, - Description: "Two (un)lucky royal guards who saw something they didn’t have to~", - IsActive: true, - Category: "series", - PostIDS: nil, - CreatorName: "2DUK", - PostCount: 11, - }, - { - ID: 453687, - Name: "MLP:His_Nocturnal_Excellency", - CreatedAt: "2023-10-13T19:24:00.576-04:00", - UpdatedAt: "2023-10-19T07:46:00.267-04:00", - CreatorID: 16883, - Description: "Two (un)lucky royal guards who saw something they didn’t have to~", - IsActive: true, - Category: "series", - PostIDS: nil, - CreatorName: "2DUK", - PostCount: 11, - }, - { - ID: 3456876, - Name: "MLP:His_Nocturnal_Excellency", - CreatedAt: "2023-10-13T19:24:00.576-04:00", - UpdatedAt: "2023-10-19T07:46:00.267-04:00", - CreatorID: 16883, - Description: "Two (un)lucky royal guards who saw something they didn’t have to~", - IsActive: true, - Category: "series", - PostIDS: nil, - CreatorName: "2DUK", - PostCount: 11, - }, - } - - jsonResponser, err := httpmock.NewJsonResponder(200, poolResponse) + response, err := utils.LoadJsonTestData[[]model.Pool]("../../../tests/pools.json") if err != nil { t.Error(err) return } - httpmock.RegisterResponder("GET", "https://e621.net/pools.json", jsonResponser) + responser, err := httpmock.NewJsonResponder(200, response) + if err != nil { + t.Error(err) + return + } + httpmock.RegisterResponder("GET", "https://e621.net/pools.json", responser) requestContext := model.RequestContext{ Client: http.Client{}, @@ -88,10 +39,10 @@ func TestGetPools(t *testing.T) { return } - if len(pools) == 4 && pools[0].ID == poolResponse[0].ID && pools[1].Name == poolResponse[1].Name && pools[2].UpdatedAt == poolResponse[2].UpdatedAt { + if pools[0].ID == response[0].ID && pools[1].Name == response[1].Name && pools[2].UpdatedAt == response[2].UpdatedAt { return } - t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", pools, poolResponse) + t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", pools, response) } diff --git a/pkg/e621/builder/post_test.go b/pkg/e621/builder/post_test.go index 2fbc770..c8f3340 100644 --- a/pkg/e621/builder/post_test.go +++ b/pkg/e621/builder/post_test.go @@ -2,6 +2,7 @@ package builder import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "github.com/jarcoal/httpmock" "net/http" "testing" @@ -11,48 +12,23 @@ 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) + data, err := utils.LoadJsonTestData[model.Post]("../../../tests/post.json") if err != nil { t.Error(err) return } - httpmock.RegisterResponder("GET", "https://e621.net/posts/658415636580.json", jsonResponser) + + response := model.PostResponse{ + Post: data, + Posts: nil, + } + + responser, err := httpmock.NewJsonResponder(200, response) + if err != nil { + t.Error(err) + return + } + httpmock.RegisterResponder("GET", "https://e621.net/posts/658415636580.json", responser) requestContext := model.RequestContext{ Client: http.Client{}, @@ -69,10 +45,10 @@ func TestGetPost(t *testing.T) { return } - if post.ID == postResponse.Post.ID && post.File.URL == postResponse.Post.File.URL { + if post.ID == response.Post.ID && post.File.URL == response.Post.File.URL { return } - t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", post, postResponse) + t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", post, response) } diff --git a/pkg/e621/builder/posts_test.go b/pkg/e621/builder/posts_test.go index 83ea370..8a912c0 100644 --- a/pkg/e621/builder/posts_test.go +++ b/pkg/e621/builder/posts_test.go @@ -2,6 +2,7 @@ package builder import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "github.com/jarcoal/httpmock" "net/http" "testing" @@ -11,114 +12,18 @@ 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) + response, err := utils.LoadJsonTestData[model.PostResponse]("../../../tests/posts.json") if err != nil { t.Error(err) return } - httpmock.RegisterResponder("GET", "https://e621.net/posts.json", jsonResponser) + + responser, err := httpmock.NewJsonResponder(200, response) + if err != nil { + t.Error(err) + return + } + httpmock.RegisterResponder("GET", "https://e621.net/posts.json", responser) requestContext := model.RequestContext{ Client: http.Client{}, @@ -135,10 +40,10 @@ func TestGetPosts(t *testing.T) { 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 { + 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 { return } - t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", posts, postResponse) + t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", posts, response) } diff --git a/pkg/e621/builder/tag_test.go b/pkg/e621/builder/tag_test.go index 26575bf..49a3678 100644 --- a/pkg/e621/builder/tag_test.go +++ b/pkg/e621/builder/tag_test.go @@ -2,6 +2,7 @@ package builder import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "github.com/jarcoal/httpmock" "net/http" "testing" @@ -11,24 +12,18 @@ func TestGetTag(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - tagResponse := model.Tag{ - ID: 165165, - Name: "35ad13", - PostCount: 59, - RelatedTags: "[]", - RelatedTagsUpdatedAt: "2023-10-16T07:58:15.897+02:00", - Category: 0, - IsLocked: false, - CreatedAt: "2023-10-16T07:58:15.917+02:00", - UpdatedAt: "2023-10-16T07:58:23.310+02:00", - } - - jsonResponser, err := httpmock.NewJsonResponder(200, tagResponse) + response, err := utils.LoadJsonTestData[model.Tag]("../../../tests/tag.json") if err != nil { t.Error(err) return } - httpmock.RegisterResponder("GET", "https://e621.net/tags/165165.json", jsonResponser) + + responser, err := httpmock.NewJsonResponder(200, response) + if err != nil { + t.Error(err) + return + } + httpmock.RegisterResponder("GET", "https://e621.net/tags/165165.json", responser) requestContext := model.RequestContext{ Client: http.Client{}, @@ -45,10 +40,10 @@ func TestGetTag(t *testing.T) { return } - if tag.ID == tagResponse.ID && tag.Name == tagResponse.Name && tag.CreatedAt == tagResponse.CreatedAt { + if tag.ID == response.ID && tag.Name == response.Name && tag.CreatedAt == response.CreatedAt { return } - t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", tag, tagResponse) + t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", tag, response) } diff --git a/pkg/e621/builder/user_test.go b/pkg/e621/builder/user_test.go index fc7c3b2..1c0664d 100644 --- a/pkg/e621/builder/user_test.go +++ b/pkg/e621/builder/user_test.go @@ -2,6 +2,7 @@ package builder import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "github.com/jarcoal/httpmock" "net/http" "testing" @@ -11,39 +12,18 @@ func TestGetUser(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - userResponse := model.User{ - WikiPageVersionCount: 0, - ArtistVersionCount: 0, - PoolVersionCount: 0, - ForumPostCount: 0, - CommentCount: 69, - FlagCount: 0, - FavoriteCount: 1337, - PositiveFeedbackCount: 0, - NeutralFeedbackCount: 0, - NegativeFeedbackCount: 0, - UploadLimit: 0, - ID: 1, - CreatedAt: "2020-04-07T07:16:40.286+02:00", - Name: "MaxMustermannDer69ste", - Level: 0, - BaseUploadLimit: 0, - PostUploadCount: 0, - PostUpdateCount: 0, - NoteUpdateCount: 0, - IsBanned: false, - CanApprovePosts: false, - CanUploadFree: false, - LevelString: "Member", - AvatarID: 7, - } - - jsonResponser, err := httpmock.NewJsonResponder(200, userResponse) + response, err := utils.LoadJsonTestData[model.User]("../../../tests/user.json") if err != nil { t.Error(err) return } - httpmock.RegisterResponder("GET", "https://e621.net/users/MaxMustermannDer69ste.json", jsonResponser) + + responser, err := httpmock.NewJsonResponder(200, response) + if err != nil { + t.Error(err) + return + } + httpmock.RegisterResponder("GET", "https://e621.net/users/MaxMustermannDer69ste.json", responser) requestContext := model.RequestContext{ Client: http.Client{}, @@ -60,10 +40,10 @@ func TestGetUser(t *testing.T) { return } - if user.ID == userResponse.ID && user.Name == userResponse.Name && user.CreatedAt == userResponse.CreatedAt { + if user.ID == response.ID && user.Name == response.Name && user.CreatedAt == response.CreatedAt { return } - t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", user, userResponse) + t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", user, response) } diff --git a/pkg/e621/builder/users_test.go b/pkg/e621/builder/users_test.go index a0ea4a8..4e84221 100644 --- a/pkg/e621/builder/users_test.go +++ b/pkg/e621/builder/users_test.go @@ -2,6 +2,7 @@ package builder import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "github.com/jarcoal/httpmock" "net/http" "testing" @@ -11,68 +12,18 @@ func TestGetUsers(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - userResponse := []model.User{ - { - - WikiPageVersionCount: 0, - ArtistVersionCount: 0, - PoolVersionCount: 0, - ForumPostCount: 0, - CommentCount: 69, - FlagCount: 0, - FavoriteCount: 1337, - PositiveFeedbackCount: 0, - NeutralFeedbackCount: 0, - NegativeFeedbackCount: 0, - UploadLimit: 0, - ID: 1, - CreatedAt: "2020-04-07T07:16:40.286+02:00", - Name: "MaxMustermannDer69ste", - Level: 0, - BaseUploadLimit: 0, - PostUploadCount: 0, - PostUpdateCount: 0, - NoteUpdateCount: 0, - IsBanned: false, - CanApprovePosts: false, - CanUploadFree: false, - LevelString: "Member", - AvatarID: 7, - }, - { - WikiPageVersionCount: 0, - ArtistVersionCount: 0, - PoolVersionCount: 0, - ForumPostCount: 0, - CommentCount: 1337, - FlagCount: 0, - FavoriteCount: 69, - PositiveFeedbackCount: 0, - NeutralFeedbackCount: 0, - NegativeFeedbackCount: 0, - UploadLimit: 0, - ID: 1, - CreatedAt: "2020-04-08T07:16:40.286+02:00", - Name: "HollaDieWaldfee", - Level: 0, - BaseUploadLimit: 0, - PostUploadCount: 0, - PostUpdateCount: 0, - NoteUpdateCount: 0, - IsBanned: false, - CanApprovePosts: false, - CanUploadFree: false, - LevelString: "Member", - AvatarID: 16, - }, - } - - jsonResponser, err := httpmock.NewJsonResponder(200, userResponse) + response, err := utils.LoadJsonTestData[[]model.User]("../../../tests/users.json") if err != nil { t.Error(err) return } - httpmock.RegisterResponder("GET", "https://e621.net/users.json", jsonResponser) + + responser, err := httpmock.NewJsonResponder(200, response) + if err != nil { + t.Error(err) + return + } + httpmock.RegisterResponder("GET", "https://e621.net/users.json", responser) requestContext := model.RequestContext{ Client: http.Client{}, @@ -89,10 +40,10 @@ func TestGetUsers(t *testing.T) { return } - if len(user) == 2 && user[0].ID == userResponse[0].ID && user[1].Name == userResponse[1].Name { + if user[0].ID == response[0].ID && user[1].Name == response[1].Name { return } - t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", user, userResponse) + t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", user, response) } diff --git a/pkg/e621/endpoints/favorite_test.go b/pkg/e621/endpoints/favorite_test.go index 4151f3a..7af8a5a 100644 --- a/pkg/e621/endpoints/favorite_test.go +++ b/pkg/e621/endpoints/favorite_test.go @@ -2,6 +2,7 @@ package endpoints import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "github.com/jarcoal/httpmock" "net/http" "testing" @@ -11,7 +12,7 @@ func TestGetFavorites(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - response, err := loadJsonTestData[model.PostResponse]("../../../tests/posts.json") + response, err := utils.LoadJsonTestData[model.PostResponse]("../../../tests/posts.json") if err != nil { t.Error(err) return diff --git a/pkg/e621/endpoints/note_test.go b/pkg/e621/endpoints/note_test.go index b20a243..045292e 100644 --- a/pkg/e621/endpoints/note_test.go +++ b/pkg/e621/endpoints/note_test.go @@ -2,6 +2,7 @@ package endpoints import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "github.com/jarcoal/httpmock" "net/http" "testing" @@ -11,7 +12,7 @@ func TestGetNote(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - response, err := loadJsonTestData[model.Note]("../../../tests/note.json") + response, err := utils.LoadJsonTestData[model.Note]("../../../tests/note.json") if err != nil { t.Error(err) return @@ -50,7 +51,7 @@ func TestGetNotes(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - response, err := loadJsonTestData[[]model.Note]("../../../tests/notes.json") + response, err := utils.LoadJsonTestData[[]model.Note]("../../../tests/notes.json") if err != nil { t.Error(err) return diff --git a/pkg/e621/endpoints/pool_test.go b/pkg/e621/endpoints/pool_test.go index d7d80ac..fc36ea0 100644 --- a/pkg/e621/endpoints/pool_test.go +++ b/pkg/e621/endpoints/pool_test.go @@ -2,6 +2,7 @@ package endpoints import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "github.com/jarcoal/httpmock" "net/http" "testing" @@ -11,7 +12,7 @@ func TestGetPool(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - response, err := loadJsonTestData[model.Pool]("../../../tests/pool.json") + response, err := utils.LoadJsonTestData[model.Pool]("../../../tests/pool.json") if err != nil { t.Error(err) return @@ -50,7 +51,7 @@ func TestGetPools(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - response, err := loadJsonTestData[[]model.Pool]("../../../tests/pools.json") + response, err := utils.LoadJsonTestData[[]model.Pool]("../../../tests/pools.json") if err != nil { t.Error(err) return diff --git a/pkg/e621/endpoints/post_test.go b/pkg/e621/endpoints/post_test.go index 9b46b2a..ca6fa35 100644 --- a/pkg/e621/endpoints/post_test.go +++ b/pkg/e621/endpoints/post_test.go @@ -2,6 +2,7 @@ package endpoints import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "github.com/jarcoal/httpmock" "net/http" "testing" @@ -11,7 +12,7 @@ func TestGetPost(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - data, err := loadJsonTestData[model.Post]("../../../tests/post.json") + data, err := utils.LoadJsonTestData[model.Post]("../../../tests/post.json") if err != nil { t.Error(err) return @@ -55,7 +56,7 @@ func TestGetPosts(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - response, err := loadJsonTestData[model.PostResponse]("../../../tests/posts.json") + response, err := utils.LoadJsonTestData[model.PostResponse]("../../../tests/posts.json") if err != nil { t.Error(err) return diff --git a/pkg/e621/endpoints/tag_test.go b/pkg/e621/endpoints/tag_test.go index b9f05ab..bd151c0 100644 --- a/pkg/e621/endpoints/tag_test.go +++ b/pkg/e621/endpoints/tag_test.go @@ -2,6 +2,7 @@ package endpoints import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "github.com/jarcoal/httpmock" "net/http" "testing" @@ -11,7 +12,7 @@ func TestGetTag(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - response, err := loadJsonTestData[model.Tag]("../../../tests/tag.json") + response, err := utils.LoadJsonTestData[model.Tag]("../../../tests/tag.json") if err != nil { t.Error(err) return diff --git a/pkg/e621/endpoints/user_test.go b/pkg/e621/endpoints/user_test.go index f259a8f..45950a6 100644 --- a/pkg/e621/endpoints/user_test.go +++ b/pkg/e621/endpoints/user_test.go @@ -2,6 +2,7 @@ package endpoints import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "github.com/jarcoal/httpmock" "net/http" "testing" @@ -11,7 +12,7 @@ func TestGetUser(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - response, err := loadJsonTestData[model.User]("../../../tests/user.json") + response, err := utils.LoadJsonTestData[model.User]("../../../tests/user.json") if err != nil { t.Error(err) return @@ -50,7 +51,7 @@ func TestGetUsers(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - response, err := loadJsonTestData[[]model.User]("../../../tests/users.json") + response, err := utils.LoadJsonTestData[[]model.User]("../../../tests/users.json") if err != nil { t.Error(err) return diff --git a/pkg/e621/endpoints/utils_test.go b/pkg/e621/utils/test.go similarity index 84% rename from pkg/e621/endpoints/utils_test.go rename to pkg/e621/utils/test.go index e596cc2..39a9a71 100644 --- a/pkg/e621/endpoints/utils_test.go +++ b/pkg/e621/utils/test.go @@ -1,11 +1,11 @@ -package endpoints +package utils import ( "encoding/json" "os" ) -func loadJsonTestData[T any](testDataPath string) (T, error) { +func LoadJsonTestData[T any](testDataPath string) (T, error) { // Create a variable to store the decoded JSON data var jsonData T