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 end-to-end tests via GitHub Actions #1

Closed
wants to merge 8 commits into from
Closed
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
27 changes: 27 additions & 0 deletions .github/workflows/e2eTests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: End-to-End Tests

on: [push, pull_request]

jobs:
e2eTests:
runs-on: ubuntu-latest
name: End-to-End Tests
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v4

- name: Set up Docker
uses: docker/setup-buildx-action@v2

- name: Start local Kusto cluster
run: |
docker run -e ACCEPT_EULA=Y -m 4G -d -p 8080:8080 -t mcr.microsoft.com/azuredataexplorer/kustainer-linux:latest

- name: Set E2E_TESTING environment variables
run: echo "E2E_TESTING=1" >> $GITHUB_ENV

- name: Run end-to-end tests
run: dotnet test ./CSharp.OpenSource.LinqToKql.Test
31 changes: 17 additions & 14 deletions .github/workflows/unitTests.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
name: Unit Tests

on: [push, pull_request]

jobs:
UnitTests:
runs-on: ubuntu-latest
name: Unit Tests
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Run unit tests
run: dotnet test ./CSharp.OpenSource.LinqToKql.Test
name: Unit Tests

on: [push, pull_request]

jobs:
UnitTests:
runs-on: ubuntu-latest
name: Unit Tests
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set E2E_TESTING environment variable
run: echo "E2E_TESTING=0" >> $GITHUB_ENV

- name: Run unit tests
run: dotnet test ./CSharp.OpenSource.LinqToKql.Test
50 changes: 50 additions & 0 deletions CSharp.OpenSource.LinqToKql.Test/EndToEndTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using CSharp.OpenSource.LinqToKql.Http;
using CSharp.OpenSource.LinqToKql.Provider;
using Microsoft.Extensions.DependencyInjection;

namespace CSharp.OpenSource.LinqToKql.Test;

public class EndToEndTests
{
private string _cluster = "https://localhost:8080";
private string _auth = "myBearerToken";
private string _defaultDbName = "myDatabaseName";

[Fact]
public async Task VerifyKqlValidityOnServer()
{
var e2eTesting = Environment.GetEnvironmentVariable("E2E_TESTING");
if (e2eTesting == "1")
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddKustoDbContext<MyDbContext, ILinqToKqlProviderExecutor>(sp => new KustoHttpClient(_cluster, _auth, _defaultDbName));

Check failure on line 20 in CSharp.OpenSource.LinqToKql.Test/EndToEndTests.cs

View workflow job for this annotation

GitHub Actions / End-to-End Tests

'ServiceCollection' does not contain a definition for 'AddKustoDbContext' and no accessible extension method 'AddKustoDbContext' accepting a first argument of type 'ServiceCollection' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 20 in CSharp.OpenSource.LinqToKql.Test/EndToEndTests.cs

View workflow job for this annotation

GitHub Actions / End-to-End Tests

'ServiceCollection' does not contain a definition for 'AddKustoDbContext' and no accessible extension method 'AddKustoDbContext' accepting a first argument of type 'ServiceCollection' could be found (are you missing a using directive or an assembly reference?)
var sp = serviceCollection.BuildServiceProvider();
var dbContext = sp.GetRequiredService<MyDbContext>();

var kql = "SampleKqlQuery";
var result = await dbContext.ExecuteKqlAsync(kql);

Assert.NotNull(result);
Assert.True(result.IsSuccess);
}
}
}

public class MyDbContext : KustoDbContext
{
public MyDbContext(IKustoDbContextExecutor<MyDbContext> executor) : base(executor)
{
}

public async Task<KqlResult> ExecuteKqlAsync(string kql)
{
var providerExecutor = (KustoHttpClient)ProviderExecutor;
var result = await providerExecutor.QueryAsync<KqlResult>(kql);
return result;
}
}

public class KqlResult
{
public bool IsSuccess { get; set; }
}
43 changes: 43 additions & 0 deletions CSharp.OpenSource.LinqToKql.Test/ORMGen/ORMGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,47 @@ public void Match_ShouldReturnCorrectMatchResult()
Assert.True(isMatch2);
Assert.False(isMatch3);
}

[Fact]
public async Task VerifyKqlValidityOnServer()
{
var e2eTesting = Environment.GetEnvironmentVariable("E2E_TESTING");
if (e2eTesting == "1")
{
SetupProviderExecutor();
var providerExecutor = _mockExecutor.Object;
var ormGenerator = new ORMGenerator(new()
{
ProviderExecutor = providerExecutor,
ModelsFolderPath = "Models",
DbContextFolderPath = "../../../../Samples/ORMGeneratorTest/AutoGen",
DbContextName = "AutoGenORMKustoDbContext",
Namespace = "AutoGen",
ModelsNamespace = "AutoGen",
DbContextNamespace = "AutoGen",
CreateDbContext = true,
CleanFolderBeforeCreate = true,
EnableNullable = true,
FileScopedNamespaces = true,
DatabaseConfigs = new List<ORMGeneratorDatabaseConfig>
{
new ORMGeneratorDatabaseConfig
{
DatabaseName = DbName1,
Filters = new ORMGeneratorFilterConfig()
},
new ORMGeneratorDatabaseConfig
{
DatabaseName = DbName2,
DatabaseDisplayName = "db2",
Filters = new ORMGeneratorFilterConfig()
}
}
});
await ormGenerator.GenerateAsync();

Assert.True(File.Exists(ormGenerator.Config.DbContextFilePath));
Assert.True(Directory.GetFiles(ormGenerator.Config.ModelsFolderPath, "*.cs", SearchOption.AllDirectories).Any());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,44 @@ public void Translate_ShouldHandleGroupByWithObject2(bool disableNestedProjectio
[_tableName, "where Id == 1", "summarize Key=take_any(Date), Count=count() by Date"],
new() { DisableNestedProjection = disableNestedProjection, }
);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void Translate_ShouldHandleGroupByWithSum(bool disableNestedProjection)
=> AssertQuery(
_q.GroupBy(x => x.Date).Select(g => new { Date = g.Key, Sum = g.Sum(x => x.Value) }),
[_tableName, "summarize Sum=sum(Value) by Date"],
new() { DisableNestedProjection = disableNestedProjection, }
);

[Theory]
[InlineData(false)]
[InlineData(true)]
public void Translate_ShouldHandleGroupByWithAverage(bool disableNestedProjection)
=> AssertQuery(
_q.GroupBy(x => x.Date).Select(g => new { Date = g.Key, Average = g.Average(x => x.Value) }),
[_tableName, "summarize Average=avg(Value) by Date"],
new() { DisableNestedProjection = disableNestedProjection, }
);

[Theory]
[InlineData(false)]
[InlineData(true)]
public void Translate_ShouldHandleGroupByWithMin(bool disableNestedProjection)
=> AssertQuery(
_q.GroupBy(x => x.Date).Select(g => new { Date = g.Key, Min = g.Min(x => x.Value) }),
[_tableName, "summarize Min=min(Value) by Date"],
new() { DisableNestedProjection = disableNestedProjection, }
);

[Theory]
[InlineData(false)]
[InlineData(true)]
public void Translate_ShouldHandleGroupByWithMax(bool disableNestedProjection)
=> AssertQuery(
_q.GroupBy(x => x.Date).Select(g => new { Date = g.Key, Max = g.Max(x => x.Value) }),
[_tableName, "summarize Max=max(Value) by Date"],
new() { DisableNestedProjection = disableNestedProjection, }
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,18 @@ public void Translate_ShouldHandleOrderByDesc()
_q.OrderByDescending(x => x.Date),
[_tableName, "sort by Date desc"]
);
}

[Fact]
public void Translate_ShouldHandleThenBy()
=> AssertQuery(
_q.OrderBy(x => x.Date).ThenBy(x => x.Id),
[_tableName, "sort by Date asc", "sort by Id asc"]
);

[Fact]
public void Translate_ShouldHandleThenByDesc()
=> AssertQuery(
_q.OrderBy(x => x.Date).ThenByDescending(x => x.Id),
[_tableName, "sort by Date asc", "sort by Id desc"]
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,32 @@
_q.Select(x => new SampleObject2 { Name2 = x.Name, Id2 = x.Id }),
[_tableName, "project Name2=Name, Id2=Id"]
);
}

[Fact]
public void Translate_ShouldHandleFirst()
=> AssertQuery(

Check failure on line 16 in CSharp.OpenSource.LinqToKql.Test/Translator/SelectTranslatorTests.cs

View workflow job for this annotation

GitHub Actions / End-to-End Tests

The type arguments for method 'LinqToKQLQueryTranslatorBaseTest.AssertQuery<T>(IQueryable<T>, string[], LinqToKQLQueryTranslatorConfig?)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Check failure on line 16 in CSharp.OpenSource.LinqToKql.Test/Translator/SelectTranslatorTests.cs

View workflow job for this annotation

GitHub Actions / End-to-End Tests

The type arguments for method 'LinqToKQLQueryTranslatorBaseTest.AssertQuery<T>(IQueryable<T>, string[], LinqToKQLQueryTranslatorConfig?)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
_q.First(),
[_tableName, "take 1"]
);

[Fact]
public void Translate_ShouldHandleFirstOrDefault()
=> AssertQuery(

Check failure on line 23 in CSharp.OpenSource.LinqToKql.Test/Translator/SelectTranslatorTests.cs

View workflow job for this annotation

GitHub Actions / End-to-End Tests

The type arguments for method 'LinqToKQLQueryTranslatorBaseTest.AssertQuery<T>(IQueryable<T>, string[], LinqToKQLQueryTranslatorConfig?)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Check failure on line 23 in CSharp.OpenSource.LinqToKql.Test/Translator/SelectTranslatorTests.cs

View workflow job for this annotation

GitHub Actions / End-to-End Tests

The type arguments for method 'LinqToKQLQueryTranslatorBaseTest.AssertQuery<T>(IQueryable<T>, string[], LinqToKQLQueryTranslatorConfig?)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
_q.FirstOrDefault(),
[_tableName, "take 1"]
);

[Fact]
public void Translate_ShouldHandleLast()
=> AssertQuery(

Check failure on line 30 in CSharp.OpenSource.LinqToKql.Test/Translator/SelectTranslatorTests.cs

View workflow job for this annotation

GitHub Actions / End-to-End Tests

The type arguments for method 'LinqToKQLQueryTranslatorBaseTest.AssertQuery<T>(IQueryable<T>, string[], LinqToKQLQueryTranslatorConfig?)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Check failure on line 30 in CSharp.OpenSource.LinqToKql.Test/Translator/SelectTranslatorTests.cs

View workflow job for this annotation

GitHub Actions / End-to-End Tests

The type arguments for method 'LinqToKQLQueryTranslatorBaseTest.AssertQuery<T>(IQueryable<T>, string[], LinqToKQLQueryTranslatorConfig?)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
_q.OrderBy(x => x.Id).Last(),
[_tableName, "sort by Id asc", "take 1"]
);

[Fact]
public void Translate_ShouldHandleLastOrDefault()
=> AssertQuery(

Check failure on line 37 in CSharp.OpenSource.LinqToKql.Test/Translator/SelectTranslatorTests.cs

View workflow job for this annotation

GitHub Actions / End-to-End Tests

The type arguments for method 'LinqToKQLQueryTranslatorBaseTest.AssertQuery<T>(IQueryable<T>, string[], LinqToKQLQueryTranslatorConfig?)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Check failure on line 37 in CSharp.OpenSource.LinqToKql.Test/Translator/SelectTranslatorTests.cs

View workflow job for this annotation

GitHub Actions / End-to-End Tests

The type arguments for method 'LinqToKQLQueryTranslatorBaseTest.AssertQuery<T>(IQueryable<T>, string[], LinqToKQLQueryTranslatorConfig?)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
_q.OrderBy(x => x.Id).LastOrDefault(),
[_tableName, "sort by Id asc", "take 1"]
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ public void Translate_ShouldHandleTake()
_q.Take(50),
[_tableName, "take 50"]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ namespace CSharp.OpenSource.LinqToKql.Translator.Builders;

public class OrderByLinqToKQLTranslator : LinqToKQLTranslatorBase
{
public OrderByLinqToKQLTranslator(LinqToKQLQueryTranslatorConfig config) : base(config, new() { nameof(Enumerable.OrderBy), nameof(Enumerable.OrderByDescending) })
public OrderByLinqToKQLTranslator(LinqToKQLQueryTranslatorConfig config) : base(config, new() { nameof(Enumerable.OrderBy), nameof(Enumerable.OrderByDescending), nameof(Enumerable.ThenBy), nameof(Enumerable.ThenByDescending) })
{
}

public override string Handle(MethodCallExpression methodCall, Expression? parent)
{
return Handle(methodCall, methodCall.Method.Name == nameof(Enumerable.OrderByDescending));
return Handle(methodCall, methodCall.Method.Name == nameof(Enumerable.OrderByDescending) || methodCall.Method.Name == nameof(Enumerable.ThenByDescending));
}

public string Handle(MethodCallExpression methodCall, bool descending)
Expand All @@ -20,4 +20,4 @@ public string Handle(MethodCallExpression methodCall, bool descending)
var direction = descending ? "desc" : "asc";
return $"sort by {key} {direction}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ public override string Handle(MethodCallExpression methodCall, Expression? paren
var action = _extendOps.Any(props.Contains) ? "extend" : "project";
return $"{action} {props}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ namespace CSharp.OpenSource.LinqToKql.Translator.Builders
{
public class TaskLinqToKQLTranslator : LinqToKQLTranslatorBase
{
public TaskLinqToKQLTranslator(LinqToKQLQueryTranslatorConfig config) : base(config, new() { nameof(Enumerable.Take), })
public TaskLinqToKQLTranslator(LinqToKQLQueryTranslatorConfig config) : base(config, new() { nameof(Enumerable.Take) })
{
}

public override string Handle(MethodCallExpression methodCall, Expression? parent)
{
var count = ((ConstantExpression)methodCall.Arguments[1]).Value.GetKQLValue();
return $"take {count}";
return methodCall.Method.Name switch
{
nameof(Enumerable.Take) => $"take {count}",
_ => throw new NotSupportedException($"{GetType().Name} - Method {methodCall.Method.Name} is not supported.")
};
}
}
}
}
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: '3.8'

services:
kusto-cluster:
image: microsoft/azure-kusto-emulator:latest
environment:
- ACCEPT_EULA=Y
- CLUSTER_NAME=localKustoCluster
- NODE_NAME=node1
- CLUSTER_TYPE=Dev
ports:
- "8080:8080"
- "8081:8081"
Loading