-
Notifications
You must be signed in to change notification settings - Fork 1
/
documenttype_test.go
92 lines (78 loc) · 2.26 KB
/
documenttype_test.go
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
package main
import (
"context"
"errors"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hansmi/paperhooks/pkg/client"
"github.com/hansmi/prometheus-paperless-exporter/internal/testutil"
)
type fakeDocumentTypeClient struct {
items []client.DocumentType
err error
}
func (c *fakeDocumentTypeClient) ListAllDocumentTypes(ctx context.Context, opts client.ListDocumentTypesOptions, handler func(context.Context, client.DocumentType) error) error {
for _, i := range c.items {
if err := handler(ctx, i); err != nil {
return err
}
}
return c.err
}
func TestDocumentType(t *testing.T) {
errTest := errors.New("test error")
for _, tc := range []struct {
name string
cl fakeDocumentTypeClient
wantErr error
}{
{
name: "empty",
},
{
name: "listing fails",
cl: fakeDocumentTypeClient{
err: errTest,
},
wantErr: errTest,
},
{
name: "documentTypes",
cl: fakeDocumentTypeClient{
items: []client.DocumentType{
{ID: 23758},
{ID: 22848},
{ID: 10504},
},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
c := newDocumentTypeCollector(&tc.cl)
err := c.collect(context.Background(), testutil.DiscardMetrics(t))
if diff := cmp.Diff(tc.wantErr, err, cmpopts.EquateErrors()); diff != "" {
t.Errorf("Error diff (-want +got):\n%s", diff)
}
})
}
}
func TestDocumentTypeCollect(t *testing.T) {
cl := fakeDocumentTypeClient{}
c := newMultiCollector(newDocumentTypeCollector(&cl))
testutil.CollectAndCompare(t, c, ``)
cl.items = append(cl.items, []client.DocumentType{
{ID: 3760, Name: "Contract", Slug: "contract"},
{ID: 5558, Name: "Purchase order", Slug: "po", DocumentCount: 20},
}...)
testutil.CollectAndCompare(t, c, `
# HELP paperless_document_type_document_count Number of documents associated with a document type.
# TYPE paperless_document_type_document_count gauge
paperless_document_type_document_count{id="3760"} 0
paperless_document_type_document_count{id="5558"} 20
# HELP paperless_document_type_info Static information about a document type.
# TYPE paperless_document_type_info gauge
paperless_document_type_info{id="3760",name="Contract",slug="contract"} 1
paperless_document_type_info{id="5558",name="Purchase order",slug="po"} 1
`)
}