Skip to content
Permalink
Browse files

Add fluent configuration with IDbConnection (#499)

* Refactor Database constructor with config

* Add fluent config option to pass in connection

* Add checks for provider name in one constructor

See #499 (comment)

* Defer reading provider name in fluent constructor

* Make UsingProviderName() available in Framework
  • Loading branch information...
asherber authored and pleb committed Feb 19, 2019
1 parent eda527e commit 69edf7d32aabe0d9eb4c469f5cf35699d34d3012
@@ -6,6 +6,9 @@

using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using PetaPoco.Core;
using PetaPoco.Providers;
using Shouldly;
using Xunit;
@@ -171,6 +174,31 @@ public void UsingConnectionString_GivenInvalidArguments_Throws()
Should.Throw<ArgumentException>(() => config.UsingConnectionString(string.Empty));
}

[Fact]
public void UsingProvider_Overrides_UsingProviderName()
{
var db = config
.UsingConnectionString("cs")
.UsingProvider<FakeProvider>()
.UsingProviderName("OracleDatabaseProvider")
.Create();

db.Provider.ShouldBeOfType<FakeProvider>();
}

[Fact]
public void UsingConnectionString_BadProvider_Throws()
{
config.UsingConnectionString("cs").UsingProviderName("pn");
Should.Throw<ArgumentException>(() => config.Create());
}

[Fact]
public void UsingConnectionString_NoProvider_Throws()
{
Should.Throw<InvalidOperationException>(() => config.UsingConnectionString("cs").Create());
}

[Fact]
public void UsingConnectionString_GivenTimeoutAndAfterCreate_ShouldBeSameAsPetaPocoInstance()
{
@@ -382,5 +410,32 @@ public void UsingExceptionThrown_AfterCreate_InstanceShouldHaveDelegate()
(db as Database).OnException(new Exception());
eventFired.ShouldBeTrue();
}

[Fact]
public void UsingConnection_AfterCreate_InstanceShouldBeValid()
{
var connString = "Data Source = foo";
var connection = new SqlConnection(connString);
var db = new Database(config.UsingConnection(connection));

db.ConnectionString.ShouldBe(connString);
db.Provider.ShouldBeOfType<SqlServerDatabaseProvider>();
}

public class FakeProvider : DatabaseProvider
{
public override DbProviderFactory GetFactory() => null;
}

[Fact]
public void UsingConnectionWithProvider_AfterCreate_InstanceShouldBeValid()
{
var connString = "Data Source = foo";
var connection = new SqlConnection(connString);
var db = new Database(config.UsingConnection(connection).UsingProvider<FakeProvider>());

db.ConnectionString.ShouldBe(connString);
db.Provider.ShouldBeOfType<FakeProvider>();
}
}
}
@@ -58,8 +58,13 @@ public void Construct_GivenInvalidArguments_ShouldThrow()

#if !NETCOREAPP
Should.Throw<ArgumentException>(() => new Database((string) null));
Should.Throw<InvalidOperationException>(() => new Database("some connection string"));
Should.Throw<InvalidOperationException>(() => new Database("connection string name"));
#endif
Should.Throw<ArgumentException>(() => new Database("connection string", (string)null));
Should.Throw<ArgumentException>(() => new Database("connection string", ""));
Should.Throw<ArgumentException>(() => new Database(null, "provider name"));
Should.Throw<ArgumentException>(() => new Database("", "provider name"));
Should.Throw<ArgumentException>(() => new Database("connection string", "provider name"));

Should.Throw<ArgumentException>(() => new Database(null, _dbProviderFactory));
Should.Throw<ArgumentNullException>(() => new Database("some connection string", (DbProviderFactory) null));
@@ -89,11 +94,7 @@ public void Construct_GivenInvalidArguments_ShouldThrow()
}
catch (Exception e)
{
#if !NETCOREAPP
e.Message.ShouldContain("Both a connection string and provider are required");
#else
e.Message.ShouldContain("Either a provider name or provider must be registered");
#endif
throw;
}
});

This file was deleted.

Oops, something went wrong.
@@ -165,6 +165,7 @@
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpRenamePlacementToArrangementMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAlwaysTreatStructAsNotReorderableMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002ECSharpPlaceAttributeOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
Oops, something went wrong.

0 comments on commit 69edf7d

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.