Skip to content

Build test procedures with AI #58

@josef-poetzl

Description

@josef-poetzl

A test with ChatGPT returned a usable result.

Prompt:

I aim to create a test procedure that uses row-test definitions similar to NUnit.
I work with VBA in Access and utilize the AccUnit testing framework.
I expect each AccUnit:Row entry to be treated as a separate test case, and for the test results to be checked directly within the test method itself.
Please use the following format for the test:

'AccUnit:Row(...).Name(...)
'AccUnit:Row(...)
Public Sub TestMethod(...)
    ' Arrange
    ...
    ' Act
    ...
    ' Assert
    Assert.That ...
End Sub

Parameters should be directly included in the signature of the test procedure. Also use an Expected parameter and define the value in the test row definition. Set optional parameters to required.
The AccUnit:Row annotations should be defined outside the procedure.
Note for assert: since Is is not allowed as a variable in VBA, the framework uses Iz (e.g. for Iz.EqualTo) as a substitute.
Please create a test procedure for the following method from the SqlTools class:

'---------------------------------------------------------------------------------------
' Function: BuildCriteria
'---------------------------------------------------------------------------------------
'/**
' <summary>
' Create SQL criteria string
' </summary>
' <param name="FieldName">Field name in the data source to be filtered</param>
' <param name="RelationalOperator">Relational operator (=, <=, etc.)</param>
' <param name="FilterValue">Filter value (can be a single value or an array of values)</param>
' <param name="FilterValue2">Optional 2nd filter value (for Between)</param>
' <param name="IgnoreValue">The value for which no filter condition is to be created. (Array transfer of values possible)</param>
' <returns>String</returns>
' <remarks>
' </remarks>
'**/
'---------------------------------------------------------------------------------------
Public Function BuildCriteria(ByVal FieldName As String, ByVal FieldDataType As SqlFieldDataType, _
                              ByVal RelationalOperator As SqlRelationalOperators, _
                              ByVal FilterValue As Variant, _
                     Optional ByVal FilterValue2 As Variant = Null, _
                     Optional ByVal IgnoreValue As Variant, _
                     Optional ByVal DisableIgnoreNullValue As Boolean = False) As String
...
End Function

Public Enum SqlRelationalOperators
   [_IgnoreAll] = &H80000000
   SQL_Not = 1
   SQL_Equal = 2
   SQL_LessThan = 4
   SQL_GreaterThan = 8
   SQL_Like = 256
   SQL_Between = 512
   SQL_In = 1024
   SQL_Add_WildCardSuffix = 2048
   SQL_Add_WildCardPrefix = 4096
   SQL_SplitValueToArray = 8192
   SQL_AllowSqlDirect = 16384
   SQL_UseLikeBehavior = 65536
End Enum

Public Enum SqlFieldDataType
   SQL_Boolean = 1
   SQL_Numeric = 2
   SQL_Text = 3
   SQL_Date = 4
End Enum

Return only the code without explanation.

Result:

'AccUnit:Row("CustomerID", SQL_Text, SQL_Equal, "1001", Null, Null, "CustomerID = '1001'").Name("TestEqualText")
'AccUnit:Row("OrderAmount", SQL_Numeric, SQL_GreaterThan, 500, Null, Null, "OrderAmount > 500").Name("TestGreaterThanNumeric")
'AccUnit:Row("OrderDate", SQL_Date, SQL_Between, #1/1/2020#, #12/31/2020#, Null, "OrderDate BETWEEN #1/1/2020# AND #12/31/2020#").Name("TestBetweenDates")
'AccUnit:Row("ProductName", SQL_Text, SQL_Like, "%milk%", Null, Null, "ProductName LIKE '%milk%'").Name("TestLikeText")
Public Sub TestBuildCriteria(ByVal FieldName As String, ByVal FieldDataType As SqlFielddataType, _
                             ByVal RelationalOperator As SqlRelationalOperators, ByVal FilterValue As Variant, _
                             ByVal FilterValue2 As Variant, ByVal IgnoreValue As Variant, ByVal Expected As String)
    ' Arrange
    Dim sqlTools As New SqlTools
    
    ' Act
    Dim result As String
    result = sqlTools.BuildCriteria(FieldName, FieldDataType, RelationalOperator, FilterValue, FilterValue2, IgnoreValue)
    
    ' Assert
    Assert.That(result, Iz.EqualTo(Expected))
End Sub

See: https://chat.openai.com/share/436f2ed0-2f21-4fc3-9552-94030ec2cb54

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions