Skip to content

Commit 0442191

Browse files
committed
Test coverage
1 parent 4d76ae1 commit 0442191

File tree

5 files changed

+279
-8
lines changed

5 files changed

+279
-8
lines changed

examples/main.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ func main() {
1212
pix.OptMerchantName("Thiago Zilli Sarmento"),
1313
pix.OptMerchantCity("Ararangua"),
1414
pix.OptKind(pix.STATIC),
15+
pix.OptAmount("100.00"),
16+
pix.OptDescription("Test Description"),
17+
pix.OptUrl("https://example.com"),
1518
}
1619

1720
p, err := pix.New(opts...)
@@ -29,11 +32,11 @@ func main() {
2932

3033
fmt.Printf("Copy and Paste: %s\n", cpy)
3134

32-
// bts, err := p.GenQRCode()
33-
// if err != nil {
34-
// fmt.Println(err.Error())
35-
// return
36-
// }
35+
bts, err := p.GenQRCode()
36+
if err != nil {
37+
fmt.Println(err.Error())
38+
return
39+
}
3740

38-
// fmt.Printf("QRCode: %b\n", bts)
41+
fmt.Printf("QRCode: %b\n", bts)
3942
}

pix/options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ type OptionsParams struct {
1919
description string
2020
amount string
2121
aditionalInfo string
22-
merchant merchant
22+
merchant Merchant
2323
kind PixKind
2424
url string
2525
qrcodeContent string
2626
qrcodeSize int
2727
}
2828

29-
type merchant struct {
29+
type Merchant struct {
3030
name string
3131
city string
3232
}

pix/options_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package pix
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestPixKindString(t *testing.T) {
8+
tests := []struct {
9+
input PixKind
10+
want string
11+
}{
12+
{STATIC, "STATIC"},
13+
{DYNAMIC, "DYNAMIC"},
14+
}
15+
16+
for _, test := range tests {
17+
got := test.input.String()
18+
if got != test.want {
19+
t.Errorf("PixKind.String() = %v; want %v", got, test.want)
20+
}
21+
}
22+
}
23+
24+
func TestOptions(t *testing.T) {
25+
params := &OptionsParams{}
26+
tests := []struct {
27+
opt Options
28+
get func() interface{}
29+
want interface{}
30+
}{
31+
{OptQRCodeSize(256), func() interface{} { return params.GetQRCodeSize() }, 256},
32+
{OptUrl("https://example.com"), func() interface{} { return params.GetUrl() }, "https://example.com"},
33+
{OptAditionalInfo("Additional Info"), func() interface{} { return params.GetAditionalInfo() }, "Additional Info"},
34+
{OptKind(DYNAMIC), func() interface{} { return params.GetKind() }, DYNAMIC},
35+
{OptTxId("Transaction123"), func() interface{} { return params.GetTxId() }, "Transaction123"},
36+
{OptPixKey("123456"), func() interface{} { return params.GetPixKey() }, "123456"},
37+
{OptDescription("Test Description"), func() interface{} { return params.GetDescription() }, "Test Description"},
38+
{OptMerchantName("Test Merchant"), func() interface{} { return params.GetMerchantName() }, "Test Merchant"},
39+
{OptMerchantCity("Test City"), func() interface{} { return params.GetMerchantCity() }, "Test City"},
40+
{OptAmount("100.00"), func() interface{} { return params.GetAmount() }, "100.00"},
41+
}
42+
43+
for _, test := range tests {
44+
if err := test.opt(params); err != nil {
45+
t.Fatalf("Option function returned error: %v", err)
46+
}
47+
got := test.get()
48+
if got != test.want {
49+
t.Errorf("Option function did not set value correctly: got %v; want %v", got, test.want)
50+
}
51+
}
52+
}
53+
54+
func TestSetQRCodeContent(t *testing.T) {
55+
params := &OptionsParams{}
56+
params.SetQRCodeContent("QR Code Content")
57+
58+
if params.GetQRCodeContent() != "QR Code Content" {
59+
t.Errorf("SetQRCodeContent did not set value correctly: got %v; want 'QR Code Content'", params.GetQRCodeContent())
60+
}
61+
}

pix/pix.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ func (p *Pix) Validates() error {
149149
}
150150

151151
func (p *Pix) GenQRCode() ([]byte, error) {
152+
if p.params.GetQRCodeContent() == "" {
153+
_ = p.GenPayload()
154+
}
152155
return qrcode.New(qrcode.QRCodeOptions{
153156
Size: p.params.GetQRCodeSize(),
154157
Content: p.params.GetQRCodeContent(),

pix/pix_test.go

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
package pix
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
func TestNew(t *testing.T) {
9+
10+
opts := []Options{
11+
OptQRCodeSize(256),
12+
OptUrl("https://example.com"),
13+
OptAditionalInfo("Aditional Info"),
14+
OptKind(STATIC),
15+
OptMerchantName("Test Merchant"),
16+
OptMerchantCity("Test City"),
17+
OptAmount("100.00"),
18+
OptDescription("Test Description"),
19+
OptPixKey("123456"),
20+
}
21+
22+
pix, err := New(opts...)
23+
if err != nil {
24+
t.Fatalf("failed to create Pix instance: %v", err)
25+
}
26+
27+
if pix.params.pixKey != "123456" {
28+
t.Errorf("expected pixKey to be '123456', got %v", pix.params.pixKey)
29+
}
30+
31+
if pix.params.merchant.name != "Test Merchant" {
32+
t.Errorf("expected merchant name to be 'Test Merchant', got %v", pix.params.merchant.name)
33+
}
34+
}
35+
36+
func TestGenPayload(t *testing.T) {
37+
opts := []Options{
38+
OptQRCodeSize(256),
39+
OptUrl("https://example.com"),
40+
OptAditionalInfo("Aditional Info"),
41+
OptKind(STATIC),
42+
OptMerchantName("Test Merchant"),
43+
OptMerchantCity("Test City"),
44+
OptAmount("100.00"),
45+
OptDescription("Test Description"),
46+
OptPixKey("11912345678"),
47+
}
48+
49+
pix, err := New(opts...)
50+
if err != nil {
51+
t.Fatalf("failed to create Pix instance: %v", err)
52+
}
53+
54+
payload := pix.GenPayload()
55+
if payload == "" {
56+
t.Errorf("expected payload to be non-empty")
57+
}
58+
59+
}
60+
61+
func TestValidates(t *testing.T) {
62+
tests := []struct {
63+
opts []Options
64+
hasError bool
65+
}{
66+
{
67+
opts: []Options{
68+
OptMerchantName("Test Merchant"),
69+
OptMerchantCity("Test City"),
70+
},
71+
hasError: true, // pixKey is missing
72+
},
73+
{
74+
opts: []Options{
75+
OptPixKey("123456"),
76+
OptMerchantCity("Test City"),
77+
},
78+
hasError: true, // merchant name is missing
79+
},
80+
{
81+
opts: []Options{
82+
OptPixKey("123456"),
83+
OptMerchantName("Test Merchant"),
84+
},
85+
hasError: true, // merchant city is missing
86+
},
87+
{
88+
opts: []Options{
89+
OptPixKey("123456"),
90+
OptMerchantName("Test Merchant"),
91+
OptMerchantCity("Test City"),
92+
},
93+
hasError: false, // all required fields are present
94+
},
95+
}
96+
97+
for _, test := range tests {
98+
pix, err := New(test.opts...)
99+
if err != nil {
100+
t.Fatalf("failed to create Pix instance: %v", err)
101+
}
102+
103+
err = pix.Validates()
104+
if (err != nil) != test.hasError {
105+
t.Errorf("expected error to be %v, got %v", test.hasError, err != nil)
106+
}
107+
}
108+
}
109+
110+
func TestGenQRCode(t *testing.T) {
111+
opts := []Options{
112+
OptQRCodeSize(256),
113+
OptUrl("https://example.com"),
114+
OptAditionalInfo("Aditional Info"),
115+
OptKind(STATIC),
116+
OptMerchantName("Test Merchant"),
117+
OptMerchantCity("Test City"),
118+
OptAmount("100.00"),
119+
OptDescription("Test Description"),
120+
OptPixKey("11912345678"),
121+
}
122+
123+
pix, err := New(opts...)
124+
if err != nil {
125+
t.Fatalf("failed to create Pix instance: %v", err)
126+
}
127+
128+
if err := pix.Validates(); err != nil {
129+
t.Fatalf("failed to validate Pix instance: %v", err)
130+
}
131+
132+
qrCode, err := pix.GenQRCode()
133+
if err != nil {
134+
t.Errorf("failed to generate QR code: %v", err)
135+
}
136+
137+
t.Logf("QR code: %v", qrCode)
138+
139+
if len(qrCode) == 0 {
140+
t.Errorf("expected QR code to be non-empty")
141+
}
142+
}
143+
144+
func TestGenerateMAI(t *testing.T) {
145+
opts := []Options{
146+
OptPixKey("11999821234"),
147+
OptMerchantName("Thiago Zilli Sarmento"),
148+
OptMerchantCity("Ararangua"),
149+
OptKind(STATIC),
150+
OptAmount("100.00"),
151+
OptDescription("Test Description"),
152+
OptAditionalInfo("https://example.com"),
153+
}
154+
155+
pix, err := New(opts...)
156+
if err != nil {
157+
t.Fatalf("failed to create Pix instance: %v", err)
158+
}
159+
160+
if err := pix.Validates(); err != nil {
161+
t.Fatalf("failed to validate Pix instance: %v", err)
162+
}
163+
164+
payload := pix.GenPayload()
165+
166+
t.Logf("payload: %v", payload)
167+
168+
if !strings.Contains(payload, "11999821234") || !strings.Contains(payload, "https://example.com") {
169+
t.Errorf("generateMAI function did not work as expected")
170+
}
171+
}
172+
173+
func TestGenerateAdditionalData(t *testing.T) {
174+
opts := []Options{
175+
OptPixKey("123456"),
176+
OptTxId("Transaction123"),
177+
}
178+
179+
pix, err := New(opts...)
180+
if err != nil {
181+
t.Fatalf("failed to create Pix instance: %v", err)
182+
}
183+
184+
payload := pix.GenPayload()
185+
if !strings.Contains(payload, "Transaction123") {
186+
t.Errorf("generateAdditionalData function did not work as expected")
187+
}
188+
}
189+
190+
func TestGetCRC16AndFindAndReplaceCRC(t *testing.T) {
191+
opts := []Options{
192+
OptPixKey("123456"),
193+
}
194+
195+
pix, err := New(opts...)
196+
if err != nil {
197+
t.Fatalf("failed to create Pix instance: %v", err)
198+
}
199+
200+
payload := pix.GenPayload()
201+
if !strings.HasSuffix(payload, pix.getCRC16(payload[:len(payload)-4])) {
202+
t.Errorf("getCRC16 or FindAndReplaceCRC function did not work as expected")
203+
}
204+
}

0 commit comments

Comments
 (0)