-
-
Notifications
You must be signed in to change notification settings - Fork 4
Description
Hi,
First off, thanks for your amazing code and YouTube tutorials :-)
I have created a set of tests using your script for a single database / testclass and I am now looking to push this out to all of my other databases.
I have created a PowerShell script which uses your module to loop through the databases that I want see here (some values modified):
$RootFolder = 'C:\Test'
$Server = 'Server'
$SqlInstance = Connect-DbaInstance -SqlInstance $Server -TrustServerCertificate
$Databases = 'Database1','Database2','Database3'
$TemplateFolder = 'C:\Test\Templates'
foreach ($Database in $Databases){
$Folder = '{0}\{1}_Testing\Stored Procedures' -f $RootFolder,$Database
$TestClass = '{0}_TestBasic' -f $Database
if (-not (Test-Path -Path $Folder)) {
New-Item -ItemType Directory -Path $Folder
}
Invoke-PSTGTestGenerator -SqlInstance $SqlInstance -Database $Database -OutputPath $Folder -TestClass $TestClass -TemplateFolder $TemplateFolder
}
I have also copied and modifiied the templates found in $projectroot\internal\templates
some examples below:
DatabaseCollationTest.template
CREATE PROCEDURE [___TESTCLASS___].[___TESTNAME___]
AS
BEGIN
SET NOCOUNT ON;
----- ASSEMBLE -----------------------------------------------
DECLARE @expected VARCHAR(255),
@actual VARCHAR(255)
SELECT @expected = '___COLLATION___'
----- ACT ----------------------------------------------------
SELECT @actual = CAST(DATABASEPROPERTYEX('___DATABASE___', 'Collation') AS VARCHAR(255));
----- ASSERT -------------------------------------------------
EXEC tSQLt.AssertEquals @expected, @actual;
END;
FunctionParameterTest.template
CREATE PROCEDURE [___TESTCLASS___].[___TESTNAME___]
AS
BEGIN
SET NOCOUNT ON;
----- ASSEMBLE -----------------------------------------------
-- Create the tables
CREATE TABLE #actual
(
[ParameterName] NVARCHAR(128) NOT NULL,
[DataType] sysname NOT NULL,
[MaxLength] SMALLINT NOT NULL,
[Precision] TINYINT NOT NULL,
[Scale] TINYINT NOT NULL
);
CREATE TABLE #expected
(
[ParameterName] NVARCHAR(128) NOT NULL,
[DataType] sysname NOT NULL,
[MaxLength] SMALLINT NOT NULL,
[Precision] TINYINT NOT NULL,
[Scale] TINYINT NOT NULL
);
INSERT INTO #expected
(
ParameterName,
DataType,
MaxLength,
Precision,
Scale
)
VALUES
___PARAMETERS___
----- ACT ----------------------------------------------------
INSERT INTO #actual
(
ParameterName,
DataType,
MaxLength,
Precision,
Scale
)
SELECT pm.name AS ParameterName,
t.name AS DataType,
pm.max_length AS MaxLength,
pm.precision AS [Precision],
pm.scale AS Scale
FROM ___DATABASE___.sys.parameters AS pm
INNER JOIN ___DATABASE___.sys.sql_modules AS sm
ON sm.object_id = pm.object_id
INNER JOIN ___DATABASE___.sys.objects AS o
ON sm.object_id = o.object_id
INNER JOIN ___DATABASE___.sys.schemas AS s
ON s.schema_id = o.schema_id
INNER JOIN ___DATABASE___.sys.types AS t
ON pm.system_type_id = t.system_type_id
AND pm.user_type_id = t.user_type_id
WHERE s.name = '___SCHEMA___'
AND o.name = '___NAME___'
AND pm.name <> '';
----- ASSERT -------------------------------------------------
-- Assert to have the same values
EXEC tSQLt.AssertEqualsTable @Expected = '#expected', @Actual = '#actual';
END;
ProcedureParameterTest.template
CREATE PROCEDURE [___TESTCLASS___].[___TESTNAME___]
AS
BEGIN
SET NOCOUNT ON;
----- ASSEMBLE -----------------------------------------------
-- Create the tables
CREATE TABLE #actual
(
[ParameterName] NVARCHAR(128) NOT NULL,
[DataType] sysname NOT NULL,
[MaxLength] SMALLINT NOT NULL,
[Precision] TINYINT NOT NULL,
[Scale] TINYINT NOT NULL
);
CREATE TABLE #expected
(
[ParameterName] NVARCHAR(128) NOT NULL,
[DataType] sysname NOT NULL,
[MaxLength] SMALLINT NOT NULL,
[Precision] TINYINT NOT NULL,
[Scale] TINYINT NOT NULL
);
INSERT INTO #expected
(
ParameterName,
DataType,
MaxLength,
Precision,
Scale
)
VALUES
___PARAMETERS___
----- ACT ----------------------------------------------------
INSERT INTO #actual
(
ParameterName,
DataType,
MaxLength,
Precision,
Scale
)
SELECT pm.name AS ParameterName,
t.name AS DataType,
pm.max_length AS MaxLength,
pm.precision AS [Precision],
pm.scale AS Scale
FROM ___DATABASE___.sys.parameters AS pm
INNER JOIN ___DATABASE___.sys.procedures AS ps
ON pm.object_id = ps.object_id
INNER JOIN ___DATABASE___.sys.schemas AS s
ON s.schema_id = ps.schema_id
INNER JOIN ___DATABASE___.sys.types AS t
ON pm.system_type_id = t.system_type_id
AND pm.user_type_id = t.user_type_id
WHERE s.name = '___SCHEMA___'
AND ps.name = '___NAME___';
----- ASSERT -------------------------------------------------
-- Assert to have the same values
EXEC tSQLt.AssertEqualsTable @Expected = '#expected', @Actual = '#actual';
END;
Out of the above scripts (there are others corresponding to all of the basic tests in the repo), only the DatabaseCollationTest.template
seems to be being applied by the script. As this is the first script in the directory, I am wondering if the folder is being correctly iterated over?
I'd appreciate any assistance in getting this working :-)