This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
forked from jdel/go-syno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models_test.go
135 lines (121 loc) · 3.3 KB
/
models_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
134
135
package syno_test // import jdel.org/go-syno/syno_test
import (
"os"
"path/filepath"
"testing"
syno "jdel.org/go-syno"
log "github.com/sirupsen/logrus"
)
var defaultModels syno.Models
func init() {
defaultModels = syno.Models{
&syno.Model{
Name: "Spaghetti",
},
&syno.Model{
Name: "Penne",
},
&syno.Model{
Name: "Farfalle",
},
&syno.Model{
Name: "Papardelle",
},
}
}
func cleanupModels(t *testing.T) {
if err := os.RemoveAll(o.ModelsFile); err != nil {
t.Skipf("Cannot cleanup %s", o.ModelsFile)
}
}
func fileExists(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
func TestModelsFileExists(t *testing.T) {
cleanupModels(t)
syno.SetLogLevel(log.ErrorLevel)
if fileExists(o.ModelsFile) {
t.Errorf("Expected file %s to not exist on disk", o.ModelsFile)
}
if err := defaultModels.SaveModelsFile(); err != nil {
t.Error(err)
}
if !fileExists(o.ModelsFile) {
t.Errorf("Expected file %s to not exist on disk", o.ModelsFile)
}
}
func TestGetModelsFromFile(t *testing.T) {
cleanupModels(t)
if err := defaultModels.SaveModelsFile(); err != nil {
t.Skip("Could not create models file to prepare for test")
}
if m, err := syno.GetModels(false); err != nil {
t.Error(err)
} else if len(m) != len(defaultModels) {
t.Errorf("Expected %d models but got %d", len(defaultModels), len(m))
}
}
func TestGetModelsFromInternet(t *testing.T) {
cleanupModels(t)
if err := os.RemoveAll(o.ModelsFile); err != nil {
t.Skip("Could not remove models file to prepare for test")
}
if m, err := syno.GetModels(false); err != nil {
t.Error(err)
} else if len(m) == len(defaultModels) {
t.Errorf("Expected more than %d models but got %d", len(defaultModels), len(m))
}
}
func TestGetModelsFromInternetForce(t *testing.T) {
cleanupModels(t)
if err := defaultModels.SaveModelsFile(); err != nil {
t.Skip("Could not create models file to prepare for test")
}
if m, err := syno.GetModels(true); err != nil {
t.Error(err)
} else if len(m) == len(defaultModels) {
t.Errorf("Expected more than %d models but got %d", len(defaultModels), len(m))
}
}
func TestModelsFilterByName(t *testing.T) {
if m := defaultModels.FilterByName("Pa"); len(m) != 2 {
t.Errorf("Expected 2 models but got %d", len(m))
}
if m := defaultModels.FilterByName("invalid"); len(m) != 0 {
t.Errorf("Expected 0 models but got %d", len(m))
}
}
func TestSaveModelsFile(t *testing.T) {
cleanupModels(t)
if err := defaultModels.SaveModelsFile(); err != nil {
t.Error(err)
}
if !fileExists(o.ModelsFile) {
t.Errorf("Expected file %s to exist on disk", o.ModelsFile)
}
if m, err := syno.GetModels(false); err != nil {
t.Error(err)
} else if len(m) != len(defaultModels) {
t.Errorf("Expected to read %d models from file but fot %d", len(defaultModels), len(m))
}
}
func TestSaveModelsFileWriteError(t *testing.T) {
cleanupModels(t)
previousModelsFile := o.ModelsFile
defer func() {
o.ModelsFile = previousModelsFile
}()
o.ModelsFile = filepath.Join(testRoot, "badcache", "models.yml")
if err := os.MkdirAll(o.CacheDir, 0666); err != nil {
t.Skipf("Cannot create directory %s to prepare for test", o.CacheDir)
}
if err := defaultModels.SaveModelsFile(); err == nil {
t.Error("Expected a write error")
}
}
func TestModelsFinished(t *testing.T) {
cleanupModels(t)
}