Functions/Get-FilteredCsvString.Tests.ps1

describe "BitTitan.Runbooks.Common/Get-FilteredCsvString" -Tag "module", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\Get-FilteredCsvString.ps1"

    it "returns the same CSV when given a CSV and no specified columns" {
        # Set up the function parameters
        $csv = @"
sep=,
"name1","name2","name3"
"1","True","False"
"2","False","True"
 
"@


        # Call the function
        $filteredCsv = Get-FilteredCsvString -Csv $csv -Filtering "and"

        # Verify the output
        $filteredCsv | Should Be $csv
    }

    it "filters the CSV by the column using 'and' filtering when given one specified column" {
        # Set up the function parameters
        $csv = @"
sep=,
"name1","name2","name3"
"1","True","False"
"2","False","True"
 
"@

        $columns = "name2"

        # Call the function
        $filteredCsv = Get-FilteredCsvString -Csv $csv -Columns $columns -Filtering "and"

        # Verify the output
        $expectedCsv = @"
sep=,
"name1","name2","name3"
"1","True","False"
 
"@

        $filteredCsv | Should Be $expectedCsv
    }

    it "filters the CSV by the column using 'and' filtering when given one specified column and the value used to filter" {
        # Set up the function parameters
        $csv = @"
sep=,
"name1","name2","name3"
"1","True","False"
"2","False","True"
 
"@

        $columns = "name2"
        $columnValues = $false

        # Call the function
        $filteredCsv = Get-FilteredCsvString -Csv $csv -Columns $columns -ColumnValues $columnValues -Filtering "and"

        # Verify the output
        $expectedCsv = @"
sep=,
"name1","name2","name3"
"2","False","True"
 
"@

        $filteredCsv | Should Be $expectedCsv
    }


    it "filters the CSV by the column using 'or' filtering when given one specified column" {
        # Set up the function parameters
        $csv = @"
sep=,
"name1","name2","name3"
"1","True","False"
"2","False","True"
 
"@

        $columns = "name2"

        # Call the function
        $filteredCsv = Get-FilteredCsvString -Csv $csv -Columns $columns -Filtering "or"

        # Verify the output
        $expectedCsv = @"
sep=,
"name1","name2","name3"
"1","True","False"
 
"@

        $filteredCsv | Should Be $expectedCsv
    }

    it "filters the CSV by columns using 'and' filtering when given more than one specified column" {
        # Set up the function parameters
        $csv = @"
sep=,
"name1","name2","name3"
"1","True","False"
"2","False","True"
 
"@

        $columns = @("name2", "name3")

        # Call the function
        $filteredCsv = Get-FilteredCsvString -Csv $csv -Columns $columns -Filtering "and"

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

    it "filters the CSV by columns using 'or' filtering when given more than one specified column" {
        # Set up the function parameters
        $csv = @"
sep=,
"name1","name2","name3"
"1","True","False"
"2","False","True"
 
"@

        $columns = @("name2", "name3")

        # Call the function
        $filteredCsv = Get-FilteredCsvString -Csv $csv -Columns $columns -Filtering "or"

        # Verify the output
        $expectedCsv = @"
sep=,
"name1","name2","name3"
"1","True","False"
"2","False","True"
 
"@

        $filteredCsv | Should Be $expectedCsv
    }

    it "filters the CSV by columns using 'and' filtering when given a CSV and a column which does not exist to return null" {
        # Set up the function parameters
        $csv = @"
sep=,
"name1","name2","name3"
"1","True","False"
"2","False","True"
 
"@

        $columns = "name4"

        # Call the function
        $filteredCsv = Get-FilteredCsvString -Csv $csv -Columns $columns -Filtering "and"

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

    it "filters the CSV by columns using 'or' filtering when given a CSV and a column which does not exist to return null" {
        # Set up the function parameters
        $csv = @"
sep=,
"name1","name2","name3"
"1","True","False"
"2","False","True"
 
"@

        $columns = "name4"

        # Call the function
        $filteredCsv = Get-FilteredCsvString -Csv $csv -Columns $columns -Filtering "or"

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

    it "returns null when the number of columns does not match the number of column values" {
        # Set up the function parameters
        $csv = @"
sep=,
"name1","name2","name3"
"1","True","False"
"2","False","True"
 
"@

        $columns = "name1"
        $columnValues = @($true, $false)

        # Call the function
        $filteredCsv = Get-FilteredCsvString -Csv $csv -Columns $columns -ColumnValues $columnValues -Filtering "or" -ErrorAction SilentlyContinue

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

    # Set up functions to fail
    $functionsToFail = @(
        "ConvertTo-CsvObject",
        "ConvertTo-CsvString"
    )
    foreach ($function in $functionsToFail) {
        context "when $($function) fails" {
            # Mock the function to return null
            mock $function {}

            it "returns null" {
                # Set up the function parameters
                $csv = $csv = @"
sep=,
"name1","name2","name3"
"1","True","False"
"2","False","True"
 
"@

                $columns = ""

                # Call the function
                $filteredCsv = Get-FilteredCsvString -Csv $csv -Columns $columns -Filtering "or" -ErrorAction SilentlyContinue

                # Verify the output
                $filteredCsv | Should Be $null

                # Call the function
                $filteredCsv = Get-FilteredCsvString -Csv $csv -Columns $columns -Filtering "and" -ErrorAction SilentlyContinue

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