Functions/Get-CombinedCsvString.Tests.ps1

describe "BitTitan.Runbooks.Common/Get-CombinedCsvString" -Tag "module", "unit" {
    # Import the function to test
    . "$($PSScriptRoot)\Get-CombinedCsvString.ps1"

    it "combines the CSVs when all the CSVs contain the same columns" {
        # Set up the function parameters
        $csvStrings = @(
            @"
sep=,
"name1","name2","name3","name4"
"0","False","False","False"
"1","False","False","True"
"2","False","True","False"
 
"@
,
            @"
sep=,
name1,name2,"name3","name4"
"3","False","True","True"
"4","True","False","False"
"5","True","False","True"
 
"@
,
            @"
"name1","name2","name3","name4"
"6","True","True","False"
"7","True","True","True"
 
"@
)

        # Call the function
        $combinedCsv = Get-CombinedCsvString -CsvStrings $csvStrings

        # Verify the output
        $expectedCsv = @"
sep=,
"name1","name2","name3","name4"
"0","False","False","False"
"1","False","False","True"
"2","False","True","False"
"3","False","True","True"
"4","True","False","False"
"5","True","False","True"
"6","True","True","False"
"7","True","True","True"
 
"@

        $combinedCsv | Should Be $expectedCsv
    }

    it "combines the CSVs while maintaining all the maintaining all the columns when the CSVs contain different columns" {
        # Set up the function parameters
        $csvStrings = @(
            @"
sep=,
"name1","name2","name3","name4"
"0","False","False","False"
"1","False","False","True"
"2","False","True","False"
 
"@
,
            @"
sep=,
"name5","name6","name7","name8"
"3","False","True","True"
"4","True","False","False"
"5","True","False","True"
 
"@
,
            @"
sep=,
"name9","name10","name11","name12"
"6","True","True","False"
"7","True","True","True"
 
"@
)

        # Call the function
        $combinedCsv = Get-CombinedCsvString -CsvStrings $csvStrings

        # Verify the output
        $expectedCsv = @"
sep=,
"name1","name2","name3","name4","name10","name11","name12","name5","name6","name7","name8","name9"
"0","False","False","False",,,,,,,,
"1","False","False","True",,,,,,,,
"2","False","True","False",,,,,,,,
,,,,,,,"3","False","True","True",
,,,,,,,"4","True","False","False",
,,,,,,,"5","True","False","True",
,,,,"True","True","False",,,,,"6"
,,,,"True","True","True",,,,,"7"
 
"@

        $combinedCsv | Should Be $expectedCsv
    }

    it "returns null when any of the CSVs are invalid" {
        # Set up the function parameters
        $csvStrings = @(
            @"
sep=,
"name1","name2","name3","name4"
"0","False","False","False"
"1","False","False","True"
"2","False","True","False"
 
"@
,
            " ",
            @"
sep=,
"name9","name10","name11","name12"
"6","True","True","False"
"7","True","True","True"
 
"@
)

        # Call the function
        $combinedCsv = Get-CombinedCsvString -CsvStrings $csvStrings -ErrorAction SilentlyContinue

        # Verify the output
        $combinedCsv | Should Be $null
    }

    context "when ConvertTo-CsvObject fails" {
        # Mock ConvertTo-CsvObject to return null
        mock ConvertTo-CsvObject {}

        it "returns null" {
            # Set up the function parameters
            $csvStrings = @(
                @"
sep=,
"name1","name2","name3","name4"
"0","False","False","False"
"1","False","False","True"
"2","False","True","False"
 
"@
,
            @"
sep=,
"name1","name2","name3","name4"
"3","False","True","True"
"4","True","False","False"
"5","True","False","True"
 
"@
,
            @"
sep=,
"name1","name2","name3","name4"
"6","True","True","False"
"7","True","True","True"
 
"@
)

            # Call the function
            $combinedCsv = Get-CombinedCsvString -CsvStrings $csvStrings -ErrorAction SilentlyContinue

            # Verify the output
            $combinedCsv | Should Be $null
        }
    }

    context "when ConvertTo-CsvString fails" {
        # Mock ConvertTo-CsvString to return null
        mock ConvertTo-CsvString {}

        it "returns null" {
            # Set up the function parameters
            $csvStrings = @(
                @"
sep=,
"name1","name2","name3","name4"
"0","False","False","False"
"1","False","False","True"
"2","False","True","False"
 
"@
,
            @"
sep=,
"name1","name2","name3","name4"
"3","False","True","True"
"4","True","False","False"
"5","True","False","True"
 
"@
,
            @"
sep=,
"name1","name2","name3","name4"
"6","True","True","False"
"7","True","True","True"
 
"@
)

            # Call the function
            $combinedCsv = Get-CombinedCsvString -CsvStrings $csvStrings -ErrorAction SilentlyContinue

            # Verify the output
            $combinedCsv | Should Be $null
        }
    }
}