🤖 Day 11: Generate test data using AI and evaluate its efficacy

I think i broke chat-gpt :smiley:

I asked for the world and it gave me a sod of grass

so i broke it up a bit
given that i am an automation engineer generate a data set of 20 records as .net objects that have
a unique auto generated numerical id starting from 1
a string description that includes the id as a suffix
3 fields that are floating point to the 10th decimal place

using System;
using System.Collections.Generic;

public class Record
{
public int Id { get; set; }
public string Description { get; set; }
public double Field1 { get; set; }
public double Field2 { get; set; }
public double Field3 { get; set; }
}

public class Program
{
public static void Main(string args)
{
// Generate 20 records
List records = GenerateRecords(20);

    // Display records
    foreach (var record in records)
    {
        Console.WriteLine($"ID: {record.Id}, Description: {record.Description}, Field1: {record.Field1}, Field2: {record.Field2}, Field3: {record.Field3}");
    }
}

public static List<Record> GenerateRecords(int count)
{
    List<Record> records = new List<Record>();

    for (int i = 1; i <= count; i++)
    {
        Record record = new Record
        {
            Id = i,
            Description = $"Description {i}",
            Field1 = Math.Round(10.0 + i * 0.1, 10), // Generating floating point numbers to the 10th decimal place
            Field2 = Math.Round(20.0 + i * 0.1, 10),
            Field3 = Math.Round(30.0 + i * 0.1, 10)
        };
        records.Add(record);
    }

    return records;
}

}

which isnt too bad looking

i then asked

using the ’ GenerateRecords’ method give me a set of .net c# tests using xunit and linq to test for precision to the 7th decimal place

using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

public class Record
{
public int Id { get; set; }
public string Description { get; set; }
public double Field1 { get; set; }
public double Field2 { get; set; }
public double Field3 { get; set; }
}

public class RecordTests
{
[Fact]
public void Field1PrecisionTest()
{
// Arrange
var records = GenerateRecords(20);

    // Act
    var inaccurateRecords = records.Where(r => Math.Round(r.Field1, 7) != r.Field1);

    // Assert
    Assert.Empty(inaccurateRecords);
}

[Fact]
public void Field2PrecisionTest()
{
    // Arrange
    var records = GenerateRecords(20);

    // Act
    var inaccurateRecords = records.Where(r => Math.Round(r.Field2, 7) != r.Field2);

    // Assert
    Assert.Empty(inaccurateRecords);
}

[Fact]
public void Field3PrecisionTest()
{
    // Arrange
    var records = GenerateRecords(20);

    // Act
    var inaccurateRecords = records.Where(r => Math.Round(r.Field3, 7) != r.Field3);

    // Assert
    Assert.Empty(inaccurateRecords);
}

public List<Record> GenerateRecords(int count)
{
    List<Record> records = new List<Record>();

    for (int i = 1; i <= count; i++)
    {
        Record record = new Record
        {
            Id = i,
            Description = $"Description {i}",
            Field1 = Math.Round(10.0 + i * 0.1, 10), // Generating floating point numbers to the 10th decimal place
            Field2 = Math.Round(20.0 + i * 0.1, 10),
            Field3 = Math.Round(30.0 + i * 0.1, 10)
        };
        records.Add(record);
    }

    return records;
}

}

and finally wanted some SOLID in there :smiley:
using the ’ GenerateRecords’ method give me a set of .net c# generic tests using xunit and linq to test for precision to the 7th decimal place using solid principles

To ensure the application of SOLID principles, particularly the Single Responsibility Principle (SRP), I’ll separate concerns by creating a utility class for record generation and another class for precision testing. Here’s how you can structure your code:
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

public class Record
{
public int Id { get; set; }
public string Description { get; set; }
public double Field1 { get; set; }
public double Field2 { get; set; }
public double Field3 { get; set; }
}

public static class RecordGenerator
{
public static List GenerateRecords(int count)
{
List records = new List();

    for (int i = 1; i <= count; i++)
    {
        Record record = new Record
        {
            Id = i,
            Description = $"Description {i}",
            Field1 = Math.Round(10.0 + i * 0.1, 10), // Generating floating point numbers to the 10th decimal place
            Field2 = Math.Round(20.0 + i * 0.1, 10),
            Field3 = Math.Round(30.0 + i * 0.1, 10)
        };
        records.Add(record);
    }

    return records;
}

}

public class PrecisionTests
{
[Theory]
[InlineData(“Field1”)]
[InlineData(“Field2”)]
[InlineData(“Field3”)]
public void PrecisionTest(string fieldName)
{
// Arrange
var records = RecordGenerator.GenerateRecords(20);
var fieldInfo = typeof(Record).GetProperty(fieldName);

    // Act
    var inaccurateRecords = records.Where(r => Math.Round((double)fieldInfo.GetValue(r), 7) != (double)fieldInfo.GetValue(r));

    // Assert
    Assert.Empty(inaccurateRecords);
}

}

once again very good at boilerplate but not so much on what i needed.
at this stage i would still say that what we put in we get out, in other words, if my prompts were more concise i am sure i would get better results.

However that poses a question of the value of time spent to what we get out of it

4 Likes