Unit testing data operations with Access database in VB.NET

DescriptionThis code sample will provide a method to unit test backend database operations, adding records, updating records and removal of records using ms-access database table.I’ve been on various developer forums where developers have issues with their data operations and in

 
 
 
 
 
(0)
169 times
Add to favorites
11/23/2017
E-mail Twitter del.icio.us Digg Facebook
Imports System.IO

Public Module Extensions
    ''' <summary>
    ''' Given a folder name return all parents according to level
    ''' </summary>
    ''' <param name="FolderName">Sub-folder name</param>
    ''' <param name="level">Level to move up the folder chain</param>
    ''' <returns>A physical folder path</returns>
    ''' <example>
    ''' <code source="CodeExamples\FolderDemos.vb" language="vbnet" title="VB.NET Examples"/>
    ''' </example>
    <Runtime.CompilerServices.Extension>
    Public Function UpperFolder(ByVal FolderName As String, ByVal level As Decimal) As String
        Return UpperFolder(FolderName, Convert.ToInt32(level))
    End Function

    ''' <summary>
    ''' Given a folder name return all parents according to level
    ''' </summary>
    ''' <param name="FolderName">Sub-folder name</param>
    ''' <param name="level">Level to move up the folder chain</param>
    ''' <returns>List of folders dependent on level parameter</returns>
    ''' <example>
    ''' <code source="CodeExamples\FolderDemos.vb" language="vbnet" title="VB.NET Examples"/>
    ''' </example>
    <Runtime.CompilerServices.Extension>
    Public Function UpperFolder(ByVal FolderName As String, ByVal level As Integer) As String
        Dim TheList As New List(Of String)()

        Do While Not String.IsNullOrEmpty(FolderName)
            Dim temp = Directory.GetParent(FolderName)
            If temp Is Nothing Then
                Exit Do
            End If
            FolderName = Directory.GetParent(FolderName).FullName
            TheList.Add(FolderName)
        Loop

        If TheList.Count > 0 AndAlso level > 0 Then
            If level - 1 <= TheList.Count - 1 Then
                Return TheList(level - 1)
            Else
                Return FolderName
            End If
        Else
            Return FolderName
        End If
    End Function
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <returns></returns>
    ''' <remarks>
    ''' Standard project is project\Bin\debug (or release) we can move
    ''' up three levels to get the folder for the project.
    ''' </remarks>
    <Runtime.CompilerServices.Extension>
    Public Function CurrentProjectFolder(ByVal sender As String) As String
        Return sender.UpperFolder(3)
    End Function
    ''' <summary>
    ''' Returns the project folder name
    ''' </summary>
    ''' <returns>Current project folder path</returns>
    ''' <example>
    ''' <code source="CodeExamples\FolderDemos.vb" language="vbnet" title="VB.NET Examples"/>
    ''' </example>
    <Runtime.CompilerServices.Extension>
    Public Function GetProjectBaseFolder(ByVal sender As String) As String
        Dim Folders As List(Of String) = UpperFolderList(AppDomain.CurrentDomain.BaseDirectory, True)
        Dim Result = (
            From this In Folders.Select(Function(item, index) New With {Key .Name = item, Key .Index = index})
            Where this.Name.ToLower().EndsWith("bin")
            Select this).FirstOrDefault()
        Return Folders(Result.Index - 1)
    End Function
    ''' <summary>
    ''' Get a list of all folders above 'FolderName'
    ''' </summary>
    ''' <param name="FolderName">Folder to start at</param>
    ''' <param name="Sort">True/False</param>
    ''' <returns>List of folder names</returns>
    ''' <example>
    ''' <code source="CodeExamples\FolderDemos.vb" language="vbnet" title="VB.NET Examples"/>
    ''' </example>
    <Runtime.CompilerServices.Extension>
    Public Function UpperFolderList(ByVal FolderName As String, ByVal Sort As Boolean) As List(Of String)
        Dim TheList As New List(Of String)()

        Do While Not String.IsNullOrEmpty(FolderName)
            Dim temp = Directory.GetParent(FolderName)
            If temp Is Nothing Then
                Exit Do
            End If
            FolderName = Directory.GetParent(FolderName).FullName
            TheList.Add(FolderName)
        Loop

        If Sort Then
            TheList.Sort()
        End If

        Return TheList
    End Function
End Module