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

Add typesense resource #143

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
Expand All @@ -30,7 +30,7 @@
<ItemGroup>
<PackageReference Include="Docker.DotNet" Version="3.125.15" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Polly" Version="7.2.4" />
<PackageReference Include="Polly" Version="8.4.2" />
<PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</PropertyGroup>

<PropertyGroup Label="Framework Versions">
<TestProjectTargetFrameworks>net6.0;net7.0;net8.0</TestProjectTargetFrameworks>
<ResourceProjectTargetFrameworks>netstandard2.0;net6.0;net7.0</ResourceProjectTargetFrameworks>
<TestProjectTargetFrameworks>net6.0;net8.0</TestProjectTargetFrameworks>
<ResourceProjectTargetFrameworks>netstandard2.0;net6.0;net8.0</ResourceProjectTargetFrameworks>
</PropertyGroup>
</Project>
28 changes: 28 additions & 0 deletions src/Squadron.sln
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClickHouse.Tests", "ClickHo
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClickHouse", "ClickHouse\ClickHouse.csproj", "{8A09F209-2008-4E4E-A524-DA2D35032FCD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Typesense", "Typesense\Typesense.csproj", "{40DF4F0E-2D25-491D-BBF0-AC760C3FF530}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Typesense.Tests", "Typesense.Tests\Typesense.Tests.csproj", "{803EB4D7-5CEA-46F1-8A0C-9BB290C5A3D7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -616,6 +620,30 @@ Global
{8A09F209-2008-4E4E-A524-DA2D35032FCD}.Release|x64.Build.0 = Release|Any CPU
{8A09F209-2008-4E4E-A524-DA2D35032FCD}.Release|x86.ActiveCfg = Release|Any CPU
{8A09F209-2008-4E4E-A524-DA2D35032FCD}.Release|x86.Build.0 = Release|Any CPU
{40DF4F0E-2D25-491D-BBF0-AC760C3FF530}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{40DF4F0E-2D25-491D-BBF0-AC760C3FF530}.Debug|Any CPU.Build.0 = Debug|Any CPU
{40DF4F0E-2D25-491D-BBF0-AC760C3FF530}.Debug|x64.ActiveCfg = Debug|Any CPU
{40DF4F0E-2D25-491D-BBF0-AC760C3FF530}.Debug|x64.Build.0 = Debug|Any CPU
{40DF4F0E-2D25-491D-BBF0-AC760C3FF530}.Debug|x86.ActiveCfg = Debug|Any CPU
{40DF4F0E-2D25-491D-BBF0-AC760C3FF530}.Debug|x86.Build.0 = Debug|Any CPU
{40DF4F0E-2D25-491D-BBF0-AC760C3FF530}.Release|Any CPU.ActiveCfg = Release|Any CPU
{40DF4F0E-2D25-491D-BBF0-AC760C3FF530}.Release|Any CPU.Build.0 = Release|Any CPU
{40DF4F0E-2D25-491D-BBF0-AC760C3FF530}.Release|x64.ActiveCfg = Release|Any CPU
{40DF4F0E-2D25-491D-BBF0-AC760C3FF530}.Release|x64.Build.0 = Release|Any CPU
{40DF4F0E-2D25-491D-BBF0-AC760C3FF530}.Release|x86.ActiveCfg = Release|Any CPU
{40DF4F0E-2D25-491D-BBF0-AC760C3FF530}.Release|x86.Build.0 = Release|Any CPU
{803EB4D7-5CEA-46F1-8A0C-9BB290C5A3D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{803EB4D7-5CEA-46F1-8A0C-9BB290C5A3D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{803EB4D7-5CEA-46F1-8A0C-9BB290C5A3D7}.Debug|x64.ActiveCfg = Debug|Any CPU
{803EB4D7-5CEA-46F1-8A0C-9BB290C5A3D7}.Debug|x64.Build.0 = Debug|Any CPU
{803EB4D7-5CEA-46F1-8A0C-9BB290C5A3D7}.Debug|x86.ActiveCfg = Debug|Any CPU
{803EB4D7-5CEA-46F1-8A0C-9BB290C5A3D7}.Debug|x86.Build.0 = Debug|Any CPU
{803EB4D7-5CEA-46F1-8A0C-9BB290C5A3D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{803EB4D7-5CEA-46F1-8A0C-9BB290C5A3D7}.Release|Any CPU.Build.0 = Release|Any CPU
{803EB4D7-5CEA-46F1-8A0C-9BB290C5A3D7}.Release|x64.ActiveCfg = Release|Any CPU
{803EB4D7-5CEA-46F1-8A0C-9BB290C5A3D7}.Release|x64.Build.0 = Release|Any CPU
{803EB4D7-5CEA-46F1-8A0C-9BB290C5A3D7}.Release|x86.ActiveCfg = Release|Any CPU
{803EB4D7-5CEA-46F1-8A0C-9BB290C5A3D7}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
22 changes: 22 additions & 0 deletions src/Typesense.Tests/Typesense.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$(CCTestProjectProps)" Condition="Exists('$(CCTestProjectProps)')" />

<PropertyGroup>
<AssemblyName>Squadron.Typesense.Tests</AssemblyName>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Typesense\Typesense.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Typesense" Version="7.21.0" />
</ItemGroup>

</Project>
58 changes: 58 additions & 0 deletions src/Typesense.Tests/TypesenseResourceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Typesense;
using Typesense.Setup;
using Xunit;

namespace Squadron
{
public class TypesenseResourceTests : IClassFixture<TypesenseResource>
{
private readonly TypesenseResource _typesenseResource;

public TypesenseResourceTests(TypesenseResource typesenseResource)
{
_typesenseResource = typesenseResource;
}

[Fact]
public void CreateAndGetDocument_NoError()
{
var provider = new ServiceCollection()
.AddTypesenseClient(config =>
{
config.ApiKey = _typesenseResource.ApiKey;
config.Nodes = new List<Node> { new Node("localhost", _typesenseResource.Port.ToString(), "http") };
}, enableHttpCompression: false)
.BuildServiceProvider();

ITypesenseClient client = provider.GetRequiredService<ITypesenseClient>();

//Act
Action action = () =>
{
var schema = new Schema("users",
new[] { new Field("id", FieldType.String),
new Field("name", FieldType.String) });

client.CreateCollection(schema);
client.CreateDocument("users", new User{ Id = "1", Name = "John Doe" });

Task<User> _ = client.RetrieveDocument<User>("users", "1");
};

//Assert
action.Should().NotThrow();
}
}

internal class User
{
public string Id { get; set; }

public string Name { get; set; }
}
}
4 changes: 4 additions & 0 deletions src/Typesense.Tests/xunit.runner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"appDomain": "denied",
"parallelizeAssembly": true
}
11 changes: 11 additions & 0 deletions src/Typesense/Typesense.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$(CCResourceProjectProps)" Condition="Exists('$(CCResourceProjectProps)')" />
<PropertyGroup>
<AssemblyName>Squadron.Typesense</AssemblyName>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
</ItemGroup>

</Project>
42 changes: 42 additions & 0 deletions src/Typesense/TypesenseDefaultOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.IO;

namespace Squadron
{
/// <summary>
/// Default RavenDB resource options
/// </summary>
public class TypesenseDefaultOptions : ContainerResourceOptions
{
/// <summary>
/// Configure resource options
/// </summary>
/// <param name="builder"></param>
public override void Configure(ContainerResourceBuilder builder)
{
var localdata = PrepareLocalDirectory();
var apiKey = "secretKey";

builder
.Name("typesense")
.Image("typesense/typesense:27.1")
.InternalPort(8108)
.Password(apiKey)
.AddCmd("--api-key", apiKey)
.AddCmd("--data-dir", "/data")
.AddVolume($"{localdata}:/data")
.PreferLocalImage();
}

private static string PrepareLocalDirectory()
{
var localdata = Path.GetTempPath();
if (!Directory.Exists(localdata))
{
Directory.CreateDirectory(localdata);
}

return localdata;
}
}
}
37 changes: 37 additions & 0 deletions src/Typesense/TypesenseResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Threading.Tasks;
using Xunit;

namespace Squadron
{
/// <inheritdoc/>
public class TypesenseResource : TypesenseResource<TypesenseDefaultOptions>
{
}

/// <summary>
/// Represents a Typesense resource that can be used by unit tests.
/// </summary>
/// <seealso cref="IDisposable"/>
public class TypesenseResource<TOptions>
: ContainerResource<TOptions>,
IAsyncLifetime
where TOptions : ContainerResourceOptions, new()
{
public string BaseUrl { get; private set; }
public int Port { get; private set; }
public string ApiKey { get; set; }

/// <inheritdoc cref="IAsyncLifetime"/>
public override async Task InitializeAsync()
{
await base.InitializeAsync();
BaseUrl = $"http://{Manager.Instance.Address}:{Manager.Instance.HostPort}";
Port = Manager.Instance.HostPort;
ApiKey = Settings.Password;

await Initializer.WaitAsync(
new TypesenseStatus(BaseUrl, ApiKey));
}
}
}
47 changes: 47 additions & 0 deletions src/Typesense/TypesenseStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.IO;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

namespace Squadron
{
/// <summary>
/// Status checker for Typesense
/// </summary>
/// <seealso cref="IResourceStatusProvider" />
public class TypesenseStatus : IResourceStatusProvider
{
private readonly string _baseUrl;
private readonly string _apiKey;

/// <summary>
/// Initializes a new instance of the <see cref="TypesenseStatus"/> class.
/// </summary>
/// <param name="baseUrl">The ConnectionString</param>
public TypesenseStatus(string baseUrl, string apiKey)
{
_baseUrl = baseUrl;
_apiKey = apiKey;
}

/// <inheritdoc/>
public async Task<Status> IsReadyAsync(CancellationToken cancellationToken)
{
var httpClient = new HttpClient() { BaseAddress = new Uri(_baseUrl) };
httpClient.DefaultRequestHeaders.Add("X-TYPESENSE-API-KEY", _apiKey);
HttpResponseMessage response = await httpClient.GetAsync("/health", cancellationToken);
if (response.IsSuccessStatusCode)
{
Stream stream = await response.Content.ReadAsStreamAsync();
using JsonDocument jsonDocument = await JsonDocument.ParseAsync(stream, cancellationToken: cancellationToken);
JsonElement jsonResponse = jsonDocument.RootElement;

return new Status { IsReady = jsonResponse.GetProperty("ok").GetBoolean() };
}

return new Status { IsReady = false };
}
}
}
Loading