tests/OledbSql.Tests.ps1


$here = Split-Path -Parent $MyInvocation.MyCommand.Path

Import-Module (Join-Path $here '..\OledbSql.psd1')

# Find the latest version of the ACE data provider, for the connection string.
$ProviderVersion = (New-Object System.Data.OleDb.OleDbEnumerator).GetElements() | Foreach {
    if ($_.SOURCES_NAME -match '^Microsoft\.ACE\.OLEDB\.([\d\.]+)$') {
        [decimal]$Matches[1]
    }
} | Sort | Select -Last 1

$connectionString = 'Provider=Microsoft.ACE.OLEDB.{0};Data Source={1};Extended Properties="Text;HDR=Yes;FMT=Delimited;"' -f $ProviderVersion, $here

Describe 'New-OledbConnection' {
    It 'Create and open an OLEDB Connection.' {
        $connection = New-OledbConnection $connectionString
        $connection.Open()
        $connection.State | Should Be 'Open'
        $connection.Close()
    }
}

Describe 'Invoke-OledbSql' {
    Context 'Create an OLEDB Connection, then read from a CSV file.' {
        $result = Invoke-OledbSql -Connection $connectionString -Sql 'select * from [test#csv]' | Select -First 1

        It 'Return the value of the first column.' {
            $result.'test#csv.test' | Should Be 'Success1'
        }

        It 'Return the value of the second column, which has the same name as the first and was de-duplicated.' {
            $result.'test#csv.test1' | Should Be 'Success2'
        }
    }
}