Skip to content

Commit

Permalink
Fixed dead links in docs
Browse files Browse the repository at this point in the history
Added basic test for PgVersionTargetedFactDiscoverer
  • Loading branch information
oskardudycz committed Jul 23, 2023
1 parent cb1d37f commit a4e0ee5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/configuration/multitenancy.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ You can place this code somewhere in the tenant initialization code. For instanc

* tenant setup procedure,
* dedicated API endpoint
* [custom session factory](/configuration/hostbuilder/#customizing-session-creation-globally), although that's not recommended for the reasons mentioned above.
* [custom session factory](/configuration/hostbuilder#customizing-session-creation-globally), although that's not recommended for the reasons mentioned above.

## Write your own tenancy strategy!

Expand Down
46 changes: 34 additions & 12 deletions src/Marten.Testing/Harness/PgVersionTargetedFact.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Marten.Testing.Harness;
using Npgsql;
using NSubstitute;
using Shouldly;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;
Expand All @@ -20,38 +24,44 @@ public sealed class PgVersionTargetedFact: FactAttribute

public sealed class PgVersionTargetedFactDiscoverer: FactDiscoverer
{
private static readonly Version Version;
internal static readonly Version Version;

static PgVersionTargetedFactDiscoverer()
{
// PG version does not change during test run so we can do static ctor
using (var c = new NpgsqlConnection(ConnectionSource.ConnectionString))
{
c.Open();
Version = c.PostgreSqlVersion;
c.Close();
}
using var c = new NpgsqlConnection(ConnectionSource.ConnectionString);
c.Open();
Version = c.PostgreSqlVersion;
c.Close();
}

public PgVersionTargetedFactDiscoverer(IMessageSink diagnosticMessageSink): base(diagnosticMessageSink)
{
}

public override IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod,
public override IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions,
ITestMethod testMethod,
IAttributeInfo factAttribute)
{
var minimumVersion = factAttribute.GetNamedArgument<string>(nameof(PgVersionTargetedFact.MinimumVersion));
var maximumVersion = factAttribute.GetNamedArgument<string>(nameof(PgVersionTargetedFact.MaximumVersion));

if (minimumVersion != null && Version.TryParse(minimumVersion, out var minVersion) && Version < minVersion)
{
yield return new TestCaseSkippedDueToVersion($"Minimum required PG version {minimumVersion} is higher than {Version}", DiagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), discoveryOptions.MethodDisplayOptionsOrDefault(), testMethod);
yield return new TestCaseSkippedDueToVersion(
$"Minimum required PG version {minimumVersion} is higher than {Version}", DiagnosticMessageSink,
discoveryOptions.MethodDisplayOrDefault(), discoveryOptions.MethodDisplayOptionsOrDefault(),
testMethod);
}

if (maximumVersion != null && Version.TryParse(maximumVersion, out var maxVersion) && Version > maxVersion)
{
yield return new TestCaseSkippedDueToVersion($"Maximum allowed PG version {maximumVersion} is higher than {Version}", DiagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), discoveryOptions.MethodDisplayOptionsOrDefault(), testMethod);
yield return new TestCaseSkippedDueToVersion(
$"Maximum allowed PG version {maximumVersion} is higher than {Version}", DiagnosticMessageSink,
discoveryOptions.MethodDisplayOrDefault(), discoveryOptions.MethodDisplayOptionsOrDefault(),
testMethod);
}

yield return base.CreateTestCase(discoveryOptions, testMethod, factAttribute);
}

Expand All @@ -62,10 +72,22 @@ public TestCaseSkippedDueToVersion()
{
}

public TestCaseSkippedDueToVersion(string skipReason, IMessageSink diagnosticMessageSink, TestMethodDisplay defaultMethodDisplay, TestMethodDisplayOptions defaultMethodDisplayOptions, ITestMethod testMethod, object[] testMethodArguments = null) : base(diagnosticMessageSink, defaultMethodDisplay, defaultMethodDisplayOptions, testMethod, testMethodArguments)
public TestCaseSkippedDueToVersion(string skipReason, IMessageSink diagnosticMessageSink,
TestMethodDisplay defaultMethodDisplay, TestMethodDisplayOptions defaultMethodDisplayOptions,
ITestMethod testMethod, object[] testMethodArguments = null): base(diagnosticMessageSink,
defaultMethodDisplay, defaultMethodDisplayOptions, testMethod, testMethodArguments)
{
SkipReason = skipReason;
}
}
}
}

public class PgVersionTargetedFactDiscovererTests
{
[Fact]
public void PgVersionTargetedFactDiscoverer_CanConnectToDatabase()
{
PgVersionTargetedFactDiscoverer.Version.ShouldNotBe(default);
}
}

0 comments on commit a4e0ee5

Please sign in to comment.