-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_live_test.go
More file actions
84 lines (75 loc) · 2.04 KB
/
client_live_test.go
File metadata and controls
84 lines (75 loc) · 2.04 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
// Live tests against api.postio.co.uk (or stage). Skipped when no key
// is in the environment. Build tag keeps these out of `go test ./...`
// by default — opt in with `go test -tags=live`.
//go:build live
package postio_test
import (
"context"
"os"
"testing"
"github.com/postio-uk/postio-go"
)
func liveClient(t *testing.T) *postio.Client {
t.Helper()
key := os.Getenv("POSTIO_API_KEY_STAGE")
baseURL := "https://stage-api.postio.co.uk/v1"
if key == "" {
key = os.Getenv("POSTIO_API_KEY_PROD")
baseURL = "https://api.postio.co.uk/v1"
}
if key == "" {
key = os.Getenv("POSTIO_API_KEY")
baseURL = "https://api.postio.co.uk/v1"
}
if key == "" {
t.Skip("no POSTIO_API_KEY* env var set")
}
c, err := postio.NewClient(postio.WithAPIKey(key), postio.WithBaseURL(baseURL))
if err != nil {
t.Fatalf("NewClient: %v", err)
}
return c
}
func TestLiveConnect(t *testing.T) {
c := liveClient(t)
r, err := c.Connect(context.Background())
if err != nil {
t.Fatalf("Connect: %v", err)
}
if !r.Success || r.Meta.RequestID == "" {
t.Errorf("unexpected response: %+v", r)
}
}
func TestLiveAddressSearch(t *testing.T) {
c := liveClient(t)
r, err := c.Address.Search(context.Background(), "downing street", &postio.SearchOptions{MaxResults: 5})
if err != nil {
t.Fatalf("Search: %v", err)
}
if len(r.Results) == 0 {
t.Fatal("no results for downing street")
}
if r.Results[0].Suggestion == "" || r.Results[0].UDPRN == 0 {
t.Errorf("unexpected first hit: %+v", r.Results[0])
}
}
func TestLiveEmailValidate(t *testing.T) {
c := liveClient(t)
r, err := c.Email.Validate(context.Background(), "admin@postio.co.uk")
if err != nil {
t.Fatalf("Validate: %v", err)
}
if len(r.Results) != 1 || !r.Results[0].IsValidSyntax {
t.Errorf("unexpected response: %+v", r)
}
}
func TestLivePhoneValidate(t *testing.T) {
c := liveClient(t)
r, err := c.Phone.Validate(context.Background(), "+442079460000")
if err != nil {
t.Fatalf("Validate: %v", err)
}
if len(r.Results) != 1 || !r.Results[0].IsValid {
t.Errorf("unexpected response: %+v", r)
}
}