-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
105 lines (96 loc) · 2.4 KB
/
util_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
package pgc
import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/cliqueinc/pgc/util"
)
func TestPathDirExists(t *testing.T) {
tmpDir := os.TempDir()
if !PathExists(tmpDir) {
t.Errorf("TmpDir (%s) should have existed", tmpDir)
}
if PathExists("/hoogaboogabuhbollaoughskiiiii/") {
t.Error("Dir hoogaboogabuhbollaoughskiiiii should NOT have existed")
}
}
func TestPathFileExists(t *testing.T) {
tmpDir := os.TempDir()
newFilePath := fmt.Sprintf("%s/golangtestfile-%d", tmpDir, util.RandomInt(10000000, 999999999))
fakeFile := tmpDir + "/lkajsldkfjsdlkfjalskdfj.out"
err := ioutil.WriteFile(newFilePath, []byte("Yarlow"), 0777)
if err != nil {
t.Fatalf("Failed to create a tmp file (%s), err: (%s)", newFilePath, err)
}
if !PathExists(tmpDir) {
t.Errorf("Tmp file (%s) should have existed", newFilePath)
}
if PathExists(fakeFile) {
t.Error("File (%s) should NOT have existed", fakeFile)
}
}
func TestMakeOrderBy(t *testing.T) {
type OrderModel struct {
ID string
CustomeField string `pgc_name:"field_custom"`
UpdatedAt string
}
tests := []struct {
name string
sortBy string
wantField, wantDirection string
wantOk bool
}{
{
name: "empty name",
sortBy: "",
},
{
name: "invalid field name",
sortBy: "invalid name",
},
{
name: "default direction",
sortBy: "id",
wantField: "id",
wantDirection: "ASC",
wantOk: true,
},
{
name: "with spaces",
sortBy: " id ",
wantField: "id",
wantDirection: "ASC",
wantOk: true,
},
{
name: "camelcased",
sortBy: "UpdatedAt",
wantField: "updated_at",
wantDirection: "ASC",
wantOk: true,
},
{
name: "custom name",
sortBy: "-CustomeField",
wantField: "field_custom",
wantDirection: "DESC",
wantOk: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotField, gotDirection, gotOk := MakeOrderBy(&OrderModel{}, tt.sortBy)
if gotField != tt.wantField {
t.Errorf("MakeOrderBy() gotField = %v, want %v", gotField, tt.wantField)
}
if gotDirection != tt.wantDirection {
t.Errorf("MakeOrderBy() gotDirection = %v, want %v", gotField, tt.wantDirection)
}
if gotOk != tt.wantOk {
t.Errorf("MakeOrderBy() gotOk = %v, want %v", gotOk, tt.wantOk)
}
})
}
}