Skip to content

Commit

Permalink
chore: table driven tests to include more model cases for moderations…
Browse files Browse the repository at this point in the history
… endpoint
  • Loading branch information
Munar committed Jul 13, 2023
1 parent e579559 commit 1e82071
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
3 changes: 2 additions & 1 deletion moderation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
const (
ModerationTextStable = "text-moderation-stable"
ModerationTextLatest = "text-moderation-latest"
ModerationText001 = "text-moderation-001"
// Deprecated: use ModerationTextStable and ModerationTextLatest instead.
ModerationText001 = "text-moderation-001"
)

var (
Expand Down
37 changes: 30 additions & 7 deletions moderation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,39 @@ func TestModerations(t *testing.T) {
checks.NoError(t, err, "Moderation error")
}

// TestModerationsWithIncorrectModel Tests passing an incorrect model to Moderations request.
func TestModerationsWithIncorrectModel(t *testing.T) {
// TestModerationsWithIncorrectModel Tests passing valid and invalid models to moderations endpoint.
func TestModerationsWithDifferentModelOptions(t *testing.T) {
var modelOptions []struct {
model string
expect error
}
modelOptions = append(modelOptions,
getModerationModelTestOption(GPT3Dot5Turbo, ErrModerationInvalidModel),
getModerationModelTestOption(ModerationTextStable, nil),
getModerationModelTestOption(ModerationTextLatest, nil),
getModerationModelTestOption("", nil),
)
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/moderations", handleModerationEndpoint)
_, err := client.Moderations(context.Background(), ModerationRequest{
Model: GPT3Dot5Turbo,
Input: "I want to kill them.",
})
checks.ErrorIs(t, err, ErrModerationInvalidModel)
for _, modelTest := range modelOptions {
_, err := client.Moderations(context.Background(), ModerationRequest{
Model: modelTest.model,
Input: "I want to kill them.",
})
checks.ErrorIs(t, err, modelTest.expect,
fmt.Sprintf("Moderations(..) expects err: %v, actual err:%v", modelTest.expect, err))
}
}

func getModerationModelTestOption(model string, expect error) struct {
model string
expect error
} {
return struct {
model string
expect error
}{model: model, expect: expect}
}

// handleModerationEndpoint Handles the moderation endpoint by the test server.
Expand Down

0 comments on commit 1e82071

Please sign in to comment.