-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_test.go
133 lines (125 loc) · 2.57 KB
/
image_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
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
//go:build integration
package vanceai
import (
"context"
"io"
"os"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
// first, you need to set API_KEY environment variable
// https://vanceai.com/ja/my-account/api/
func TestClient_Upload_Process_Check_Download(t *testing.T) {
cli, err := NewClient(os.Getenv("API_KEY"), "")
if err != nil {
t.Fatal(err)
}
image, err := os.Open("testdata/cat.jpg")
if err != nil {
t.Fatal(err)
}
defer image.Close()
// upload image
uresp, err := cli.UploadImage(context.Background(), image, "cat.jpg")
if err != nil {
t.Fatal(err)
}
want := Response{
Code: 200,
CSCode: 200,
Data: Data{
Name: "cat.jpg",
Thumbnail: "",
W: 1200,
H: 1199,
FileSize: 98052,
},
}
if diff := cmp.Diff(want, uresp,
cmpopts.IgnoreFields(
Response{}, "IP",
),
cmpopts.IgnoreFields(
Data{}, "UID",
),
); diff != "" {
t.Errorf("response mismatch (-want +got):\n%s", diff)
}
// process image
presp, err := cli.ProcessImage(context.Background(), uresp.Data.UID, &JobConfig{
Job: "enlarge",
Config: Config{
Module: "enlarge",
ModuleParams: ModuleParams{
ModelName: "EnlargeStable",
SuppressNoise: 26,
RemoveBlur: 26,
Scale: "2x",
},
OutParams: OutParams{},
},
})
if err != nil {
t.Fatal(err)
}
want = Response{
Code: 200,
CSCode: 200,
Data: Data{
Status: "finish",
},
}
if diff := cmp.Diff(want, presp,
cmpopts.IgnoreFields(
Response{}, "IP",
),
cmpopts.IgnoreFields(
Data{}, "TransID",
),
); diff != "" {
t.Errorf("response mismatch (-want +got):\n%s", diff)
}
// check process status
sresp, err := cli.GetProgress(context.Background(), presp.Data.TransID)
if err != nil {
t.Fatal(err)
}
want = Response{
Code: 200,
CSCode: 200,
Data: Data{
Status: "finish",
FileSize: 1288112,
},
}
if diff := cmp.Diff(want, sresp,
cmpopts.IgnoreFields(
Response{}, "IP",
),
); diff != "" {
t.Errorf("response mismatch (-want +got):\n%s", diff)
}
// download image
dresp, err := cli.Download(context.Background(), presp.Data.TransID)
if err != nil {
t.Fatal(err)
}
if dresp == nil {
t.Fatal("response is nil")
}
d := t.TempDir()
f, err := os.Create(d + "/cat.jpg")
if err != nil {
t.Fatal(err)
}
defer f.Close()
written, err := io.Copy(f, dresp)
if err != nil {
t.Fatal(err)
}
if written <= uresp.Data.FileSize {
// enlarged image size is larger than original image size
t.Errorf("written size mismatch: want %d, got %d", sresp.Data.FileSize, written)
}
}