-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexperiment_test.go
More file actions
194 lines (173 loc) · 5.48 KB
/
experiment_test.go
File metadata and controls
194 lines (173 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package opik
import (
"testing"
)
func TestExperimentGetters(t *testing.T) {
e := &Experiment{
id: "exp-123",
name: "test-experiment",
datasetName: "my-dataset",
metadata: map[string]any{"key": "value"},
}
if e.ID() != "exp-123" {
t.Errorf("ID() = %q, want %q", e.ID(), "exp-123")
}
if e.Name() != "test-experiment" {
t.Errorf("Name() = %q, want %q", e.Name(), "test-experiment")
}
if e.DatasetName() != "my-dataset" {
t.Errorf("DatasetName() = %q, want %q", e.DatasetName(), "my-dataset")
}
if e.Metadata()["key"] != "value" {
t.Errorf("Metadata()[key] = %v, want %q", e.Metadata()["key"], "value")
}
}
func TestExperimentItemFields(t *testing.T) {
item := ExperimentItem{
ID: "item-123",
ExperimentID: "exp-456",
DatasetItemID: "ds-item-789",
TraceID: "trace-abc",
Input: map[string]any{"prompt": "hello"},
Output: map[string]any{"response": "world"},
}
if item.ID != "item-123" {
t.Errorf("ID = %q, want %q", item.ID, "item-123")
}
if item.ExperimentID != "exp-456" {
t.Errorf("ExperimentID = %q, want %q", item.ExperimentID, "exp-456")
}
if item.DatasetItemID != "ds-item-789" {
t.Errorf("DatasetItemID = %q, want %q", item.DatasetItemID, "ds-item-789")
}
if item.TraceID != "trace-abc" {
t.Errorf("TraceID = %q, want %q", item.TraceID, "trace-abc")
}
}
func TestExperimentStatusConstants(t *testing.T) {
tests := []struct {
status ExperimentStatus
want string
}{
{ExperimentStatusRunning, "running"},
{ExperimentStatusCompleted, "completed"},
{ExperimentStatusCancelled, "cancelled"},
}
for _, tt := range tests {
if string(tt.status) != tt.want {
t.Errorf("ExperimentStatus = %q, want %q", tt.status, tt.want)
}
}
}
func TestExperimentTypeConstants(t *testing.T) {
tests := []struct {
expType ExperimentType
want string
}{
{ExperimentTypeRegular, "regular"},
{ExperimentTypeTrial, "trial"},
{ExperimentTypeMiniBatch, "mini-batch"},
}
for _, tt := range tests {
if string(tt.expType) != tt.want {
t.Errorf("ExperimentType = %q, want %q", tt.expType, tt.want)
}
}
}
func TestExperimentOptions(t *testing.T) {
t.Run("WithExperimentName", func(t *testing.T) {
opts := &experimentOptions{}
WithExperimentName("my-experiment")(opts)
if opts.name != "my-experiment" {
t.Errorf("name = %q, want %q", opts.name, "my-experiment")
}
})
t.Run("WithExperimentMetadata", func(t *testing.T) {
opts := &experimentOptions{}
metadata := map[string]any{"model": "gpt-4", "version": 2}
WithExperimentMetadata(metadata)(opts)
if opts.metadata["model"] != "gpt-4" {
t.Errorf("metadata[model] = %v, want %q", opts.metadata["model"], "gpt-4")
}
if opts.metadata["version"] != 2 {
t.Errorf("metadata[version] = %v, want 2", opts.metadata["version"])
}
})
t.Run("WithExperimentType", func(t *testing.T) {
opts := &experimentOptions{}
WithExperimentType(ExperimentTypeTrial)(opts)
if opts.experimentType != ExperimentTypeTrial {
t.Errorf("experimentType = %q, want %q", opts.experimentType, ExperimentTypeTrial)
}
})
t.Run("WithExperimentStatus", func(t *testing.T) {
opts := &experimentOptions{}
WithExperimentStatus(ExperimentStatusCompleted)(opts)
if opts.status != ExperimentStatusCompleted {
t.Errorf("status = %q, want %q", opts.status, ExperimentStatusCompleted)
}
})
}
func TestExperimentItemOptions(t *testing.T) {
t.Run("WithExperimentItemInput", func(t *testing.T) {
opts := &experimentItemOptions{}
input := map[string]any{"prompt": "test prompt"}
WithExperimentItemInput(input)(opts)
if inputMap, ok := opts.input.(map[string]any); !ok {
t.Error("input should be map[string]any")
} else if inputMap["prompt"] != "test prompt" {
t.Errorf("input[prompt] = %v, want %q", inputMap["prompt"], "test prompt")
}
})
t.Run("WithExperimentItemOutput", func(t *testing.T) {
opts := &experimentItemOptions{}
output := map[string]any{"response": "test response"}
WithExperimentItemOutput(output)(opts)
if outputMap, ok := opts.output.(map[string]any); !ok {
t.Error("output should be map[string]any")
} else if outputMap["response"] != "test response" {
t.Errorf("output[response] = %v, want %q", outputMap["response"], "test response")
}
})
}
func TestExperimentEmptyState(t *testing.T) {
e := &Experiment{}
if e.ID() != "" {
t.Errorf("ID() = %q, want empty", e.ID())
}
if e.Name() != "" {
t.Errorf("Name() = %q, want empty", e.Name())
}
if e.DatasetName() != "" {
t.Errorf("DatasetName() = %q, want empty", e.DatasetName())
}
if e.Metadata() != nil {
t.Errorf("Metadata() = %v, want nil", e.Metadata())
}
}
func TestExperimentOptionsChaining(t *testing.T) {
// Test that multiple options can be applied together
opts := &experimentOptions{}
// Apply multiple options
options := []ExperimentOption{
WithExperimentName("chained-experiment"),
WithExperimentMetadata(map[string]any{"chain": true}),
WithExperimentType(ExperimentTypeMiniBatch),
WithExperimentStatus(ExperimentStatusRunning),
}
for _, opt := range options {
opt(opts)
}
if opts.name != "chained-experiment" {
t.Errorf("name = %q, want %q", opts.name, "chained-experiment")
}
if opts.metadata["chain"] != true {
t.Errorf("metadata[chain] = %v, want true", opts.metadata["chain"])
}
if opts.experimentType != ExperimentTypeMiniBatch {
t.Errorf("experimentType = %q, want %q", opts.experimentType, ExperimentTypeMiniBatch)
}
if opts.status != ExperimentStatusRunning {
t.Errorf("status = %q, want %q", opts.status, ExperimentStatusRunning)
}
}