-
Notifications
You must be signed in to change notification settings - Fork 0
/
document_test.go
92 lines (66 loc) · 2.72 KB
/
document_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
package couchdb_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/simia-tech/couchdb"
)
func TestDocument(t *testing.T) {
e := setUpTestEnvironment(t)
defer e.tearDown()
db := couchdb.NewDatabase(e.client, "test")
require.NoError(t, db.Create(e.ctx))
defer db.Delete(e.ctx)
t.Run("Store", func(t *testing.T) {
t.Run("WithoutID", func(t *testing.T) {
document := couchdb.NewDocument(db, "", "")
require.NoError(t, document.Store(e.ctx, map[string]interface{}{"test": "value"}))
assert.Regexp(t, `^[0-9a-f]+$`, document.ID())
assert.Regexp(t, `^\d+\-[0-9a-f]+$`, document.Revision())
})
t.Run("WithID", func(t *testing.T) {
document := couchdb.NewDocument(db, "test", "")
require.NoError(t, document.Store(e.ctx, map[string]interface{}{"test": "value"}))
assert.Equal(t, "test", document.ID())
assert.Regexp(t, `^\d+\-[0-9a-f]+$`, document.Revision())
})
t.Run("WithIDAndRevision", func(t *testing.T) {
document := couchdb.NewDocument(db, "", "")
require.NoError(t, document.Store(e.ctx, map[string]interface{}{"test": "value"}))
id, revision := document.ID(), document.Revision()
require.NoError(t, document.Store(e.ctx, map[string]interface{}{"test": "another value"}))
assert.Equal(t, id, document.ID())
assert.NotEqual(t, revision, document.Revision())
})
t.Run("WithIDAndMalformedRevision", func(t *testing.T) {
document := couchdb.NewDocument(db, "", "")
require.NoError(t, document.Store(e.ctx, map[string]interface{}{"test": "value"}))
otherDocument := couchdb.NewDocument(db, document.ID(), "malformed")
err := otherDocument.Store(e.ctx, map[string]interface{}{"test": "another value"})
assert.ErrorIs(t, err, couchdb.ErrBadRequest)
})
t.Run("WithIDAndInvalidRevision", func(t *testing.T) {
document := couchdb.NewDocument(db, "", "")
require.NoError(t, document.Store(e.ctx, map[string]interface{}{"test": "value"}))
otherDocument := couchdb.NewDocument(db, document.ID(), "0-123")
err := otherDocument.Store(e.ctx, map[string]interface{}{"test": "another value"})
assert.ErrorIs(t, err, couchdb.ErrConflict)
})
})
t.Run("Fetch", func(t *testing.T) {
d := couchdb.NewDocument(db, "", "")
require.NoError(t, d.Store(e.ctx, map[string]interface{}{"test": "value"}))
t.Run("WithID", func(t *testing.T) {
document := couchdb.NewDocument(db, d.ID(), "")
data := map[string]interface{}{}
require.NoError(t, document.Fetch(e.ctx, &data))
assert.Equal(t, d.ID(), document.ID())
assert.Regexp(t, `^\d+\-[0-9a-f]+$`, document.Revision())
assert.Equal(t, map[string]interface{}{
"_id": d.ID(),
"_rev": d.Revision(),
"test": "value",
}, data)
})
})
}