Skip to content

Commit

Permalink
Feature/cobertura (#21)
Browse files Browse the repository at this point in the history
* adding test db project and corbertura summary
  • Loading branch information
GoEddie authored Oct 4, 2017
1 parent 6a3af12 commit 20180c5
Show file tree
Hide file tree
Showing 187 changed files with 4,636 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/SQLCover/SQLCover.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
# Visual Studio 15
VisualStudioVersion = 15.0.26730.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SQLCover", "SQLCover\SQLCover.csproj", "{2AB093C7-88AE-4F6B-940E-9AE6DBC4CC87}"
EndProject
Expand All @@ -15,6 +15,8 @@ Project("{00D1A9C2-B5F0-4AF3-8072-F6C62B433612}") = "DatabaseProject", "Database
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestLib", "test\TestLib\TestLib.csproj", "{892698FB-618A-4D80-B035-3520148603BF}"
EndProject
Project("{00D1A9C2-B5F0-4AF3-8072-F6C62B433612}") = "DatabaseWithTests", "test\DatabaseWithTests\DatabaseWithTests.sqlproj", "{021EDCB7-E7A1-4F6E-BF0D-73AA4DA7B62A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -43,6 +45,12 @@ Global
{892698FB-618A-4D80-B035-3520148603BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{892698FB-618A-4D80-B035-3520148603BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{892698FB-618A-4D80-B035-3520148603BF}.Release|Any CPU.Build.0 = Release|Any CPU
{021EDCB7-E7A1-4F6E-BF0D-73AA4DA7B62A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{021EDCB7-E7A1-4F6E-BF0D-73AA4DA7B62A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{021EDCB7-E7A1-4F6E-BF0D-73AA4DA7B62A}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{021EDCB7-E7A1-4F6E-BF0D-73AA4DA7B62A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{021EDCB7-E7A1-4F6E-BF0D-73AA4DA7B62A}.Release|Any CPU.Build.0 = Release|Any CPU
{021EDCB7-E7A1-4F6E-BF0D-73AA4DA7B62A}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -52,5 +60,9 @@ Global
{4D42F47A-D116-4881-8270-7B9250FB898C} = {DB9215AD-D611-49A3-8061-2AA1E957F12C}
{25389F17-3E61-491E-B99A-B845AE8529BA} = {DB9215AD-D611-49A3-8061-2AA1E957F12C}
{892698FB-618A-4D80-B035-3520148603BF} = {DB9215AD-D611-49A3-8061-2AA1E957F12C}
{021EDCB7-E7A1-4F6E-BF0D-73AA4DA7B62A} = {DB9215AD-D611-49A3-8061-2AA1E957F12C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5524B744-8BA0-4B61-8D23-CD1685FEE6F0}
EndGlobalSection
EndGlobal
4 changes: 4 additions & 0 deletions src/SQLCover/SQLCover/CodeCoverage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public CodeCoverage(string connectionString, string databaseName, string[] exclu
{
}

public CodeCoverage(string connectionString, string databaseName, string[] excludeFilter, bool logging, bool debugger) : this(connectionString, databaseName, excludeFilter, logging, debugger, TraceControllerType.Default)
{
}

public CodeCoverage(string connectionString, string databaseName, string[] excludeFilter, bool logging, bool debugger, TraceControllerType traceType)
{
if (debugger)
Expand Down
20 changes: 20 additions & 0 deletions src/SQLCover/SQLCover/CoverageResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,26 @@ public void SaveSourceFiles(string path)
}
}

/// <summary>
/// https://raw.githubusercontent.com/jenkinsci/cobertura-plugin/master/src/test/resources/hudson/plugins/cobertura/coverage-with-data.xml
/// http://cobertura.sourceforge.net/xml/coverage-03.dtd
/// </summary>
/// <returns></returns>
public string Cobertura()
{
var statements = _batches.Sum(p => p.StatementCount);
var coveredStatements = _batches.Sum(p => p.CoveredStatementCount);

var builder = new StringBuilder();
builder.Append("<?xml version=\"1.0\"?>");
builder.Append("<!--DOCTYPE coverage SYSTEM \"http://cobertura.sourceforge.net/xml/coverage-03.dtd\"-->");
builder.AppendFormat("<coverage lines-valid=\"{0}\" lines-covered=\"{1}\" line-rate=\"{2}\" branch-rate=\"0.0\" version=\"1.9\" timestamp=\"{3}\">", statements, coveredStatements,coveredStatements / (float)statements, (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds);
builder.Append("<coverage>\r\n");

return builder.ToString();

}

public string OpenCoverXml()
{
var statements = _batches.Sum(p => p.StatementCount);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

CREATE FUNCTION Accelerator.GetParticlesInRectangle(
@X1 DECIMAL(10,2),
@Y1 DECIMAL(10,2),
@X2 DECIMAL(10,2),
@Y2 DECIMAL(10,2)
)
RETURNS TABLE
AS RETURN (
SELECT Id, X, Y, Value
FROM Accelerator.Particle
WHERE X > @X1 AND X < @X2
AND
Y > @Y1 AND Y < @Y2
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

CREATE FUNCTION Accelerator.GetStatusMessage()
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE @NumParticles INT;
SELECT @NumParticles = COUNT(1) FROM Accelerator.Particle;
RETURN 'The Accelerator is prepared with ' + CAST(@NumParticles AS NVARCHAR(MAX)) + ' particles.';
END;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

CREATE FUNCTION Accelerator.IsExperimentReady()
RETURNS BIT
AS
BEGIN
DECLARE @NumParticles INT;

SELECT @NumParticles = COUNT(1) FROM Accelerator.Particle;

IF @NumParticles > 2
RETURN 1;

RETURN 0;
END;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

CREATE PROCEDURE Accelerator.AlertParticleDiscovered
@ParticleDiscovered NVARCHAR(MAX)
AS
BEGIN
IF @ParticleDiscovered = 'Higgs Boson'
BEGIN
EXEC Accelerator.SendHiggsBosonDiscoveryEmail '[email protected]';
END;
END;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

CREATE PROCEDURE Accelerator.SendHiggsBosonDiscoveryEmail
@EmailAddress NVARCHAR(MAX)
AS
BEGIN
RAISERROR('Not Implemented - yet',16,10);
END;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE [Accelerator].[Color] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[ColorName] NVARCHAR (MAX) NOT NULL,
CONSTRAINT [PK_Color_Id] PRIMARY KEY CLUSTERED ([Id] ASC)
);

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE [Accelerator].[Particle] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[X] DECIMAL (10, 2) NOT NULL,
[Y] DECIMAL (10, 2) NOT NULL,
[Value] NVARCHAR (MAX) NOT NULL,
[ColorId] INT NOT NULL,
CONSTRAINT [PK_Point_Id] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_ParticleColor] FOREIGN KEY ([ColorId]) REFERENCES [Accelerator].[Color] ([Id])
);

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

CREATE PROCEDURE AcceleratorTests.[test a particle is included only if it fits inside the boundaries of the rectangle]
AS
BEGIN
--Assemble: Fake the Particle table to make sure it is empty and that constraints will not be a problem
EXEC tSQLt.FakeTable 'Accelerator.Particle';
-- Populate the Particle table with rows that hug the rectangle boundaries
INSERT INTO Accelerator.Particle (Id, X, Y) VALUES ( 1, -0.01, 0.50);
INSERT INTO Accelerator.Particle (Id, X, Y) VALUES ( 2, 0.00, 0.50);
INSERT INTO Accelerator.Particle (Id, X, Y) VALUES ( 3, 0.01, 0.50);
INSERT INTO Accelerator.Particle (Id, X, Y) VALUES ( 4, 0.99, 0.50);
INSERT INTO Accelerator.Particle (Id, X, Y) VALUES ( 5, 1.00, 0.50);
INSERT INTO Accelerator.Particle (Id, X, Y) VALUES ( 6, 1.01, 0.50);
INSERT INTO Accelerator.Particle (Id, X, Y) VALUES ( 7, 0.50, -0.01);
INSERT INTO Accelerator.Particle (Id, X, Y) VALUES ( 8, 0.50, 0.00);
INSERT INTO Accelerator.Particle (Id, X, Y) VALUES ( 9, 0.50, 0.01);
INSERT INTO Accelerator.Particle (Id, X, Y) VALUES (10, 0.50, 0.99);
INSERT INTO Accelerator.Particle (Id, X, Y) VALUES (11, 0.50, 1.00);
INSERT INTO Accelerator.Particle (Id, X, Y) VALUES (12, 0.50, 1.01);

--Act: Call the GetParticlesInRectangle Table-Valued Function and capture the relevant columns into the #Actual temp table
SELECT Id, X, Y
INTO #Actual
FROM Accelerator.GetParticlesInRectangle(0.0, 0.0, 1.0, 1.0);

--Assert: Create an empty #Expected temp table that has the same structure as the #Actual table
SELECT TOP(0) *
INTO #Expected
FROM #Actual;

-- The expected data is inserted into the #Expected table
INSERT INTO #Expected (Id, X, Y) VALUES (3, 0.01, 0.50);
INSERT INTO #Expected (Id, X, Y) VALUES (4, 0.99, 0.50);
INSERT INTO #Expected (Id, X, Y) VALUES (9, 0.50, 0.01);
INSERT INTO #Expected (Id, X, Y) VALUES (10, 0.50, 0.99);

-- Compare the data in the #Expected and #Actual tables
EXEC tSQLt.AssertEqualsTable '#Expected', '#Actual';
END;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

CREATE PROCEDURE AcceleratorTests.[test a particle within the rectangle is returned with an Id, Point Location and Value]
AS
BEGIN
--Assemble: Fake the Particle table to make sure it is empty and that constraints will not be a problem
EXEC tSQLt.FakeTable 'Accelerator.Particle';
-- Put a test particle into the table
INSERT INTO Accelerator.Particle (Id, X, Y, Value) VALUES (1, 0.5, 0.5, 'MyValue');

--Act: Call the GetParticlesInRectangle Table-Valued Function and capture the relevant columns into the #Actual temp table
SELECT Id, X, Y, Value
INTO #Actual
FROM Accelerator.GetParticlesInRectangle(0.0, 0.0, 1.0, 1.0);

--Assert: Create an empty #Expected temp table that has the same structure as the #Actual table
SELECT TOP(0) *
INTO #Expected
FROM #Actual;

-- A single row with the expected data is inserted into the #Expected table
INSERT INTO #Expected (Id, X, Y, Value) VALUES (1, 0.5, 0.5, 'MyValue');

-- Compare the data in the #Expected and #Actual tables
EXEC tSQLt.AssertEqualsTable '#Expected', '#Actual';
END;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

CREATE PROCEDURE AcceleratorTests.[test a particle within the rectangle is returned]
AS
BEGIN
--Assemble: Fake the Particle table to make sure it is empty and that constraints will not be a problem
EXEC tSQLt.FakeTable 'Accelerator.Particle';
-- Put a test particle into the table
INSERT INTO Accelerator.Particle (Id, X, Y) VALUES (1, 0.5, 0.5);

--Act: Call the GetParticlesInRectangle Table-Valued Function and capture the Id column into the #Actual temp table
SELECT Id
INTO #Actual
FROM Accelerator.GetParticlesInRectangle(0.0, 0.0, 1.0, 1.0);

--Assert: Create an empty #Expected temp table that has the same structure as the #Actual table
SELECT TOP(0) *
INTO #Expected
FROM #Actual;

-- A single row with an Id value of 1 is expected
INSERT INTO #Expected (Id) VALUES (1);

-- Compare the data in the #Expected and #Actual tables
EXEC tSQLt.AssertEqualsTable '#Expected', '#Actual';
END;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@


CREATE PROCEDURE AcceleratorTests.[test email is not sent if we detected something other than higgs-boson]
AS
BEGIN
--Assemble: Replace the SendHiggsBosonDiscoveryEmail with a spy.
EXEC tSQLt.SpyProcedure 'Accelerator.SendHiggsBosonDiscoveryEmail';

--Act: Call the AlertParticleDiscovered procedure - this is the procedure being tested.
EXEC Accelerator.AlertParticleDiscovered 'Proton';

--Assert: A spy records the parameters passed to the procedure in a *_SpyProcedureLog table.
-- Copy the EmailAddress parameter values that the spy recorded into the #Actual temp table.
SELECT EmailAddress
INTO #Actual
FROM Accelerator.SendHiggsBosonDiscoveryEmail_SpyProcedureLog;

-- Create an empty #Expected temp table that has the same structure as the #Actual table
SELECT TOP(0) * INTO #Expected FROM #Actual;

-- The SendHiggsBosonDiscoveryEmail should not have been called. So the #Expected table is empty.

-- Compare the data in the #Expected and #Actual tables
EXEC tSQLt.AssertEqualsTable '#Expected', '#Actual';

END;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

CREATE PROCEDURE AcceleratorTests.[test email is sent if we detected a higgs-boson]
AS
BEGIN
--Assemble: Replace the SendHiggsBosonDiscoveryEmail with a spy.
EXEC tSQLt.SpyProcedure 'Accelerator.SendHiggsBosonDiscoveryEmail';

--Act: Call the AlertParticleDiscovered procedure - this is the procedure being tested.
EXEC Accelerator.AlertParticleDiscovered 'Higgs Boson';

--Assert: A spy records the parameters passed to the procedure in a *_SpyProcedureLog table.
-- Copy the EmailAddress parameter values that the spy recorded into the #Actual temp table.
SELECT EmailAddress
INTO #Actual
FROM Accelerator.SendHiggsBosonDiscoveryEmail_SpyProcedureLog;

-- Create an empty #Expected temp table that has the same structure as the #Actual table
SELECT TOP(0) * INTO #Expected FROM #Actual;

-- Add a row to the #Expected table with the expected email address.
INSERT INTO #Expected
(EmailAddress)
VALUES
('[email protected]');

-- Compare the data in the #Expected and #Actual tables
EXEC tSQLt.AssertEqualsTable '#Expected', '#Actual';
END;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

CREATE PROC AcceleratorTests.[test foreign key is not violated if Particle color is in Color table]
AS
BEGIN
--Assemble: Fake the Particle and the Color tables to make sure they are empty and other
-- constraints will not be a problem
EXEC tSQLt.FakeTable 'Accelerator.Particle';
EXEC tSQLt.FakeTable 'Accelerator.Color';
-- Put the FK_ParticleColor foreign key constraint back onto the Particle table
-- so we can test it.
EXEC tSQLt.ApplyConstraint 'Accelerator.Particle', 'FK_ParticleColor';

-- Insert a record into the Color table. We'll reference this Id again in the Act
-- step.
INSERT INTO Accelerator.Color (Id) VALUES (7);

--Act: Attempt to insert a record into the Particle table.
INSERT INTO Accelerator.Particle (ColorId) VALUES (7);

--Assert: If any exception was thrown, the test will automatically fail. Therefore, the test
-- passes as long as there was no exception. This is one of the VERY rare cases when
-- at test case does not have an Assert step.
END
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

CREATE PROCEDURE AcceleratorTests.[test foreign key violated if Particle color is not in Color table]
AS
BEGIN
--Assemble: Fake the Particle and the Color tables to make sure they are empty and other
-- constraints will not be a problem
EXEC tSQLt.FakeTable 'Accelerator.Particle';
EXEC tSQLt.FakeTable 'Accelerator.Color';
-- Put the FK_ParticleColor foreign key constraint back onto the Particle table
-- so we can test it.
EXEC tSQLt.ApplyConstraint 'Accelerator.Particle', 'FK_ParticleColor';

--Act: Attempt to insert a record into the Particle table without any records in Color table.
-- We expect an exception to happen, so we capture the ERROR_MESSAGE()
DECLARE @err NVARCHAR(MAX); SET @err = '<No Exception Thrown!>';
BEGIN TRY
INSERT INTO Accelerator.Particle (ColorId) VALUES (7);
END TRY
BEGIN CATCH
SET @err = ERROR_MESSAGE();
END CATCH

--Assert: Check that trying to insert the record resulted in the FK_ParticleColor foreign key being violated.
-- If no exception happened the value of @err is still '<No Exception Thrown>'.
IF (@err NOT LIKE '%FK_ParticleColor%')
BEGIN
EXEC tSQLt.Fail 'Expected exception (FK_ParticleColor exception) not thrown. Instead:',@err;
END;
END;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

CREATE PROCEDURE AcceleratorTests.[test no particles are in a rectangle when there are no particles in the table]
AS
BEGIN
--Assemble: Fake the Particle table to make sure it is empty
EXEC tSQLt.FakeTable 'Accelerator.Particle';

DECLARE @ParticlesInRectangle INT;

--Act: Call the GetParticlesInRectangle Table-Valued Function and capture the number of rows it returns.
SELECT @ParticlesInRectangle = COUNT(1)
FROM Accelerator.GetParticlesInRectangle(0.0, 0.0, 1.0, 1.0);

--Assert: Check that 0 rows were returned
EXEC tSQLt.AssertEquals 0, @ParticlesInRectangle;
END;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

CREATE PROCEDURE
AcceleratorTests.[test ready for experimentation if 2 particles]
AS
BEGIN
--Assemble: Fake the Particle table to make sure
-- it is empty and has no constraints
EXEC tSQLt.FakeTable 'Accelerator.Particle';
INSERT INTO Accelerator.Particle (Id) VALUES (1);
INSERT INTO Accelerator.Particle (Id) VALUES (2);

DECLARE @Ready BIT;

--Act: Call the IsExperimentReady function
SELECT @Ready = Accelerator.IsExperimentReady();

--Assert: Check that 1 is returned from IsExperimentReady
EXEC tSQLt.AssertEquals 1, @Ready;

END;
Loading

0 comments on commit 20180c5

Please sign in to comment.