Skip to content

Commit

Permalink
test: add unit tests for models (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
vorobeyme authored Jun 27, 2024
1 parent 8b66732 commit 5257703
Show file tree
Hide file tree
Showing 66 changed files with 6,317 additions and 2,416 deletions.
101 changes: 0 additions & 101 deletions crowdin/ai_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,57 +210,6 @@ func TestAIService_AddPrompt(t *testing.T) {
assert.Equal(t, expected, prompt)
}

func TestAIService_AddPrompt_requestValidation(t *testing.T) {
tests := []struct {
name string
req *model.PromptAddRequest
err string
}{
{
name: "nil request",
req: nil,
err: "request cannot be nil",
},
{
name: "empty name",
req: &model.PromptAddRequest{},
err: "name is required",
},
{
name: "empty action",
req: &model.PromptAddRequest{Name: "Pre-translate prompt"},
err: "action is required",
},
{
name: "empty aiProviderId",
req: &model.PromptAddRequest{Name: "Pre-translate prompt", Action: "pre_translate"},
err: "aiProviderId is required",
},
{
name: "empty aiModelId",
req: &model.PromptAddRequest{Name: "Pre-translate prompt", Action: "pre_translate", AIProviderID: 1},
err: "aiModelId is required",
},
{
name: "empty config mode",
req: &model.PromptAddRequest{
Name: "Pre-translate prompt",
Action: "pre_translate",
AIProviderID: 1,
AIModelID: "gpt-3.5-turbo-instruct",
Config: model.PromptConfig{},
},
err: "config.mode is required",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.EqualError(t, tt.req.Validate(), tt.err)
})
}
}

func TestAIService_EditPrompt(t *testing.T) {
client, mux, teardown := setupClient()
defer teardown()
Expand Down Expand Up @@ -577,36 +526,6 @@ func TestAIService_AddProvider(t *testing.T) {
assert.Equal(t, expected, provider)
}

func TestAIService_AddProvider_requestValidation(t *testing.T) {
tests := []struct {
name string
req *model.ProviderAddRequest
err string
}{
{
name: "nil request",
req: nil,
err: "request cannot be nil",
},
{
name: "empty name",
req: &model.ProviderAddRequest{},
err: "name is required",
},
{
name: "empty type",
req: &model.ProviderAddRequest{Name: "OpenAI"},
err: "type is required",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.EqualError(t, tt.req.Validate(), tt.err)
})
}
}

func TestAIService_EditProvider(t *testing.T) {
client, mux, teardown := setupClient()
defer teardown()
Expand Down Expand Up @@ -749,23 +668,3 @@ func TestAIService_CreateProxyChatCompletion(t *testing.T) {
expected := &model.ProxyChatCompletion{}
assert.Equal(t, expected, completion)
}

func TestAIService_CreateProxyChatCompletion_requestValidation(t *testing.T) {
tests := []struct {
name string
req *model.CreateProxyChatCompletionRequest
err string
}{
{
name: "nil request",
req: nil,
err: "request cannot be nil",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.EqualError(t, tt.req.Validate(), tt.err)
})
}
}
25 changes: 0 additions & 25 deletions crowdin/applications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,31 +278,6 @@ func TestApplicationsService_Install(t *testing.T) {
}
}

func TestApplicationsService_Install_validationErrors(t *testing.T) {
tests := []struct {
name string
req *model.InstallApplicationRequest
err string
}{
{
name: "nil request",
req: nil,
err: "request cannot be nil",
},
{
name: "empty request",
req: &model.InstallApplicationRequest{},
err: "url is required",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.EqualError(t, tt.req.Validate(), tt.err)
})
}
}

func TestApplicationsService_EditInstallation(t *testing.T) {
client, mux, teardown := setupClient()
defer teardown()
Expand Down
78 changes: 0 additions & 78 deletions crowdin/branches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,31 +281,6 @@ func TestBranchesService_AddWithRequiredBodyParams(t *testing.T) {
}
}

func TestBranchesService_AddWithValidationErrors(t *testing.T) {
client, _, teardown := setupClient()
defer teardown()

requests := []struct {
req *model.BranchesAddRequest
expected string
}{
{nil, "request cannot be nil"},
{&model.BranchesAddRequest{}, "name is required"},
{&model.BranchesAddRequest{Title: "Master branch"}, "name is required"},
}

for _, r := range requests {
_, _, err := client.Branches.Add(context.Background(), 2, r.req)
if err == nil {
t.Error("Branches.Add expected an error, got nil")
}

if err.Error() != r.expected {
t.Errorf("Branches.Add returned %q, want %q", err.Error(), r.expected)
}
}
}

func TestBranchesService_Edit(t *testing.T) {
client, mux, teardown := setupClient()
defer teardown()
Expand Down Expand Up @@ -459,31 +434,6 @@ func TestBranchesService_MergeWithRequiredBodyParams(t *testing.T) {
}
}

func TestBranchesService_MergeWithValidationErrors(t *testing.T) {
client, _, teardown := setupClient()
defer teardown()

requests := []struct {
req *model.BranchesMergeRequest
expected string
}{
{nil, "request cannot be nil"},
{&model.BranchesMergeRequest{}, "sourceBranchId is required"},
{&model.BranchesMergeRequest{DeleteAfterMerge: ToPtr(true)}, "sourceBranchId is required"},
}

for _, tt := range requests {
_, _, err := client.Branches.Merge(context.Background(), 1, 2, tt.req)
if err == nil {
t.Error("Branches.Merge expected an error, got nil")
}

if err.Error() != tt.expected {
t.Errorf("Branches.Merge returned %q, want %q", err.Error(), tt.expected)
}
}
}

func TestBranchesService_CheckMergeStatus(t *testing.T) {
client, mux, teardown := setupClient()
defer teardown()
Expand Down Expand Up @@ -655,34 +605,6 @@ func TestBranchesService_CloneWithRequiredBodyParams(t *testing.T) {
}
}

func TestBranchesService_CloneWithValidationErrors(t *testing.T) {
client, _, teardown := setupClient()
defer teardown()

requests := []struct {
name string
req *model.BranchesCloneRequest
expected string
}{
{"nil request", nil, "request cannot be nil"},
{"empry request", &model.BranchesCloneRequest{}, "name is required"},
{"empty name", &model.BranchesCloneRequest{Title: "Master branch"}, "name is required"},
}

for _, tt := range requests {
t.Run(tt.name, func(t *testing.T) {
_, _, err := client.Branches.Clone(context.Background(), 2, 34, tt.req)
if err == nil {
t.Errorf("Branches.Clone expected an error, got nil")
}

if err.Error() != tt.expected {
t.Errorf("Branches.Clone returned %q, want %q", err.Error(), tt.expected)
}
})
}
}

func TestBranchesService_GetClone(t *testing.T) {
client, mux, teardown := setupClient()
defer teardown()
Expand Down
71 changes: 39 additions & 32 deletions crowdin/bundles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,19 @@ func TestBundlesService_List(t *testing.T) {
assert.Equal(t, 25, resp.Pagination.Limit)
}

func TestBundlesService_List_invalidJSON(t *testing.T) {
client, mux, teardown := setupClient()
defer teardown()

mux.HandleFunc("/api/v2/projects/2/bundles", func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprint(w, `invalid json`)
})

res, _, err := client.Bundles.List(context.Background(), 2, nil)
require.Error(t, err)
assert.Nil(t, res)
}

func TestBundlesService_Add(t *testing.T) {
client, mux, teardown := setupClient()
defer teardown()
Expand Down Expand Up @@ -224,38 +237,6 @@ func TestBundlesService_Add(t *testing.T) {
assert.Equal(t, expected, bundle)
}

func TestBundlesService_Add_WithValidationError(t *testing.T) {
tests := []struct {
req *model.BundleAddRequest
expectedErr string
}{
{
req: nil,
expectedErr: "request cannot be nil",
},
{
req: &model.BundleAddRequest{},
expectedErr: "name is required",
},
{
req: &model.BundleAddRequest{Name: "Resx bundle"},
expectedErr: "format is required",
},
{
req: &model.BundleAddRequest{Name: "Resx bundle", Format: "crowdin-resx"},
expectedErr: "sourcePatterns is required",
},
{
req: &model.BundleAddRequest{Name: "Resx bundle", Format: "crowdin-resx", SourcePatterns: []string{"/master"}},
expectedErr: "exportPattern is required",
},
}

for _, tt := range tests {
assert.EqualError(t, tt.req.Validate(), tt.expectedErr)
}
}

func TestBundlesService_Edit(t *testing.T) {
client, mux, teardown := setupClient()
defer teardown()
Expand Down Expand Up @@ -495,6 +476,19 @@ func TestBundlesService_ListFiles(t *testing.T) {
assert.Equal(t, 3, resp.Pagination.Limit)
}

func TestBundlesService_ListFiles_invalidJSON(t *testing.T) {
client, mux, teardown := setupClient()
defer teardown()

mux.HandleFunc("/api/v2/projects/2/bundles/3/files", func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprint(w, `invalid json`)
})

res, _, err := client.Bundles.ListFiles(context.Background(), 2, 3, nil)
require.Error(t, err)
assert.Nil(t, res)
}

func TestBundlesService_ListBranches(t *testing.T) {
client, mux, teardown := setupClient()
defer teardown()
Expand Down Expand Up @@ -561,3 +555,16 @@ func TestBundlesService_ListBranches(t *testing.T) {
assert.Equal(t, 10, resp.Pagination.Offset)
assert.Equal(t, 3, resp.Pagination.Limit)
}

func TestBundlesService_ListBranches_invalidJSON(t *testing.T) {
client, mux, teardown := setupClient()
defer teardown()

mux.HandleFunc("/api/v2/projects/2/bundles/3/branches", func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprint(w, `invalid json`)
})

res, _, err := client.Bundles.ListBranches(context.Background(), 2, 3, nil)
require.Error(t, err)
assert.Nil(t, res)
}
13 changes: 13 additions & 0 deletions crowdin/dictionaries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ func TestDictionariesService_List(t *testing.T) {
}
}

func TestDictionariesService_List_invalidJSON(t *testing.T) {
client, mux, teardown := setupClient()
defer teardown()

mux.HandleFunc("/api/v2/projects/2/dictionaries", func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprint(w, `invalid json`)
})

res, _, err := client.Dictionaries.List(context.Background(), 2, nil)
require.Error(t, err)
assert.Nil(t, res)
}

func TestDictionariesService_Edit(t *testing.T) {
client, mux, teardown := setupClient()
defer teardown()
Expand Down
Loading

0 comments on commit 5257703

Please sign in to comment.