-
Notifications
You must be signed in to change notification settings - Fork 4
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
Increase test coverage, split up some files #23
Changes from all commits
b8adb08
ed2d8f6
c6edfca
bf5640a
705fb5a
9707d1c
8e891d7
6391a6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package azuremodels | ||
|
||
import "fmt" | ||
|
||
// ModelDetails includes detailed information about a model. | ||
type ModelDetails struct { | ||
Description string `json:"description"` | ||
Evaluation string `json:"evaluation"` | ||
License string `json:"license"` | ||
LicenseDescription string `json:"license_description"` | ||
Notes string `json:"notes"` | ||
Tags []string `json:"tags"` | ||
SupportedInputModalities []string `json:"supported_input_modalities"` | ||
SupportedOutputModalities []string `json:"supported_output_modalities"` | ||
SupportedLanguages []string `json:"supported_languages"` | ||
MaxOutputTokens int `json:"max_output_tokens"` | ||
MaxInputTokens int `json:"max_input_tokens"` | ||
RateLimitTier string `json:"rateLimitTier"` | ||
} | ||
|
||
// ContextLimits returns a summary of the context limits for the model. | ||
func (m *ModelDetails) ContextLimits() string { | ||
return fmt.Sprintf("up to %d input tokens and %d output tokens", m.MaxInputTokens, m.MaxOutputTokens) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package azuremodels | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestModelDetails(t *testing.T) { | ||
t.Run("ContextLimits", func(t *testing.T) { | ||
details := &ModelDetails{MaxInputTokens: 123, MaxOutputTokens: 456} | ||
result := details.ContextLimits() | ||
require.Equal(t, "up to 123 input tokens and 456 output tokens", result) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package azuremodels | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestModelSummary(t *testing.T) { | ||
t.Run("IsChatModel", func(t *testing.T) { | ||
embeddingModel := &ModelSummary{Task: "embeddings"} | ||
chatCompletionModel := &ModelSummary{Task: "chat-completion"} | ||
otherModel := &ModelSummary{Task: "something-else"} | ||
|
||
require.False(t, embeddingModel.IsChatModel()) | ||
require.True(t, chatCompletionModel.IsChatModel()) | ||
require.False(t, otherModel.IsChatModel()) | ||
}) | ||
|
||
t.Run("HasName", func(t *testing.T) { | ||
model := &ModelSummary{Name: "foo123", FriendlyName: "Foo 123"} | ||
|
||
require.True(t, model.HasName(model.Name)) | ||
require.True(t, model.HasName("FOO123")) | ||
require.True(t, model.HasName(model.FriendlyName)) | ||
require.True(t, model.HasName("fOo 123")) | ||
require.False(t, model.HasName("completely different value")) | ||
require.False(t, model.HasName("foo")) | ||
}) | ||
|
||
t.Run("SortModels sorts given slice in-place by friendly name, case-insensitive", func(t *testing.T) { | ||
modelA := &ModelSummary{Name: "z", FriendlyName: "AARDVARK"} | ||
modelB := &ModelSummary{Name: "y", FriendlyName: "betta"} | ||
modelC := &ModelSummary{Name: "x", FriendlyName: "Cat"} | ||
models := []*ModelSummary{modelB, modelA, modelC} | ||
|
||
SortModels(models) | ||
|
||
require.Equal(t, 3, len(models)) | ||
require.Equal(t, "AARDVARK", models[0].FriendlyName) | ||
require.Equal(t, "betta", models[1].FriendlyName) | ||
require.Equal(t, "Cat", models[2].FriendlyName) | ||
}) | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The type is |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I brought these tests over from https://github.com/Azure/azure-sdk-for-go/blob/4661007ca1fd68b2e31f3502d4282904014fd731/sdk/ai/azopenai/event_reader_test.go. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package sse | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type sampleContent struct { | ||
Name string `json:"name"` | ||
NestedData []*struct { | ||
Count int `json:"count"` | ||
Value string `json:"value"` | ||
} `json:"nested_data"` | ||
} | ||
|
||
type badReader struct{} | ||
|
||
func (br badReader) Read(p []byte) (n int, err error) { | ||
return 0, io.ErrClosedPipe | ||
} | ||
|
||
func TestEventReader(t *testing.T) { | ||
t.Run("invalid type", func(t *testing.T) { | ||
data := []string{ | ||
"invaliddata: {\"name\":\"chatcmpl-7Z4kUpXX6HN85cWY28IXM4EwemLU3\",\"object\":\"chat.completion.chunk\",\"created\":1688594090,\"model\":\"gpt-4-0613\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\"},\"finish_reason\":null}]}\n\n", | ||
} | ||
|
||
text := strings.NewReader(strings.Join(data, "\n")) | ||
eventReader := NewEventReader[sampleContent](io.NopCloser(text)) | ||
|
||
firstEvent, err := eventReader.Read() | ||
require.Empty(t, firstEvent) | ||
require.EqualError(t, err, "unexpected event type: invaliddata") | ||
}) | ||
|
||
t.Run("bad reader", func(t *testing.T) { | ||
eventReader := NewEventReader[sampleContent](io.NopCloser(badReader{})) | ||
defer eventReader.Close() | ||
|
||
firstEvent, err := eventReader.Read() | ||
require.Empty(t, firstEvent) | ||
require.ErrorIs(t, io.ErrClosedPipe, err) | ||
}) | ||
|
||
t.Run("stream is closed before done", func(t *testing.T) { | ||
buf := strings.NewReader("data: {}") | ||
|
||
eventReader := NewEventReader[sampleContent](io.NopCloser(buf)) | ||
|
||
evt, err := eventReader.Read() | ||
require.Empty(t, evt) | ||
require.NoError(t, err) | ||
|
||
evt, err = eventReader.Read() | ||
require.Empty(t, evt) | ||
require.EqualError(t, err, "incomplete stream") | ||
}) | ||
|
||
t.Run("spaces around areas", func(t *testing.T) { | ||
buf := strings.NewReader( | ||
// spaces between data | ||
"data: {\"name\":\"chatcmpl-7Z4kUpXX6HN85cWY28IXM4EwemLU3\",\"nested_data\":[{\"count\":0,\"value\":\"with-spaces\"}]}\n" + | ||
// no spaces | ||
"data:{\"name\":\"chatcmpl-7Z4kUpXX6HN85cWY28IXM4EwemLU3\",\"nested_data\":[{\"count\":0,\"value\":\"without-spaces\"}]}\n", | ||
) | ||
|
||
eventReader := NewEventReader[sampleContent](io.NopCloser(buf)) | ||
|
||
evt, err := eventReader.Read() | ||
require.NoError(t, err) | ||
require.Equal(t, "with-spaces", evt.NestedData[0].Value) | ||
|
||
evt, err = eventReader.Read() | ||
require.NoError(t, err) | ||
require.NotEmpty(t, evt) | ||
require.Equal(t, "without-spaces", evt.NestedData[0].Value) | ||
}) | ||
} |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This package was able to go away because its two functions made more sense elsewhere, either as a method on a type rather than a standalone function (
IsChatModel
) or because the function dealt with types defined in another package (SortModels
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I agree that's a sensible move.