Skip to content

Commit

Permalink
Merge pull request #60 from andrewabest/friendlier-test-database-hand…
Browse files Browse the repository at this point in the history
…ling

Allow alternative development databases
  • Loading branch information
andrewabest authored May 10, 2018
2 parents 9d80a29 + b507f9a commit 6fa46ca
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,4 @@ FakesAssemblies/
# nuget pack output
*.nupkg

/development.json
5 changes: 4 additions & 1 deletion Conventional.Tests.Roslyn/Conventional.Tests.Roslyn.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>10DC5795-E692-4B6E-854B-E1E1AA9F1B36</ProjectGuid>
<ProjectGuid>{10DC5795-E692-4B6E-854B-E1E1AA9F1B36}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Conventional.Tests.Roslyn</RootNamespace>
Expand Down Expand Up @@ -104,6 +104,9 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Data;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Resources;
using FluentAssertions;
Expand All @@ -9,16 +11,26 @@ namespace Conventional.Tests.Conventional.Conventions.Database
{
public class DatabaseConventionSpecificationTests
{
#if DEBUG
private const string TestDbConnectionString = @"Server=.\SQLEXPRESS;Database=Conventional;Integrated Security=true;";
#else
private const string TestDbConnectionString = @"Server=(local)\SQL2014;Database=Conventional;User ID=sa;Password=Password12!";
#endif
private readonly DevelopmentSettings _settings = DevelopmentSettings.Create();

[SetUp]
public void Setup()
{
CreateDatabase();
try
{
CreateDatabase();
}
catch (SqlException e)
{
Assert.Fail(
"Conventional's test suite requires a default named .\\SQLEXPRESS instance. If you want to use an alternative database instance, create a development.json file in the solution root with your desired connection string.\n See development.json.example in the solution root for an example that uses LocalDB."
+ Environment.NewLine
+ Environment.NewLine
+ $"Exception Message: {e.Message}"
+ Environment.NewLine
+ Environment.NewLine
+ $"Stack Trace: {e.StackTrace}");
}
}

[TearDown]
Expand All @@ -33,7 +45,7 @@ public void AllCheckConstraintsMustBeNamedConventionalSpecification_Success()
ExecuteSqlScriptFromResource("AllCheckConstraintsMustBeNamedConventionalSpecification_Success.sql");

TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.AllCheckConstraintsMustBeNamed)
.IsSatisfied
.Should()
Expand All @@ -46,7 +58,7 @@ public void AllCheckConstraintsMustBeNamedConventionalSpecification_Fail()
ExecuteSqlScriptFromResource("AllCheckConstraintsMustBeNamedConventionalSpecification_Fail.sql");

var result = TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.AllCheckConstraintsMustBeNamed);

result.IsSatisfied.Should().BeFalse();
Expand All @@ -59,7 +71,7 @@ public void AllDefaultConstraintsMustBeNamedConventionalSpecification_Success()
ExecuteSqlScriptFromResource("AllDefaultConstraintsMustBeNamedConventionalSpecification_Success.sql");

TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.AllDefaultConstraintsMustBeNamed)
.IsSatisfied
.Should()
Expand All @@ -72,7 +84,7 @@ public void AllDefaultConstraintsMustBeNamedConventionalSpecification_Fail()
ExecuteSqlScriptFromResource("AllDefaultConstraintsMustBeNamedConventionalSpecification_Fail.sql");

var result = TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.AllDefaultConstraintsMustBeNamed);

result.IsSatisfied.Should().BeFalse();
Expand All @@ -85,7 +97,7 @@ public void AllPrimaryKeyConstraintsMustBeNamedConventionalSpecification_Success
ExecuteSqlScriptFromResource("AllPrimaryKeyConstraintsMustBeNamedConventionalSpecification_Success.sql");

TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.AllPrimaryKeyConstraintsMustBeNamed)
.IsSatisfied
.Should()
Expand All @@ -98,7 +110,7 @@ public void AllPrimaryKeyConstraintsMustBeNamedConventionalSpecification_Fail()
ExecuteSqlScriptFromResource("AllPrimaryKeyConstraintsMustBeNamedConventionalSpecification_Fail.sql");

var result = TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.AllPrimaryKeyConstraintsMustBeNamed);

result.IsSatisfied.Should().BeFalse();
Expand All @@ -111,7 +123,7 @@ public void AllReferenceConstraintsMustBeNamedConventionalSpecification_Success(
ExecuteSqlScriptFromResource("AllReferenceConstraintsMustBeNamedConventionalSpecification_Success.sql");

TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.AllReferenceConstraintsMustBeNamed)
.IsSatisfied
.Should()
Expand All @@ -124,7 +136,7 @@ public void AllReferenceConstraintsMustBeNamedConventionalSpecification_Fail()
ExecuteSqlScriptFromResource("AllReferenceConstraintsMustBeNamedConventionalSpecification_Fail.sql");

var result = TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.AllReferenceConstraintsMustBeNamed);

result.IsSatisfied.Should().BeFalse();
Expand All @@ -137,7 +149,7 @@ public void AllUniqueConstraintsMustBeNamedConventionalSpecification_Success()
ExecuteSqlScriptFromResource("AllUniqueConstraintsMustBeNamedConventionalSpecification_Success.sql");

TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.AllUniqueConstraintsMustBeNamed)
.IsSatisfied
.Should()
Expand All @@ -150,7 +162,7 @@ public void AllUniqueConstraintsMustBeNamedConventionalSpecification_Fail()
ExecuteSqlScriptFromResource("AllUniqueConstraintsMustBeNamedConventionalSpecification_Fail.sql");

var result = TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.AllUniqueConstraintsMustBeNamed);

result.IsSatisfied.Should().BeFalse();
Expand All @@ -163,7 +175,7 @@ public void AllNamedColumnsMustBeNullableConventionSpecification_Success()
ExecuteSqlScriptFromResource("AllNamedColumnsMustBeNullableConventionalSpecification_Success.sql");

TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.AllNamedColumnsMustBeNullable("UpdatedDateTime"))
.IsSatisfied
.Should()
Expand All @@ -176,19 +188,20 @@ public void AllNamedColumnsMustBeNonNullableConventionSpecification_Fails()
ExecuteSqlScriptFromResource("AllNamedColumnsMustBeNullableConventionalSpecification_Fail.sql");

var result = TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.AllNamedColumnsMustBeNullable("UpdatedDateTime"));

result.IsSatisfied.Should().BeFalse();
result.Failures.Should().HaveCount(1);
}

[Test]
public void AllNamedColumnsMustBeNonNullableConventionSpecification_Success()
{
ExecuteSqlScriptFromResource("AllNamedColumnsMustBeNullableConventionalSpecification_Success.sql");

TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.AllNamedColumnsMustBeNonNullable("CreatedDateTime"))
.IsSatisfied
.Should()
Expand All @@ -201,7 +214,7 @@ public void AllNamedColumnsMustBeNullableConventionSpecification_Fails()
ExecuteSqlScriptFromResource("AllNamedColumnsMustBeNullableConventionalSpecification_Fail.sql");

var result = TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.AllNamedColumnsMustBeNullable("UpdatedDateTime"));

result.IsSatisfied.Should().BeFalse();
Expand All @@ -214,7 +227,7 @@ public void AllIdentityColumnsMustBeNamedTableNameIdConventionSpecification_Succ
ExecuteSqlScriptFromResource("AllIdentityColumnsMustBeNamedTableNameIdConventionSpecificationSuccess.sql");

TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.AllIdentityColumnsMustBeNamedTableNameId)
.IsSatisfied
.Should()
Expand All @@ -227,7 +240,7 @@ public void AllIdentityColumnsMustBeNamedTableNameIdConventionSpecification_Fail
ExecuteSqlScriptFromResource("AllIdentityColumnsMustBeNamedTableNameIdConventionSpecificationFailure.sql");

var result = TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.AllIdentityColumnsMustBeNamedTableNameId);

result.IsSatisfied.Should().BeFalse();
Expand All @@ -240,7 +253,7 @@ public void AllTablesMustHaveAClusteredIndex_Success()
ExecuteSqlScriptFromResource("TablesWithoutClusteredIndexSuccess.sql");

TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.AllTablesMustHaveAClusteredIndex)
.IsSatisfied
.Should()
Expand All @@ -253,7 +266,7 @@ public void AllTablesMustHaveAClusteredIndex_Failure()
ExecuteSqlScriptFromResource("TablesWithoutClusteredIndexFailure.sql");

var result = TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.AllTablesMustHaveAClusteredIndex);

result.IsSatisfied.Should().BeFalse();
Expand All @@ -266,7 +279,7 @@ public void EachRowMustHaveACorrespondingEnum_Success()
ExecuteSqlScriptFromResource("EachRowMustHaveACorrespondingEnum_Success.sql");

TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.EachRowMustHaveACorrespondingEnum<CloudServiceEnum>("dbo.CloudService", "CloudServiceId"))
.IsSatisfied
.Should()
Expand All @@ -279,14 +292,14 @@ public void EachRowMustHaveACorrespondingEnum_Fail()
ExecuteSqlScriptFromResource("EachRowMustHaveACorrespondingEnum_Fail.sql");

var result = TheDatabase
.WithConnectionString(TestDbConnectionString)
.WithConnectionString(_settings.ConnectionString)
.MustConformTo(Convention.EachRowMustHaveACorrespondingEnum<CloudServiceEnum>("dbo.CloudService", "CloudServiceId"));

result.IsSatisfied.Should().BeFalse();
result.Failures.Should().HaveCount(1);
}

private static void ExecuteSqlScriptFromResource(string resourceName)
private void ExecuteSqlScriptFromResource(string resourceName)
{
string script;

Expand All @@ -301,7 +314,7 @@ private static void ExecuteSqlScriptFromResource(string resourceName)
}
}

using (IDbConnection dbConnection = new SqlConnection(TestDbConnectionString))
using (IDbConnection dbConnection = new SqlConnection(_settings.ConnectionString))
{
dbConnection.Open();
var command = dbConnection.CreateCommand();
Expand All @@ -310,9 +323,9 @@ private static void ExecuteSqlScriptFromResource(string resourceName)
}
}

private static void CreateDatabase()
private void CreateDatabase()
{
var sb = new SqlConnectionStringBuilder(TestDbConnectionString);
var sb = new SqlConnectionStringBuilder(_settings.ConnectionString);
var dbName = sb.InitialCatalog;
sb.InitialCatalog = "master";

Expand All @@ -325,9 +338,9 @@ private static void CreateDatabase()
}
}

private static void DropDatabase()
private void DropDatabase()
{
var sb = new SqlConnectionStringBuilder(TestDbConnectionString);
var sb = new SqlConnectionStringBuilder(_settings.ConnectionString);
var dbName = sb.InitialCatalog;
sb.InitialCatalog = "master";

Expand Down
31 changes: 31 additions & 0 deletions Conventional.Tests/DevelopmentSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.IO;
using Newtonsoft.Json;

namespace Conventional.Tests
{
public class DevelopmentSettings
{
private DevelopmentSettings()
{
}

#if DEBUG
public string ConnectionString { get; set; } = @"Server=.\SQLEXPRESS;Database=Conventional;Integrated Security=true;";
#else
public string ConnectionString { get; set; } = @"Server=(local)\SQL2014;Database=Conventional;User ID=sa;Password=Password12!";
#endif

public static DevelopmentSettings Create()
{
DevelopmentSettings settings = null;

var developmentSettingsFile = Path.Combine(KnownPaths.SolutionRoot, "development.json");
if (File.Exists(developmentSettingsFile))
{
settings = JsonConvert.DeserializeObject<DevelopmentSettings>(File.ReadAllText(developmentSettingsFile));
}

return settings ?? new DevelopmentSettings();
}
}
}
1 change: 1 addition & 0 deletions Conventional.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Conventional.Tests.Roslyn",
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Getting Started", "Getting Started", "{C38DA37B-5388-4443-8C6C-617A677D86AD}"
ProjectSection(SolutionItems) = preProject
development.json.example = development.json.example
README.md = README.md
EndProjectSection
EndProject
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ To get started with Conventional, please check out [the wiki](https://github.com

Not sure how to get started with Conventional? Check out [the sample repository](https://github.com/andrewabest/Conventional.Samples) which contains a bunch of real-world usage examples

## Contributing

Conventional's test suite requires a default named `.\SQLEXPRESS` instance. If you have another instance you would like to use for development, create a copy of `development.settings.example` in the solution root and rename to `development.settings`, and supply your own connection string.

## License

Licensed under the terms of the [MS-PL](https://opensource.org/licenses/MS-PL) license
3 changes: 3 additions & 0 deletions development.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"connectionString": "Server=(localdb)\\MSSQLLocalDB;Database=Conventional;Integrated Security=true;"
}

0 comments on commit 6fa46ca

Please sign in to comment.