Skip to content

Commit

Permalink
[ksqlDB.RestApi.Client]: HasColumnName to Fluent API unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasfabian committed May 31, 2024
1 parent 37845cc commit db09da2
Show file tree
Hide file tree
Showing 14 changed files with 391 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ public void Property_IgnoreField()
entityMetadata!.FieldsMetadata.First(c => c.MemberInfo.Name == nameof(Payment.Description)).Ignore.Should().BeTrue();
}

[Test]
public void Property_HasColumnName()
{
//Arrange
var columnName = "desc";

//Act
var fieldTypeBuilder = builder.Entity<Payment>()
.Property(b => b.Description)
.HasColumnName(columnName);

//Assert
fieldTypeBuilder.Should().NotBeNull();
var entityMetadata = builder.GetEntities().FirstOrDefault(c => c.Type == typeof(Payment));
entityMetadata.Should().NotBeNull();
entityMetadata!.FieldsMetadata.First(c => c.MemberInfo.Name == nameof(Payment.Description)).ColumnName.Should().Be(columnName);
}

[Test]
public void MultiplePropertiesForSameType()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text;
using System.Text.Json.Serialization;
using FluentAssertions;
using ksqlDb.RestApi.Client.FluentAPI.Builders;
using ksqlDB.RestApi.Client.Infrastructure.Extensions;
using ksqlDB.RestApi.Client.KSql.Linq;
using ksqlDB.RestApi.Client.KSql.Query;
Expand Down Expand Up @@ -373,7 +374,7 @@ public void GetMemberName()
var member = type.GetProperty(nameof(MySensor.Title));

//Act
var memberName = member!.GetMemberName();
var memberName = member!.GetMemberName(new ModelBuilder());

//Assert
memberName.Should().Be(nameof(MySensor.Title));
Expand All @@ -388,9 +389,31 @@ public void GetMemberName_JsonPropertyNameOverride()
//Act
var member = type.GetProperty(nameof(MySensor.SensorId2));

var memberName = member!.GetMemberName();
var memberName = member!.GetMemberName(new ModelBuilder());

//Assert
memberName.Should().Be("SensorId");
}

[Test]
public void GetMemberName_ModelBuilderHasColumnName()
{
//Arrange
var columnName = "Id";

var modelBuilder = new ModelBuilder();
modelBuilder.Entity<MySensor>()
.Property(c => c.SensorId2)
.HasColumnName(columnName);

var type = typeof(MySensor);

//Act
var member = type.GetProperty(nameof(MySensor.SensorId2));

var memberName = member!.GetMemberName(modelBuilder);

//Assert
memberName.Should().Be(columnName);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using ksqlDb.RestApi.Client.FluentAPI.Builders;
using ksqlDB.RestApi.Client.KSql.Linq;
using ksqlDB.RestApi.Client.KSql.Query.Context;
using ksqlDB.RestApi.Client.KSql.Query.Windows;
Expand Down Expand Up @@ -507,6 +508,12 @@ public TestableDbProvider(string ksqlDbUrl, string httpResponse)
});
}

public TestableDbProvider(KSqlDBContextOptions contextOptions, ModelBuilder modelBuilder)
: base(contextOptions, modelBuilder)
{
RegisterKSqlQueryGenerator = false;
}

public TestableDbProvider(KSqlDBContextOptions contextOptions)
: base(contextOptions)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using ksqlDb.RestApi.Client.FluentAPI.Builders;
using ksqlDB.RestApi.Client.KSql.Query;
using ksqlDB.RestApi.Client.KSql.Query.Context;
using ksqlDB.RestApi.Client.KSql.RestApi;
Expand All @@ -8,12 +9,20 @@ namespace ksqlDb.RestApi.Client.Tests.KSql.Query.Context;

public class TestableDbProvider<TValue> : KSqlDBContext
{
public TestableDbProvider(string ksqlDbUrl) : base(ksqlDbUrl)
public TestableDbProvider(string ksqlDbUrl)
: base(ksqlDbUrl)
{
InitMocks();
}

public TestableDbProvider(KSqlDBContextOptions contextOptions) : base(contextOptions)
public TestableDbProvider(KSqlDBContextOptions contextOptions)
: base(contextOptions)
{
InitMocks();
}

public TestableDbProvider(KSqlDBContextOptions contextOptions, ModelBuilder modelBuilder)
: base(contextOptions, modelBuilder)
{
InitMocks();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text.Json.Serialization;
using FluentAssertions;
using ksqlDb.RestApi.Client.FluentAPI.Builders;
using ksqlDB.RestApi.Client.KSql.Linq;
using ksqlDB.RestApi.Client.KSql.Query;
using ksqlDB.RestApi.Client.KSql.Query.Context;
Expand All @@ -19,7 +20,7 @@ namespace ksqlDb.RestApi.Client.Tests.KSql.Query;

#pragma warning disable CA1861

public class KSqlQueryLanguageVisitorTests : TestBase
public class KSqlQueryGeneratorTests : TestBase
{
private KSqlQueryGenerator ClassUnderTest { get; set; } = null!;

Expand Down Expand Up @@ -87,6 +88,77 @@ public void BuildKSql_SelectPropertyWithJsonPropertyNameAttribute()
ksql.Should().BeEquivalentTo(expectedKsql.ReplaceLineEndings());
}

[Test]
public void BuildKSql_ModelBuilder_HasColumnNameOverride()
{
//Arrange
string idColumnName = "Id";
ModelBuilder modelBuilder = new ModelBuilder();
modelBuilder.Entity<MySensor>()
.Property(c => c.SensorId2)
.HasColumnName(idColumnName);

var query = new TestableDbProvider(contextOptions, modelBuilder)
.CreatePushQuery<MySensor>()
.Where(c => c.SensorId2 == "1")
.Select(c => c.SensorId2);

queryContext = new QueryContext()
{
ModelBuilder = modelBuilder
};

//Act
var ksql = ClassUnderTest.BuildKSql(query.Expression, queryContext);

//Assert
string expectedKsql =
@$"SELECT {idColumnName} FROM {nameof(MySensor)}s
WHERE {idColumnName} = '1' EMIT CHANGES;";

ksql.Should().BeEquivalentTo(expectedKsql.ReplaceLineEndings());
}

private class Base
{
public int Id { get; set; }
}
private class Derived : Base
{
public string Description { get; set; }

Check warning on line 128 in Tests/ksqlDB.RestApi.Client.Tests/KSql/Query/KSqlQueryGeneratorTests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Description' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 128 in Tests/ksqlDB.RestApi.Client.Tests/KSql/Query/KSqlQueryGeneratorTests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Description' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 128 in Tests/ksqlDB.RestApi.Client.Tests/KSql/Query/KSqlQueryGeneratorTests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Description' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}

[Test]
public void BuildKSql_ModelBuilder_HasColumnNameOverride_ForPropertyInBaseClass()
{
//Arrange
string idColumnName = "SensorId";
ModelBuilder modelBuilder = new ModelBuilder();
modelBuilder.Entity<Derived>()
.Property(c => c.Id)
.HasColumnName(idColumnName);

var query = new TestableDbProvider(contextOptions, modelBuilder)
.CreatePushQuery<Derived>()
.Where(c => c.Id == 1)
.Select(c => c.Id);

queryContext = new QueryContext()
{
ModelBuilder = modelBuilder
};

//Act
var ksql = ClassUnderTest.BuildKSql(query.Expression, queryContext);

//Assert
string expectedKsql =
@$"SELECT {idColumnName} FROM {nameof(Derived)}s
WHERE {idColumnName} = 1 EMIT CHANGES;";

ksql.Should().BeEquivalentTo(expectedKsql.ReplaceLineEndings());
}

[Test]
public void BuildKSql_SelectFromJoinPropertyWithJsonPropertyNameAttribute()
{
Expand Down
37 changes: 34 additions & 3 deletions Tests/ksqlDB.RestApi.Client.Tests/KSql/Query/KSqlVisitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,13 @@ public void MemberAccess_BuildKSql_PrintsNameOfTheProperty()
public void Predicate_BuildKSql_PrintsOperatorAndOperands()
{
//Arrange
Expression<Func<Location, bool>> predicate = l => l.Latitude != "ahoj svet";
Expression<Func<Location, bool>> predicate = l => l.Latitude != "40.71427000";

//Act
var query = ClassUnderTest.BuildKSql(predicate);

//Assert
query.Should().BeEquivalentTo($"{nameof(Location.Latitude)} != 'ahoj svet'");
query.Should().BeEquivalentTo($"{nameof(Location.Latitude)} != '40.71427000'");
}

private record Update
Expand All @@ -364,7 +364,7 @@ public void Field_BuildKSql_PrintsFieldName()
public void PredicateCompareWithVariable_BuildKSql_PrintsOperatorAndOperands()
{
//Arrange
string value = "ahoj svet";
string value = "40.71427000";

Expression<Func<Location, bool>> predicate = l => l.Latitude != value;

Expand Down Expand Up @@ -450,6 +450,37 @@ public void PredicateDeeplyNestedArrayProperty_BuildKSql_PrintsAllFields()
query.Should().BeEquivalentTo("ARRAY_LENGTH(After->Model->Capabilities) > 0");
}

[Test]
public void MemberAccess_BuildKSql_JsonPropertyName()
{
//Arrange
Expression<Func<Tweet, string>> predicate = l => l.Message;

//Act
var query = ClassUnderTest.BuildKSql(predicate);

//Assert
query.Should().BeEquivalentTo($"{nameof(Tweet.Message).ToUpper()}");
}

[Test]
public void MemberAccess_BuildKSql_ModelBuilderHasColumnName()
{
//Arrange
var amountColumnName = "amount";
modelBuilder.Entity<Tweet>()
.Property(c => c.Amount)
.HasColumnName(amountColumnName);

Expression<Func<Tweet, double>> predicate = l => l.Amount;

//Act
var query = ClassUnderTest.BuildKSql(predicate);

//Assert
query.Should().BeEquivalentTo(amountColumnName);
}

#endregion

#region Generics
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using ksqlDb.RestApi.Client.FluentAPI.Builders;
using ksqlDB.RestApi.Client.KSql.Linq;
using ksqlDB.RestApi.Client.KSql.Query.Context;
using ksqlDB.RestApi.Client.KSql.Query.Functions;
Expand All @@ -24,7 +25,7 @@ public override void TestInitialize()
base.TestInitialize();

var contextOptions = new KSqlDBContextOptions(TestParameters.KsqlDbUrl);
KSqlDbContext = new KSqlDBContext(contextOptions);
KSqlDbContext = new KSqlDBContext(contextOptions, new ModelBuilder());
}

private static string MovieAlias => "movie";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private string GetExpectedClauses(bool isTable)
return @$" MyMovies (
Id INT{keyClause} KEY,
Title VARCHAR,
Release_Year INT,
ReleaseYear INT,
NumberOfDays ARRAY<INT>,
Dictionary MAP<VARCHAR, INT>,
Dictionary2 MAP<VARCHAR, INT>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json.Serialization;
using FluentAssertions;
using ksqlDb.RestApi.Client.FluentAPI.Builders;
using ksqlDB.RestApi.Client.KSql.RestApi.Enums;
Expand All @@ -10,7 +11,13 @@ namespace ksqlDb.RestApi.Client.Tests.KSql.RestApi.Generators;

public class TypeGeneratorTests
{
private readonly ModelBuilder modelBuilder = new();
private ModelBuilder modelBuilder;

Check warning on line 14 in Tests/ksqlDB.RestApi.Client.Tests/KSql/RestApi/Generators/TypeGeneratorTests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'modelBuilder' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 14 in Tests/ksqlDB.RestApi.Client.Tests/KSql/RestApi/Generators/TypeGeneratorTests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'modelBuilder' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 14 in Tests/ksqlDB.RestApi.Client.Tests/KSql/RestApi/Generators/TypeGeneratorTests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'modelBuilder' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

[SetUp]
public void TestInitialize()
{
modelBuilder = new();
}

[Test]
public void CreateType()
Expand All @@ -25,6 +32,42 @@ public void CreateType()
.Be($"CREATE TYPE {nameof(Address)} AS STRUCT<Number INT, Street VARCHAR, City VARCHAR>;");
}

private record Test
{
[JsonPropertyName("Id")]
public int Override { get; set; }
}

[Test]
public void CreateType_JsonPropertyName()
{
//Arrange

//Act
var statement = new TypeGenerator(modelBuilder).Print<Test>(new TypeProperties());

//Assert
statement.Should()
.Be($"CREATE TYPE {nameof(Test)} AS STRUCT<Id INT>;");
}

[Test]
public void CreateType_ModelBuilder_HasColumnName()
{
//Arrange
string columnName = "No";
modelBuilder.Entity<Address>()
.Property(b => b.Number)
.HasColumnName(columnName);

//Act
var statement = new TypeGenerator(modelBuilder).Print<Address>(new TypeProperties());

//Assert
statement.Should()
.Be($"CREATE TYPE {nameof(Address)} AS STRUCT<{columnName} INT, Street VARCHAR, City VARCHAR>;");
}

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

0 comments on commit db09da2

Please sign in to comment.