Verify.EntityFramework/readme.md

267 строки
9.3 KiB
Markdown
Исходник Обычный вид История

2020-01-06 07:58:10 +03:00
<!--
GENERATED FILE - DO NOT EDIT
This file was generated by [MarkdownSnippets](https://github.com/SimonCropp/MarkdownSnippets).
Source File: /readme.source.md
To change this file edit the source file and then run MarkdownSnippets.
-->
2020-01-06 11:53:20 +03:00
# <img src="/src/icon.png" height="30px"> Verify.EntityFramework
2020-01-06 07:58:10 +03:00
2020-01-06 13:38:28 +03:00
[![Build status](https://ci.appveyor.com/api/projects/status/g6njwv0aox62atu0?svg=true)](https://ci.appveyor.com/project/SimonCropp/verify-entityframework)
2020-01-06 11:53:20 +03:00
[![NuGet Status](https://img.shields.io/nuget/v/Verify.EntityFramework.svg)](https://www.nuget.org/packages/Verify.EntityFramework/)
2020-02-19 02:19:19 +03:00
[![NuGet Status](https://img.shields.io/nuget/v/Verify.EntityFrameworkClassic.svg)](https://www.nuget.org/packages/Verify.EntityFrameworkClassic/)
2020-01-06 07:58:10 +03:00
2020-01-06 13:41:14 +03:00
Extends [Verify](https://github.com/SimonCropp/Verify) to allow verification of EntityFramework bits.
2020-01-06 07:58:10 +03:00
2020-02-17 12:13:48 +03:00
Support is available via a [Tidelift Subscription](https://tidelift.com/subscription/pkg/nuget-verify.entityframework?utm_source=nuget-verify.entityframework&utm_medium=referral&utm_campaign=enterprise).
2020-01-06 07:58:10 +03:00
<!-- toc -->
## Contents
* [Usage](#usage)
2020-01-07 05:08:39 +03:00
* [ChangeTracking](#changetracking)
2020-01-17 12:42:09 +03:00
* [Queryable](#queryable)
* [Security contact information](#security-contact-information)<!-- endtoc -->
2020-01-06 07:58:10 +03:00
## NuGet package
2020-02-19 02:19:19 +03:00
* https://nuget.org/packages/Verify.EntityFramework/
* https://nuget.org/packages/Verify.EntityFrameworkClassic/
2020-01-06 07:58:10 +03:00
## Usage
2020-01-06 13:38:28 +03:00
Enable VerifyEntityFramewok once at assembly load time:
2020-01-06 07:58:10 +03:00
<!-- snippet: Enable -->
<a id='snippet-enable'/></a>
```cs
2020-01-06 11:53:20 +03:00
VerifyEntityFramework.Enable();
2020-01-06 07:58:10 +03:00
```
2020-02-18 09:12:51 +03:00
<sup><a href='/src/Verify.EntityFramework.Tests/GlobalSetup.cs#L9-L11' title='File snippet `enable` was extracted from'>snippet source</a> | <a href='#snippet-enable' title='Navigate to start of snippet `enable`'>anchor</a></sup>
<a id='snippet-enable-1'/></a>
```cs
2020-02-19 01:44:24 +03:00
VerifyEntityFrameworkClassic.Enable();
2020-02-18 09:12:51 +03:00
```
<sup><a href='/src/Verify.EntityFrameworkClassic.Tests/GlobalSetup.cs#L9-L11' title='File snippet `enable` was extracted from'>snippet source</a> | <a href='#snippet-enable-1' title='Navigate to start of snippet `enable`'>anchor</a></sup>
2020-01-06 07:58:10 +03:00
<!-- endsnippet -->
2020-01-07 05:08:39 +03:00
### ChangeTracking
Added, deleted, and Modified entities can be verified by performing changes on a DbContext and then verifying that context. This approach leverages the [EntityFramework ChangeTracker](https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.changetracking.changetracker).
#### Added entity
2020-01-06 07:58:10 +03:00
2020-01-06 13:38:28 +03:00
This test:
<!-- snippet: Added -->
<a id='snippet-added'/></a>
```cs
[Fact]
public async Task Added()
{
var options = DbContextOptions();
await using var context = new SampleDbContext(options);
2020-01-07 05:18:37 +03:00
context.Add(new Company {Content = "before"});
2020-01-06 13:38:28 +03:00
await Verify(context);
}
```
2020-02-18 09:12:51 +03:00
<sup><a href='/src/Verify.EntityFramework.Tests/Tests.cs#L12-L22' title='File snippet `added` was extracted from'>snippet source</a> | <a href='#snippet-added' title='Navigate to start of snippet `added`'>anchor</a></sup>
2020-01-06 13:38:28 +03:00
<!-- endsnippet -->
Will result in the following verified file:
2020-01-06 07:58:10 +03:00
2020-01-06 13:38:28 +03:00
<!-- snippet: Tests.Added.verified.txt -->
<a id='snippet-Tests.Added.verified.txt'/></a>
```txt
{
Added: {
Company: {
2020-01-06 13:39:54 +03:00
Id: 0,
Content: 'before'
2020-01-06 13:38:28 +03:00
}
}
}
```
2020-02-18 09:12:51 +03:00
<sup><a href='/src/Verify.EntityFramework.Tests/Tests.Added.verified.txt#L1-L8' title='File snippet `Tests.Added.verified.txt` was extracted from'>snippet source</a> | <a href='#snippet-Tests.Added.verified.txt' title='Navigate to start of snippet `Tests.Added.verified.txt`'>anchor</a></sup>
<a id='snippet-Tests.Added.verified.txt-1'/></a>
```txt
{
Added: {
Company: {
Id: 0,
Content: 'before'
}
}
}
```
<sup><a href='/src/Verify.EntityFrameworkClassic.Tests/Tests.Added.verified.txt#L1-L8' title='File snippet `Tests.Added.verified.txt` was extracted from'>snippet source</a> | <a href='#snippet-Tests.Added.verified.txt-1' title='Navigate to start of snippet `Tests.Added.verified.txt`'>anchor</a></sup>
2020-01-06 13:38:28 +03:00
<!-- endsnippet -->
2020-01-07 05:08:39 +03:00
#### Deleted entity
2020-01-06 07:58:10 +03:00
This test:
2020-01-06 13:38:28 +03:00
<!-- snippet: Deleted -->
<a id='snippet-deleted'/></a>
```cs
[Fact]
public async Task Deleted()
{
var options = DbContextOptions();
2020-01-07 05:19:58 +03:00
await using var context = new SampleDbContext(options);
context.Add(new Company {Content = "before"});
2020-03-18 05:25:13 +03:00
await context.SaveChangesAsync();
2020-01-06 13:38:28 +03:00
2020-01-07 05:19:58 +03:00
var company = context.Companies.Single();
context.Companies.Remove(company);
await Verify(context);
2020-01-06 13:38:28 +03:00
}
```
2020-02-18 09:12:51 +03:00
<sup><a href='/src/Verify.EntityFramework.Tests/Tests.cs#L24-L38' title='File snippet `deleted` was extracted from'>snippet source</a> | <a href='#snippet-deleted' title='Navigate to start of snippet `deleted`'>anchor</a></sup>
2020-01-06 13:38:28 +03:00
<!-- endsnippet -->
2020-01-06 07:58:10 +03:00
Will result in the following verified file:
2020-01-06 13:38:28 +03:00
<!-- snippet: Tests.Deleted.verified.txt -->
<a id='snippet-Tests.Deleted.verified.txt'/></a>
```txt
{
Deleted: {
Company: {
Id: 0
}
}
}
```
2020-02-18 09:12:51 +03:00
<sup><a href='/src/Verify.EntityFramework.Tests/Tests.Deleted.verified.txt#L1-L7' title='File snippet `Tests.Deleted.verified.txt` was extracted from'>snippet source</a> | <a href='#snippet-Tests.Deleted.verified.txt' title='Navigate to start of snippet `Tests.Deleted.verified.txt`'>anchor</a></sup>
<a id='snippet-Tests.Deleted.verified.txt-1'/></a>
```txt
{
Deleted: {
Company: {
Id: 0
}
}
}
```
<sup><a href='/src/Verify.EntityFrameworkClassic.Tests/Tests.Deleted.verified.txt#L1-L7' title='File snippet `Tests.Deleted.verified.txt` was extracted from'>snippet source</a> | <a href='#snippet-Tests.Deleted.verified.txt-1' title='Navigate to start of snippet `Tests.Deleted.verified.txt`'>anchor</a></sup>
2020-01-06 13:38:28 +03:00
<!-- endsnippet -->
2020-01-07 05:08:39 +03:00
#### Modified entity
2020-01-06 13:38:28 +03:00
This test:
<!-- snippet: Modified -->
<a id='snippet-modified'/></a>
```cs
[Fact]
public async Task Modified()
{
var options = DbContextOptions();
2020-01-07 05:18:37 +03:00
await using var context = new SampleDbContext(options);
var company = new Company {Content = "before"};
context.Add(company);
2020-03-18 05:25:13 +03:00
await context.SaveChangesAsync();
2020-01-06 13:38:28 +03:00
2020-01-07 05:18:37 +03:00
context.Companies.Single().Content = "after";
await Verify(context);
2020-01-06 13:38:28 +03:00
}
```
2020-02-18 09:12:51 +03:00
<sup><a href='/src/Verify.EntityFramework.Tests/Tests.cs#L40-L54' title='File snippet `modified` was extracted from'>snippet source</a> | <a href='#snippet-modified' title='Navigate to start of snippet `modified`'>anchor</a></sup>
2020-01-06 13:38:28 +03:00
<!-- endsnippet -->
2020-01-06 07:58:10 +03:00
2020-01-06 13:38:28 +03:00
Will result in the following verified file:
<!-- snippet: Tests.Modified.verified.txt -->
<a id='snippet-Tests.Modified.verified.txt'/></a>
```txt
{
Modified: {
Company: {
Id: 0,
Content: {
Original: 'before',
Current: 'after'
}
}
}
}
```
2020-02-18 09:12:51 +03:00
<sup><a href='/src/Verify.EntityFramework.Tests/Tests.Modified.verified.txt#L1-L11' title='File snippet `Tests.Modified.verified.txt` was extracted from'>snippet source</a> | <a href='#snippet-Tests.Modified.verified.txt' title='Navigate to start of snippet `Tests.Modified.verified.txt`'>anchor</a></sup>
<a id='snippet-Tests.Modified.verified.txt-1'/></a>
```txt
{
Modified: {
Company: {
Id: 0,
Content: {
Original: 'before',
Current: 'after'
}
}
}
}
```
<sup><a href='/src/Verify.EntityFrameworkClassic.Tests/Tests.Modified.verified.txt#L1-L11' title='File snippet `Tests.Modified.verified.txt` was extracted from'>snippet source</a> | <a href='#snippet-Tests.Modified.verified.txt-1' title='Navigate to start of snippet `Tests.Modified.verified.txt`'>anchor</a></sup>
2020-01-06 13:38:28 +03:00
<!-- endsnippet -->
2020-01-06 07:58:10 +03:00
2020-01-06 15:56:57 +03:00
### Queryable
This test:
<!-- snippet: Queryable -->
<a id='snippet-queryable'/></a>
```cs
[Fact]
public async Task Queryable()
{
var database = await DbContextBuilder.GetDatabase("Queryable");
var dbContext = database.Context;
var queryable = dbContext.Companies.Where(x => x.Content == "value");
await Verify(queryable);
}
```
2020-02-18 09:12:51 +03:00
<sup><a href='/src/Verify.EntityFramework.Tests/Tests.cs#L114-L123' title='File snippet `queryable` was extracted from'>snippet source</a> | <a href='#snippet-queryable' title='Navigate to start of snippet `queryable`'>anchor</a></sup>
2020-01-06 15:56:57 +03:00
<!-- endsnippet -->
Will result in the following verified file:
<!-- snippet: Tests.Queryable.verified.txt -->
<a id='snippet-Tests.Queryable.verified.txt'/></a>
```txt
SELECT [c].[Id], [c].[Content]
FROM [Companies] AS [c]
WHERE [c].[Content] = N'value'
```
2020-02-18 09:12:51 +03:00
<sup><a href='/src/Verify.EntityFramework.Tests/Tests.Queryable.verified.txt#L1-L3' title='File snippet `Tests.Queryable.verified.txt` was extracted from'>snippet source</a> | <a href='#snippet-Tests.Queryable.verified.txt' title='Navigate to start of snippet `Tests.Queryable.verified.txt`'>anchor</a></sup>
<a id='snippet-Tests.Queryable.verified.txt-1'/></a>
```txt
2020-02-19 01:44:24 +03:00
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Content] AS [Content]
FROM [dbo].[Companies] AS [Extent1]
WHERE N'value' = [Extent1].[Content]
2020-02-18 09:12:51 +03:00
```
2020-02-19 01:44:24 +03:00
<sup><a href='/src/Verify.EntityFrameworkClassic.Tests/Tests.Queryable.verified.txt#L1-L5' title='File snippet `Tests.Queryable.verified.txt` was extracted from'>snippet source</a> | <a href='#snippet-Tests.Queryable.verified.txt-1' title='Navigate to start of snippet `Tests.Queryable.verified.txt`'>anchor</a></sup>
2020-01-06 15:56:57 +03:00
<!-- endsnippet -->
2020-01-17 12:42:09 +03:00
## Security contact information
2020-01-27 03:46:18 +03:00
To report a security vulnerability, use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
2020-01-17 12:42:09 +03:00
2020-01-06 07:58:10 +03:00
## Icon
2020-01-06 13:46:25 +03:00
[Database](https://thenounproject.com/term/database/310841/) designed by [Creative Stall](https://thenounproject.com/creativestall/) from [The Noun Project](https://thenounproject.com/creativepriyanka).