Skip to content

Commit 1875a62

Browse files
committed
feat: test cases for SLA calculator, string utilities and attachments
1 parent f60c4e8 commit 1875a62

File tree

4 files changed

+775
-1
lines changed

4 files changed

+775
-1
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ require (
2828
github.com/redis/go-redis/v9 v9.5.4
2929
github.com/rhnvrm/simples3 v0.8.4
3030
github.com/spf13/pflag v1.0.5
31+
github.com/stretchr/testify v1.9.0
3132
github.com/valyala/fasthttp v1.54.0
3233
github.com/volatiletech/null/v9 v9.0.0
3334
github.com/zerodha/fastglue v1.8.0
@@ -68,7 +69,6 @@ require (
6869
github.com/rivo/uniseg v0.4.4 // indirect
6970
github.com/savsgio/gotils v0.0.0-20240303185622-093b76447511 // indirect
7071
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
71-
github.com/stretchr/testify v1.9.0 // indirect
7272
github.com/valyala/bytebufferpool v1.0.0 // indirect
7373
golang.org/x/image v0.18.0 // indirect
7474
golang.org/x/net v0.33.0 // indirect
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package attachment
2+
3+
import (
4+
"net/textproto"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
func TestMakeHeader(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
contentType string
13+
contentID string
14+
fileName string
15+
encoding string
16+
disposition string
17+
want textproto.MIMEHeader
18+
}{
19+
{
20+
name: "inline attachment",
21+
contentType: "image/jpeg",
22+
contentID: "123",
23+
fileName: "test.jpg",
24+
encoding: "base64",
25+
disposition: "inline",
26+
want: textproto.MIMEHeader{
27+
"Content-Disposition": []string{"inline"},
28+
"Content-Id": []string{"<123>"},
29+
"Content-Type": []string{"image/jpeg; name=\"test.jpg\""},
30+
"Content-Transfer-Encoding": []string{"base64"},
31+
},
32+
},
33+
{
34+
name: "regular attachment",
35+
contentType: "application/pdf",
36+
contentID: "",
37+
fileName: "doc.pdf",
38+
encoding: "base64",
39+
disposition: "attachment",
40+
want: textproto.MIMEHeader{
41+
"Content-Disposition": []string{"attachment; filename=\"doc.pdf\""},
42+
"Content-Type": []string{"application/pdf; name=\"doc.pdf\""},
43+
"Content-Transfer-Encoding": []string{"base64"},
44+
},
45+
},
46+
{
47+
name: "default values",
48+
contentType: "",
49+
contentID: "",
50+
fileName: "file.txt",
51+
encoding: "",
52+
disposition: "",
53+
want: textproto.MIMEHeader{
54+
"Content-Disposition": []string{"attachment; filename=\"file.txt\""},
55+
"Content-Type": []string{"application/octet-stream; name=\"file.txt\""},
56+
"Content-Transfer-Encoding": []string{"base64"},
57+
},
58+
},
59+
}
60+
61+
for _, tt := range tests {
62+
t.Run(tt.name, func(t *testing.T) {
63+
got := MakeHeader(tt.contentType, tt.contentID, tt.fileName, tt.encoding, tt.disposition)
64+
if !reflect.DeepEqual(got, tt.want) {
65+
t.Errorf("MakeHeader() = %v, want %v", got, tt.want)
66+
}
67+
})
68+
}
69+
}
70+
71+
func TestAttachments_Scan(t *testing.T) {
72+
tests := []struct {
73+
name string
74+
input interface{}
75+
want Attachments
76+
wantErr bool
77+
}{
78+
{
79+
name: "nil input",
80+
input: nil,
81+
want: Attachments{},
82+
wantErr: false,
83+
},
84+
{
85+
name: "valid json",
86+
input: []byte(`[{"name":"test.jpg","size":1024,"content_type":"image/jpeg","disposition":"attachment"}]`),
87+
want: Attachments{
88+
{
89+
Name: "test.jpg",
90+
Size: 1024,
91+
ContentType: "image/jpeg",
92+
Disposition: "attachment",
93+
},
94+
},
95+
wantErr: false,
96+
},
97+
{
98+
name: "invalid type",
99+
input: "not bytes",
100+
want: nil,
101+
wantErr: true,
102+
},
103+
}
104+
105+
for _, tt := range tests {
106+
t.Run(tt.name, func(t *testing.T) {
107+
var a Attachments
108+
err := a.Scan(tt.input)
109+
if (err != nil) != tt.wantErr {
110+
t.Errorf("Attachments.Scan() error = %v, wantErr %v", err, tt.wantErr)
111+
return
112+
}
113+
if !tt.wantErr && !reflect.DeepEqual(a, tt.want) {
114+
t.Errorf("Attachments.Scan() = %v, want %v", a, tt.want)
115+
}
116+
})
117+
}
118+
}

0 commit comments

Comments
 (0)