Skip to content

Commit

Permalink
Add method overloads with document ID.
Browse files Browse the repository at this point in the history
fix #146
  • Loading branch information
DiscoPYF committed Aug 15, 2020
1 parent 80f22cf commit 4d80d49
Show file tree
Hide file tree
Showing 4 changed files with 551 additions and 66 deletions.
285 changes: 242 additions & 43 deletions arangodb-net-standard.Test/GraphApi/GraphApiClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ await _fixture.ArangoDBClient.Graph.PostGraphAsync(new PostGraphBody
toClx,
new { myKey = "myValue" });

// Create the edge
// Create the edges

var createEdgeResponse = await _client.PostEdgeAsync(
graphName,
Expand All @@ -802,12 +802,28 @@ await _fixture.ArangoDBClient.Graph.PostGraphAsync(new PostGraphBody
ReturnNew = true,
WaitForSync = true
});
// Delete edge

var createEdgeResponse2 = await _client.PostEdgeAsync(
graphName,
edgeClx,
new
{
_from = fromResponse._id,
_to = toResponse._id,
myKey = "myValue"
},
new PostEdgeQuery
{
ReturnNew = true,
WaitForSync = true
});

// Delete edge with document ID

DeleteEdgeResponse<DeleteGraphEdgeMockModel> response =
await _client.DeleteEdgeAsync<DeleteGraphEdgeMockModel>(
graphName,
edgeClx,
createEdgeResponse.Edge._key,
createEdgeResponse.Edge._id,
new DeleteEdgeQuery
{
ReturnOld = true,
Expand All @@ -818,6 +834,23 @@ await _client.DeleteEdgeAsync<DeleteGraphEdgeMockModel>(
Assert.Equal(createEdgeResponse.New.myKey, response.Old.myKey);
Assert.True(response.Removed);
Assert.False(response.Error);

// Delete edge with collection name and key

response = await _client.DeleteEdgeAsync<DeleteGraphEdgeMockModel>(
graphName,
edgeClx,
createEdgeResponse2.Edge._key,
new DeleteEdgeQuery
{
ReturnOld = true,
WaitForSync = true
});

Assert.Equal(HttpStatusCode.OK, response.Code);
Assert.Equal(createEdgeResponse2.New.myKey, response.Old.myKey);
Assert.True(response.Removed);
Assert.False(response.Error);
}

[Fact]
Expand Down Expand Up @@ -863,7 +896,21 @@ public async Task GetVertexAsync_ShouldSucceed()
Name = clxToAdd + "_vtx"
});

var response = await _client.GetVertexAsync<GetVertexMockModel>(graphName, clxToAdd, createVtxResponse.Vertex._key);
var response = await _client.GetVertexAsync<GetVertexMockModel>(
graphName,
createVtxResponse.Vertex._id);

Assert.Equal(HttpStatusCode.OK, response.Code);
Assert.False(response.Error);
Assert.NotNull(response.Vertex);
Assert.Equal(clxToAdd + "_vtx", response.Vertex.Name);
Assert.Equal(createVtxResponse.Vertex._key, response.Vertex._key);
Assert.Equal(createVtxResponse.Vertex._id, response.Vertex._id);

response = await _client.GetVertexAsync<GetVertexMockModel>(
graphName,
clxToAdd,
createVtxResponse.Vertex._key);

Assert.Equal(HttpStatusCode.OK, response.Code);
Assert.False(response.Error);
Expand Down Expand Up @@ -966,6 +1013,8 @@ public async Task DeleteVertexAsync_ShouldSucceed()
Collection = clxToAdd
});

// Create vertex

var vertexProperty = clxToAdd + "_vtx";

var createVtxResponse = await _client.PostVertexAsync(graphName, clxToAdd, new
Expand All @@ -975,12 +1024,37 @@ public async Task DeleteVertexAsync_ShouldSucceed()

Assert.Equal(HttpStatusCode.Accepted, createVtxResponse.Code);

var response = await _client.DeleteVertexAsync<DeleteVertexMockModel>(graphName, clxToAdd, createVtxResponse.Vertex._key, new DeleteVertexQuery
var createVtxResponse2 = await _client.PostVertexAsync(graphName, clxToAdd, new
{
ReturnOld = true,
WaitForSync = true
Name = vertexProperty
});

Assert.Equal(HttpStatusCode.Accepted, createVtxResponse2.Code);

var response = await _client.DeleteVertexAsync<DeleteVertexMockModel>(
graphName,
createVtxResponse.Vertex._id,
new DeleteVertexQuery
{
ReturnOld = true,
WaitForSync = true
});

Assert.Equal(HttpStatusCode.OK, response.Code);
Assert.False(response.Error);
Assert.True(response.Removed);
Assert.Equal(vertexProperty, response.Old.Name);

response = await _client.DeleteVertexAsync<DeleteVertexMockModel>(
graphName,
clxToAdd,
createVtxResponse2.Vertex._key,
new DeleteVertexQuery
{
ReturnOld = true,
WaitForSync = true
});

Assert.Equal(HttpStatusCode.OK, response.Code);
Assert.False(response.Error);
Assert.True(response.Removed);
Expand Down Expand Up @@ -1090,15 +1164,46 @@ public async Task PatchVertexAsync_ShouldSucceed()
WaitForSync = true
});

var response = await _client.PatchVertexAsync<dynamic, PatchVertexMockModel>(graphName, clxToAdd, createVtxResponse.Vertex._key, new
{
Name = clxToAdd + "_vtx_2"
}, new PatchVertexQuery
{
ReturnNew = true,
ReturnOld = true,
WaitForSync = true
});
// Patch with document ID

var response = await _client.PatchVertexAsync<dynamic, PatchVertexMockModel>(
graphName,
createVtxResponse.Vertex._id,
new
{
Name = clxToAdd + "_vtx_2"
},
new PatchVertexQuery
{
ReturnNew = true,
ReturnOld = true,
WaitForSync = true
});

Assert.Equal(HttpStatusCode.OK, response.Code);
Assert.False(response.Error);
Assert.NotNull(response.Vertex);
Assert.NotEqual(createVtxResponse.Vertex._rev, response.Vertex._rev);
Assert.NotEqual(createVtxResponse.Vertex._rev, response.New._rev);
Assert.NotEqual(createVtxResponse.New.Name, response.New.Name);
Assert.Equal(createVtxResponse.New.Value, response.New.Value);

// Patch with collection name and document key

response = await _client.PatchVertexAsync<dynamic, PatchVertexMockModel>(
graphName,
clxToAdd,
createVtxResponse.Vertex._key,
new
{
Name = clxToAdd + "_vtx_3"
},
new PatchVertexQuery
{
ReturnNew = true,
ReturnOld = true,
WaitForSync = true
});

Assert.Equal(HttpStatusCode.OK, response.Code);
Assert.False(response.Error);
Expand Down Expand Up @@ -1231,23 +1336,56 @@ await _fixture.ArangoDBClient.Graph.PostGraphAsync(new PostGraphBody
WaitForSync = true
});

var response = await _client.PutEdgeAsync(graphName, edgeClx, createEdgeResponse.Edge._key, new
{
_from = fromResponse._id,
_to = toResponse._id,
myKey = "newValue"
}, new PutEdgeQuery
{
ReturnNew = true,
ReturnOld = true,
WaitForSync = true
});
// Put with document ID

var response = await _client.PutEdgeAsync(
graphName,
createEdgeResponse.Edge._id,
new
{
_from = fromResponse._id,
_to = toResponse._id,
myKey = "newValue"
},
new PutEdgeQuery
{
ReturnNew = true,
ReturnOld = true,
WaitForSync = true
});

Assert.Equal(HttpStatusCode.OK, response.Code);
Assert.Equal(createEdgeResponse.New.myKey, response.Old.myKey);
Assert.NotEqual(createEdgeResponse.New.myKey, response.New.myKey);
Assert.False(response.Error);
Assert.NotEqual(response.Edge._rev, createEdgeResponse.Edge._rev);

string previousValue = response.New.myKey;

// Put with collection name and document key

response = await _client.PutEdgeAsync(
graphName,
edgeClx,
createEdgeResponse.Edge._key,
new
{
_from = fromResponse._id,
_to = toResponse._id,
myKey = "newValue2"
},
new PutEdgeQuery
{
ReturnNew = true,
ReturnOld = true,
WaitForSync = true
});

Assert.Equal(HttpStatusCode.OK, response.Code);
Assert.Equal(previousValue, response.Old.myKey);
Assert.NotEqual(createEdgeResponse.New.myKey, response.New.myKey);
Assert.False(response.Error);
Assert.NotEqual(response.Edge._rev, createEdgeResponse.Edge._rev);
}

[Fact]
Expand Down Expand Up @@ -1547,10 +1685,11 @@ await _fixture.ArangoDBClient.Graph.PostGraphAsync(new PostGraphBody
WaitForSync = true
});

// Patch with document ID

var response = await _client.PatchEdgeAsync<object, PatchEdgeMockModel>(
graphName,
edgeClx,
createEdgeResponse.Edge._key,
createEdgeResponse.Edge._id,
new
{
_from = fromResponse._id,
Expand All @@ -1570,6 +1709,34 @@ await _fixture.ArangoDBClient.Graph.PostGraphAsync(new PostGraphBody
Assert.NotEqual(createEdgeResponse.Edge._rev, response.Edge._rev);
Assert.Equal(createEdgeResponse.New.value, response.New.value);
Assert.Equal(createEdgeResponse.New.value, response.Old.value);

string previousValue = response.New.myKey;

// Patch with collection name and document key

response = await _client.PatchEdgeAsync<object, PatchEdgeMockModel>(
graphName,
edgeClx,
createEdgeResponse.Edge._key,
new
{
_from = fromResponse._id,
_to = toResponse._id,
myKey = "newValue2"
}, new PatchEdgeQuery
{
ReturnNew = true,
ReturnOld = true,
WaitForSync = true
});

Assert.Equal(HttpStatusCode.OK, response.Code);
Assert.Equal(previousValue, response.Old.myKey);
Assert.NotEqual(createEdgeResponse.New.myKey, response.New.myKey);
Assert.False(response.Error);
Assert.NotEqual(createEdgeResponse.Edge._rev, response.Edge._rev);
Assert.Equal(createEdgeResponse.New.value, response.New.value);
Assert.Equal(createEdgeResponse.New.value, response.Old.value);
}

[Fact]
Expand Down Expand Up @@ -1608,26 +1775,58 @@ public async Task PutVertexAsync_ShouldSuceed()
Collection = vertexClx
});

var beforeVertexClxVtx = vertexClx + "_vtx";
var afterVertexClxVtx = vertexClx + "_vtx_2";
string initialValue = vertexClx + "_vtx";
string putValue = vertexClx + "_vtx_2";

var createVertexResponse = await _client.PostVertexAsync(graphName, vertexClx, new
{
Name = beforeVertexClxVtx
});
var response = await _client.PutVertexAsync(graphName, vertexClx, createVertexResponse.Vertex._key, new PutVertexMockModel
{
Name = afterVertexClxVtx
}, new PutVertexQuery
{
ReturnNew = true,
ReturnOld = true,
WaitForSync = true
Name = initialValue
});

// Put with document ID

var response = await _client.PutVertexAsync(
graphName,
createVertexResponse.Vertex._id,
new PutVertexMockModel
{
Name = putValue
},
new PutVertexQuery
{
ReturnNew = true,
ReturnOld = true,
WaitForSync = true
});

Assert.Equal(HttpStatusCode.OK, response.Code);
Assert.False(response.Error);
Assert.Equal(putValue, response.New.Name);
Assert.NotEqual(response.New.Name, response.Old.Name);
Assert.NotEqual(createVertexResponse.Vertex._rev, response.Vertex._rev);

// Put with collection name and document key

putValue = vertexClx + "_vtx_3";

response = await _client.PutVertexAsync(
graphName,
vertexClx,
createVertexResponse.Vertex._key,
new PutVertexMockModel
{
Name = putValue
},
new PutVertexQuery
{
ReturnNew = true,
ReturnOld = true,
WaitForSync = true
});

Assert.Equal(HttpStatusCode.OK, response.Code);
Assert.False(response.Error);
Assert.Equal(afterVertexClxVtx, response.New.Name);
Assert.Equal(putValue, response.New.Name);
Assert.NotEqual(response.New.Name, response.Old.Name);
Assert.NotEqual(createVertexResponse.Vertex._rev, response.Vertex._rev);
}
Expand Down
6 changes: 6 additions & 0 deletions arangodb-net-standard/ApiClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ protected async Task<ApiErrorException> GetApiErrorException(IApiClientResponse
}
}

/// <summary>
/// Checks whether the provided document ID is in the correct form
/// of "{collection}/{key}".
/// </summary>
/// <exception cref="ArgumentException">The document ID is invalid</exception>
/// <param name="documentId">The document ID to validate.</param>
protected void ValidateDocumentId(string documentId)
{
if (documentId.Split('/').Length != 2)
Expand Down
Loading

0 comments on commit 4d80d49

Please sign in to comment.