forked from TBD54566975/ssi-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers_test.go
142 lines (123 loc) · 4.01 KB
/
helpers_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
140
141
142
package util
import (
"testing"
"time"
"github.com/piprate/json-gold/ld"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestInterfaceToStrings(t *testing.T) {
t.Run("simple string", func(tt *testing.T) {
data := "hello"
res, err := InterfaceToStrings(data)
assert.NoError(tt, err)
assert.True(tt, len(res) == 1)
assert.True(tt, res[0] == data)
})
t.Run("simple string array", func(tt *testing.T) {
data := []string{"hello"}
res, err := InterfaceToStrings(data)
assert.NoError(tt, err)
assert.True(tt, len(res) == 1)
assert.True(tt, res[0] == data[0])
})
t.Run("multi value string array", func(tt *testing.T) {
data := []string{"hello", "goodbye"}
res, err := InterfaceToStrings(data)
assert.NoError(tt, err)
assert.True(tt, len(res) == 2)
assert.EqualValues(tt, data, res)
})
t.Run("non string value", func(tt *testing.T) {
bad := 2
_, err := InterfaceToStrings(bad)
assert.Error(tt, err)
})
t.Run("non string array", func(tt *testing.T) {
bad := []int{2}
_, err := InterfaceToStrings(bad)
assert.Error(tt, err)
})
}
func TestArrayInterfaceToStr(t *testing.T) {
t.Run("simple string array", func(tt *testing.T) {
data := []any{"hello"}
res, err := ArrayInterfaceToStr(data)
assert.NoError(tt, err)
assert.True(tt, len(res) == 1)
assert.True(tt, res[0] == data[0])
})
t.Run("multi value string array", func(tt *testing.T) {
data := []any{"hello", "goodbye"}
res, err := ArrayInterfaceToStr(data)
assert.NoError(tt, err)
assert.True(tt, len(res) == 2)
})
t.Run("non string array", func(tt *testing.T) {
bad := []any{2}
_, err := ArrayInterfaceToStr(bad)
assert.Error(tt, err)
})
}
func TestMergeUniqueValues(t *testing.T) {
t.Run("No union", func(tt *testing.T) {
a := []string{"a", "b", "c"}
b := []string{"d", "e", "f"}
res := MergeUniqueValues(a, b)
assert.True(tt, len(res) == len(a)+len(b))
})
t.Run("All overlap", func(tt *testing.T) {
a := []string{"a", "b", "c"}
b := []string{"a", "b", "c"}
res := MergeUniqueValues(a, b)
assert.True(tt, len(res) == len(a))
assert.EqualValues(tt, a, res)
})
t.Run("Some overlap", func(tt *testing.T) {
a := []string{"a", "b", "c"}
b := []string{"c", "d", "e"}
res := MergeUniqueValues(a, b)
assert.True(tt, len(res) == len(a)+2)
})
}
func TestLDProcessor(t *testing.T) {
testJSONDLContextURLStr := "http://schema.org/"
ldProcessor, err := NewLDProcessor()
require.NoError(t, err)
t.Run("caching document loader", func(tt *testing.T) {
numOfLoads := 5
nonCachedLoader := ld.NewDefaultDocumentLoader(nil)
t0 := time.Now()
for i := 0; i < numOfLoads; i++ {
doc, err := nonCachedLoader.LoadDocument(testJSONDLContextURLStr)
assert.NoError(tt, err)
assert.NotNil(tt, doc)
}
dtNonCached := time.Now().Sub(t0)
tt.Logf("non-cached document loader for %d tests dt: %v\n", numOfLoads, dtNonCached)
ldProcessor, err := NewLDProcessor()
require.NoError(tt, err)
t1 := time.Now()
for i := 0; i < numOfLoads; i++ {
doc, err := ldProcessor.DocumentLoader.LoadDocument(testJSONDLContextURLStr)
assert.NoError(tt, err)
assert.NotNil(tt, doc)
}
dtCached := time.Now().Sub(t1)
tt.Logf("caching document loader for %d tests dt: %v\n", numOfLoads, dtCached)
assert.True(tt, dtNonCached/dtCached > 2.0)
})
t.Run("get context from map", func(tt *testing.T) {
contextMap := map[string]any{
"dc": "http://purl.org/dc/elements/1.1/",
"ex": "http://example.org/vocab#",
"ex:contains": map[string]any{
"@type": "@id",
},
}
activeCtx, err := ldProcessor.GetContextFromMap(contextMap)
// expected activeCtx output is &{values:map[@base: processingMode:json-ld-1.1] options:0xc0001288f0 termDefinitions:map[dc:map[@id:http://purl.org/dc/elements/1.1/ @reverse:false _prefix:true] ex:map[@id:http://example.org/vocab# @reverse:false _prefix:true] ex:contains:map[@id:http://example.org/vocab#contains @reverse:false @type:@id]] inverse:map[] protected:map[] previousContext:<nil>}
assert.NoError(tt, err)
assert.NotNil(tt, activeCtx)
})
}