Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

genai: test invalid text file encoding #224

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading