Skip to content

Commit 17278d3

Browse files
authored
[Feature] [V2] Add Transactions V2 Tests (#285)
1 parent 79c0f60 commit 17278d3

34 files changed

+1648
-404
lines changed

v2/arangodb/client_database_impl.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,20 @@ func (c clientDatabase) CreateDatabase(ctx context.Context, name string, options
5555
Name: name,
5656
}
5757

58-
resp, err := connection.CallPost(ctx, c.client.connection, url, nil, &createRequest)
58+
response := struct {
59+
shared.ResponseStruct `json:",inline"`
60+
}{}
61+
62+
resp, err := connection.CallPost(ctx, c.client.connection, url, &response, &createRequest)
5963
if err != nil {
6064
return nil, errors.WithStack(err)
6165
}
6266

63-
switch resp.Code() {
67+
switch code := resp.Code(); code {
6468
case http.StatusCreated:
6569
return newDatabase(c.client, name), nil
6670
default:
67-
return nil, connection.NewError(resp.Code(), "unexpected code")
71+
return nil, response.AsArangoError()
6872
}
6973
}
7074

@@ -93,16 +97,22 @@ func (c clientDatabase) Databases(ctx context.Context) ([]Database, error) {
9397

9498
func (c clientDatabase) Database(ctx context.Context, name string) (Database, error) {
9599
url := connection.NewUrl("_db", name, "_api", "database", "current")
96-
resp, err := connection.CallGet(ctx, c.client.connection, url, nil)
100+
101+
var response struct {
102+
shared.ResponseStruct `json:",inline"`
103+
VersionInfo `json:",inline"`
104+
}
105+
106+
resp, err := connection.CallGet(ctx, c.client.connection, url, &response)
97107
if err != nil {
98108
return nil, errors.WithStack(err)
99109
}
100110

101-
switch resp.Code() {
111+
switch code := resp.Code(); code {
102112
case http.StatusOK:
103113
return newDatabase(c.client, name), nil
104114
default:
105-
return nil, connection.NewError(resp.Code(), "unexpected code")
115+
return nil, response.AsArangoErrorWithCode(code)
106116
}
107117
}
108118

@@ -117,7 +127,7 @@ func (c clientDatabase) databases(ctx context.Context, url string) ([]Database,
117127
return nil, errors.WithStack(err)
118128
}
119129

120-
switch resp.Code() {
130+
switch code := resp.Code(); code {
121131
case http.StatusOK:
122132
dbs := make([]Database, len(databases.Result))
123133

@@ -127,6 +137,6 @@ func (c clientDatabase) databases(ctx context.Context, url string) ([]Database,
127137

128138
return dbs, nil
129139
default:
130-
return nil, connection.NewError(resp.Code(), "unexpected code")
140+
return nil, databases.AsArangoErrorWithCode(code)
131141
}
132142
}

v2/arangodb/client_server_info_impl.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"context"
2727
"net/http"
2828

29+
"github.com/arangodb/go-driver/v2/arangodb/shared"
30+
2931
"github.com/arangodb/go-driver/v2/connection"
3032
"github.com/pkg/errors"
3133
)
@@ -45,17 +47,20 @@ type clientServerInfo struct {
4547
func (c clientServerInfo) Version(ctx context.Context) (VersionInfo, error) {
4648
url := connection.NewUrl("_api", "version")
4749

48-
var version VersionInfo
50+
var response struct {
51+
shared.ResponseStruct `json:",inline"`
52+
VersionInfo
53+
}
4954

50-
resp, err := connection.CallGet(ctx, c.client.connection, url, &version)
55+
resp, err := connection.CallGet(ctx, c.client.connection, url, &response)
5156
if err != nil {
5257
return VersionInfo{}, errors.WithStack(err)
5358
}
5459

55-
switch resp.Code() {
60+
switch code := resp.Code(); code {
5661
case http.StatusOK:
57-
return version, nil
62+
return response.VersionInfo, nil
5863
default:
59-
return VersionInfo{}, connection.NewError(resp.Code(), "unexpected code")
64+
return VersionInfo{}, response.AsArangoErrorWithCode(code)
6065
}
6166
}

v2/arangodb/collection_documents.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ type CollectionDocuments interface {
3232

3333
CollectionDocumentCreate
3434
CollectionDocumentRead
35+
CollectionDocumentUpdate
3536
}

v2/arangodb/collection_documents_create.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,13 @@ type CollectionDocumentCreate interface {
7171
}
7272

7373
type CollectionDocumentCreateResponseReader interface {
74-
Read() (CollectionDocumentCreateResponse, bool, error)
74+
Read() (CollectionDocumentCreateResponse, error)
7575
}
7676

7777
type CollectionDocumentCreateResponse struct {
7878
DocumentMeta
79-
shared.ResponseStruct
80-
81-
Old, New interface{}
79+
shared.ResponseStruct `json:",inline"`
80+
Old, New interface{}
8281
}
8382

8483
type CollectionDocumentCreateOverwriteMode string
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2020 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Adam Janikowski
21+
//
22+
23+
package arangodb
24+
25+
import (
26+
"context"
27+
"io"
28+
"net/http"
29+
30+
"github.com/arangodb/go-driver/v2/arangodb/shared"
31+
"github.com/arangodb/go-driver/v2/connection"
32+
"github.com/arangodb/go-driver/v2/utils"
33+
"github.com/pkg/errors"
34+
)
35+
36+
func newCollectionDocumentCreate(collection *collection) *collectionDocumentCreate {
37+
return &collectionDocumentCreate{
38+
collection: collection,
39+
}
40+
}
41+
42+
var _ CollectionDocumentCreate = &collectionDocumentCreate{}
43+
44+
type collectionDocumentCreate struct {
45+
collection *collection
46+
}
47+
48+
func (c collectionDocumentCreate) CreateDocumentsWithOptions(ctx context.Context, documents interface{}, opts *CollectionDocumentCreateOptions) (CollectionDocumentCreateResponseReader, error) {
49+
if !utils.IsListPtr(documents) && !utils.IsList(documents) {
50+
return nil, errors.Errorf("Input documents should be list")
51+
}
52+
53+
url := c.collection.url("document")
54+
55+
req, err := c.collection.connection().NewRequest(http.MethodPost, url)
56+
if err != nil {
57+
return nil, err
58+
}
59+
60+
for _, modifier := range c.collection.withModifiers(opts.modifyRequest, connection.WithBody(documents), connection.WithFragment("multiple")) {
61+
if err = modifier(req); err != nil {
62+
return nil, err
63+
}
64+
}
65+
66+
var arr connection.Array
67+
68+
resp, err := c.collection.connection().Do(ctx, req, &arr)
69+
if err != nil {
70+
return nil, err
71+
}
72+
73+
switch code := resp.Code(); code {
74+
case http.StatusCreated:
75+
fallthrough
76+
case http.StatusAccepted:
77+
return newCollectionDocumentCreateResponseReader(arr, opts), nil
78+
default:
79+
return nil, shared.NewResponseStruct().AsArangoErrorWithCode(code)
80+
}
81+
}
82+
83+
func (c collectionDocumentCreate) CreateDocuments(ctx context.Context, documents interface{}) (CollectionDocumentCreateResponseReader, error) {
84+
return c.CreateDocumentsWithOptions(ctx, documents, nil)
85+
}
86+
87+
func (c collectionDocumentCreate) CreateDocumentWithOptions(ctx context.Context, document interface{}, options *CollectionDocumentCreateOptions) (CollectionDocumentCreateResponse, error) {
88+
url := c.collection.url("document")
89+
90+
var meta CollectionDocumentCreateResponse
91+
92+
if options != nil {
93+
meta.Old = options.OldObject
94+
meta.New = options.NewObject
95+
}
96+
97+
response := struct {
98+
*DocumentMeta `json:",inline"`
99+
*shared.ResponseStruct `json:",inline"`
100+
Old *UnmarshalInto `json:"old,omitempty"`
101+
New *UnmarshalInto `json:"new,omitempty"`
102+
}{
103+
DocumentMeta: &meta.DocumentMeta,
104+
ResponseStruct: &meta.ResponseStruct,
105+
106+
Old: newUnmarshalInto(meta.Old),
107+
New: newUnmarshalInto(meta.New),
108+
}
109+
110+
resp, err := connection.CallPost(ctx, c.collection.connection(), url, &response, document, c.collection.withModifiers(options.modifyRequest)...)
111+
if err != nil {
112+
return CollectionDocumentCreateResponse{}, err
113+
}
114+
115+
switch code := resp.Code(); code {
116+
case http.StatusCreated:
117+
fallthrough
118+
case http.StatusAccepted:
119+
return meta, nil
120+
default:
121+
return CollectionDocumentCreateResponse{}, response.AsArangoErrorWithCode(code)
122+
}
123+
}
124+
125+
func (c collectionDocumentCreate) CreateDocument(ctx context.Context, document interface{}) (CollectionDocumentCreateResponse, error) {
126+
return c.CreateDocumentWithOptions(ctx, document, nil)
127+
}
128+
129+
func newCollectionDocumentCreateResponseReader(array connection.Array, options *CollectionDocumentCreateOptions) *collectionDocumentCreateResponseReader {
130+
c := &collectionDocumentCreateResponseReader{array: array, options: options}
131+
132+
if c.options != nil {
133+
c.response.Old = newUnmarshalInto(c.options.OldObject)
134+
c.response.New = newUnmarshalInto(c.options.NewObject)
135+
}
136+
137+
return c
138+
}
139+
140+
var _ CollectionDocumentCreateResponseReader = &collectionDocumentCreateResponseReader{}
141+
142+
type collectionDocumentCreateResponseReader struct {
143+
array connection.Array
144+
options *CollectionDocumentCreateOptions
145+
response struct {
146+
*DocumentMeta
147+
*shared.ResponseStruct `json:",inline"`
148+
Old *UnmarshalInto `json:"old,omitempty"`
149+
New *UnmarshalInto `json:"new,omitempty"`
150+
}
151+
}
152+
153+
func (c *collectionDocumentCreateResponseReader) Read() (CollectionDocumentCreateResponse, error) {
154+
if !c.array.More() {
155+
return CollectionDocumentCreateResponse{}, shared.NoMoreDocumentsError{}
156+
}
157+
158+
var meta CollectionDocumentCreateResponse
159+
160+
if c.options != nil {
161+
meta.Old = c.options.OldObject
162+
meta.New = c.options.NewObject
163+
}
164+
165+
c.response.DocumentMeta = &meta.DocumentMeta
166+
c.response.ResponseStruct = &meta.ResponseStruct
167+
168+
if err := c.array.Unmarshal(&c.response); err != nil {
169+
if err == io.EOF {
170+
return CollectionDocumentCreateResponse{}, shared.NoMoreDocumentsError{}
171+
}
172+
return CollectionDocumentCreateResponse{}, err
173+
}
174+
175+
return meta, nil
176+
}

0 commit comments

Comments
 (0)