Skip to content

Commit 26e29b2

Browse files
committed
[ksqlDB.RestApi.Client]: added ModelBuilder integration tests
1 parent 0742222 commit 26e29b2

File tree

2 files changed

+144
-2
lines changed

2 files changed

+144
-2
lines changed

Tests/ksqlDB.RestApi.Client.IntegrationTests/Infrastructure/IntegrationTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using ksqlDb.RestApi.Client.FluentAPI.Builders;
12
using ksqlDb.RestApi.Client.IntegrationTests.KSql.RestApi;
23
using ksqlDB.RestApi.Client.KSql.Query.Context;
34
using ksqlDB.RestApi.Client.KSql.Query.Options;
@@ -32,15 +33,16 @@ public override void TestInitialize()
3233
Context = CreateKSqlDbContext(EndpointType.QueryStream);
3334
}
3435

35-
protected KSqlDBContext CreateKSqlDbContext(EndpointType endpointType)
36+
protected KSqlDBContext CreateKSqlDbContext(EndpointType endpointType, ModelBuilder? modelBuilder = null)
3637
{
3738
ContextOptions = new KSqlDBContextOptions(KSqlDbRestApiProvider.KsqlDbUrl)
3839
{
3940
ShouldPluralizeFromItemName = false,
4041
EndpointType = endpointType
4142
};
4243

43-
return new KSqlDBContext(ContextOptions);
44+
modelBuilder ??= new ModelBuilder();
45+
return new KSqlDBContext(ContextOptions, modelBuilder);
4446
}
4547

4648
[TestCleanup]
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using System.Text.Json.Serialization;
2+
using FluentAssertions;
3+
using ksqlDb.RestApi.Client.FluentAPI.Builders;
4+
using ksqlDb.RestApi.Client.IntegrationTests.Helpers;
5+
using ksqlDb.RestApi.Client.IntegrationTests.Models;
6+
using ksqlDB.RestApi.Client.KSql.Linq;
7+
using ksqlDB.RestApi.Client.KSql.RestApi;
8+
using ksqlDB.RestApi.Client.KSql.RestApi.Enums;
9+
using ksqlDB.RestApi.Client.KSql.RestApi.Http;
10+
using ksqlDB.RestApi.Client.KSql.RestApi.Statements;
11+
using ksqlDB.RestApi.Client.KSql.RestApi.Statements.Properties;
12+
using Microsoft.VisualStudio.TestTools.UnitTesting;
13+
using NUnit.Framework;
14+
15+
namespace ksqlDb.RestApi.Client.IntegrationTests.KSql.Linq
16+
{
17+
public class ModelBuilderTests : Infrastructure.IntegrationTests
18+
{
19+
protected static string StreamName = nameof(ModelBuilderTests);
20+
private static readonly string TopicName = StreamName;
21+
private static KSqlDbRestApiClient kSqlDbRestApiClient = null!;
22+
private static ModelBuilder modelBuilder = null!;
23+
24+
[OneTimeSetUp]
25+
public static async Task ClassInitialize()
26+
{
27+
await InitializeDatabase();
28+
}
29+
30+
public record Tweet : Record
31+
{
32+
public int Id { get; set; }
33+
[JsonPropertyName("MESSAGE")]
34+
public string Message { get; set; } = null!;
35+
public bool IsRobot { get; set; }
36+
public double Amount { get; set; }
37+
public decimal AccountBalance { get; set; }
38+
}
39+
40+
public static readonly Tweet Tweet1 = new()
41+
{
42+
Id = 1,
43+
Message = "Hello world",
44+
IsRobot = true,
45+
Amount = 0.00042,
46+
};
47+
48+
public static readonly Tweet Tweet2 = new()
49+
{
50+
Id = 2,
51+
Message = "Wall-e",
52+
IsRobot = false,
53+
Amount = 1,
54+
};
55+
56+
protected static async Task InitializeDatabase()
57+
{
58+
modelBuilder = new ModelBuilder();
59+
modelBuilder.Entity<Tweet>()
60+
.Property(c => c.Id)
61+
.HasColumnName("TweetId");
62+
modelBuilder.Entity<Tweet>()
63+
.Property(c => c.AccountBalance)
64+
.Ignore();
65+
66+
var httpClient = new HttpClient
67+
{
68+
BaseAddress = new Uri(TestConfig.KSqlDbUrl)
69+
};
70+
kSqlDbRestApiClient = new KSqlDbRestApiClient(new HttpClientFactory(httpClient), modelBuilder);
71+
72+
var entityCreationMetadata = new EntityCreationMetadata(TopicName, 1)
73+
{
74+
EntityName = StreamName,
75+
ShouldPluralizeEntityName = false,
76+
IdentifierEscaping = IdentifierEscaping.Always
77+
};
78+
var result = await kSqlDbRestApiClient.CreateStreamAsync<Tweet>(entityCreationMetadata, true);
79+
result.IsSuccess().Should().BeTrue();
80+
81+
var insertProperties = new InsertProperties()
82+
{
83+
EntityName = StreamName,
84+
IdentifierEscaping = IdentifierEscaping.Always
85+
};
86+
result = await kSqlDbRestApiClient.InsertIntoAsync(Tweet1, insertProperties);
87+
result.IsSuccess().Should().BeTrue();
88+
89+
result = await kSqlDbRestApiClient.InsertIntoAsync(Tweet2, insertProperties);
90+
result.IsSuccess().Should().BeTrue();
91+
}
92+
93+
[OneTimeTearDown]
94+
public static async Task ClassCleanup()
95+
{
96+
var dropFromItemProperties = new DropFromItemProperties
97+
{
98+
IdentifierEscaping = IdentifierEscaping.Always,
99+
ShouldPluralizeEntityName = false,
100+
EntityName = StreamName,
101+
UseIfExistsClause = true,
102+
DeleteTopic = true,
103+
};
104+
await kSqlDbRestApiClient.DropStreamAsync<Models.Tweet>(dropFromItemProperties);
105+
}
106+
107+
[SetUp]
108+
public override void TestInitialize()
109+
{
110+
base.TestInitialize();
111+
112+
Context = CreateKSqlDbContext(ksqlDB.RestApi.Client.KSql.Query.Options.EndpointType.QueryStream, modelBuilder);
113+
}
114+
115+
protected virtual IQbservable<Tweet> QuerySource =>
116+
Context.CreatePushQuery<Tweet>($"`{StreamName}`");
117+
118+
[Test]
119+
public async Task Select()
120+
{
121+
//Arrange
122+
int expectedItemsCount = 2;
123+
124+
var source = QuerySource
125+
.ToAsyncEnumerable();
126+
127+
//Act
128+
var actualValues = await CollectActualValues(source, expectedItemsCount);
129+
130+
//Assert
131+
var expectedValues = new List<Tweet>
132+
{
133+
Tweet1, Tweet2
134+
};
135+
136+
expectedItemsCount.Should().Be(actualValues.Count);
137+
CollectionAssert.AreEqual(expectedValues, actualValues);
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)