forked from neosapience/cast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoices_test.go
More file actions
88 lines (75 loc) · 2.28 KB
/
Copy pathvoices_test.go
File metadata and controls
88 lines (75 loc) · 2.28 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
package client
import (
"encoding/json"
"net/http"
"testing"
)
func TestListVoices_ReturnsVoices(t *testing.T) {
want := []Voice{
{VoiceID: "v1", VoiceName: "Alice", Gender: "female", Age: "young_adult"},
{VoiceID: "v2", VoiceName: "Bob", Gender: "male", Age: "middle_age"},
}
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v2/voices" {
t.Errorf("unexpected path: %s", r.URL.Path)
}
json.NewEncoder(w).Encode(want)
})
voices, err := c.ListVoices(ListVoicesParams{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(voices) != 2 {
t.Fatalf("expected 2 voices, got %d", len(voices))
}
if voices[0].VoiceID != "v1" || voices[1].VoiceID != "v2" {
t.Errorf("unexpected voices: %+v", voices)
}
}
func TestListVoices_SendsQueryParams(t *testing.T) {
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
if q.Get("gender") != "female" {
t.Errorf("expected gender=female, got %q", q.Get("gender"))
}
if q.Get("age") != "young_adult" {
t.Errorf("expected age=young_adult, got %q", q.Get("age"))
}
json.NewEncoder(w).Encode([]Voice{})
})
c.ListVoices(ListVoicesParams{Gender: "female", Age: "young_adult"})
}
func TestListVoices_ReturnsErrorOnAPIFailure(t *testing.T) {
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})
_, err := c.ListVoices(ListVoicesParams{})
if err == nil {
t.Fatal("expected error, got nil")
}
}
func TestGetVoice_ReturnsVoice(t *testing.T) {
want := Voice{VoiceID: "v1", VoiceName: "Alice", Gender: "female"}
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v2/voices/v1" {
t.Errorf("unexpected path: %s", r.URL.Path)
}
json.NewEncoder(w).Encode(want)
})
voice, err := c.GetVoice("v1")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if voice.VoiceID != "v1" || voice.VoiceName != "Alice" {
t.Errorf("unexpected voice: %+v", voice)
}
}
func TestGetVoice_ReturnsErrorOnAPIFailure(t *testing.T) {
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
_, err := c.GetVoice("nonexistent")
if err == nil {
t.Fatal("expected error, got nil")
}
}