Skip to content

Commit

Permalink
genai: test invalid text file encoding (#224)
Browse files Browse the repository at this point in the history
For #221
  • Loading branch information
eliben authored Nov 18, 2024
1 parent 6d58228 commit ae7597a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions genai/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@ func TestLive(t *testing.T) {

// Use the uploaded file to generate content.
model := client.GenerativeModel("gemini-1.5-pro-latest")
resp, err := model.GenerateContent(ctx, FileData{URI: file.URI})
resp, err := model.GenerateContent(ctx,
Text("describe this image"), FileData{URI: file.URI})
if err != nil {
t.Fatal(err)
}
Expand All @@ -476,7 +477,8 @@ func TestLive(t *testing.T) {
if got, want := file.MIMEType, "audio/mpeg"; got != want {
t.Errorf("got MIME type %q, want %q", got, want)
}
resp, err = model.GenerateContent(ctx, FileData{URI: file.URI})
resp, err = model.GenerateContent(ctx,
Text("describe this"), FileData{URI: file.URI})
if err != nil {
t.Fatal(err)
}
Expand All @@ -502,6 +504,31 @@ func TestLive(t *testing.T) {
// TODO(jba): verify metadata when it is populated.
t.Logf("Metadata: %+v\n", file.Metadata)
})

t.Run("txt-encoding-valid", func(t *testing.T) {
// Upload a file that has valid UTF-8 encoding (ASCII).
poem := uploadFile(t, ctx, client, filepath.Join("testdata", "poem.txt"))
model := client.GenerativeModel("gemini-1.5-flash")
_, err := model.GenerateContent(ctx,
Text("summarize this"),
FileData{URI: poem.URI})
if err != nil {
t.Error(err)
}
})

t.Run("txt-encoding-invalid", func(t *testing.T) {
// Try uploading a "text" file with garbled encoding (this is just random
// data).
f := uploadFile(t, ctx, client, filepath.Join("testdata", "badencoding.txt"))
model := client.GenerativeModel("gemini-1.5-flash")
_, err := model.GenerateContent(ctx,
Text("summarize this"),
FileData{URI: f.URI})
if err == nil {
t.Errorf("want encoding error")
}
})
})

t.Run("JSON", func(t *testing.T) {
Expand Down
Binary file added genai/testdata/badencoding.txt
Binary file not shown.

0 comments on commit ae7597a

Please sign in to comment.