-
Notifications
You must be signed in to change notification settings - Fork 2
/
schema_test.go
86 lines (82 loc) · 3.21 KB
/
schema_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
package myjson
import (
"context"
// import embed package
_ "embed"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSchema(t *testing.T) {
t.Run("basic", func(t *testing.T) {
schema, err := newCollectionSchema([]byte(userSchema))
assert.NoError(t, err)
assert.NotNil(t, schema.Indexing())
assert.NotEmpty(t, schema.Indexing()["_id.primaryidx"])
assert.NotEmpty(t, schema.Indexing()["account_id.foreignidx"])
assert.NotEmpty(t, schema.Indexing()["contact.email.uniqueidx"])
assert.NotEmpty(t, schema.Indexing()["account_email_idx"])
assert.NotEmpty(t, schema.Indexing()["language_idx"])
})
t.Run("json schema validation", func(t *testing.T) {
schema, err := newCollectionSchema([]byte(userSchema))
assert.NoError(t, err)
assert.NoError(t, schema.ValidateDocument(context.Background(), newUserDoc()))
assert.Error(t, schema.ValidateDocument(context.Background(), NewDocument()))
})
t.Run("primary key", func(t *testing.T) {
schema, err := newCollectionSchema([]byte(taskSchema))
assert.NoError(t, err)
assert.Equal(t, "_id", schema.PrimaryKey())
})
t.Run("collection", func(t *testing.T) {
schema, err := newCollectionSchema([]byte(taskSchema))
assert.NoError(t, err)
assert.Equal(t, "task", schema.Collection())
})
t.Run("indexing not nil", func(t *testing.T) {
schema, err := newCollectionSchema([]byte(userSchema))
assert.NoError(t, err)
assert.NotNil(t, schema.Indexing())
})
t.Run("authz not nil", func(t *testing.T) {
schema, err := newCollectionSchema([]byte(accountSchema))
assert.NoError(t, err)
assert.NotNil(t, schema.Authz())
})
t.Run("MarshalJSON/UnmarshalJSON", func(t *testing.T) {
schema, err := newCollectionSchema([]byte(userSchema))
assert.NoError(t, err)
bits, _ := schema.MarshalJSON()
assert.NoError(t, schema.UnmarshalJSON(bits))
})
t.Run("MarshalYAML/UnmarshalYAML", func(t *testing.T) {
schema, err := newCollectionSchema([]byte(userSchema))
assert.NoError(t, err)
before, _ := schema.MarshalJSON()
bits, _ := schema.MarshalYAML()
assert.NoError(t, schema.UnmarshalYAML(bits))
after, _ := schema.MarshalJSON()
assert.JSONEq(t, string(before), string(after))
})
t.Run("properties", func(t *testing.T) {
schema, err := newCollectionSchema([]byte(userSchema))
assert.NoError(t, err)
assert.Equal(t, "age", schema.Properties()["age"].Name)
assert.Equal(t, "name", schema.Properties()["name"].Name)
assert.Equal(t, "account_id", schema.Properties()["account_id"].Name)
})
t.Run("properties foreign", func(t *testing.T) {
schema, err := newCollectionSchema([]byte(userSchema))
assert.NoError(t, err)
assert.Equal(t, "account", schema.Properties()["account_id"].ForeignKey.Collection)
assert.Equal(t, true, schema.Properties()["account_id"].ForeignKey.Cascade)
assert.Equal(t, true, schema.PropertyPaths()["account_id"].ForeignKey.Cascade)
})
t.Run("properties nested", func(t *testing.T) {
schema, err := newCollectionSchema([]byte(userSchema))
assert.NoError(t, err)
assert.Equal(t, "email", schema.Properties()["contact"].Properties["email"].Name)
assert.Equal(t, "contact.email", schema.Properties()["contact"].Properties["email"].Path)
assert.Equal(t, "contact.email", schema.PropertyPaths()["contact.email"].Path)
})
}