Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Islam Monjid committed Oct 15, 2024
1 parent 34c079e commit b35c8d5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
10 changes: 9 additions & 1 deletion internal/project/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,16 +383,19 @@ func (s *ServiceImpl) CreateLanguagesVersion(ctx context.Context, projectID int,
})
if err != nil {
jobErrors = append(jobErrors, errors.Wrap(err, "Failed to export project"))
return
}

d, err := http.Get(resp.Result.URL)
if err != nil {
jobErrors = append(jobErrors, errors.Wrap(err, "Failed to download project language file"))
return
}
defer d.Body.Close()

if d.StatusCode != http.StatusOK {
jobErrors = append(jobErrors, errors.Errorf("Response code '%d' from download GET", d.StatusCode))
return
}

s3Key := fmt.Sprintf("%s/%s.%s", versionKey, language.Code, contentMeta.Extension)
Expand All @@ -416,6 +419,7 @@ func (s *ServiceImpl) CreateLanguagesVersion(ctx context.Context, projectID int,

if err := s.storage.PutObject(ctx, s3Key, d.Body, meta, contentMeta.Type); err != nil {
jobErrors = append(jobErrors, errors.Wrap(err, "Failed to upload project language file to S3"))
return
}

l.Debug("Done uploading")
Expand All @@ -430,7 +434,11 @@ func (s *ServiceImpl) CreateLanguagesVersion(ctx context.Context, projectID int,
return errors.Wrap(err, "Failed to delete project version in S3")
}

return errors.New("Failed to create language versions")
var err = errors.New("Failed to create language versions")
for _, e := range jobErrors {
err = errors.Wrap(err, e.Error())
}
return err
}

return nil
Expand Down
16 changes: 10 additions & 6 deletions internal/project/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func TestServiceCreateLanguagesVersion(t *testing.T) {
storage := storage.NewMockStorage(controller)
poeditClient := poedit.NewMockClient(controller)

service := NewService(poeditClient, storage, nil, testRenewalThreshold, nil)
service := NewService(poeditClient, storage, nil, testRenewalThreshold, logrus.NewEntry(logrus.New()))
service.generateUUID = testGenerateUUID
service.generateTimestamp = testGenerateTimestamp
service.getContentMetaMap = testGetContentMetaMap
Expand Down Expand Up @@ -366,7 +366,6 @@ func TestServiceCreateLanguagesVersion(t *testing.T) {
})

t.Run("Fail, version already exist in S3", func(t *testing.T) {

prefixExist := fmt.Sprintf("%d/%s_%s_%d", testID, testUUID, testName, testGenerateTimestamp())

listObjectsV2Output := &s3.ListObjectsV2Output{
Expand Down Expand Up @@ -395,29 +394,33 @@ func TestServiceCreateLanguagesVersion(t *testing.T) {
})

t.Run("Fail, fails to export project", func(t *testing.T) {
fileName := fmt.Sprintf("%d/%s_%s_%d", testID, testUUID, testName, testGenerateTimestamp())
storage.EXPECT().ListObjects(testCtx, testStorageKeyForListing).Times(1).Return(listObjectsV2Output, nil)
poeditClient.EXPECT().ListProjectLanguages(testCtx, listProjectLanguagesRequest).Return(listAvailableLanguagesResponse, nil)
poeditClient.EXPECT().ExportProject(testCtx, exportProjectRequest).Return(nil, errTest)

storage.EXPECT().DeleteObjects(testCtx, fileName).Times(1).Return(nil)
err := service.CreateLanguagesVersion(testCtx, int(testProjectID), testName)

assert.ErrorIs(t, err, errTest)
assert.ErrorContains(t, err, "Failed to create language versions")
})

t.Run("Fail, fails to download file", func(t *testing.T) {
fileName := fmt.Sprintf("%d/%s_%s_%d", testID, testUUID, testName, testGenerateTimestamp())
storage.EXPECT().ListObjects(testCtx, testStorageKeyForListing).Times(1).Return(listObjectsV2Output, nil)
poeditClient.EXPECT().ListProjectLanguages(testCtx, listProjectLanguagesRequest).Return(listAvailableLanguagesResponse, nil)
poeditClient.EXPECT().ExportProject(testCtx, exportProjectRequest).Return(exportProjectResponse, nil)
storage.EXPECT().DeleteObjects(testCtx, fileName).Times(1).Return(nil)

httpmock.RegisterResponder("GET", "http://example.com/file.json",
httpmock.NewErrorResponder(errTest))

err := service.CreateLanguagesVersion(testCtx, int(testProjectID), testName)

assert.ErrorIs(t, err, errTest)
assert.ErrorContains(t, err, "Failed to create language versions")
})

t.Run("Fail, fails to upload file to S3", func(t *testing.T) {
fileNameForDeletion := fmt.Sprintf("%d/%s_%s_%d", testID, testUUID, testName, testGenerateTimestamp())
storage.EXPECT().ListObjects(testCtx, testStorageKeyForListing).Times(1).Return(listObjectsV2Output, nil)
poeditClient.EXPECT().ListProjectLanguages(testCtx, listProjectLanguagesRequest).Return(listAvailableLanguagesResponse, nil)
poeditClient.EXPECT().ExportProject(testCtx, exportProjectRequest).Return(exportProjectResponse, nil)
Expand All @@ -426,10 +429,11 @@ func TestServiceCreateLanguagesVersion(t *testing.T) {
httpmock.NewStringResponder(200, `{"key":"value"}`))

storage.EXPECT().PutObject(testCtx, fileName, gomock.Any(), gomock.Any(), "application/json").Return(errTest)
storage.EXPECT().DeleteObjects(testCtx, fileNameForDeletion).Times(1).Return(nil)

err := service.CreateLanguagesVersion(testCtx, int(testProjectID), testName)

assert.ErrorIs(t, err, errTest)
assert.ErrorContains(t, err, "Failed to create language versions")
})
}

Expand Down
11 changes: 10 additions & 1 deletion internal/rest/v1/private/controllers/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http/httptest"
"testing"
"time"
"regexp"

"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -290,11 +291,19 @@ func (cv *CustomValidator) Validate(i interface{}) error {
return cv.validator.Struct(i)
}

func versionValidator(fl validator.FieldLevel) bool {
versionPattern := `^[a-zA-Z0-9.-_]*$`
matched, _ := regexp.MatchString(versionPattern, fl.Field().String())
return matched
}

func TestPostProjectVersion(t *testing.T) {
t.Parallel()

e := echo.New()
e.Validator = &CustomValidator{validator: validator.New()}
validator := validator.New()
validator.RegisterValidation("version", versionValidator)
e.Validator = &CustomValidator{validator: validator}

projectService := project.NewMockService(gomock.NewController(t))

Expand Down

0 comments on commit b35c8d5

Please sign in to comment.