-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
urn_test.go
139 lines (123 loc) · 3.26 KB
/
urn_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
136
137
138
139
package folio
import (
"testing"
"github.com/stretchr/testify/assert"
)
/*
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
BenchmarkURN/new-24 5232127 227.1 ns/op 24 B/op 1 allocs/op
BenchmarkURN/parse-24 3904395 310.8 ns/op 0 B/op 0 allocs/op
*/
func BenchmarkURN(b *testing.B) {
b.ReportAllocs()
b.Run("new", func(b *testing.B) {
for i := 0; i < b.N; i++ {
NewURN("my_project", "kind1")
}
})
b.Run("parse", func(b *testing.B) {
urn, _ := NewURN("my_project", "kind1")
txt := urn.String()
for i := 0; i < b.N; i++ {
_, err := ParseURN(txt)
assert.NoError(b, err)
}
})
}
func TestNewURN(t *testing.T) {
urn, err := NewURN("my_project", "kind1")
assert.NoError(t, err)
assert.NotEmpty(t, urn)
assert.Equal(t, "my_project", urn.Namespace)
assert.Equal(t, "kind1", string(urn.Kind))
assert.Len(t, urn.ID, 20)
assert.NotEmpty(t, urn.String())
}
func TestMarshalURN(t *testing.T) {
urn := "urn:my_project:deployment:clmai59hq4ui597oseng"
parsed, err := ParseURN(urn)
assert.NoError(t, err)
data, err := parsed.MarshalJSON()
assert.NoError(t, err)
var decoded URN
assert.NoError(t, decoded.UnmarshalJSON(data))
assert.Equal(t, parsed, decoded)
}
func TestParseURN(t *testing.T) {
urn := "urn:my_project:kind1:clmai59hq4ui597oseng"
parsed, err := ParseURN(urn)
assert.NoError(t, err)
assert.NotEmpty(t, parsed)
assert.Equal(t, "my_project", parsed.Namespace)
assert.Equal(t, "kind1", string(parsed.Kind))
assert.Equal(t, "clmai59hq4ui597oseng", parsed.ID)
assert.Equal(t, urn, parsed.String())
assert.True(t, parsed.IsValid())
}
func TestParseURN_WithoutID(t *testing.T) {
urn := "urn:my_project:kind1:*"
parsed, err := ParseURN(urn)
assert.NoError(t, err)
assert.NotEmpty(t, parsed)
assert.Equal(t, "my_project", parsed.Namespace)
assert.Equal(t, "kind1", string(parsed.Kind))
assert.Len(t, parsed.ID, 20)
assert.True(t, parsed.IsValid())
}
func TestParseURN_Shortest(t *testing.T) {
urn := "urn:ab:cd:*"
parsed, err := ParseURN(urn)
assert.NoError(t, err)
assert.NotEmpty(t, parsed)
assert.Equal(t, "ab", parsed.Namespace)
assert.Equal(t, "cd", string(parsed.Kind))
assert.Len(t, parsed.ID, 20)
assert.True(t, parsed.IsValid())
}
func TestParseURN_Errors(t *testing.T) {
tests := []string{
"",
"urn",
"urn:",
"urn:project",
"urn:project:kind",
"urn:project:kind:",
"urn:project:kind:clmai59hq4ui597oseng:",
"urn:project:kind:clmai59hq4ui597oseng:extra",
"urn:project:kind:clmai59hq4ui597oseng1",
"urn:project:123:clmai59hq4ui597oseng",
"urn:123:clmai59hq4ui597oseng",
"urn:project:123:clmai59hq4ui597oseng",
}
for _, test := range tests {
t.Run(test, func(t *testing.T) {
_, err := ParseURN(test)
assert.Error(t, err)
})
}
}
func TestNewURN_Errors(t *testing.T) {
tests := []struct {
Project string
Kind string
}{
{"", ""},
{"", ""},
{"project", ""},
{"project", "123"},
}
for _, test := range tests {
t.Run(test.Project+":"+test.Kind, func(t *testing.T) {
_, err := NewURN(test.Project, Kind(test.Kind))
assert.Error(t, err)
})
}
}
func TestEmptyURN(t *testing.T) {
urn := URN{}
assert.False(t, urn.IsValid())
assert.Empty(t, urn.String())
assert.Empty(t, urn.Namespace)
assert.Empty(t, string(urn.Kind))
assert.Empty(t, urn.ID)
}