Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepared working solution of Postgres that can be used with integration tests #1

Merged
merged 14 commits into from
Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
x64/
x86/
bld/
[Bb]in/
src/[Bb]in/
[Oo]bj/
[Ll]og/

Expand Down
6 changes: 6 additions & 0 deletions Postgres2Go.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.0.0
Global
EndGlobal
19 changes: 19 additions & 0 deletions src/Postgres2Go.Samples/Postgres2Go.Samples.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="more.xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="Npgsql" Version="3.2.6" />
<PackageReference Include="System.Diagnostics.Process" Version="4.3.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.analyzers" Version="0.8.0" />
<PackageReference Include="xunit.runner.console" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Postgres2Go\Postgres2Go.csproj" />
</ItemGroup>
</Project>
98 changes: 98 additions & 0 deletions src/Postgres2Go.Samples/when_using_as_class_fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Npgsql;
using Xunit;

namespace Postgres2Go.Samples
{
public class PgFixture : IDisposable
{
private readonly PostgresRunner _pgRunner;

public PgFixture()
{
_pgRunner = PostgresRunner
.Start(postgresBinariesSearchPattern: GetPgBinariesRelativePath());
}

public void Dispose()
{
if (_pgRunner is IDisposable)
(_pgRunner as IDisposable).Dispose();
}

public string ConnectionString => _pgRunner.ConnectionString;

private string GetPgBinariesRelativePath()
{
if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return "\\tools\\pgsql-10.1-windows64-binaries\\bin";
else if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
return "/tools/pgsql-10.1-linux-binaries/bin";
else
throw new NotSupportedException("OSX is not yet supported");
}
}

public class when_using_as_class_fixture : IClassFixture<PgFixture>
{
private readonly PgFixture _fixture;

public when_using_as_class_fixture(PgFixture fixture)
{
_fixture = fixture;
}

[Fact]
public async Task should_create_new_database_and_allow_exec_query_1()
{
// PREPARE
var dbName = "test_db_1";

var cmdBuilder = new StringBuilder();
cmdBuilder.AppendLine($"CREATE DATABASE {dbName}");
cmdBuilder.AppendLine("CONNECTION LIMIT = -1");

// RUN
using (var conn = new Npgsql.NpgsqlConnection(_fixture.ConnectionString))
{
await conn.OpenAsync();
var cmd = new NpgsqlCommand(cmdBuilder.ToString(), conn);
cmd.ExecuteNonQuery();

// ASSERT
var dbExists = await new NpgsqlCommand($"SELECT datname FROM pg_catalog.pg_database WHERE datname = '{dbName}'",conn)
.ExecuteScalarAsync();

Assert.NotNull(dbExists);
}
}

[Fact]
public async Task should_create_new_database_and_allow_exec_query_2()
{
// PREPARE
var dbName = "test_db_2";

var cmdBuilder = new StringBuilder();
cmdBuilder.AppendLine($"CREATE DATABASE {dbName}");
cmdBuilder.AppendLine("CONNECTION LIMIT = -1");

// RUN
using (var conn = new Npgsql.NpgsqlConnection(_fixture.ConnectionString))
{
await conn.OpenAsync();
var cmd = new NpgsqlCommand(cmdBuilder.ToString(), conn);
cmd.ExecuteNonQuery();

// ASSERT
var dbExists = await new NpgsqlCommand($"SELECT datname FROM pg_catalog.pg_database WHERE datname = '{dbName}'",conn)
.ExecuteScalarAsync();

Assert.NotNull(dbExists);
}
}
}
}
14 changes: 14 additions & 0 deletions src/Postgres2Go.Tests/Postgres2Go.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Postgres2Go\Postgres2Go.csproj" />
</ItemGroup>
</Project>
41 changes: 41 additions & 0 deletions src/Postgres2Go.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2020
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Postgres2Go", "Postgres2Go\Postgres2Go.csproj", "{DC273622-0954-432E-A722-8E0D7C78E21B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Postgres2Go.Tests", "Postgres2Go.Tests\Postgres2Go.Tests.csproj", "{D9527249-990B-4557-8DC1-FA9434F46A82}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{EB802F83-E581-4C4B-9B16-7D00D66C9329}"
ProjectSection(SolutionItems) = preProject
GCop.json = GCop.json
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Postgres2Go.Samples", "Postgres2Go.Samples\Postgres2Go.Samples.csproj", "{DB99F089-0B3C-4963-A194-6BFAAA619D2E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DC273622-0954-432E-A722-8E0D7C78E21B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DC273622-0954-432E-A722-8E0D7C78E21B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DC273622-0954-432E-A722-8E0D7C78E21B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DC273622-0954-432E-A722-8E0D7C78E21B}.Release|Any CPU.Build.0 = Release|Any CPU
{D9527249-990B-4557-8DC1-FA9434F46A82}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D9527249-990B-4557-8DC1-FA9434F46A82}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D9527249-990B-4557-8DC1-FA9434F46A82}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D9527249-990B-4557-8DC1-FA9434F46A82}.Release|Any CPU.Build.0 = Release|Any CPU
{DB99F089-0B3C-4963-A194-6BFAAA619D2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DB99F089-0B3C-4963-A194-6BFAAA619D2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DB99F089-0B3C-4963-A194-6BFAAA619D2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DB99F089-0B3C-4963-A194-6BFAAA619D2E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4857BFD8-1DB9-4BFA-BDAA-1D8B9B9386E7}
EndGlobalSection
EndGlobal
36 changes: 36 additions & 0 deletions src/Postgres2Go/Common/RecognizedOSPlatform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace Postgres2Go.Common
{
internal enum RecognizedOSPlatformEnum
{
Unknown = 0,
Windows = 1,
Linux = 2,
OSX = 3
}

internal static class RecognizedOSPlatform
{
internal static RecognizedOSPlatformEnum Determine()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return RecognizedOSPlatformEnum.Windows;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return RecognizedOSPlatformEnum.Linux;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return RecognizedOSPlatformEnum.OSX;
}

return RecognizedOSPlatformEnum.Unknown;
}
}
}
11 changes: 11 additions & 0 deletions src/Postgres2Go/Common/UnsupportedPlatformException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace Postgres2Go.Common
{
public class UnsupportedPlatformException : Exception
{
public UnsupportedPlatformException() { }
public UnsupportedPlatformException(string message) : base(message) { }
public UnsupportedPlatformException(string message, Exception inner) : base(message, inner) { }
}
}
43 changes: 43 additions & 0 deletions src/Postgres2Go/Helper/FileSystem/FileSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.IO;

namespace Postgres2Go.Helper.FileSystem
{
internal static class FileSystem
{
internal static void CreateFolder(string path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}

internal static void DeleteFolder(string path)
{
if (Directory.Exists(path))
{
Directory.Delete(path, true);
}
}

internal static void DeleteFile(string fullFileName)
{
if (File.Exists(fullFileName))
{
File.Delete(fullFileName);
}
}

internal static void GrantExecutablePermission(string path)
{
var p = System.Diagnostics.Process.Start("chmod", $"+x {path}");
p.WaitForExit();

if (p.ExitCode != 0)
{
throw new System.IO.IOException($"Could not set executable bit for {path}");
}
}

}
}
95 changes: 95 additions & 0 deletions src/Postgres2Go/Helper/FileSystem/FolderSearch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

namespace Postgres2Go.Helper.FileSystem
{
internal static class FolderSearch
{
internal const int MaxLevelOfRecursion = 6;

internal static string CurrentExecutingDirectory()
{
string filePath = new Uri(typeof(FolderSearch).GetTypeInfo().Assembly.CodeBase).LocalPath;
return Path.GetDirectoryName(filePath);
}

internal static string FindFolder(this string startPath, string searchPattern)
{
string currentPath = startPath;
string subPathToLookup = searchPattern.StartsWith(Path.DirectorySeparatorChar.ToString())
? searchPattern.Substring(1,searchPattern.Length - 1)
: searchPattern
;

foreach (var part in subPathToLookup.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.None))
{
string[] matchesDirectory = Directory.GetDirectories(currentPath, part);
if (!matchesDirectory.Any())
{
return null;
}
currentPath = matchesDirectory.OrderBy(x => x).Last();
}

return currentPath;
}

internal static string FindFolderUpwards(this string startPath, string searchPattern)
{
return FindFolderUpwards(startPath, searchPattern, 0);
}

private static string FindFolderUpwards(this string startPath, string searchPattern, int currentLevel)
{
if (startPath == null)
{
return null;
}

if (currentLevel >= MaxLevelOfRecursion)
{
return null;
}

string matchingFolder = startPath.FindFolder(searchPattern);
return matchingFolder ?? startPath.RemoveLastPart().FindFolderUpwards(searchPattern, currentLevel + 1);
}

internal static string RemoveLastPart(this string path)
{
if (!path.Contains(Path.DirectorySeparatorChar))
{
return null;
}

List<string> parts = path.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.None).ToList();
parts.RemoveAt(parts.Count() - 1);
return string.Join(Path.DirectorySeparatorChar.ToString(), parts.ToArray());
}

/// <summary>
/// Absolute path stays unchanged, relative path will be relative to current executing directory (usually the /bin folder)
/// </summary>
internal static string FinalizePath(string fileName)
{
string finalPath;

if (Path.IsPathRooted(fileName))
{
finalPath = fileName;
}
else
{
finalPath = Path.Combine (CurrentExecutingDirectory (), fileName);
finalPath = Path.GetFullPath(finalPath);
}

return finalPath;
}
}
}
Loading