Skip to content

Commit

Permalink
Make unit test case insensitive since case preservation varies betwee…
Browse files Browse the repository at this point in the history
…n DBs

Fix up ListDatabasesAsync using lifetime and add unit tests
  • Loading branch information
jas88 committed Jun 6, 2024
1 parent bf99587 commit 441f27d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
11 changes: 6 additions & 5 deletions FAnsiSql/Discovery/DiscoveredServerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading;
using FAnsi.Connections;
Expand Down Expand Up @@ -108,16 +109,16 @@ public virtual DbConnectionStringBuilder ChangeDatabase(DbConnectionStringBuilde
public abstract IEnumerable<string> ListDatabases(DbConnectionStringBuilder builder);
public abstract IEnumerable<string> ListDatabases(DbConnection con);

public IAsyncEnumerable<string> ListDatabasesAsync(DbConnectionStringBuilder builder, CancellationToken token)
public async IAsyncEnumerable<string> ListDatabasesAsync(DbConnectionStringBuilder builder, [EnumeratorCancellation] CancellationToken token)
{
//list the database on the server
using var con = GetConnection(builder);
await using var con = GetConnection(builder);

//this will work or timeout
var openTask = con.OpenAsync(token);
openTask.Wait(token);
await con.OpenAsync(token);

return ListDatabases(con).ToAsyncEnumerable();
foreach (var db in ListDatabases(con))
yield return db;
}

public abstract DbConnectionStringBuilder EnableAsync(DbConnectionStringBuilder builder);
Expand Down
54 changes: 41 additions & 13 deletions Tests/FAnsiTests/Database/DatabaseLevelTests.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,79 @@
using FAnsi;
using System;
using System.Collections;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FAnsi;
using FAnsi.Discovery;
using FAnsi.Implementation;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using Oracle.ManagedDataAccess.Client;
using TypeGuesser;

namespace FAnsiTests.Database;

internal sealed class DatabaseLevelTests : DatabaseTests
{
[TestCaseSource(typeof(All),nameof(All.DatabaseTypes))]
[TestCaseSource(typeof(All), nameof(All.DatabaseTypes))]
public void Database_Exists(DatabaseType type)
{
var server = GetTestDatabase(type);
Assert.That(server.Exists(), "Server " + server + " did not exist");
}


[TestCase(DatabaseType.MySql,false)]
[TestCase(DatabaseType.MicrosoftSQLServer,false)]
[TestCase(DatabaseType.Oracle,true)]
[TestCase(DatabaseType.PostgreSql,false)]
[TestCase(DatabaseType.MySql, false)]
[TestCase(DatabaseType.MicrosoftSQLServer, false)]
[TestCase(DatabaseType.Oracle, true)]
[TestCase(DatabaseType.PostgreSql, false)]
public void Test_ExpectDatabase(DatabaseType type, bool upperCase)
{
var helper = ImplementationManager.GetImplementation(type).GetServerHelper();
var server = new DiscoveredServer(helper.GetConnectionStringBuilder("loco","db","frank","kangaro"));
var server = new DiscoveredServer(helper.GetConnectionStringBuilder("loco", "db", "frank", "kangaro"));
var db = server.ExpectDatabase("omg");
Assert.That(db.GetRuntimeName(), Is.EqualTo(upperCase ?"OMG":"omg"));
Assert.That(db.GetRuntimeName(), Is.EqualTo(upperCase ? "OMG" : "omg"));
}

[TestCaseSource(typeof(All),nameof(All.DatabaseTypes))]
[TestCaseSource(typeof(All), nameof(All.DatabaseTypes))]
public void Test_CreateSchema(DatabaseType type)
{
var db = GetTestDatabase(type);

Assert.DoesNotThrow(()=>db.CreateSchema("Fr ank"));
Assert.DoesNotThrow(()=>db.CreateSchema("Fr ank"));
Assert.DoesNotThrow(() => db.CreateSchema("Fr ank"));
Assert.DoesNotThrow(() => db.CreateSchema("Fr ank"));

db.Server.GetQuerySyntaxHelper().EnsureWrapped("Fr ank");

if (type is not (DatabaseType.MicrosoftSQLServer or DatabaseType.PostgreSql)) return;

var tbl = db.CreateTable("Heyyy",
[new DatabaseColumnRequest("fff", new DatabaseTypeRequest(typeof(string), 10))],"Fr ank");
[new DatabaseColumnRequest("fff", new DatabaseTypeRequest(typeof(string), 10))], "Fr ank");

Assert.That(tbl.Exists());

if(type == DatabaseType.MicrosoftSQLServer)
if (type == DatabaseType.MicrosoftSQLServer)
Assert.That(tbl.Schema, Is.EqualTo("Fr ank"));
}

[TestCaseSource(typeof(All), nameof(All.DatabaseTypes))]
public void TestListDatabasesAsync(DatabaseType type)
{
var db = GetTestDatabase(type, false);

Constraint exceptionType = type switch
{
DatabaseType.MySql => Throws.TypeOf<OperationCanceledException>(),
DatabaseType.MicrosoftSQLServer => Throws.TypeOf<TaskCanceledException>(),
DatabaseType.PostgreSql => Throws.Nothing,
DatabaseType.Oracle => Throws.TypeOf<OracleException>(),
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};

Assert.That(
() => db.Server.Helper.ListDatabasesAsync(db.Server.Builder, new CancellationToken(true))
.ToBlockingEnumerable().ToList(), exceptionType);
var databases = db.Server.Helper.ListDatabasesAsync(db.Server.Builder, CancellationToken.None).ToBlockingEnumerable().ToList();
Assert.That(databases, Has.Member(db.GetRuntimeName()).Using((IEqualityComparer)StringComparer.OrdinalIgnoreCase));
}
}

0 comments on commit 441f27d

Please sign in to comment.