DataConformance.Checking package
The DataConformance.Checking package is used to turn your expectations about the data in your database into assertions that can be automatically tested against reality.
Example
If you have below database schema for a very simple blog
schema.sql
and want to express the assertion Authors can not publish two blog posts with the same name. as code, you can do so with
CREATE TABLE authors (
id UUID PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE posts (
id UUID PRIMARY KEY,
name text NOT NULL,
author_id UUID NOT NULL REFERENCES authors (id)
);
Post.cs
using DataConformance.Checking;
using DataConformance.Checking.Constraints;
using DataConformance.Checking.Entities;
public class Post : Entity
{
public override TableReference TableReference => new("posts");
public override IReadOnlyCollection<ColumnReference>
PrimaryKeyColumns => new[] { Id };
public ColumnReference Id => ColumnReference.String(this, "id");
public ColumnReference AuthorId => ColumnReference.String(this, "author_id");
public ColumnReference Name => ColumnReference.String(this, "name");
[Assertion]
public Constraint AuthorsCanNotPublishTwoBlogPostsWithTheSameName()
{
return Check.For(Times.All, (Post postA) =>
Check.For(Times.All, (Post postB) =>
postA.IsNotEqualTo(postB)
.And(postA.AuthorId.IsEqualTo(postB.AuthorId))
.Implies(postA.Name.IsNotEqualTo(postB.Name))
)
);
}
}
Combine this with below Program.cs inside a C# console project and you are ready to go!
Program.cs
using DataConformance.Checking;
new DefinitionExporter()
.AddAssembly(typeof(Program).Assembly)
.ExportToFile("./definition.json");
Installation
The package is hosted on nuget.org and can be installed with below command, when in your project folder.