-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #350 from arangodb/feature-3.8/DE-167-support-for-…
…indexes-api Support for indexes api
- Loading branch information
Showing
17 changed files
with
845 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
arangodb-net-standard.Test/IndexApi/IndexApiClientTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using ArangoDBNetStandard; | ||
using ArangoDBNetStandard.IndexApi; | ||
using ArangoDBNetStandard.IndexApi.Models; | ||
using ArangoDBNetStandard.Transport; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace ArangoDBNetStandardTest.IndexApi | ||
{ | ||
public class IndexApiClientTest : IClassFixture<IndexApiClientTestFixture>, IAsyncLifetime | ||
{ | ||
private IndexApiClient _indexApi; | ||
private ArangoDBClient _adb; | ||
private readonly string _testIndexName; | ||
private readonly string _testIndexId; | ||
private readonly string _testCollection; | ||
|
||
public IndexApiClientTest(IndexApiClientTestFixture fixture) | ||
{ | ||
_adb = fixture.ArangoDBClient; | ||
_indexApi = _adb.Index; | ||
_testIndexName = fixture.TestIndexName; | ||
_testIndexId = fixture.TestIndexId; | ||
_testCollection = fixture.TestCollectionName; | ||
} | ||
|
||
public Task InitializeAsync() | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task DisposeAsync() | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
[Fact] | ||
public async Task PostIndexAsync_ShouldSucceed() | ||
{ | ||
var createResponse = await _indexApi.PostIndexAsync( | ||
IndexType.Persistent, | ||
new PostIndexQuery() | ||
{ | ||
CollectionName = _testCollection, | ||
}, | ||
new PostIndexBody() | ||
{ | ||
Fields = new string[] | ||
{ | ||
"field1", | ||
"field2" | ||
}, | ||
Unique = true | ||
}); | ||
string indexId = createResponse.Id; | ||
Assert.False(createResponse.Error); | ||
Assert.NotNull(indexId); | ||
} | ||
|
||
|
||
[Fact] | ||
public async Task GetIndexAsync_ShouldSucceed() | ||
{ | ||
var index = await _indexApi.GetIndexAsync(_testIndexId); | ||
Assert.NotNull(index); | ||
Assert.Equal(_testIndexId, index.Id); | ||
} | ||
|
||
[Fact] | ||
public async Task GetIndexAsync_ShouldThrow_WhenNotFound() | ||
{ | ||
var ex = await Assert.ThrowsAsync<ApiErrorException>(async () => | ||
{ | ||
await _indexApi.GetIndexAsync("MyNonExistentIndexId"); | ||
}); | ||
Assert.Equal(HttpStatusCode.BadRequest, ex.ApiError.Code); | ||
} | ||
|
||
[Fact] | ||
public async Task GetAllCollectionIndexesAsync_ShouldSucceed() | ||
{ | ||
var indexRes = await _indexApi.GetAllCollectionIndexesAsync( | ||
new GetAllCollectionIndexesQuery() | ||
{ | ||
CollectionName = _testCollection | ||
}); | ||
Assert.NotNull(indexRes); | ||
Assert.False(indexRes.Error); | ||
Assert.NotEmpty(indexRes.Indexes); | ||
} | ||
|
||
[Fact] | ||
public async Task GetAllCollectionIndexesAsync_ShouldThrow_WhenNotFound() | ||
{ | ||
var ex = await Assert.ThrowsAsync<ApiErrorException>(async () => | ||
{ | ||
await _indexApi.GetAllCollectionIndexesAsync( | ||
new GetAllCollectionIndexesQuery() | ||
{ | ||
CollectionName = "MyNonExistentCollection" | ||
}); | ||
}); | ||
Assert.Equal(HttpStatusCode.NotFound, ex.ApiError.Code); | ||
} | ||
|
||
[Fact] | ||
public async Task DeleteIndexAsync_ShouldSucceed() | ||
{ | ||
//Create the index first | ||
var createResponse = await _indexApi.PostIndexAsync( | ||
IndexType.Persistent, | ||
new PostIndexQuery() | ||
{ | ||
CollectionName = _testCollection, | ||
}, | ||
new PostIndexBody() | ||
{ | ||
Fields = new string[] | ||
{ | ||
"field1", | ||
"field2" | ||
}, | ||
Unique = true | ||
}); | ||
string indexId = createResponse.Id; | ||
Assert.False(createResponse.Error); | ||
Assert.NotNull(indexId); | ||
|
||
//delete the new index | ||
var deleteResponse = await _indexApi.DeleteIndexAsync(indexId); | ||
Assert.False(deleteResponse.Error); | ||
Assert.Equal(indexId, deleteResponse.Id); | ||
} | ||
|
||
[Fact] | ||
public async Task DeleteIndexAsync_ShouldThrow_WhenNotExist() | ||
{ | ||
var ex = await Assert.ThrowsAsync<ApiErrorException>( | ||
async () => await _indexApi.DeleteIndexAsync("NonExistentIndexId") | ||
); | ||
Assert.Equal(400, ex.ApiError.ErrorNum); | ||
} | ||
|
||
|
||
|
||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
arangodb-net-standard.Test/IndexApi/IndexApiClientTestFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using ArangoDBNetStandard; | ||
using ArangoDBNetStandard.CollectionApi.Models; | ||
using ArangoDBNetStandard.IndexApi.Models; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace ArangoDBNetStandardTest.IndexApi | ||
{ | ||
public class IndexApiClientTestFixture : ApiClientTestFixtureBase | ||
{ | ||
public ArangoDBClient ArangoDBClient { get; internal set; } | ||
public string TestCollectionName { get; internal set; } = "OurIndexTestCollection"; | ||
public string TestIndexName { get; internal set; } = "OurIndexTestCollection_FirstIndex"; | ||
public string TestIndexId { get; internal set; } | ||
|
||
public IndexApiClientTestFixture() | ||
{ | ||
} | ||
|
||
public override async Task InitializeAsync() | ||
{ | ||
await base.InitializeAsync(); | ||
string dbName = nameof(IndexApiClientTestFixture); | ||
await CreateDatabase(dbName); | ||
Console.WriteLine("Database " + dbName + " created successfully"); | ||
ArangoDBClient = GetArangoDBClient(dbName); | ||
try | ||
{ | ||
var dbRes = await ArangoDBClient.Database.GetCurrentDatabaseInfoAsync(); | ||
if (dbRes.Error) | ||
throw new Exception("GetCurrentDatabaseInfoAsync failed: " + dbRes.Code.ToString()); | ||
else | ||
{ | ||
Console.WriteLine("In database " + dbRes.Result.Name); | ||
var colRes = await ArangoDBClient.Collection.PostCollectionAsync(new PostCollectionBody() { Name = TestCollectionName }); | ||
if (colRes.Error) | ||
throw new Exception("PostCollectionAsync failed: " + colRes.Code.ToString()); | ||
else | ||
{ | ||
Console.WriteLine("Collection " + TestCollectionName + " created successfully"); | ||
var idxRes = await ArangoDBClient.Index.PostIndexAsync( | ||
IndexType.Persistent, | ||
new PostIndexQuery() | ||
{ | ||
CollectionName = TestCollectionName, | ||
}, | ||
new PostIndexBody() | ||
{ | ||
Name = TestIndexName, | ||
Fields = new string[] { "TestName" }, | ||
Unique = true | ||
}); | ||
if (idxRes.Error) | ||
throw new Exception("PostIndexAsync failed: " + idxRes.Code.ToString()); | ||
else | ||
{ | ||
TestIndexId = idxRes.Id; | ||
TestIndexName = idxRes.Name; | ||
|
||
Console.WriteLine("DB: " + dbRes.Result.Name); | ||
Console.WriteLine("Collection: " + TestCollectionName); | ||
Console.WriteLine("Index: " + string.Format("{0} - {1}",TestIndexId, TestIndexName)); | ||
} | ||
} | ||
} | ||
} | ||
catch (ApiErrorException ex) | ||
{ | ||
Console.WriteLine(ex.Message); | ||
throw ex; | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using ArangoDBNetStandard.IndexApi.Models; | ||
using System.Threading.Tasks; | ||
|
||
namespace ArangoDBNetStandard.IndexApi | ||
{ | ||
/// <summary> | ||
/// Defines a client to access the ArangoDB Indexes API. | ||
/// </summary> | ||
internal interface IIndexApiClient | ||
{ | ||
/// <summary> | ||
/// Fetches data about the specified index. | ||
/// </summary> | ||
/// <param name="indexId">The index identifier.</param> | ||
/// <returns></returns> | ||
Task<GetIndexResponse> GetIndexAsync(string indexId); | ||
|
||
/// <summary> | ||
/// Delete an index permanently. | ||
/// </summary> | ||
/// <param name="indexId">The index identifier.</param> | ||
/// <returns></returns> | ||
Task<DeleteIndexResponse> DeleteIndexAsync(string indexId); | ||
|
||
/// <summary> | ||
/// Fetch the list of indexes for a collection. | ||
/// </summary> | ||
/// <param name="query">Query parameters for the request.</param> | ||
/// <returns></returns> | ||
Task<GetAllCollectionIndexesResponse> GetAllCollectionIndexesAsync(GetAllCollectionIndexesQuery query); | ||
|
||
/// <summary> | ||
/// Creates a new index | ||
/// </summary> | ||
/// <param name="indexType">The type of index to create.</param> | ||
/// <param name="query">Query parameters for the request.</param> | ||
/// <param name="body">The properties of the new index.</param> | ||
/// <returns></returns> | ||
Task<IndexResponseBase> PostIndexAsync(IndexType indexType, PostIndexQuery query, PostIndexBody body); | ||
} | ||
} |
Oops, something went wrong.