-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
url_test.go
100 lines (92 loc) · 2.39 KB
/
url_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 datatypes_test
import (
"net/url"
"testing"
"gorm.io/datatypes"
. "gorm.io/gorm/utils/tests"
)
func TestURL(t *testing.T) {
type StructWithURL struct {
ID uint
FileName string
Storage datatypes.URL
}
_ = DB.Migrator().DropTable(&StructWithURL{})
if err := DB.Migrator().AutoMigrate(&StructWithURL{}); err != nil {
t.Fatalf("failed to migrate, got error: %v", err)
}
f1 := StructWithURL{
FileName: "FLocal1",
Storage: datatypes.URL{
Scheme: "file",
Path: "/tmp/f1",
},
}
us := "sftp://user:[email protected]/f2?query=1#frag"
u2, _ := url.Parse(us)
f2 := StructWithURL{
FileName: "FRemote2",
Storage: datatypes.URL(*u2),
}
uf1 := url.URL(f1.Storage)
uf2 := url.URL(f2.Storage)
DB.Create(&f1)
DB.Create(&f2)
result := StructWithURL{}
if err := DB.First(
&result, "file_name = ? AND storage LIKE ?",
"FLocal1",
datatypes.URL{
Scheme: "file",
Path: "/tmp/f1",
}).Error; err != nil {
t.Fatalf("failed to find record with url, got error: %v", err)
}
AssertEqual(t, uf1.String(), result.Storage.String())
result = StructWithURL{}
if err := DB.First(
&result, "file_name = ? AND storage LIKE ?",
"FRemote2",
datatypes.URL{
Scheme: "sftp",
User: url.UserPassword("user", "pwd"),
Host: "127.0.0.1",
Path: "/f2",
RawPath: "should not affects",
RawQuery: "query=1",
Fragment: "frag",
RawFragment: "should not affects",
}).Error; err != nil {
t.Fatalf("failed to find record with url, got error: %v", err)
}
AssertEqual(t, u2.String(), uf2.String())
AssertEqual(t, uf2.String(), result.Storage.String())
AssertEqual(t, us, result.Storage.String())
result = StructWithURL{}
if err := DB.First(
&result, "file_name = ? AND storage LIKE ?",
"FRemote2",
datatypes.URL{
Scheme: "sftp",
Opaque: "//user:[email protected]/f2",
RawQuery: "query=1",
Fragment: "frag",
}).Error; err != nil {
t.Fatalf("failed to find record with url, got error: %v", err)
}
AssertEqual(t, us, result.Storage.String())
result = StructWithURL{}
if err := DB.First(
&result, "file_name = ? AND storage LIKE ?",
"FRemote2",
datatypes.URL{
Scheme: "sftp",
User: url.User("user"),
Host: "127.0.0.1",
Path: "/f2",
RawQuery: "query=1",
Fragment: "frag",
}).Error; err == nil {
t.Fatalf("record couldn't have been identical: %v vs %v", result.Storage, us)
}
}