Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
ConvertFrom-Csv.Tests.ps183 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe "ConvertFrom-Csv" -Tags "CI" {5 6    BeforeAll {7        $testObject = "a", "1"8        $testcsv = Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath assets) -ChildPath TestCsv2.csv9        $testName = "Zaphod BeebleBrox"10        $testColumns = @"11a,b,c121,2,313"@14        $testTypeData = @"15#TYPE My.Custom.Object16a,b,c171,2,318"@19    }20 21    It "Should be able to be called" {22        { ConvertFrom-Csv -InputObject $testObject } | Should -Not -Throw23    }24 25    It "Should be able to pipe" {26        { $testObject | ConvertFrom-Csv } | Should -Not -Throw27    }28 29    It "Should have expected results when using piped inputs" {30        $csvContent   = Get-Content $testcsv31        $actualresult = $csvContent | ConvertFrom-Csv32 33        ,$actualresult | Should -BeOfType System.Array34        $actualresult[0] | Should -BeOfType PSCustomObject35 36        #Should have a name property in the result37        $actualresult[0].Name | Should -Be $testName38    }39 40    It "Should be able to set a delimiter" {41        { $testcsv | ConvertFrom-Csv -Delimiter ";" } | Should -Not -Throw42    }43 44    It "Should actually delimit the output" {45        $csvContent   = Get-Content $testcsv46        $actualresult = $csvContent | ConvertFrom-Csv -Delimiter ";"47 48        ,$actualresult | Should -BeOfType System.Array49        $actualresult[0] | Should -BeOfType PSCustomObject50 51        # ConvertFrom-Csv takes the first line of the input as a header by default52        $actualresult.Length | Should -Be $($csvContent.Length - 1)53    }54 55    It "Should be able to have multiple columns" {56        $actualData   = $testColumns | ConvertFrom-Csv57 58        $actualLength = $($( $actualData | Get-Member) | Where-Object {$_.MemberType -eq "NoteProperty" }).Length59 60        $actualLength | Should -Be 361    }62 63    It "Should Contain the Imported Type data" {64        $actualData = $testTypeData | ConvertFrom-Csv65        $actualData.PSObject.TypeNames.Count | Should -Be 266        $actualData.PSObject.TypeNames[0] | Should -BeExactly "My.Custom.Object"67        $actualData.PSObject.TypeNames[1] | Should -BeExactly "CSV:My.Custom.Object"68    }69}70 71Describe "ConvertFrom-Csv DRT Unit Tests" -Tags "CI" {72    It "Test ConvertFrom-Csv with pipelined InputObject and Header" {73        $inputObject = [pscustomobject]@{ First = 1; Second = 2 }74        $res = $inputObject | ConvertTo-Csv75        $result = $res | ConvertFrom-Csv -Header "Header1","Header2"76 77        $result[0].Header1 | Should -BeExactly "First"78        $result[0].Header2 | Should -BeExactly "Second"79        $result[1].Header1 | Should -BeExactly "1"80        $result[1].Header2 | Should -BeExactly "2"81    }82}83