Skip to content

Allow for strongly-typed query properties #100

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

Open
wants to merge 4 commits into
base: main
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ public override void TestInitialize()
Context = CreateKSqlDbContext(EndpointType.QueryStream);
}

protected KSqlDBContext CreateKSqlDbContext(EndpointType endpointType, ModelBuilder? modelBuilder = null)
protected KSqlDBContext CreateKSqlDbContext(EndpointType endpointType, ModelBuilder? modelBuilder = null, Action<KSqlDBContextOptions>? configureOptions = null)
{
ContextOptions = new KSqlDBContextOptions(KSqlDbRestApiProvider.KsqlDbUrl)
{
ShouldPluralizeFromItemName = false,
EndpointType = endpointType
};
configureOptions?.Invoke(ContextOptions);

modelBuilder ??= new ModelBuilder();
return new KSqlDBContext(ContextOptions, modelBuilder);
Expand All @@ -62,7 +63,7 @@ protected static async Task<List<T>> CollectActualValues<T>(IAsyncEnumerable<T>

if (expectedItemsCount.HasValue)
source = source.Take(expectedItemsCount.Value);

await foreach (var item in source.WithCancellation(cts.Token))
{
actualValues.Add(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using ksqlDB.RestApi.Client.KSql.Linq;
using ksqlDB.RestApi.Client.KSql.Query.Context;
using ksqlDB.RestApi.Client.KSql.Query.Functions;
using ksqlDB.RestApi.Client.KSql.Query.Options;
using ksqlDB.RestApi.Client.KSql.RestApi;
using ksqlDB.RestApi.Client.KSql.RestApi.Parameters;
using ksqlDB.RestApi.Client.KSql.RestApi.Statements;
Expand Down Expand Up @@ -110,9 +111,9 @@ public void TransformMap_WithNestedMap()

QueryStreamParameters queryStreamParameters = new()
{
Sql = ksql,
[QueryStreamParameters.AutoOffsetResetPropertyName] = "earliest",
Sql = ksql
};
queryStreamParameters.Set(QueryStreamParameters.AutoOffsetResetPropertyName, AutoOffsetReset.Earliest);

//Act
var source = Context.CreatePushQuery<Foo>(queryStreamParameters)
Expand Down Expand Up @@ -147,9 +148,9 @@ public void TransformMap_WithNestedStruct()

QueryStreamParameters queryStreamParameters = new()
{
Sql = ksql,
[QueryStreamParameters.AutoOffsetResetPropertyName] = "earliest",
Sql = ksql
};
queryStreamParameters.Set(QueryStreamParameters.AutoOffsetResetPropertyName, AutoOffsetReset.Earliest);

//Act
var source = Context.CreatePushQuery<Foo>(queryStreamParameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,8 @@ public async Task QueryRawKSql()
QueryStreamParameters queryParameters = new()
{
Sql = ksql,
[QueryStreamParameters.AutoOffsetResetPropertyName] = "earliest",
};
queryParameters.Set(QueryStreamParameters.AutoOffsetResetPropertyName, AutoOffsetReset.Earliest);

var source = Context.CreatePushQuery<Tweet>(queryParameters);

Expand All @@ -467,9 +467,9 @@ public async Task QueryStreamRawKSql()

QueryStreamParameters queryStreamParameters = new()
{
Sql = ksql,
[QueryStreamParameters.AutoOffsetResetPropertyName] = "earliest",
Sql = ksql
};
queryStreamParameters.Set(QueryStreamParameters.AutoOffsetResetPropertyName, AutoOffsetReset.Earliest);

var source = Context.CreatePushQuery<Tweet>(queryStreamParameters);

Expand Down Expand Up @@ -656,9 +656,9 @@ public async Task SelectAsInt()

QueryStreamParameters queryStreamParameters = new()
{
Sql = ksql,
[QueryParameters.AutoOffsetResetPropertyName] = "earliest",
Sql = ksql
};
queryStreamParameters.Set(QueryStreamParameters.AutoOffsetResetPropertyName, AutoOffsetReset.Earliest);

var source = Context.CreatePushQuery<int>(queryStreamParameters);

Expand All @@ -680,9 +680,9 @@ public async Task SelectAsArray()

QueryStreamParameters queryStreamParameters = new()
{
Sql = ksql,
[QueryParameters.AutoOffsetResetPropertyName] = "earliest",
Sql = ksql
};
queryStreamParameters.Set(QueryStreamParameters.AutoOffsetResetPropertyName, AutoOffsetReset.Earliest);

var source = Context.CreatePushQuery<int[]>(queryStreamParameters);

Expand All @@ -709,9 +709,9 @@ public async Task SelectAsStruct()

QueryStreamParameters queryStreamParameters = new()
{
Sql = ksql,
[QueryParameters.AutoOffsetResetPropertyName] = "earliest",
Sql = ksql
};
queryStreamParameters.Set(QueryStreamParameters.AutoOffsetResetPropertyName, AutoOffsetReset.Earliest);

var source = Context.CreatePushQuery<MyStruct>(queryStreamParameters);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using ksqlDB.RestApi.Client.KSql.RestApi.Statements.Properties;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using EndpointType = ksqlDB.RestApi.Client.KSql.Query.Options.EndpointType;

namespace ksqlDb.RestApi.Client.IntegrationTests.KSql.Query.Context;

Expand Down Expand Up @@ -178,5 +179,26 @@ public async Task TimeTypes_InsertValues_ValuesReceived()
// ";
}

[Test]
public async Task ContextOptions_EndpointParameters_NonStringParameter()
{
// Arrange
var context = CreateKSqlDbContext(EndpointType.Query, configureOptions: options =>
{
options.PullQueryParameters.Set("ksql.query.pull.table.scan.enabled", true);
});

var config = new InsertProperties { EntityName = EntityName, ShouldPluralizeEntityName = false };
var entity1 = new Movie { Id = 1, Title = "T1" };

//Act
context.Add(entity1, config);
var response = await context.SaveChangesAsync();

// Assert
response.Should().NotBeNull();
response!.StatusCode.Should().Be(HttpStatusCode.OK);
}

#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void ConfigureKSqlDb_IKSqlDBContext()

//Assert
var descriptor = ClassUnderTest.TryGetRegistration<IKSqlDBContext>();

descriptor.Should().NotBeNull();
descriptor!.Lifetime.Should().Be(ServiceLifetime.Scoped);
}
Expand All @@ -53,7 +53,7 @@ public void ConfigureKSqlDb_SetupParametersAction()

//Assert
var descriptor = ClassUnderTest.TryGetRegistration<IKSqlDBContext>();

descriptor.Should().NotBeNull();
descriptor!.Lifetime.Should().Be(ServiceLifetime.Scoped);
}
Expand All @@ -72,7 +72,7 @@ public void ConfigureKSqlDb_BuildServiceProviderAndResolve()

//Assert
context.Should().NotBeNull();
context?.ContextOptions.QueryStreamParameters[KSqlDbConfigs.ProcessingGuarantee].ToProcessingGuarantee().Should().Be(ProcessingGuarantee.AtLeastOnce);
context?.ContextOptions.QueryStreamParameters.Get<ProcessingGuarantee>(KSqlDbConfigs.ProcessingGuarantee).Should().Be(ProcessingGuarantee.AtLeastOnce);
}

[Test]
Expand All @@ -85,7 +85,7 @@ public void ConfigureKSqlDb_IKSqlDbRestApiClient()

//Assert
var descriptor = ClassUnderTest.TryGetRegistration<IKSqlDbRestApiClient>();

descriptor.Should().NotBeNull();
descriptor!.Lifetime.Should().Be(ServiceLifetime.Scoped);
}
Expand Down Expand Up @@ -113,7 +113,7 @@ public void ConfigureKSqlDb_IHttpClientFactory()

//Assert
var descriptor = ClassUnderTest.TryGetRegistration<IHttpClientFactory>();

descriptor.Should().NotBeNull();
descriptor!.Lifetime.Should().Be(ServiceLifetime.Transient);
}
Expand All @@ -128,7 +128,7 @@ public void ConfigureKSqlDb_KSqlDBContextOptions()

//Assert
var descriptor = ClassUnderTest.TryGetRegistration<KSqlDBContextOptions>();

descriptor.Should().NotBeNull();
descriptor!.Lifetime.Should().Be(ServiceLifetime.Singleton);
}
Expand Down Expand Up @@ -174,7 +174,7 @@ public void AddDbContext_RegisterAsInterface()
//Assert
context.Should().NotBeNull();
}

[Test]
public void AddDbContext_KSqlDBContext_DefaultLifetimeIsScoped()
{
Expand All @@ -188,7 +188,7 @@ public void AddDbContext_KSqlDBContext_DefaultLifetimeIsScoped()
descriptor.Should().NotBeNull();
descriptor!.Lifetime.Should().Be(ServiceLifetime.Scoped);
}

[Test]
public void AddDbContext_KSqlDBContext_ContextLifetimeChangedToTransientScope()
{
Expand Down Expand Up @@ -239,9 +239,9 @@ public void AddDbContext_RestApiLifetimeChangedToTransientScope()
descriptor.Should().NotBeNull();
descriptor!.Lifetime.Should().Be(ServiceLifetime.Transient);
}

#endregion

#region ContextFactory

[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Text.Json.Serialization;
using FluentAssertions;
using ksqlDb.RestApi.Client.Infrastructure.Attributes;
using NUnit.Framework;

namespace ksqlDb.RestApi.Client.Tests.Infrastructure.Attributes
{
enum TestEnum
{
A,
B
}

public class JsonSnakeCaseStringEnumConverterAttributeTests
{
[Test]
public void CreatesConverter()
{
// Arrange
var attr = new JsonSnakeCaseStringEnumConverterAttribute<TestEnum>();

// Act
var converter = attr.CreateConverter(typeof(TestEnum));

// Assert
converter.Should().BeOfType<JsonStringEnumConverter<TestEnum>>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void Url_ShouldNotBeEmpty()
//Assert
url.Should().Be(TestParameters.KsqlDbUrl);
}

[Test]
public void NotSetBasicAuthCredentials()
{
Expand All @@ -45,7 +45,7 @@ public void NotSetBasicAuthCredentials()
ClassUnderTest.BasicAuthUserName.Should().BeEmpty();
ClassUnderTest.BasicAuthPassword.Should().BeEmpty();
}

[Test]
public void SetBasicAuthCredentials()
{
Expand Down Expand Up @@ -73,7 +73,7 @@ public void SetProcessingGuarantee_WasNotSet()

//Assert
Assert.ThrowsException<KeyNotFoundException>(() =>
ClassUnderTest.QueryStreamParameters[parameterName].Should().BeEmpty());
ClassUnderTest.QueryStreamParameters.Get<ProcessingGuarantee>(parameterName));
}

[Test]
Expand All @@ -87,9 +87,7 @@ public void SetProcessingGuarantee_SetToAtLeastOnce()
ClassUnderTest.SetProcessingGuarantee(processingGuarantee);

//Assert
string expectedValue = "at_least_once";

ClassUnderTest.QueryStreamParameters[parameterName].Should().Be(expectedValue);
ClassUnderTest.QueryStreamParameters.Get<ProcessingGuarantee>(parameterName).Should().Be(processingGuarantee);
}

[Test]
Expand All @@ -103,9 +101,7 @@ public void SetProcessingGuarantee_SetToExactlyOnce()
ClassUnderTest.SetProcessingGuarantee(processingGuarantee);

//Assert
string expectedValue = "exactly_once";

ClassUnderTest.QueryStreamParameters[parameterName].Should().Be(expectedValue);
ClassUnderTest.QueryStreamParameters.Get<ProcessingGuarantee>(parameterName).Should().Be(processingGuarantee);
}

[Test]
Expand All @@ -119,9 +115,7 @@ public void SetProcessingGuarantee_SetToExactlyOnceV2()
ClassUnderTest.SetProcessingGuarantee(processingGuarantee);

//Assert
string expectedValue = "exactly_once_v2";

ClassUnderTest.QueryStreamParameters[parameterName].Should().Be(expectedValue);
ClassUnderTest.QueryStreamParameters.Get<ProcessingGuarantee>(parameterName).Should().Be(processingGuarantee);
}

[Test]
Expand All @@ -134,12 +128,10 @@ public void SetAutoOffsetReset()
ClassUnderTest.SetAutoOffsetReset(autoOffsetReset);

//Assert
string expectedValue = autoOffsetReset.ToString().ToLower();

ClassUnderTest.QueryStreamParameters[QueryStreamParameters.AutoOffsetResetPropertyName].Should().Be(expectedValue);
ClassUnderTest.QueryStreamParameters.Get<AutoOffsetReset>(QueryStreamParameters.AutoOffsetResetPropertyName).Should().Be(autoOffsetReset);
}


[Test]
public void JsonSerializerOptions()
{
Expand Down
Loading