-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_test.go
More file actions
153 lines (137 loc) · 3.19 KB
/
validate_test.go
File metadata and controls
153 lines (137 loc) · 3.19 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"testing"
)
func TestProcessVAT(t *testing.T) {
t.Run("invalid format", func(t *testing.T) {
testVAT := "123"
_, err := processVAT(testVAT)
if err != ErrorNotEnoughLetters {
t.Fatalf("expected ErrorNotEnoughLetters, received %s", err.Error())
}
})
t.Run("invalid", func(t *testing.T) {
testVAT := "CZ2"
result, err := processVAT(testVAT)
if err != nil {
t.Fatal(err)
}
if result != "Invalid" {
t.Fatalf("expected Invalid, received %s", result)
}
})
t.Run("valid", func(t *testing.T) {
testVAT := "CZ28987373"
result, err := processVAT(testVAT)
if err != nil {
t.Fatal(err)
}
if result != "Valid" {
t.Fatalf("expected Valid, received %s", result)
}
})
}
func TestSplitVAT(t *testing.T) {
t.Run("valid", func(t *testing.T) {
testVAT := "123"
_, err := splitVAT(testVAT)
if err != ErrorNotEnoughLetters {
t.Fatal("Did not receive expected error")
}
})
t.Run("invalid", func(t *testing.T) {
testVAT := "CZ28987373"
expected := VAT{
countryCode: "CZ",
number: "28987373",
}
result, err := splitVAT(testVAT)
if err != nil {
t.Fatal("Received unexpected error")
}
if result != expected {
t.Fatalf("Did not receive expected result. Expected %#v, received %#v", expected, result)
}
})
}
func TestIsAllLetters(t *testing.T) {
t.Run("all letters", func(t *testing.T) {
input := "abc"
expected := true
result := isAllLetters(input)
if result != expected {
t.Fatalf("expected %v, got %v", expected, result)
}
})
t.Run("includes number", func(t *testing.T) {
input := "ab3"
expected := false
result := isAllLetters(input)
if result != expected {
t.Fatalf("expected %v, got %v", expected, result)
}
})
t.Run("empty string", func(t *testing.T) {
input := ""
expected := false
result := isAllLetters(input)
if result != expected {
t.Fatalf("expected %v, got %v", expected, result)
}
})
}
func TestBuildRequest(t *testing.T) {
vat := VAT{
countryCode: "CZ",
number: "28987373",
}
request, err := buildRequest(vat)
if err != nil {
t.Fatal("received unexpected error")
}
if request == nil {
t.Fatal("expected request, got nil")
}
if request.Header.Get("content-type") != "text/xml" {
t.Errorf("got incorrect content-type: expected text/xml, received %s", request.Header.Get("content-type"))
}
if request.Body == nil {
t.Error("body was nil")
}
}
func TestProcessRequest(t *testing.T) {
t.Run("valid", func(t *testing.T) {
vat := VAT{
countryCode: "CZ",
number: "28987373",
}
request, err := buildRequest(vat)
if err != nil {
t.Fatal("received unexpected error")
}
result, err := processRequest(request)
if err != nil {
t.Fatal("received unexpected error")
}
if result != "Valid" {
t.Fatalf("expected valid, received %s", result)
}
})
t.Run("invalid", func(t *testing.T) {
vat := VAT{
countryCode: "CZ",
number: "2",
}
request, err := buildRequest(vat)
if err != nil {
t.Fatal("received unexpected error")
}
result, err := processRequest(request)
if err != nil {
t.Fatalf("received unexpected error: %s", err.Error())
}
if result != "Invalid" {
t.Fatal("did not receive a valid result")
}
})
}