Skip to content

Commit

Permalink
switch to mockery
Browse files Browse the repository at this point in the history
Signed-off-by: Sarah Funkhouser <[email protected]>
  • Loading branch information
golanglemonade committed Oct 13, 2024
1 parent 39fca10 commit 9b67aef
Show file tree
Hide file tree
Showing 14 changed files with 816 additions and 158 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- +goose Up
-- modify "file_history" table
ALTER TABLE "file_history" DROP COLUMN "correlation_id";
-- modify "files" table
ALTER TABLE "files" DROP COLUMN "correlation_id";

-- +goose Down
-- reverse: modify "files" table
ALTER TABLE "files" ADD COLUMN "correlation_id" character varying NULL;
-- reverse: modify "file_history" table
ALTER TABLE "file_history" ADD COLUMN "correlation_id" character varying NULL;
3 changes: 2 additions & 1 deletion db/migrations-goose-postgres/atlas.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
h1:5HK/jJ6q2tqKEynGWCEZQ/u0ipJTTHBimqAi4JakOhA=
h1:gBid8oQKGsjLr4xXWB0xd0cAiX9lVX+I4YJWlh0YKFE=
20240827061503_init.sql h1:D0Ce7h0FSKpjtQOHZK5gXOpaPvlNAFHHzqfQQ8re0T4=
20241003145053_objectstorage.sql h1:gm1kjZx/NlREPP1WPrDZwojFJ6M4p9vf9NV9yAv0mkA=
20241013020406_correlation_id_removal.sql h1:jwwUP6zeNeOFnQESwVyOJa5iLRVCppZdDoW6riQgUcQ=
4 changes: 4 additions & 0 deletions db/migrations/20241013020405_correlation_id_removal.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Modify "file_history" table
ALTER TABLE "file_history" DROP COLUMN "correlation_id";
-- Modify "files" table
ALTER TABLE "files" DROP COLUMN "correlation_id";
3 changes: 2 additions & 1 deletion db/migrations/atlas.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
h1:18C+RU6eEYjoiY+nBL3/pCciKU2tQkqZH3vgLC1ywTE=
h1:qRZYyu/zyecH2SqrYvxFuBhbxOf1s769i3IgSHiGCAs=
20240827061437_init.sql h1:9pQTZIsiDF3hW0HraVTzaU3M25iiy3MdxvhsZosxgvo=
20241003145052_objectstorage.sql h1:kKVtyWYZ8sZO0vgjp/toNG+3f9kzUmuk/+zJ9eZU5V0=
20241013020405_correlation_id_removal.sql h1:R89+z/ocglNfWwgvsn6inwXIG9WH7Nl4iOqw1fBYcpE=
58 changes: 18 additions & 40 deletions pkg/objects/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package objects_test

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -14,11 +15,11 @@ import (
"testing"

"github.com/sebdah/goldie/v2"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"

"github.com/theopenlane/core/pkg/objects"
"github.com/theopenlane/core/pkg/objects/mocks"
mocks "github.com/theopenlane/core/pkg/objects/mocks"
)

func verifyMatch(t *testing.T, v interface{}) {
Expand All @@ -43,7 +44,7 @@ func TestObjects(t *testing.T) {
name string
maxFileSize int64
pathToFile string
fn func(store *mocks.MockStorage, size int64)
fn func(store *mocks.Storage, size int64)
expectedStatusCode int
validMimeTypes []string
// ignoreFormField instructs the test to not add the
Expand All @@ -55,13 +56,13 @@ func TestObjects(t *testing.T) {
{
name: "uploading succeeds",
maxFileSize: 1024,
fn: func(store *mocks.MockStorage, size int64) {
fn: func(store *mocks.Storage, size int64) {
store.EXPECT().
Upload(gomock.Any(), gomock.Any(), gomock.Any()).
Upload(context.Background(), mock.Anything, mock.Anything).
Return(&objects.UploadedFileMetadata{
Size: size,
}, nil).
Times(1)
Once()
},
expectedStatusCode: http.StatusAccepted,
pathToFile: "objects.md",
Expand All @@ -70,13 +71,8 @@ func TestObjects(t *testing.T) {
{
name: "upload fails because form field does not exist",
maxFileSize: 1024,
fn: func(store *mocks.MockStorage, size int64) {
store.EXPECT().
Upload(gomock.Any(), gomock.Any(), gomock.Any()).
Return(&objects.UploadedFileMetadata{
Size: size,
}, errors.New("could not upload file")). // nolint:err113
Times(0) // make sure this is never called
fn: func(store *mocks.Storage, size int64) {
// leave empty because we should not be calling Upload
},
expectedStatusCode: http.StatusInternalServerError,
pathToFile: "objects.md",
Expand All @@ -87,13 +83,8 @@ func TestObjects(t *testing.T) {
// this test case will use the WithIgnore option
name: "upload middleware succeeds even if the form field does not exist",
maxFileSize: 1024,
fn: func(store *mocks.MockStorage, size int64) {
store.EXPECT().
Upload(gomock.Any(), gomock.Any(), gomock.Any()).
Return(&objects.UploadedFileMetadata{
Size: size,
}, errors.New("could not upload file")). // nolint:err113
Times(0) // make sure this is never called
fn: func(store *mocks.Storage, size int64) {
// leave empty because we should not be calling Upload
},
expectedStatusCode: http.StatusAccepted,
pathToFile: "objects.md",
Expand All @@ -104,13 +95,8 @@ func TestObjects(t *testing.T) {
{
name: "upload fails because of mimetype validation constraints",
maxFileSize: 1024,
fn: func(store *mocks.MockStorage, size int64) {
store.EXPECT().
Upload(gomock.Any(), gomock.Any(), gomock.Any()).
Return(&objects.UploadedFileMetadata{
Size: size,
}, errors.New("could not upload file")). // nolint:err113
Times(0) // make sure this is never called
fn: func(store *mocks.Storage, size int64) {
// leave empty because we should not be calling Upload
},
expectedStatusCode: http.StatusInternalServerError,
pathToFile: "objects.md",
Expand All @@ -119,9 +105,9 @@ func TestObjects(t *testing.T) {
{
name: "upload fails because of storage layer",
maxFileSize: 1024,
fn: func(store *mocks.MockStorage, size int64) {
fn: func(store *mocks.Storage, size int64) {
store.EXPECT().
Upload(gomock.Any(), gomock.Any(), gomock.Any()).
Upload(context.Background(), mock.Anything, mock.Anything).
Return(&objects.UploadedFileMetadata{
Size: size,
}, errors.New("could not upload file")). // nolint:err113
Expand All @@ -134,13 +120,8 @@ func TestObjects(t *testing.T) {
{
name: "upload fails because file is too large",
maxFileSize: 1024,
fn: func(store *mocks.MockStorage, size int64) {
store.EXPECT().
Upload(gomock.Any(), gomock.Any(), gomock.Any()).
Return(&objects.UploadedFileMetadata{
Size: size,
}, errors.New("could not upload file")). // nolint:err113
Times(0) // never call this
fn: func(store *mocks.Storage, size int64) {
// leave empty because we should not be calling Upload
},
expectedStatusCode: http.StatusInternalServerError,
pathToFile: "image.jpg",
Expand All @@ -150,10 +131,7 @@ func TestObjects(t *testing.T) {

for _, v := range tt {
t.Run(v.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

storage := mocks.NewMockStorage(ctrl)
storage := mocks.NewStorage(t)

opts := []objects.Option{
objects.WithMaxFileSize(v.maxFileSize),
Expand Down
82 changes: 82 additions & 0 deletions pkg/objects/mocks/BindFunc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions pkg/objects/mocks/ErrResponseHandler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9b67aef

Please sign in to comment.