Functions/New-MergedObjectHashTable.Tests.ps1

describe "BitTitan.Runbooks.Common/New-MergedObjectHashTable" -Tag "module", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\New-MergedObjectHashTable.ps1"

    it "merges two lists which contain the same values" {
        # Set up the function input
        $currentObjects = @(
            [PSCustomObject]@{
                Key     = "1"
                Value   = "one"
            },
            [PSCustomObject]@{
                Key     = "2"
                Value   = "two"
            }
        )
        $expectedObjects = @(
            [PSCustomObject]@{
                Key     = "1"
                Value   = "one"
            },
            [PSCustomObject]@{
                Key     = "2"
                Value   = "two"
            }
        )

        # Call the function
        $mergedHashTable = New-MergedObjectHashTable -CurrentObjects $currentObjects -ExpectedObjects $expectedObjects -KeyProperty "Key"

        # Verify the output
        $mergedHashTable.count | Should Be 2
        $mergedHashTable."1" | Should Not Be $null
        $mergedHashTable."1".Current | Should Not Be $null
        $mergedHashTable."1".Current.Value | Should Be "one"
        $mergedHashTable."1".Expected | Should Not Be $null
        $mergedHashTable."1".Expected.Value | Should Be "one"

        $mergedHashTable."2" | Should Not Be $null
        $mergedHashTable."2".Current | Should Not Be $null
        $mergedHashTable."2".Current.Value | Should Be "two"
        $mergedHashTable."2".Expected | Should Not Be $null
        $mergedHashTable."2".Expected.Value | Should Be "two"
    }

    it "merges two lists which contain different values" {
        # Set up the function input
        $currentObjects = @(
            [PSCustomObject]@{
                Key     = "1"
                Value   = "one"
            },
            [PSCustomObject]@{
                Key     = "2"
                Value   = "two"
            }
        )
        $expectedObjects = @(
            [PSCustomObject]@{
                Key     = "3"
                Value   = "three"
            },
            [PSCustomObject]@{
                Key     = "4"
                Value   = "four"
            }
        )

        # Call the function
        $mergedHashTable = New-MergedObjectHashTable -CurrentObjects $currentObjects -ExpectedObjects $expectedObjects -KeyProperty "Key"

        # Verify the output
        $mergedHashTable.count | Should Be 4
        $mergedHashTable."1" | Should Not Be $null
        $mergedHashTable."1".Current.Value | Should Be "one"
        $mergedHashTable."1".Expected | Should Be $null

        $mergedHashTable."2" | Should Not Be $null
        $mergedHashTable."2".Current.Value | Should Be "two"
        $mergedHashTable."2".Expected | Should Be $null

        $mergedHashTable."3" | Should Not Be $null
        $mergedHashTable."3".Current | Should Be $null
        $mergedHashTable."3".Expected | Should Not Be $null
        $mergedHashTable."3".Expected.Value | Should Be "three"

        $mergedHashTable."4" | Should Not Be $null
        $mergedHashTable."4".Current | Should Be $null
        $mergedHashTable."4".Expected | Should Not Be $null
        $mergedHashTable."4".Expected.Value | Should Be "four"
    }

    it "returns null when any of the lists contain values with null key properties" {
        # Set up the function input
        $currentObjects = @(
            [PSCustomObject]@{
                Key     = $null
                Value   = "one"
            },
            [PSCustomObject]@{
                Key     = "2"
                Value   = "two"
            }
        )
        $expectedObjects = @(
            [PSCustomObject]@{
                Key     = "3"
                Value   = "three"
            },
            [PSCustomObject]@{
                Key     = "4"
                Value   = "four"
            }
        )

        # Call the function
        $mergedHashTable = New-MergedObjectHashTable -CurrentObjects $currentObjects -ExpectedObjects $expectedObjects -KeyProperty "Key"

        # Verify the output
        $mergedHashTable | Should Be $null

        # Set up the function input
        $currentObjects = @(
            [PSCustomObject]@{
                Key     = "1"
                Value   = "one"
            },
            [PSCustomObject]@{
                Key     = "2"
                Value   = "two"
            }
        )
        $expectedObjects = @(
            [PSCustomObject]@{
                Key     = $null
                Value   = "three"
            },
            [PSCustomObject]@{
                Key     = "4"
                Value   = "four"
            }
        )

        # Call the function
        $mergedHashTable = New-MergedObjectHashTable -CurrentObjects $currentObjects -ExpectedObjects $expectedObjects -KeyProperty "Key"

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