Skip to content

Commit 4a75395

Browse files
Tests spin up container and then spin it down
1 parent 34116ce commit 4a75395

File tree

5 files changed

+146
-29
lines changed

5 files changed

+146
-29
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Xunit.Abstractions;
2+
using Xunit.Sdk;
3+
4+
#pragma warning disable CS8509 // C# compiler doesn't understand nested discriminated unions - use EXHAUSTION001 instead
5+
#pragma warning disable SA1512 // Single-line comments should not be followed by blank line
6+
7+
8+
namespace NucliaDbClient.Tests;
9+
10+
public class PriorityOrderer : ITestCaseOrderer
11+
{
12+
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases)
13+
where TTestCase : ITestCase =>
14+
testCases.OrderBy(tc =>
15+
tc.TestMethod.Method.GetCustomAttributes(
16+
typeof(TestPriorityAttribute).AssemblyQualifiedName!
17+
)
18+
.FirstOrDefault()
19+
?.GetNamedArgument<int>("Priority") ?? 0
20+
);
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma warning disable CS8509 // C# compiler doesn't understand nested discriminated unions - use EXHAUSTION001 instead
2+
#pragma warning disable SA1512 // Single-line comments should not be followed by blank line
3+
4+
5+
namespace NucliaDbClient.Tests;
6+
7+
[AttributeUsage(AttributeTargets.Method)]
8+
public sealed class TestPriorityAttribute : Attribute
9+
{
10+
public int Priority { get; }
11+
12+
public TestPriorityAttribute(int priority) => Priority = priority;
13+
}

Samples/NucliaDbClient.Tests/NucliaDbApiTests.cs

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,24 @@
22
using NucliaDB.Generated;
33
using Outcome;
44
using Xunit;
5-
using Xunit.Abstractions;
6-
using Xunit.Sdk;
75

86
#pragma warning disable CS8509 // C# compiler doesn't understand nested discriminated unions - use EXHAUSTION001 instead
97
#pragma warning disable SA1512 // Single-line comments should not be followed by blank line
108

11-
129
namespace NucliaDbClient.Tests;
1310

1411
[Collection("NucliaDB Tests")]
1512
[TestCaseOrderer("NucliaDbClient.Tests.PriorityOrderer", "NucliaDbClient.Tests")]
1613
public class NucliaDbApiTests
1714
{
15+
#region Setup
1816
private readonly IHttpClientFactory _httpClientFactory;
1917
private static string? _createdResourceId;
2018
private static string? _knowledgeBoxId;
2119

22-
public NucliaDbApiTests()
20+
public NucliaDbApiTests(NucliaDbFixture fixture)
2321
{
22+
_ = fixture; // Ensure fixture is initialized
2423
var services = new ServiceCollection();
2524
services.AddHttpClient();
2625
var serviceProvider = services.BuildServiceProvider();
@@ -72,6 +71,10 @@ private async Task<string> EnsureResourceExists()
7271
return _createdResourceId;
7372
}
7473

74+
#endregion
75+
76+
#region Tests
77+
7578
[Fact]
7679
[TestPriority(-1)]
7780
public async Task CreateKnowledgeBox_CreatesKB()
@@ -234,7 +237,6 @@ public async Task GetResource_ReturnsResource()
234237
Assert.NotNull(resource.Id);
235238
}
236239

237-
#pragma warning disable xUnit1004
238240
[Fact]
239241
[TestPriority(5)]
240242
public async Task ModifyResource_ReturnsResourceUpdated()
@@ -278,7 +280,6 @@ public async Task ModifyResource_ReturnsResourceUpdated()
278280
Assert.NotNull(updated.Seqid);
279281
}
280282

281-
#pragma warning disable xUnit1004
282283
[Fact]
283284
[TestPriority(6)]
284285
public async Task GetKnowledgeBoxCounters_ReturnsCounters()
@@ -307,7 +308,6 @@ public async Task GetKnowledgeBoxCounters_ReturnsCounters()
307308
Assert.True(counters.Resources >= 1, "Should have at least 1 resource");
308309
}
309310

310-
#pragma warning disable xUnit1004
311311
[Fact]
312312
[TestPriority(9)]
313313
public async Task AddTextFieldToResource_ReturnsResourceFieldAdded()
@@ -342,7 +342,6 @@ public async Task AddTextFieldToResource_ReturnsResourceFieldAdded()
342342
Assert.NotNull(fieldAdded.Seqid);
343343
}
344344

345-
#pragma warning disable xUnit1004
346345
[Fact]
347346
[TestPriority(10)]
348347
public async Task DeleteResource_ReturnsUnit()
@@ -373,25 +372,5 @@ public async Task DeleteResource_ReturnsUnit()
373372
// Clear the resource ID since it's been deleted
374373
_createdResourceId = null;
375374
}
376-
}
377-
378-
[AttributeUsage(AttributeTargets.Method)]
379-
public sealed class TestPriorityAttribute : Attribute
380-
{
381-
public int Priority { get; }
382-
383-
public TestPriorityAttribute(int priority) => Priority = priority;
384-
}
385-
386-
public class PriorityOrderer : ITestCaseOrderer
387-
{
388-
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases)
389-
where TTestCase : ITestCase =>
390-
testCases.OrderBy(tc =>
391-
tc.TestMethod.Method.GetCustomAttributes(
392-
typeof(TestPriorityAttribute).AssemblyQualifiedName!
393-
)
394-
.FirstOrDefault()
395-
?.GetNamedArgument<int>("Priority") ?? 0
396-
);
375+
#endregion
397376
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System.Diagnostics;
2+
3+
#pragma warning disable CS8509 // C# compiler doesn't understand nested discriminated unions - use EXHAUSTION001 instead
4+
#pragma warning disable SA1512 // Single-line comments should not be followed by blank line
5+
6+
7+
namespace NucliaDbClient.Tests;
8+
9+
public sealed class NucliaDbFixture : IDisposable
10+
{
11+
private static readonly string DockerComposeDir = Path.Combine(
12+
Directory.GetCurrentDirectory(),
13+
"..",
14+
"..",
15+
"..",
16+
"..",
17+
"NucliaDbClient"
18+
);
19+
20+
public NucliaDbFixture()
21+
{
22+
CleanDockerEnvironment();
23+
StartDockerEnvironment();
24+
WaitForNucliaDbReady();
25+
}
26+
27+
private static void CleanDockerEnvironment()
28+
{
29+
var startInfo = new ProcessStartInfo
30+
{
31+
FileName = "docker",
32+
Arguments = "compose down -v --remove-orphans",
33+
WorkingDirectory = DockerComposeDir,
34+
RedirectStandardOutput = true,
35+
RedirectStandardError = true,
36+
UseShellExecute = false,
37+
};
38+
39+
using var process = Process.Start(startInfo);
40+
process?.WaitForExit();
41+
}
42+
43+
private static void StartDockerEnvironment()
44+
{
45+
var startInfo = new ProcessStartInfo
46+
{
47+
FileName = "docker",
48+
Arguments = "compose up -d",
49+
WorkingDirectory = DockerComposeDir,
50+
RedirectStandardOutput = true,
51+
RedirectStandardError = true,
52+
UseShellExecute = false,
53+
};
54+
55+
using var process = Process.Start(startInfo);
56+
process?.WaitForExit();
57+
}
58+
59+
private static void WaitForNucliaDbReady()
60+
{
61+
using var httpClient = new HttpClient();
62+
var maxAttempts = 30;
63+
var delayMs = 1000;
64+
var uri = new Uri("http://localhost:8080/");
65+
66+
for (var i = 0; i < maxAttempts; i++)
67+
{
68+
try
69+
{
70+
var response = httpClient.GetAsync(uri).GetAwaiter().GetResult();
71+
if (
72+
response.IsSuccessStatusCode
73+
|| response.StatusCode == System.Net.HttpStatusCode.NotFound
74+
)
75+
{
76+
return;
77+
}
78+
}
79+
catch
80+
{
81+
// Ignore exceptions during startup
82+
}
83+
84+
Thread.Sleep(delayMs);
85+
}
86+
87+
throw new InvalidOperationException("NucliaDB failed to start within the expected time");
88+
}
89+
90+
public void Dispose()
91+
{
92+
// Optionally clean up after tests
93+
// CleanDockerEnvironment();
94+
}
95+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Xunit;
2+
3+
#pragma warning disable CS8509 // C# compiler doesn't understand nested discriminated unions - use EXHAUSTION001 instead
4+
#pragma warning disable SA1512 // Single-line comments should not be followed by blank line
5+
6+
namespace NucliaDbClient.Tests;
7+
8+
[CollectionDefinition("NucliaDB Tests")]
9+
public sealed class NucliaDbTestList : ICollectionFixture<NucliaDbFixture> { }

0 commit comments

Comments
 (0)