Private/Tests/Get-EuroExchange.Tests.ps1
# ? TITEL Get-EuroExchange (UTest) # ? DESCRIPTION # ? TAGS UTest Pester # ? VERSION 2019.01.01 using namespace System using namespace System.Runtime.InteropServices $ErrorActionPreference = [System.Management.Automation.ActionPreference]::Continue Set-StrictMode -Version Latest $CmdletRoot = Split-Path -Parent $MyInvocation.MyCommand.Path $CmdletName = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.ps1', [String]::Empty Set-Location -Path (Join-Path -Path $CmdletRoot -ChildPath "..\..\Public") . ".\$CmdletName.ps1" Describe "$CmdletName.ps1" { Context "Parameter" { It "Parameter-Signatur korrekt" { Get-Command -Name Get-EuroExchange | Should -HaveParameter Currency -Type String -Mandatory Get-Command -Name Get-EuroExchange | Should -HaveParameter Euros -Type Decimal -DefaultValue 1 Get-Command -Name Get-EuroExchange | Should -HaveParameter ListCurrency -Type System.Management.Automation.SwitchParameter } It "Parameter-Metadaten => 'Currency'" { $target = (Get-Command Get-EuroExchange).Parameters['Currency'] $target.ParameterSets.Keys | Should -Be 'Calculate' $target.Aliases | Should -Be 'Währung' $target1 = $target.Attributes.Where({$_ -is [System.Management.Automation.ValidateSetAttribute]}) $target1.ValidValues | Should -Be 'USD','JPY','BGN','CZK','DKK','GBP','HUF','PLN','RON','SEK','CHF','ISK','NOK','HRK','RUB','TRY','AUD','BRL','CAD','CNY','HKD','IDR','ILS','INR','KRW','MXN','MYR','NZD','PHP','SGD','THB','ZAR' $target2 = $target.Attributes.Where({$_ -is [System.Management.Automation.ParameterAttribute]}) $target2.Mandatory | should -BeTrue $target2.ValueFromPipeline | should -BeTrue $target2.ValueFromPipelineByPropertyName | should -BeTrue } It "Parameter-Metadaten => 'Euros'" { $target = (Get-Command Get-EuroExchange).Parameters['Euros'] $target.ParameterSets.Keys | Should -Be 'Calculate' $target.Aliases | Should -Be 'Euronen' $target1 = $target.Attributes.Where({$_ -is [System.Management.Automation.ValidateRangeAttribute]}) $target1.MinRange | Should -Be 0.0001 $target1.MaxRange | Should -Be 1000000 $target2 = $target.Attributes.Where({$_ -is [System.Management.Automation.ParameterAttribute]}) $target2.Mandatory | should -BeFalse $target2.ValueFromPipeline | should -BeFalse $target2.ValueFromPipelineByPropertyName | should -BeTrue } It "Parameter-Metadaten => 'ListCurrency'" { $target = (Get-Command Get-EuroExchange).Parameters['ListCurrency'] $target.ParameterSets.Keys | Should -Be 'Overview' $target.Aliases | Should -BeNullOrEmpty $target.SwitchParameter | Should -BeTrue $target1 = $target.Attributes.Where({$_ -is [System.Management.Automation.ParameterAttribute]}) $target1.Mandatory | should -BeTrue $target1.ValueFromPipeline | should -BeFalse $target1.ValueFromPipelineByPropertyName | should -BeFalse } It "Throw => Get-EuroExchange -Currency USD -ListCurrency" { { Get-EuroExchange -Currency USD -ListCurrency } | Should -Throw -ErrorId "AmbiguousParameterSet" } It "Throw => Get-EuroExchange -Euros 100 -ListCurrency" { { Get-EuroExchange -Euros 100 -ListCurrency } | Should -Throw -ErrorId "AmbiguousParameterSet" } It "Throw => Get-EuroExchange -Currency XXX" { { Get-EuroExchange -Currency XXX } | Should -Throw -ErrorId "ParameterArgumentValidationError" } It "Throw => Get-EuroExchange -Currency USD -Euros -100" { { Get-EuroExchange -Currency USD -Euros -100 } | Should -Throw -ErrorId "ParameterArgumentValidationError" } It "Throw => Get-EuroExchange -Currency USD -Euros 1000001" { { Get-EuroExchange -Currency USD -Euros 1000001 } | Should -Throw -ErrorId "ParameterArgumentValidationError" } It "Throw => Get-EuroExchange -Currency USD -Euros 0" { { Get-EuroExchange -Currency USD -Euros 0 } | Should -Throw -ErrorId "ParameterArgumentValidationError" } It "Throw => Get-EuroExchange -Currency USD -Euros hundert" { { Get-EuroExchange -Currency USD -Euros hundert } | Should -Throw -ErrorId "ParameterArgumentTransformationError" } It "Throw => Get-EuroExchange -Currency USD, RUB" { { Get-EuroExchange -Currency USD, RUB } | Should -Throw -ErrorId "ParameterArgumentTransformationError" } It "Throw => Get-EuroExchange -Currency USD -Euros 50, 100" { { Get-EuroExchange -Currency USD -Euros 50, 100 } | Should -Throw -ErrorId "ParameterArgumentTransformationError" } } Context "Rückgabeobjekte" { $content = Invoke-WebRequest -Uri "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml" | Select-Object -ExpandProperty Content $cubes = ([xml]$content).Envelope.Cube.Cube.Cube It "Get-EuroExchange -Currency USD: target -isNot [Array]" { Get-EuroExchange -Currency USD | Should -Not -BeOfType Array } It "Get-EuroExchange -Currency USD: Rückgabeobjekt -is [PSCustomObject]" { Get-EuroExchange -Currency USD | Should -BeOfType PSCustomObject } It "Get-EuroExchange -Currency USD: target.Currency -ceq USD" { $target = Get-EuroExchange -Currency USD $target.Currency | Should -BeExactly "USD" $target.Currency | Should -BeOfType String } It "Get-EuroExchange -Currency USD: target.Rate -eq ECB.Value" { [decimal]$expected = $cubes | Where-Object -Property Currency -EQ -Value USD | Select-Object -ExpandProperty Rate $target = Get-EuroExchange -Currency USD $target.Rate | Should -BeExactly $expected $target.Rate | Should -BeOfType Decimal } It "Get-EuroExchange -Currency USD: target.Euros -eq 1" { $target = Get-EuroExchange -Currency USD $target.Euros | Should -BeExactly 1 $target.Euros | Should -BeOfType Decimal } It "Get-EuroExchange -Currency USD: target.SumCurrency -eq (1 * ECB.Value)" { [decimal]$expected = $cubes | Where-Object -Property Currency -EQ -Value USD | Select-Object -ExpandProperty Rate $target = Get-EuroExchange -Currency USD $target.SumCurrency | Should -BeExactly $expected $target.SumCurrency | Should -BeOfType Decimal } It "Get-EuroExchange -Currency USD -Euros 100: target.SumCurrency -eq (100 * ECB.Value)" { [decimal]$expected = $cubes | Where-Object -Property Currency -EQ -Value USD | Select-Object -ExpandProperty Rate $expected *= 100 $target = Get-EuroExchange -Currency USD -Euros 100 $target.SumCurrency | Should -BeExactly $expected $target.SumCurrency | Should -BeOfType Decimal } It "Get-EuroExchange -ListCurrency => target -is PSCustomObject" { Get-EuroExchange -ListCurrency | Should -BeOfType PSCustomObject } It "Get-EuroExchange -ListCurrency => target -eq Ecb" { $expected = $cubes.currency | Sort-Object Get-EuroExchange -ListCurrency | Select-Object -ExpandProperty Currency | Should -be $expected } It "'USD', 'RUB', 'AUD' | Get-EuroExchange => target -eq Ecb" { $expected = $cubes | Where-Object -Property currency -in 'USD', 'RUB', 'AUD' | Select-Object -ExpandProperty rate | ForEach-Object { [decimal]$_} $target = 'USD', 'RUB', 'AUD' | Get-EuroExchange | Select-Object -ExpandProperty Rate $target | Should -BeExactly $expected } It "'USD', 'RUB', 'AUD' | Get-EuroExchange -Euros 100 => target -eq Ecb" { $expected = $cubes | Where-Object -Property currency -in 'USD', 'RUB', 'AUD' | Select-Object -ExpandProperty rate | ForEach-Object {[decimal]$_ * 100} $target = 'USD', 'RUB', 'AUD' | Get-EuroExchange -Euros 100 | Select-Object -ExpandProperty SumCurrency $target | Should -BeExactly $expected } It "Get-EuroExchange -ListCurrency | Get-EuroExchange -Euros 1000 => target -eq Ecb" { $expected = $cubes | Sort-Object -Property currency | Select-Object -ExpandProperty rate | ForEach-Object {[decimal]$_ * 1000} $target = Get-EuroExchange -ListCurrency | Get-EuroExchange -Euros 1000 | Select-Object -ExpandProperty SumCurrency $target | Should -BeExactly $expected } It "'USD,10', 'RUB,100', 'AUD,1000' | ConvertFrom-Csv -Header Currency, Euros | Get-EuroExchange => target -eq Ecb" { $expected0 = $cubes | Where-Object currency -eq 'USD'| Select-Object -ExpandProperty rate | ForEach-Object {[decimal]$_ * 10} $expected1 = $cubes | Where-Object currency -eq 'RUB'| Select-Object -ExpandProperty rate | ForEach-Object {[decimal]$_ * 100} $expected2 = $cubes | Where-Object currency -eq 'AUD'| Select-Object -ExpandProperty rate | ForEach-Object {[decimal]$_ * 1000} $target = 'USD,10', 'RUB,100', 'AUD,1000' | ConvertFrom-Csv -Header Currency, Euros | Get-EuroExchange $target[0].SumCurrency | Should -BeExactly $expected0 $target[1].SumCurrency | Should -BeExactly $expected1 $target[2].SumCurrency | Should -BeExactly $expected2 } It "'USD,10', 'RUB,100', 'AUD,1000' | ConvertFrom-Csv -Header Währung, Euronen | Get-EuroExchange => target -eq Ecb" { $expected0 = $cubes | Where-Object currency -eq 'USD'| Select-Object -ExpandProperty rate | ForEach-Object {[decimal]$_ * 10} $expected1 = $cubes | Where-Object currency -eq 'RUB'| Select-Object -ExpandProperty rate | ForEach-Object {[decimal]$_ * 100} $expected2 = $cubes | Where-Object currency -eq 'AUD'| Select-Object -ExpandProperty rate | ForEach-Object {[decimal]$_ * 1000} $target = 'USD,10', 'RUB,100', 'AUD,1000' | ConvertFrom-Csv -Header Währung, Euronen | Get-EuroExchange $target[0].SumCurrency | Should -BeExactly $expected0 $target[1].SumCurrency | Should -BeExactly $expected1 $target[2].SumCurrency | Should -BeExactly $expected2 } } Context 'ScriptAnalyzer Tests' { [Array]$testCase = @() $testCase += Invoke-ScriptAnalyzer -Path ".\$CmdletName.ps1" -Severity Error, Warning -ExcludeRule PSUseSingularNouns | Foreach-Object { @{ Line = $_.Line RuleName = $_.RuleName Message = $_.Message } } It "Ist die Skript-Datei frei von Regelverletzungen?" { $testCase.Count | Should -Be 0 } if ($testCase.Count -gt 0) { It "REGEL-Verletzung: LINE <Line> | RULE <RuleName> | MESSAGE <Message>" -TestCases $testCase { param( $Line, $RuleName, $Message ) $RuleName | Should BeNullOrEmpty } } } } |