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

TemplateFolder parameter does not seem to be obeyed in all scripts #53

Open
danieledwardgeorgehitchcock opened this issue Oct 21, 2024 · 0 comments

Comments

@danieledwardgeorgehitchcock
Copy link

danieledwardgeorgehitchcock commented Oct 21, 2024

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 :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant