Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for indexes api #350

Merged
merged 15 commits into from
Mar 3, 2022
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,4 @@ ASALocalRun/
healthchecksdb

# Backup folder for Package Reference Convert tool in Visual Studio 2017
MigrationBackup/

# Ionide (cross platform F# VS Code tools) working folder
.ionide/
MigrationBackup/
151 changes: 151 additions & 0 deletions arangodb-net-standard.Test/IndexApi/IndexApiClientTest.cs
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 arangodb-net-standard.Test/IndexApi/IndexApiClientTestFixture.cs
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;
}

}
}
}
4 changes: 2 additions & 2 deletions arangodb-net-standard.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.852
# Visual Studio Version 17
VisualStudioVersion = 17.1.32210.238
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ArangoDBNetStandard", "arangodb-net-standard\ArangoDBNetStandard.csproj", "{A46089A1-FF27-4C00-AAF4-134ACF6E8FB2}"
EndProject
Expand Down
7 changes: 7 additions & 0 deletions arangodb-net-standard/ArangoDBClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using ArangoDBNetStandard.DatabaseApi;
using ArangoDBNetStandard.DocumentApi;
using ArangoDBNetStandard.GraphApi;
using ArangoDBNetStandard.IndexApi;
using ArangoDBNetStandard.Serialization;
using ArangoDBNetStandard.TransactionApi;
using ArangoDBNetStandard.Transport;
Expand Down Expand Up @@ -69,6 +70,11 @@ public class ArangoDBClient : IArangoDBClient
/// </summary>
public UserApiClient User { get; private set; }

/// <summary>
/// Index management API.
/// </summary>
public IndexApiClient Index { get; private set; }

/// <summary>
/// Create an instance of <see cref="ArangoDBClient"/> from an existing
/// <see cref="HttpClient"/> instance, using the default JSON serialization.
Expand Down Expand Up @@ -131,6 +137,7 @@ private void InitializeApis(
Transaction = new TransactionApiClient(transport, serialization);
Graph = new GraphApiClient(transport, serialization);
User = new UserApiClient(transport, serialization);
Index = new IndexApiClient(transport, serialization);
}
}
}
6 changes: 6 additions & 0 deletions arangodb-net-standard/IArangoDBClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using ArangoDBNetStandard.DatabaseApi;
using ArangoDBNetStandard.DocumentApi;
using ArangoDBNetStandard.GraphApi;
using ArangoDBNetStandard.IndexApi;
using ArangoDBNetStandard.TransactionApi;
using ArangoDBNetStandard.UserApi;

Expand Down Expand Up @@ -57,5 +58,10 @@ public interface IArangoDBClient : IDisposable
/// User management API.
/// </summary>
UserApiClient User { get; }

/// <summary>
/// Index management API.
/// </summary>
IndexApiClient Index { get; }
}
}
41 changes: 41 additions & 0 deletions arangodb-net-standard/IndexApi/IIndexApiClient.cs
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);
}
}
Loading