Skip to content

Commit 0455d00

Browse files
committed
[ksqlDb.RestApi.Client]: added AsStruct to Fluent API unit tests #89
1 parent 5e1a8e3 commit 0455d00

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

Tests/ksqlDB.RestApi.Client.Tests/FluentAPI/Builders/ModelBuilderTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,37 @@ public void Headers()
234234

235235
metadata.HasHeaders.Should().BeTrue();
236236
}
237+
238+
private record KeyValuePair
239+
{
240+
public string Key { get; set; } = null!;
241+
public byte[] Value { get; set; } = null!;
242+
}
243+
244+
private record Record
245+
{
246+
public KeyValuePair[] Headers { get; init; } = null!;
247+
}
248+
249+
[Test]
250+
public void AsStruct()
251+
{
252+
//Arrange
253+
254+
//Act
255+
var fieldTypeBuilder = builder.Entity<Record>()
256+
.Property(b => b.Headers)
257+
.AsStruct();
258+
259+
//Assert
260+
fieldTypeBuilder.Should().NotBeNull();
261+
262+
var entityMetadata = ((IMetadataProvider)builder).GetEntities().FirstOrDefault(c => c.Type == typeof(Record));
263+
entityMetadata.Should().NotBeNull();
264+
265+
var metadata = entityMetadata!.FieldsMetadata.First(c => c.IsStruct && c.Path == "Headers");
266+
metadata.IsStruct.Should().BeTrue();
267+
}
237268
}
238269

239270
internal record Payment

Tests/ksqlDB.RestApi.Client.Tests/KSql/RestApi/Generators/StatementGeneratorTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using FluentAssertions;
2+
using ksqlDb.RestApi.Client.FluentAPI.Builders;
23
using ksqlDB.RestApi.Client.KSql.RestApi.Enums;
34
using ksqlDB.RestApi.Client.KSql.RestApi.Generators;
45
using ksqlDB.RestApi.Client.KSql.RestApi.Serialization;
@@ -162,6 +163,36 @@ public void CreateOrReplaceTableWithEnumProperty()
162163
//Assert
163164
statement.Should().Contain($"{nameof(PortType)} VARCHAR");
164165
}
166+
private record KeyValuePair
167+
{
168+
public string Key { get; set; } = null!;
169+
public byte[] Value { get; set; } = null!;
170+
}
171+
172+
private record Record
173+
{
174+
public KeyValuePair[] Headers { get; init; } = null!;
175+
}
176+
177+
[Test]
178+
public void CreateTable_UseModelBuilder_WithFieldAsStruct()
179+
{
180+
//Arrange
181+
var modelBuilder = new ModelBuilder();
182+
modelBuilder.Entity<Record>()
183+
.Property(b => b.Headers)
184+
.AsStruct();
185+
186+
var creationMetadata = new EntityCreationMetadata("my_topic", partitions: 3);
187+
188+
//Act
189+
var statement = new StatementGenerator(modelBuilder).CreateTable<Record>(creationMetadata, ifNotExists: true);
190+
191+
//Assert
192+
statement.Should().Be(@"CREATE TABLE IF NOT EXISTS Records (
193+
Headers ARRAY<STRUCT<Key VARCHAR, Value BYTES>>
194+
) WITH ( KAFKA_TOPIC='my_topic', VALUE_FORMAT='Json', PARTITIONS='3' );".ReplaceLineEndings());
195+
}
165196
}
166197

167198
internal class Port

Tests/ksqlDB.RestApi.Client.Tests/KSql/RestApi/Statements/KSqlTypeTranslatorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace ksqlDb.RestApi.Client.Tests.KSql.RestApi.Statements
1212
public class KSqlTypeTranslatorTests
1313
{
1414
private ModelBuilder modelBuilder = null!;
15-
private KSqlTypeTranslator kSqlTypeTranslator = null!;
15+
private KSqlTypeTranslator<Poco> kSqlTypeTranslator = null!;
1616

1717
[SetUp]
1818
public void Init()
@@ -396,7 +396,7 @@ public void Translate_UseModelBuilderConfiguration()
396396
.Decimal(10, 2);
397397

398398
//Act
399-
string ksqlType = kSqlTypeTranslator.Translate(type);
399+
string ksqlType = kSqlTypeTranslator.Translate(type, type.GetMember(nameof(Poco.Amount))[0]);
400400

401401
//Assert
402402
ksqlType.Should().Be($"{KSqlTypes.Struct}<{nameof(Poco.Amount)} {KSqlTypes.Decimal}(10,2)>");

0 commit comments

Comments
 (0)