-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplates_test.go
More file actions
94 lines (83 loc) · 2.32 KB
/
templates_test.go
File metadata and controls
94 lines (83 loc) · 2.32 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
package cloudlayer
import (
"context"
"errors"
"fmt"
"net/http"
"testing"
)
func TestListTemplates_NoOptions(t *testing.T) {
c := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v2/templates" {
t.Errorf("path = %q, want /v2/templates", r.URL.Path)
}
w.Header().Set("Content-Type", "application/json")
_, _ = fmt.Fprint(w, `[{"id":"t1","name":"Invoice"}]`)
})
templates, err := c.ListTemplates(context.Background(), nil)
if err != nil {
t.Fatal(err)
}
if len(templates) != 1 {
t.Fatalf("len = %d", len(templates))
}
if templates[0].ID != "t1" {
t.Errorf("ID = %q", templates[0].ID)
}
}
func TestListTemplates_WithFilters(t *testing.T) {
c := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("category") != "business" {
t.Errorf("category = %q", r.URL.Query().Get("category"))
}
if r.URL.Query().Get("type") != "pdf" {
t.Errorf("type = %q", r.URL.Query().Get("type"))
}
w.Header().Set("Content-Type", "application/json")
_, _ = fmt.Fprint(w, `[]`)
})
_, err := c.ListTemplates(context.Background(), &ListTemplatesOptions{
Category: stringPtr("business"),
Type: stringPtr("pdf"),
})
if err != nil {
t.Fatal(err)
}
}
func TestListTemplates_Empty(t *testing.T) {
c := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = fmt.Fprint(w, `[]`)
})
templates, err := c.ListTemplates(context.Background(), nil)
if err != nil {
t.Fatal(err)
}
if templates == nil {
t.Error("should return empty slice, not nil")
}
}
func TestGetTemplate_Valid(t *testing.T) {
c := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v2/template/t1" {
t.Errorf("path = %q", r.URL.Path)
}
w.Header().Set("Content-Type", "application/json")
_, _ = fmt.Fprint(w, `{"id":"t1","name":"Invoice","type":"pdf"}`)
})
tmpl, err := c.GetTemplate(context.Background(), "t1")
if err != nil {
t.Fatal(err)
}
if tmpl.ID != "t1" {
t.Errorf("ID = %q", tmpl.ID)
}
}
func TestGetTemplate_EmptyID(t *testing.T) {
c := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {})
_, err := c.GetTemplate(context.Background(), "")
var ve *ValidationError
if !errors.As(err, &ve) {
t.Fatalf("expected *ValidationError, got %T", err)
}
}