SqlForgery 1.2.0

dotnet add package SqlForgery --version 1.2.0
                    
NuGet\Install-Package SqlForgery -Version 1.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SqlForgery" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SqlForgery" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="SqlForgery" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SqlForgery --version 1.2.0
                    
#r "nuget: SqlForgery, 1.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SqlForgery@1.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SqlForgery&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=SqlForgery&version=1.2.0
                    
Install as a Cake Tool

SqlForgery

Versions

Overview

<a name="versions"></a> Versions

  • v1.2.0 (latest)

    • Added support for owned types.
  • v1.1.0

    • Added support for json entities.
  • v1.0.1

    • Fixed navigation fields population bug.
    • Updated tests.
  • v1.0.0

    • Initial version.

<a name="overview"></a> Overview

This is a simple library for faking SQL (relational) data using EntityFramework Core. It is intended for testing purposes only.

I strongly believe that faking relational data is far superior to mocking, even in unit tests. However, SQL relationship constraints can make that process very tedious. This library aims to automate it, by populating navigation properties in the class.

How it works

Consider the following simple example from EF Core documentation page:

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(
            @"Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;ConnectRetryCount=0");
    }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
    public int Rating { get; set; }

    // navigation property
    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int BlogId { get; set; }

    // navigation property
    public Blog Blog { get; set; }
}

In this example Blog.Posts and Post.Blog are navigational properties. Since this is a one to many relationship, inserting Blog without Post can be done, while vice-versa is not true. SqlForgery will only populate required navigational fields.

How to use

SqlForgery is unopinionated how you will fake the data. You must supply IDictionary<Type, Delegate> for each DbSet<T> class, where Type is class you wish to fake, and Delegate a function that produces faked object. Example:

// keep IDs unique (or use Guids)
private static int idCount = 0;
private static int GetId()
{
    idCount++;
    return idCount;
}

var fakingFunctions = new Dictionary<Type, Delegate>()
{
    {
        typeof(Blog),
        () => new Blog
        {
            BlogId = GetId(),
            Url = "http://blog.com",
            Rating = 5
        }
    },
    {
       typeof(Post),
        () => new Post
        {
            PostId = GetId(),
            Title = "test",
            Content = "content"
        }
    }
};

Notice the absence of navigational properties. Ideally, this dictionary should be defined just once, or be made static. You can use Faker library or something similar if you want real looking data.

The faking function in here is just a draft. You can customize properties as needed when faking an entity on the spot (see below). Check source repository tests for inspiration.

Using EF Core SQLite for example, faking Post can be done like so:

// ideally automate this part in tests
var connection = new SqliteConnection("Filename=:memory:");
connection.Open();

var options = new DbContextOptionsBuilder<BloggingContext>()
    .UseSqlite(connection)
    .Options;

var context = new BloggingContext(options);

var forger = new Forger(context, fakingFunctions);

Fake Post and Blog will be created automatically:

var post = forger.Fake<Post>(); // returns faked object
context.SaveChanges();
/*
you won't get foreign key exception because Blog was inserted as well.
*/

var entity = context.Posts
    .Include(x => x.Blog)
    .First(x => x.Id == post.Id);

Customize faked object

var post = forger.Fake<Post>(p => {
    x.Title = "I'm different"
});

Customize Post and its related Blog

var post = forger.Fake<Post>(p => {
    x.Title = "I'm different";
    // use navigation property here
    x.Blog = forger.Fake<Blog>(b => b.Rating = 10);
});

Fake Blog (no Post will be created)

var blog = forger.Fake<Blog>();

Fake Blog with 10 related Posts

var blog = forger.Fake<Blog>(b => {
    b.Posts = Enumerable.Range(0, 10)
        .Select(_ => forger.Fake<Post>(p => p.Blog = b))
        .ToArray();
});
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 116 10/21/2024
1.1.0 151 9/10/2024
1.0.1 139 9/9/2024
1.0.0 125 7/17/2024