Skip to content

Commit

Permalink
Refactor Tests Big
Browse files Browse the repository at this point in the history
  • Loading branch information
ili101 committed Aug 17, 2019
1 parent d894b8b commit f62a9cd
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 180 deletions.
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
"args": [""],
"cwd": "${workspaceFolder}"
},
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell Pester Tests Big",
"script": "Import-Module -Name '.\\' -Force ; Invoke-Pester -Script .\\Tests\\Join-Object.Tests.Big.ps1",
"args": [
""
],
"cwd": "${workspaceFolder}"
},
{
"type": "PowerShell",
"request": "launch",
Expand Down
78 changes: 31 additions & 47 deletions Tests/Join-Object.Tests.Big.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#Requires -Modules Pester
#Requires -Modules @{ ModuleName = 'Assert' ; ModuleVersion = '999.9.2' }
#Requires -Modules PSSQLite

$Verbose = @{ Verbose = $false }
#$Verbose = @{ Verbose = $true }

if ($PSScriptRoot) {
$ScriptRoot = $PSScriptRoot
}
Expand All @@ -17,43 +13,16 @@ elseif ($null -ne $psEditor.GetEditorContext().CurrentFile.Path -and $psEditor.G
else {
$ScriptRoot = '.'
}
. "$ScriptRoot\TestHelpers.ps1"

$TestDataSetBig10k = "$ScriptRoot\TestDataSetBig10k.db"
$TestDataSetBig100k = "$ScriptRoot\TestDataSetBig100k.db"

function Format-Test {
[CmdletBinding()]
param
(
[Parameter(Mandatory)]
[Hashtable]$Test
)
if ($TestDataSetName, $Test.Params.Left, $Test.Params.Right, $Test.Description -contains $null) {
Throw 'Missing param'
}

$Test.TestName = '{0}, {3}. {1} - {2}' -f $TestDataSetName, ($Test.Params.Left.Values -join ''), ($Test.Params.Right.Values -join ''), $Test.Description
$Test
}

function Get-Params {
[CmdletBinding()]
param
(
[Parameter(Mandatory)]
[Hashtable]$Param
)
$Output = Invoke-SqliteQuery -DataSource (Get-Variable -Name $TestDataSetName -ValueOnly) -Query ('SELECT * FROM {0}' -f $Param.Table) -As $Param.As
, $Output
}
$DataSetBig10k = "$ScriptRoot\TestDataSetBig10k.db"
$DataSetBig100k = "$ScriptRoot\TestDataSetBig100k.db"

Describe -Name 'Join-Object' -Fixture {
$TestDataSetName = 'TestDataSetBig100k'
Context -Name $TestDataSetName -Fixture {
It -name "Testing <TestName>" -TestCases @(
Context -Name ($DataSetName = 'DataSetBig100k') -Fixture {
It -Name "Testing: <TestName>" -TestCases @(
Format-Test @{
Description = 'Basic'
Params = @{
Params = @{
Left = @{ Table = 'authors' ; As = 'DataTable' }
Right = @{ Table = 'posts' ; As = 'DataTable' }
LeftJoinProperty = 'author_id'
Expand All @@ -62,28 +31,43 @@ Describe -Name 'Join-Object' -Fixture {
#RightMultiMode = 'SubGroups'
}
}
) -test {
) -Test {
param (
$Params,
#$TestDataSet,
$DataSet,
$TestName,
$Description,
$RunScript
$RunScript,
$ExpectedErrorMessage,
[ValidateSet('Test', 'Run')]
$ExpectedErrorOn
)
#if ($TestName -ne 'Small: PSCustomObjects - DataTable, DataTable') { Continue }

# Load Data
if ($RunScript) {
. $RunScript
}
$Params.Left = Get-Params -Param $Params.Left
$Params.Right = Get-Params -Param $Params.Right

# Load Data
try {
$DbConnection = New-SQLiteConnection -DataSource $DataSet
$Params.Left = Get-Params -Param $Params.Left -DbConnection $DbConnection
$Params.Right = Get-Params -Param $Params.Right -DbConnection $DbConnection
}
finally {
$DbConnection.Dispose()
}

# Execute Cmdlet
$Measure = Measure-Command {
$JoinedOutput = Join-Object @Params
}
Write-Host ("Execution Time: {0}, Count: {1}, Sample: {2}" -f $Measure, $JoinedOutput.Count, $JoinedOutput[-1])

if ($JoinedOutput -is [Data.DataTable]) {
Write-Host ("Execution Time: {0}, Count: {1}, Type: {2}." -f $Measure, $JoinedOutput.Rows.Count, $JoinedOutput.GetType())
Write-Host ('Sample:' + ($JoinedOutput.Rows[$JoinedOutput.Rows.Count - 1] | Out-String).TrimEnd())
}
else {
Write-Host ("Execution Time: {0}, Count: {1}, Type: {2}." -f $Measure, $JoinedOutput.Count, $JoinedOutput.GetType())
Write-Host ('Sample:' + ($JoinedOutput[-1] | Out-String).TrimEnd())
}
}
}
}
Expand Down
138 changes: 5 additions & 133 deletions Tests/Join-Object.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ elseif ($null -ne $psEditor.GetEditorContext().CurrentFile.Path -and $psEditor.G
else {
$ScriptRoot = '.'
}
. "$ScriptRoot\TestHelpers.ps1"

$DataSetSmall = {
$PSCustomObject = @(
Expand Down Expand Up @@ -67,136 +68,6 @@ $DataSetSmall = {
}
#. $DataSetSmall

function ConvertFrom-DataTable {
<#
.SYNOPSIS
Convert DataTable to PSCustomObject, Support Deserialized DataTable.
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateScript( { $_ -is [System.Data.DataRow] } )]
$InputObject
)
begin {
$First = $true
}
process {
if ($First) {
$First = $false
$Properties = if ($InputObject -is [System.Data.DataTable]) {
$InputObject.Columns.ColumnName
}
else {
$InputObject.PSObject.Properties.Name | Where-Object { $_ -notin ('RowError', 'RowState', 'Table', 'ItemArray', 'HasErrors') }
}
}
foreach ($DataRow in $InputObject) {
$RowHash = [ordered]@{ }
foreach ($Property in $Properties) {
if ($DataRow.$Property -is [DBNull]) {
$RowHash[$Property] = $null
}
else {
$RowHash[$Property] = $DataRow.$Property
}
}
[PSCustomObject]$RowHash
}
}
}
function ConvertTo-DataTable {
<#
.SYNOPSIS
Convert PSCustomObject to DataTable.
Warning: Column type taken from firs line, null will be [Object].
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)]
$InputObject
)
begin {
$OutputDataTable = [Data.DataTable]::new()
$First = $true
}
process {
foreach ($PSCustomObject in $InputObject) {
if ($First) {
$First = $false
foreach ($Property in $PSCustomObject.PSObject.Properties) {
$null = $OutputDataTable.Columns.Add($Property.Name, $Property.TypeNameOfValue)
}
}
$null = $OutputDataTable.Rows.Add($PSCustomObject.PSObject.Properties.Value)
}
}
end {
, $OutputDataTable
}
}


function Format-Test {
<#
.SYNOPSIS
Adds "TestName" and "DataSet" to the test.
Assumes $DataSetName and its value exists.
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory)]
[Hashtable]$Test
)
if ($DataSetName, $Test.Params.Left, $Test.Params.Right -contains $null) {
Throw 'Missing param'
}

$Data = '{0}, {1} - {2}' -f $DataSetName, $Test.Params.Left, $Test.Params.Right
$OutputType = switch ($Test.Params) {
{ $_.DataTable } { 'DataTable' }
{ $_.PassThru } { 'PassThru' }
}
$OutputType = if ($OutputType) {
'{0}' -f ($OutputType -join '&')
}
$Description = if ($Test.Description) {
'({0})' -f $Test.Description
}



$Test.TestName = (($Data, $Test.Params.Type, $OutputType, $Description) -ne $null) -join '. '
$Test.DataSet = Get-Variable -Name $DataSetName -ValueOnly
$Test
}
function Get-Params {
[CmdletBinding()]
param
(
[Parameter(Mandatory)]
[String]$Param
)
$ParamSplit = $Param -split '(?<=\])(?<!$)|(?=\[)(?!^)'
if ($ParamSplit[0] -eq '[PSCustomObject]') {
$Output = (Get-Variable -Name $ParamSplit[1]).Value | ConvertFrom-DataTable
}
elseif ($ParamSplit[0] -eq '[DataTable]') {
$Output = (Get-Variable -Name $ParamSplit[1]).Value | ConvertTo-DataTable
}
else {
$Output = (Get-Variable -Name $ParamSplit[0]).Value
}
if ($ParamSplit[-1] -like '`[*]') {
, $Output.Item($ParamSplit[-1].Trim('[]'))
}
else {
, $Output
}
}

Describe -Name 'Join-Object' -Fixture {
Context -Name ($DataSetName = 'DataSetSmall') -Fixture {
Class EqualityComparerMy : Collections.Generic.EqualityComparer[Object] {
Expand Down Expand Up @@ -931,7 +802,7 @@ Describe -Name 'Join-Object' -Fixture {
if ($FilterTests) {
$TestCases = $TestCases | Where-Object TestName -In $FilterTests
}
It -name "Testing <TestName>" -TestCases $TestCases -test {
It -Name "Testing: <TestName>" -TestCases $TestCases -Test {
param (
$Params,
$DataSet,
Expand All @@ -941,11 +812,12 @@ Describe -Name 'Join-Object' -Fixture {
[ValidateSet('Test', 'Run')]
$ExpectedErrorOn
)
# Load Data
. $DataSet
if ($RunScript) {
. $RunScript
}

# Load Data
. $DataSet
$Params.Left = Get-Params -Param $Params.Left
$Params.Right = Get-Params -Param $Params.Right

Expand Down
Loading

0 comments on commit f62a9cd

Please sign in to comment.