Csv.Tests.ps1

####################################################################################################
# Declarations
####################################################################################################

# Set InformationPreference
$InformationPreference = "Continue"

####################################################################################################
# Functions
####################################################################################################

# Add modules folder to path if not already added
if (!$env:PSModulePath.EndsWith(";$(Get-Location)\Modules")) {
    $env:PSModulePath += ";$(Get-Location)\Modules"
}

# Import functions from BitTitan.Runbooks.Modules
Import-Module BitTitan.Runbooks.Common -Force

####################################################################################################
# The tests
####################################################################################################

describe "Modules/BitTitan.Runbooks.Common/Csv/Get-FilteredCsvString" -Tags "module", "common", "unit" {

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

        $columns = ""

        it "returns the same CSV" {
            # Call the function
            $filteredCsv = Get-FilteredCsvString -Csv $csv -Columns $columns -Filtering "and"

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

    context "when given a CSV and one specified column" {
        # Set up the function parameters
        $csv = @"
sep=,
"name1","name2","name3"
"1","True","False"
"2","False","True"
 
"@

        $columns = "name2"

        it "filters the CSV by the column using 'and' filtering" {
            # 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 'or' filtering" {
            # 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
        }
    }

    context "when given a CSV and more than one specified column" {
        # Set up the function parameters
        $csv = @"
sep=,
"name1","name2","name3"
"1","True","False"
"2","False","True"
 
"@

        $columns = "name2, name3"

        it "filters the CSV by columns using 'and' filtering" {
            # Call the function
            $filteredCsv = Get-FilteredCsvString -Csv $csv -Columns $columns -Filtering "and"

            # Verify the output_
            $expectedCsv = ""
            $filteredCsv | Should Be $expectedCsv
        }

        it "filters the CSV by the column using 'or' filtering" {
            # 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
        }
    }

}