This repository has been archived by the owner on May 14, 2024. It is now read-only.
/
wti_test.go
110 lines (94 loc) · 3.19 KB
/
wti_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
package webtranslateit_go_client
import (
"path/filepath"
"io/ioutil"
"testing"
"fmt"
"os"
)
const TEST_PROJECT_TOKEN = "98e71ee45042066f1053ed900b4e8f4ec1f98451"
func TestNewWebTranslateIt(t *testing.T) {
wti := NewWebTranslateIt(TEST_PROJECT_TOKEN)
expected := fmt.Sprintf("%T", &WebTranslateIt{})
if fmt.Sprintf("%T", wti) != expected {
t.Errorf("Error while creating new instance of WebTranslateIt expected type %s but got %T", expected, wti)
} else if wti.GetToken() != TEST_PROJECT_TOKEN {
t.Errorf("Error creating new instance of WebTranslateIt expected token %q but got %q", TEST_PROJECT_TOKEN, wti.GetToken())
} else if wti.GetApiUrl() != DEFAULT_API_URL {
t.Errorf("Error creating new instance of WebTranslateIt expected API url %q but got %q", DEFAULT_API_URL, wti.GetApiUrl())
}
}
func TestGetProject(t *testing.T) {
wti := NewWebTranslateIt(TEST_PROJECT_TOKEN)
project, err := wti.GetProject()
if err != nil {
t.Errorf("Error getting project: %v", err)
} else if project.Name != "WebTranslateIt" {
t.Errorf("Received wrong project from API, expected name WebTranslateIt but got %q", project.Name)
}
}
func TestGetProjectWithError(t *testing.T) {
wti := NewWebTranslateIt("empty")
if project, err := wti.GetProject(); err == nil {
t.Errorf("Request of Empty project should be an error, but got %v", project)
}
}
func TestGetZip(t *testing.T) {
wti := NewWebTranslateIt(TEST_PROJECT_TOKEN)
project, err := wti.GetProject()
if err != nil {
t.Errorf("Error getting project: %v", err)
} else if project.Name != "WebTranslateIt" {
t.Errorf("Received wrong project from API, expected name WebTranslateIt but got %q", project.Name)
} else if zipFile, err := project.ZipFile(); err != nil {
t.Errorf("Error getting zip file of the project %q: %v", project.Name, err)
} else if zipFile.Size() == 0 {
t.Errorf("Zip file is empty")
} else {
var (
err error
path string
file *os.File
data map[string][]byte
)
// Save as *os.File
file, err = ioutil.TempFile(os.TempDir(), "webtranslateit_go_client")
if err != nil {
t.Fatalf("Error creating temp file %v", err)
}
// Save file
if err = zipFile.SaveToFile(file); err != nil {
file.Close()
t.Fatalf("Error saving zip file to *os.File %q %v", file.Name(), err)
}
file.Close()
// Remove temp file
if err = os.Remove(file.Name()); err != nil {
t.Fatalf("Error removing temp file %q %v", file.Name(), err)
}
// Save to path
path = filepath.Join(os.TempDir(), "webtranslateit_go_client_TestGetZip.zip")
if err = zipFile.SaveToPath(path); err != nil {
t.Fatalf("Error saving zip file to path %q %v", path, err)
}
// Remove temp file
if err = os.Remove(path); err != nil {
t.Fatalf("Error removing temp file %q %v", path, err)
}
data, err = zipFile.Extract()
if err != nil {
t.Errorf("Error extracting zip file %v", err)
}
for _, f := range project.ProjectFiles {
if _, ok := data[f.Name]; !ok {
t.Errorf("Among the extracted files no file %q", f.Name)
}
}
}
}
func TestGetZipWithEmptyWti(t *testing.T) {
project := &Project{}
if _, err := project.ZipFile(); err != WtiNil {
t.Errorf("Request of ZipFile of the project should be an error, but got %v", project)
}
}