Skip to content

Commit

Permalink
Merge pull request #6 from FrendsPlatform/issue-4
Browse files Browse the repository at this point in the history
PostgreSQL.ExecuteQuery fixed memory leak
  • Loading branch information
Svenskapojkarna authored Feb 7, 2023
2 parents ef0a485 + 31791f1 commit 34a3bc9
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 198 deletions.
12 changes: 8 additions & 4 deletions Frends.PostgreSQL.ExecuteQuery/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [1.0.0] - 2022-10-13
### Added
# Changelog

## [1.0.1] - 2023-02-02
### Fixed
- Fixed memory leak issue by adding cleanup method to the main class.

## [1.0.0] - 2022-10-13
### Added
- Initial implementation
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
using Frends.PostgreSQL.ExecuteQuery.Definitions;
using Npgsql;
using Npgsql;
using NUnit.Framework;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Frends.PostgreSQL.ExecuteQuery.Tests;

[TestFixture]
public class ExecuteQueryTests
using System.Threading;
using System.Threading.Tasks;

namespace Frends.PostgreSQL.ExecuteQuery.Tests;

[TestFixture]
public class ExecuteQueryTests
{

/// <summary>
/// These test requires local postgres database, create it e.g. with
///
/// docker run -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgres
///
/// </summary>
/// <summary>
/// These test requires local postgres database, create it e.g. with
///
/// docker run -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgres
///
/// </summary>

private readonly string _connection = "Host=localhost;Database=postgres;Port=5432;User Id=postgres;Password=mysecretpassword;";

private readonly string _connection = "Host=localhost;Database=postgres;Port=5432;User Id=postgres;Password=mysecretpassword;";

private readonly Options _options = new()
{
CommandTimeoutSeconds = 10,
SqlTransactionIsolationLevel = TransactionIsolationLevel.Default
};

[OneTimeSetUp]
public void TestSetup()
{
};

[OneTimeSetUp]
public void TestSetup()
{
using var conn = new NpgsqlConnection(_connection);
conn.Open();

Expand All @@ -39,80 +39,78 @@ public void TestSetup()
using (var cmd = new NpgsqlCommand(@"INSERT INTO ""lista"" (Id, Selite) VALUES (1, 'Ensimmäinen'), (2, 'foobar'), (3, ''), (4, null)", conn))
{
cmd.ExecuteNonQuery();
}
conn.Close();
}

[OneTimeTearDown]
public void OneTimeTearDown()
{
}
conn.Close();
}

[OneTimeTearDown]
public void OneTimeTearDown()
{
using var conn = new NpgsqlConnection(_connection);
conn.Open();

using var cmd = new NpgsqlCommand(@"DROP TABLE ""lista""", conn);
cmd.ExecuteNonQuery();
conn.Close();
}

/// <summary>
/// Check that returned id values are 1,2,3.
/// </summary>
///
[Test]
public async Task QuerydataThreeRows()
{
var input = new Input
{
Query = "SELECT * FROM lista",
Parameters = null,
ConnectionString = _connection
};

var result = await PostgreSQL.ExecuteQuery(input, _options, new CancellationToken());

Console.WriteLine(result.QueryResult);

Assert.AreEqual(1, (int)result.QueryResult[0]["id"]);
Assert.AreEqual(2, (int)result.QueryResult[1]["id"]);
Assert.AreEqual(3, (int)result.QueryResult[2]["id"]);
}

/// <summary>
/// Check that returns no rows with wrong id.
/// </summary>
[Test]
public async Task QuerydataNoRows()
{
var input = new Input
{
Query = "SELECT * from lista WHERE id=:ehto",
Parameters = new[] { new Parameter { Name = "ehto", Value = 0 } },
ConnectionString = _connection
};


var result = await PostgreSQL.ExecuteQuery(input, _options, new CancellationToken());
Assert.AreEqual(0, result.QueryResult.Count);
}

/// <summary>
/// Test non-query operation.
/// </summary>
[Test]
public async Task TestInsertQuery()
{
var input = new Input
{
Query = @"INSERT INTO ""lista"" (Id, Selite) VALUES (5, 'Viides')",
Parameters = null,
ConnectionString = _connection
};

var result = await PostgreSQL.ExecuteQuery(input, _options, new CancellationToken());
Assert.AreEqual(1, (int)result.QueryResult["AffectedRows"]);

input.Query = "SELECT * from lista WHERE id=5";
result = await PostgreSQL.ExecuteQuery(input, _options, new CancellationToken());
Assert.AreEqual("Viides", (string)result.QueryResult[0]["selite"]);
}
}
}

/// <summary>
/// Check that returned id values are 1,2,3.
/// </summary>
///
[Test]
public async Task QuerydataThreeRows()
{
var input = new Input
{
Query = "SELECT * FROM lista",
Parameters = null,
ConnectionString = _connection
};

var result = await PostgreSQL.ExecuteQuery(input, _options, new CancellationToken());

Assert.AreEqual(1, (int)result.QueryResult[0]["id"]);
Assert.AreEqual(2, (int)result.QueryResult[1]["id"]);
Assert.AreEqual(3, (int)result.QueryResult[2]["id"]);
}

/// <summary>
/// Check that returns no rows with wrong id.
/// </summary>
[Test]
public async Task QuerydataNoRows()
{
var input = new Input
{
Query = "SELECT * from lista WHERE id=:ehto",
Parameters = new[] { new Parameter { Name = "ehto", Value = 0 } },
ConnectionString = _connection
};


var result = await PostgreSQL.ExecuteQuery(input, _options, new CancellationToken());
Assert.AreEqual(0, result.QueryResult.Count);
}

/// <summary>
/// Test non-query operation.
/// </summary>
[Test]
public async Task TestInsertQuery()
{
var input = new Input
{
Query = @"INSERT INTO ""lista"" (Id, Selite) VALUES (5, 'Viides')",
Parameters = null,
ConnectionString = _connection
};

var result = await PostgreSQL.ExecuteQuery(input, _options, new CancellationToken());
Assert.AreEqual(1, (int)result.QueryResult["AffectedRows"]);

input.Query = "SELECT * from lista WHERE id=5";
result = await PostgreSQL.ExecuteQuery(input, _options, new CancellationToken());
Assert.AreEqual("Viides", (string)result.QueryResult[0]["selite"]);
}
}
75 changes: 38 additions & 37 deletions Frends.PostgreSQL.ExecuteQuery/Frends.PostgreSQL.ExecuteQuery.sln
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29613.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Frends.PostgreSQL.ExecuteQuery", "Frends.PostgreSQL.ExecuteQuery\Frends.PostgreSQL.ExecuteQuery.csproj", "{35C305C0-8108-4A98-BB1D-AFE5C926239E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Frends.PostgreSQL.ExecuteQuery.Tests", "Frends.PostgreSQL.ExecuteQuery.Tests\Frends.PostgreSQL.ExecuteQuery.Tests.csproj", "{8CA92187-8E4F-4414-803B-EC899479022E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{78F7F22E-6E20-4BCE-8362-0C558568B729}"
ProjectSection(SolutionItems) = preProject
.gitignore = .gitignore
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{35C305C0-8108-4A98-BB1D-AFE5C926239E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{35C305C0-8108-4A98-BB1D-AFE5C926239E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{35C305C0-8108-4A98-BB1D-AFE5C926239E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{35C305C0-8108-4A98-BB1D-AFE5C926239E}.Release|Any CPU.Build.0 = Release|Any CPU
{8CA92187-8E4F-4414-803B-EC899479022E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8CA92187-8E4F-4414-803B-EC899479022E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8CA92187-8E4F-4414-803B-EC899479022E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8CA92187-8E4F-4414-803B-EC899479022E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {55BC6629-85C9-48D8-8CA2-B0046AF1AF4B}
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29613.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Frends.PostgreSQL.ExecuteQuery", "Frends.PostgreSQL.ExecuteQuery\Frends.PostgreSQL.ExecuteQuery.csproj", "{35C305C0-8108-4A98-BB1D-AFE5C926239E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Frends.PostgreSQL.ExecuteQuery.Tests", "Frends.PostgreSQL.ExecuteQuery.Tests\Frends.PostgreSQL.ExecuteQuery.Tests.csproj", "{8CA92187-8E4F-4414-803B-EC899479022E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{78F7F22E-6E20-4BCE-8362-0C558568B729}"
ProjectSection(SolutionItems) = preProject
.gitignore = .gitignore
README.md = README.md
CHANGELOG.md = CHANGELOG.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{35C305C0-8108-4A98-BB1D-AFE5C926239E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{35C305C0-8108-4A98-BB1D-AFE5C926239E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{35C305C0-8108-4A98-BB1D-AFE5C926239E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{35C305C0-8108-4A98-BB1D-AFE5C926239E}.Release|Any CPU.Build.0 = Release|Any CPU
{8CA92187-8E4F-4414-803B-EC899479022E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8CA92187-8E4F-4414-803B-EC899479022E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8CA92187-8E4F-4414-803B-EC899479022E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8CA92187-8E4F-4414-803B-EC899479022E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {55BC6629-85C9-48D8-8CA2-B0046AF1AF4B}
EndGlobalSection
EndGlobal
Loading

0 comments on commit 34a3bc9

Please sign in to comment.