-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
100 lines (88 loc) · 2.76 KB
/
client_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
93
94
95
96
97
98
99
100
package govdata
import (
"context"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type Fixture struct {
t *testing.T
Paths map[string]string
}
func (fixture *Fixture) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f, err := os.Open(fixture.Paths[r.URL.Path])
if err != nil {
fixture.t.Fatal(err)
}
if _, err := io.Copy(w, f); err != nil {
fixture.t.Fatal(err)
}
}
func TestClient_ResourceShow(t *testing.T) {
mock := httptest.NewServer(
&Fixture{t,
map[string]string{
"/api/3/action/resource_show": "./test/resource.json",
},
},
)
BaseURL = mock.URL
client := NewClient()
actual, err := client.ResourceShow(context.Background(), "blah-blah")
require.NoError(t, err)
timestamp, err := time.Parse(ResourceCreatedTimeFormat, "2019-11-12 01:00:00")
require.NoError(t, err)
expected := Resource{
ID: "1235678-1234-1234-1234-000123456789",
MimeType: "application/json",
LastModified: LastModifiedTime{timestamp.Add(12 * time.Hour)},
Name: "example.json",
URL: "https://data.gov.ua/dataset/00000000-0000-0000-0000-00000000000/resource/1235678-1234-1234-1234-000123456789/download/example.json",
Revisions: []Revision{
{
ID: "12112019_2",
ResourceID: "1235678-1234-1234-1234-000123456789",
MimeType: "application/json",
Name: "example.json",
Format: "JSON",
URL: "https://data.gov.ua/dataset/00000000-0000-0000-0000-000000000000/resource/1235678-1234-1234-1234-000123456789/revision/12112019_2",
ResourceCreated: ResourceCreatedTime{timestamp.Add(12 * time.Hour)},
Size: 10000000,
},
{
ID: "12112019_1",
ResourceID: "1235678-1234-1234-1234-000123456789",
MimeType: "application/json",
Name: "example.json",
Format: "JSON",
URL: "https://data.gov.ua/dataset/00000000-0000-0000-0000-000000000000/resource/1235678-1234-1234-1234-000123456789/revision/12112019_1",
ResourceCreated: ResourceCreatedTime{timestamp},
Size: 20000000,
},
},
PackageID: "00000000-0000-0000-0000-000000000000",
}
assert.Equal(t, expected, *actual)
}
func TestClient_ResourceRevision(t *testing.T) {
mock := httptest.NewServer(
&Fixture{t,
map[string]string{
"/dataset/00000000-0000-0000-0000-000000000000/resource/235678-1234-1234-1234-000123456789/revision/12112019_1": "./test/example.json",
},
},
)
BaseURL = mock.URL
client := NewClient()
_, err := client.ResourceRevision(context.Background(),
"00000000-0000-0000-0000-000000000000",
"235678-1234-1234-1234-000123456789",
"12112019_1",
)
assert.NoError(t, err)
}