Skip to content

Commit

Permalink
chore: refactor mock objects for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Oct 23, 2023
1 parent c1932a7 commit e2f9cb6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 114 deletions.
93 changes: 27 additions & 66 deletions internal/storage/cp/cp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ import (
"gopkg.in/h2non/gock.v1"
)

var mockFile = client.ObjectResponse{
Name: "abstract.pdf",
Id: utils.Ptr("9b7f9f48-17a6-4ca8-b14a-39b0205a63e9"),
UpdatedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
CreatedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
LastAccessedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
Metadata: &client.ObjectMetadata{
ETag: `"887ea9be3c68e6f2fca7fd2d7c77d8fe"`,
Size: 82702,
Mimetype: "application/pdf",
CacheControl: "max-age=3600",
LastModified: "2023-10-13T18:08:22.000Z",
ContentLength: 82702,
HttpStatusCode: 200,
},
}

func TestStorageCP(t *testing.T) {
t.Run("copy local to remote", func(t *testing.T) {
// Setup in-memory fs
Expand Down Expand Up @@ -314,25 +331,12 @@ func TestUploadAll(t *testing.T) {
Name: "service_role",
ApiKey: "service-key",
}})
fileObject := mockFile
fileObject.Name = "file"
gock.New("https://" + utils.GetSupabaseHost(projectRef)).
Post("/storage/v1/object/list/private").
Reply(http.StatusOK).
JSON([]client.ObjectResponse{{
Name: "file",
Id: utils.Ptr("9b7f9f48-17a6-4ca8-b14a-39b0205a63e9"),
UpdatedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
CreatedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
LastAccessedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
Metadata: &client.ObjectMetadata{
ETag: `"887ea9be3c68e6f2fca7fd2d7c77d8fe"`,
Size: 82702,
Mimetype: "application/pdf",
CacheControl: "max-age=3600",
LastModified: "2023-10-13T18:08:22.000Z",
ContentLength: 82702,
HttpStatusCode: 200,
},
}})
JSON([]client.ObjectResponse{fileObject})
gock.New("https://" + utils.GetSupabaseHost(projectRef)).
Post("/storage/v1/object/private/file").
Reply(http.StatusOK)
Expand Down Expand Up @@ -513,26 +517,13 @@ func TestDownloadAll(t *testing.T) {
Reply(http.StatusOK).
JSON([]client.ObjectResponse{{
Name: "docs",
}, {
Name: "abstract.pdf",
Id: utils.Ptr("9b7f9f48-17a6-4ca8-b14a-39b0205a63e9"),
UpdatedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
CreatedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
LastAccessedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
Metadata: &client.ObjectMetadata{
ETag: `"887ea9be3c68e6f2fca7fd2d7c77d8fe"`,
Size: 82702,
Mimetype: "application/pdf",
CacheControl: "max-age=3600",
LastModified: "2023-10-13T18:08:22.000Z",
ContentLength: 82702,
HttpStatusCode: 200,
},
}})
}, mockFile})
gock.New("https://" + utils.GetSupabaseHost(projectRef)).
Get("/storage/v1/object/private/tmp/abstract.pdf").
Reply(http.StatusOK)
// Lists /private/tmp/docs directory
readme := mockFile
readme.Name = "readme.md"
gock.New("https://" + utils.GetSupabaseHost(projectRef)).
Post("/storage/v1/object/list/private").
JSON(client.ListObjectsQuery{
Expand All @@ -542,24 +533,9 @@ func TestDownloadAll(t *testing.T) {
Offset: 0,
}).
Reply(http.StatusOK).
JSON([]client.ObjectResponse{{
Name: "readme.pdf",
Id: utils.Ptr("9b7f9f48-17a6-4ca8-b14a-39b0205a63e9"),
UpdatedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
CreatedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
LastAccessedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
Metadata: &client.ObjectMetadata{
ETag: `"887ea9be3c68e6f2fca7fd2d7c77d8fe"`,
Size: 82702,
Mimetype: "application/pdf",
CacheControl: "max-age=3600",
LastModified: "2023-10-13T18:08:22.000Z",
ContentLength: 82702,
HttpStatusCode: 200,
},
}})
JSON([]client.ObjectResponse{readme})
gock.New("https://" + utils.GetSupabaseHost(projectRef)).
Get("/storage/v1/object/private/tmp/docs/readme.pdf").
Get("/storage/v1/object/private/tmp/docs/readme.md").
Reply(http.StatusOK)
// Run test
err := DownloadStorageObjectAll(context.Background(), projectRef, "private/tmp/", "/", fsys)
Expand All @@ -569,7 +545,7 @@ func TestDownloadAll(t *testing.T) {
exists, err := afero.Exists(fsys, "/tmp/abstract.pdf")
assert.NoError(t, err)
assert.True(t, exists)
exists, err = afero.Exists(fsys, "/tmp/docs/readme.pdf")
exists, err = afero.Exists(fsys, "/tmp/docs/readme.md")
assert.NoError(t, err)
assert.True(t, exists)
})
Expand All @@ -589,22 +565,7 @@ func TestDownloadAll(t *testing.T) {
gock.New("https://" + utils.GetSupabaseHost(projectRef)).
Post("/storage/v1/object/list/private").
Reply(http.StatusOK).
JSON([]client.ObjectResponse{{
Name: "abstract.pdf",
Id: utils.Ptr("9b7f9f48-17a6-4ca8-b14a-39b0205a63e9"),
UpdatedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
CreatedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
LastAccessedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
Metadata: &client.ObjectMetadata{
ETag: `"887ea9be3c68e6f2fca7fd2d7c77d8fe"`,
Size: 82702,
Mimetype: "application/pdf",
CacheControl: "max-age=3600",
LastModified: "2023-10-13T18:08:22.000Z",
ContentLength: 82702,
HttpStatusCode: 200,
},
}})
JSON([]client.ObjectResponse{mockFile})
gock.New("https://" + utils.GetSupabaseHost(projectRef)).
Get("/storage/v1/object/private/abstract.pdf").
Reply(http.StatusOK)
Expand Down
68 changes: 20 additions & 48 deletions internal/storage/ls/ls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ import (
"gopkg.in/h2non/gock.v1"
)

var mockFile = client.ObjectResponse{
Name: "abstract.pdf",
Id: utils.Ptr("9b7f9f48-17a6-4ca8-b14a-39b0205a63e9"),
UpdatedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
CreatedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
LastAccessedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
Metadata: &client.ObjectMetadata{
ETag: `"887ea9be3c68e6f2fca7fd2d7c77d8fe"`,
Size: 82702,
Mimetype: "application/pdf",
CacheControl: "max-age=3600",
LastModified: "2023-10-13T18:08:22.000Z",
ContentLength: 82702,
HttpStatusCode: 200,
},
}

func TestStorageLS(t *testing.T) {
t.Run("lists buckets", func(t *testing.T) {
// Setup in-memory fs
Expand Down Expand Up @@ -175,22 +192,7 @@ func TestListStoragePaths(t *testing.T) {
Reply(http.StatusOK).
JSON([]client.ObjectResponse{{
Name: "folder",
}, {
Name: "abstract.pdf",
Id: utils.Ptr("9b7f9f48-17a6-4ca8-b14a-39b0205a63e9"),
UpdatedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
CreatedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
LastAccessedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
Metadata: &client.ObjectMetadata{
ETag: `"887ea9be3c68e6f2fca7fd2d7c77d8fe"`,
Size: 82702,
Mimetype: "application/pdf",
CacheControl: "max-age=3600",
LastModified: "2023-10-13T18:08:22.000Z",
ContentLength: 82702,
HttpStatusCode: 200,
},
}})
}, mockFile})
// Run test
paths, err := ListStoragePaths(context.Background(), projectRef, "bucket/")
// Check error
Expand Down Expand Up @@ -331,22 +333,7 @@ func TestListStoragePathsAll(t *testing.T) {
Offset: 0,
}).
Reply(http.StatusOK).
JSON([]client.ObjectResponse{{
Name: "abstract.pdf",
Id: utils.Ptr("9b7f9f48-17a6-4ca8-b14a-39b0205a63e9"),
UpdatedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
CreatedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
LastAccessedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
Metadata: &client.ObjectMetadata{
ETag: `"887ea9be3c68e6f2fca7fd2d7c77d8fe"`,
Size: 82702,
Mimetype: "application/pdf",
CacheControl: "max-age=3600",
LastModified: "2023-10-13T18:08:22.000Z",
ContentLength: 82702,
HttpStatusCode: 200,
},
}})
JSON([]client.ObjectResponse{mockFile})
// Run test
paths, err := ListStoragePathsAll(context.Background(), projectRef, "")
// Check error
Expand Down Expand Up @@ -377,22 +364,7 @@ func TestListStoragePathsAll(t *testing.T) {
Reply(http.StatusOK).
JSON([]client.ObjectResponse{{
Name: "error",
}, {
Name: "abstract.pdf",
Id: utils.Ptr("9b7f9f48-17a6-4ca8-b14a-39b0205a63e9"),
UpdatedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
CreatedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
LastAccessedAt: utils.Ptr("2023-10-13T18:08:22.068Z"),
Metadata: &client.ObjectMetadata{
ETag: `"887ea9be3c68e6f2fca7fd2d7c77d8fe"`,
Size: 82702,
Mimetype: "application/pdf",
CacheControl: "max-age=3600",
LastModified: "2023-10-13T18:08:22.000Z",
ContentLength: 82702,
HttpStatusCode: 200,
},
}})
}, mockFile})
gock.New("https://" + utils.GetSupabaseHost(projectRef)).
Post("/storage/v1/object/list/private").
JSON(client.ListObjectsQuery{
Expand Down

0 comments on commit e2f9cb6

Please sign in to comment.