cleanup
This commit is contained in:
Родитель
3369c0535e
Коммит
296f1b0474
32
readme.md
32
readme.md
|
@ -65,9 +65,9 @@ public async Task Added()
|
|||
{
|
||||
var options = DbContextOptions();
|
||||
|
||||
await using var context = new SampleDbContext(options);
|
||||
context.Add(new Company {Content = "before"});
|
||||
await Verify(context);
|
||||
await using var data = new SampleDbContext(options);
|
||||
data.Add(new Company {Content = "before"});
|
||||
await Verify(data);
|
||||
}
|
||||
```
|
||||
<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>
|
||||
|
@ -115,13 +115,13 @@ public async Task Deleted()
|
|||
{
|
||||
var options = DbContextOptions();
|
||||
|
||||
await using var context = new SampleDbContext(options);
|
||||
context.Add(new Company {Content = "before"});
|
||||
await context.SaveChangesAsync();
|
||||
await using var data = new SampleDbContext(options);
|
||||
data.Add(new Company {Content = "before"});
|
||||
await data.SaveChangesAsync();
|
||||
|
||||
var company = context.Companies.Single();
|
||||
context.Companies.Remove(company);
|
||||
await Verify(context);
|
||||
var company = data.Companies.Single();
|
||||
data.Companies.Remove(company);
|
||||
await Verify(data);
|
||||
}
|
||||
```
|
||||
<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>
|
||||
|
@ -167,13 +167,13 @@ public async Task Modified()
|
|||
{
|
||||
var options = DbContextOptions();
|
||||
|
||||
await using var context = new SampleDbContext(options);
|
||||
await using var data = new SampleDbContext(options);
|
||||
var company = new Company {Content = "before"};
|
||||
context.Add(company);
|
||||
await context.SaveChangesAsync();
|
||||
data.Add(company);
|
||||
await data.SaveChangesAsync();
|
||||
|
||||
context.Companies.Single().Content = "after";
|
||||
await Verify(context);
|
||||
data.Companies.Single().Content = "after";
|
||||
await Verify(data);
|
||||
}
|
||||
```
|
||||
<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>
|
||||
|
@ -226,8 +226,8 @@ This test:
|
|||
public async Task Queryable()
|
||||
{
|
||||
var database = await DbContextBuilder.GetDatabase("Queryable");
|
||||
var dbContext = database.Context;
|
||||
var queryable = dbContext.Companies.Where(x => x.Content == "value");
|
||||
var data = database.Context;
|
||||
var queryable = data.Companies.Where(x => x.Content == "value");
|
||||
await Verify(queryable);
|
||||
}
|
||||
```
|
||||
|
|
|
@ -14,9 +14,9 @@ public static class DbContextBuilder
|
|||
|
||||
static SqlInstance<SampleDbContext> sqlInstance;
|
||||
|
||||
static async Task CreateDb(SampleDbContext context)
|
||||
static async Task CreateDb(SampleDbContext data)
|
||||
{
|
||||
await context.Database.EnsureCreatedAsync();
|
||||
await data.Database.EnsureCreatedAsync();
|
||||
|
||||
var company1 = new Company
|
||||
{
|
||||
|
@ -59,8 +59,8 @@ public static class DbContextBuilder
|
|||
Id = 7,
|
||||
Content = "Company4"
|
||||
};
|
||||
context.AddRange(company1, employee1, employee2, company2, company3, company4, employee4);
|
||||
await context.SaveChangesAsync();
|
||||
data.AddRange(company1, employee1, employee2, company2, company3, company4, employee4);
|
||||
await data.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public static Task<SqlDatabase<SampleDbContext>> GetDatabase(string suffix)
|
||||
|
|
|
@ -15,9 +15,9 @@ public class Tests :
|
|||
{
|
||||
var options = DbContextOptions();
|
||||
|
||||
await using var context = new SampleDbContext(options);
|
||||
context.Add(new Company {Content = "before"});
|
||||
await Verify(context);
|
||||
await using var data = new SampleDbContext(options);
|
||||
data.Add(new Company {Content = "before"});
|
||||
await Verify(data);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
@ -27,13 +27,13 @@ public class Tests :
|
|||
{
|
||||
var options = DbContextOptions();
|
||||
|
||||
await using var context = new SampleDbContext(options);
|
||||
context.Add(new Company {Content = "before"});
|
||||
await context.SaveChangesAsync();
|
||||
await using var data = new SampleDbContext(options);
|
||||
data.Add(new Company {Content = "before"});
|
||||
await data.SaveChangesAsync();
|
||||
|
||||
var company = context.Companies.Single();
|
||||
context.Companies.Remove(company);
|
||||
await Verify(context);
|
||||
var company = data.Companies.Single();
|
||||
data.Companies.Remove(company);
|
||||
await Verify(data);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
@ -43,13 +43,13 @@ public class Tests :
|
|||
{
|
||||
var options = DbContextOptions();
|
||||
|
||||
await using var context = new SampleDbContext(options);
|
||||
await using var data = new SampleDbContext(options);
|
||||
var company = new Company {Content = "before"};
|
||||
context.Add(company);
|
||||
await context.SaveChangesAsync();
|
||||
data.Add(company);
|
||||
await data.SaveChangesAsync();
|
||||
|
||||
context.Companies.Single().Content = "after";
|
||||
await Verify(context);
|
||||
data.Companies.Single().Content = "after";
|
||||
await Verify(data);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
@ -58,23 +58,23 @@ public class Tests :
|
|||
{
|
||||
var options = DbContextOptions();
|
||||
|
||||
await using var context = new SampleDbContext(options);
|
||||
await using var data = new SampleDbContext(options);
|
||||
var company = new Company
|
||||
{
|
||||
Content = "companyBefore"
|
||||
};
|
||||
context.Add(company);
|
||||
data.Add(company);
|
||||
var employee = new Employee
|
||||
{
|
||||
Content = "employeeBefore",
|
||||
Company = company
|
||||
};
|
||||
context.Add(employee);
|
||||
await context.SaveChangesAsync();
|
||||
data.Add(employee);
|
||||
await data.SaveChangesAsync();
|
||||
|
||||
context.Companies.Single().Content = "companyAfter";
|
||||
context.Employees.Single().Content = "employeeAfter";
|
||||
await Verify(context);
|
||||
data.Companies.Single().Content = "companyAfter";
|
||||
data.Employees.Single().Content = "employeeAfter";
|
||||
await Verify(data);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
@ -82,16 +82,16 @@ public class Tests :
|
|||
{
|
||||
var options = DbContextOptions();
|
||||
|
||||
await using var context = new SampleDbContext(options);
|
||||
context.Add(new Employee
|
||||
await using var data = new SampleDbContext(options);
|
||||
data.Add(new Employee
|
||||
{
|
||||
Content = "before",
|
||||
Age = 10
|
||||
});
|
||||
await context.SaveChangesAsync();
|
||||
await data.SaveChangesAsync();
|
||||
|
||||
context.Employees.Single().Content = "after";
|
||||
await Verify(context);
|
||||
data.Employees.Single().Content = "after";
|
||||
await Verify(data);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
@ -99,16 +99,16 @@ public class Tests :
|
|||
{
|
||||
var options = DbContextOptions();
|
||||
|
||||
await using var context = new SampleDbContext(options);
|
||||
context.Add(new Employee
|
||||
await using var data = new SampleDbContext(options);
|
||||
data.Add(new Employee
|
||||
{
|
||||
Content = "before",
|
||||
});
|
||||
await context.SaveChangesAsync();
|
||||
await data.SaveChangesAsync();
|
||||
|
||||
var employee = context.Employees.Single();
|
||||
context.Update(employee).Entity.Content = "after";
|
||||
await Verify(context);
|
||||
var employee = data.Employees.Single();
|
||||
data.Update(employee).Entity.Content = "after";
|
||||
await Verify(data);
|
||||
}
|
||||
|
||||
#region Queryable
|
||||
|
@ -116,8 +116,8 @@ public class Tests :
|
|||
public async Task Queryable()
|
||||
{
|
||||
var database = await DbContextBuilder.GetDatabase("Queryable");
|
||||
var dbContext = database.Context;
|
||||
var queryable = dbContext.Companies.Where(x => x.Content == "value");
|
||||
var data = database.Context;
|
||||
var queryable = data.Companies.Where(x => x.Content == "value");
|
||||
await Verify(queryable);
|
||||
}
|
||||
#endregion
|
||||
|
|
|
@ -8,16 +8,16 @@ using Verify;
|
|||
class DbContextConverter :
|
||||
WriteOnlyJsonConverter<DbContext>
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, DbContext? context, JsonSerializer serializer)
|
||||
public override void WriteJson(JsonWriter writer, DbContext? data, JsonSerializer serializer)
|
||||
{
|
||||
if (context == null)
|
||||
if (data == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
writer.WriteStartObject();
|
||||
|
||||
var entries = context.ChangeTracker.Entries().ToList();
|
||||
var entries = data.ChangeTracker.Entries().ToList();
|
||||
HandleAdded(entries, writer, serializer);
|
||||
HandleModified(entries, writer, serializer);
|
||||
HandleDeleted(entries, writer, serializer);
|
||||
|
|
|
@ -8,27 +8,27 @@ using Verify;
|
|||
class QueryableConverter :
|
||||
WriteOnlyJsonConverter
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, object? context, JsonSerializer serializer)
|
||||
public override void WriteJson(JsonWriter writer, object? data, JsonSerializer serializer)
|
||||
{
|
||||
if (context == null)
|
||||
if (data == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var sql = QueryToSql(context);
|
||||
var sql = QueryToSql(data);
|
||||
serializer.Serialize(writer, sql);
|
||||
}
|
||||
|
||||
public static string QueryToSql(object context)
|
||||
public static string QueryToSql(object data)
|
||||
{
|
||||
var entityType = context.GetType().GetGenericArguments().Single();
|
||||
var entityType = data.GetType().GetGenericArguments().Single();
|
||||
var queryableSerializer = typeof(QueryableSerializer<>).MakeGenericType(entityType);
|
||||
return (string) queryableSerializer.InvokeMember(
|
||||
name: "ToSql",
|
||||
invokeAttr: BindingFlags.InvokeMethod,
|
||||
binder: null,
|
||||
target: null,
|
||||
args: new[] {context});
|
||||
args: new[] {data});
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type type)
|
||||
|
|
|
@ -10,14 +10,14 @@ public static class DbContextBuilder
|
|||
sqlInstance = new SqlInstance<SampleDbContext>(
|
||||
buildTemplate: CreateDb,
|
||||
constructInstance: connection => new SampleDbContext(connection),
|
||||
instanceSuffix:"Classic");
|
||||
instanceSuffix: "Classic");
|
||||
}
|
||||
|
||||
static SqlInstance<SampleDbContext> sqlInstance;
|
||||
|
||||
static async Task CreateDb(SampleDbContext context)
|
||||
static async Task CreateDb(SampleDbContext data)
|
||||
{
|
||||
await context.CreateOnExistingDb();
|
||||
await data.CreateOnExistingDb();
|
||||
|
||||
var company1 = new Company
|
||||
{
|
||||
|
@ -60,14 +60,14 @@ public static class DbContextBuilder
|
|||
Id = 7,
|
||||
Content = "Company4"
|
||||
};
|
||||
context.Companies.Add(company1);
|
||||
context.Companies.Add(company2);
|
||||
context.Companies.Add(company3);
|
||||
context.Companies.Add(company4);
|
||||
context.Employees.Add(employee1);
|
||||
context.Employees.Add(employee2);
|
||||
context.Employees.Add(employee4);
|
||||
await context.SaveChangesAsync();
|
||||
data.Companies.Add(company1);
|
||||
data.Companies.Add(company2);
|
||||
data.Companies.Add(company3);
|
||||
data.Companies.Add(company4);
|
||||
data.Employees.Add(employee1);
|
||||
data.Employees.Add(employee2);
|
||||
data.Employees.Add(employee4);
|
||||
await data.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public static Task<SqlDatabase<SampleDbContext>> GetDatabase(string suffix)
|
||||
|
|
|
@ -15,9 +15,9 @@ public class Tests :
|
|||
public async Task Added()
|
||||
{
|
||||
using var database = await sqlInstance.Build();
|
||||
var context = database.Context;
|
||||
context.Companies.Add(new Company {Content = "before"});
|
||||
await Verify(context);
|
||||
var data = database.Context;
|
||||
data.Companies.Add(new Company {Content = "before"});
|
||||
await Verify(data);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
@ -26,13 +26,13 @@ public class Tests :
|
|||
public async Task Deleted()
|
||||
{
|
||||
using var database = await sqlInstance.Build();
|
||||
var context = database.Context;
|
||||
context.Companies.Add(new Company {Content = "before"});
|
||||
await context.SaveChangesAsync();
|
||||
var data = database.Context;
|
||||
data.Companies.Add(new Company {Content = "before"});
|
||||
await data.SaveChangesAsync();
|
||||
|
||||
var company = context.Companies.Single();
|
||||
context.Companies.Remove(company);
|
||||
await Verify(context);
|
||||
var company = data.Companies.Single();
|
||||
data.Companies.Remove(company);
|
||||
await Verify(data);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
@ -41,13 +41,13 @@ public class Tests :
|
|||
public async Task Modified()
|
||||
{
|
||||
using var database = await sqlInstance.Build();
|
||||
var context = database.Context;
|
||||
var data = database.Context;
|
||||
var company = new Company {Content = "before"};
|
||||
context.Companies.Add(company);
|
||||
await context.SaveChangesAsync();
|
||||
data.Companies.Add(company);
|
||||
await data.SaveChangesAsync();
|
||||
|
||||
context.Companies.Single().Content = "after";
|
||||
await Verify(context);
|
||||
data.Companies.Single().Content = "after";
|
||||
await Verify(data);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
@ -55,59 +55,59 @@ public class Tests :
|
|||
public async Task WithNavigationProp()
|
||||
{
|
||||
using var database = await sqlInstance.Build();
|
||||
var context = database.Context;
|
||||
var data = database.Context;
|
||||
var company = new Company
|
||||
{
|
||||
Content = "companyBefore"
|
||||
};
|
||||
context.Companies.Add(company);
|
||||
data.Companies.Add(company);
|
||||
var employee = new Employee
|
||||
{
|
||||
Content = "employeeBefore",
|
||||
Company = company
|
||||
};
|
||||
context.Employees.Add(employee);
|
||||
await context.SaveChangesAsync();
|
||||
data.Employees.Add(employee);
|
||||
await data.SaveChangesAsync();
|
||||
|
||||
context.Companies.Single().Content = "companyAfter";
|
||||
context.Employees.Single().Content = "employeeAfter";
|
||||
await Verify(context);
|
||||
data.Companies.Single().Content = "companyAfter";
|
||||
data.Employees.Single().Content = "employeeAfter";
|
||||
await Verify(data);
|
||||
}
|
||||
|
||||
[Fact(Skip = "TODO")]
|
||||
public async Task SomePropsModified()
|
||||
{
|
||||
using var database = await sqlInstance.Build();
|
||||
var context = database.Context;
|
||||
var data = database.Context;
|
||||
var company = new Company
|
||||
{
|
||||
Content = "before",
|
||||
};
|
||||
context.Companies.Add(company);
|
||||
await context.SaveChangesAsync();
|
||||
var entity = context.Companies.Attach(new Company {Id = company.Id});
|
||||
data.Companies.Add(company);
|
||||
await data.SaveChangesAsync();
|
||||
var entity = data.Companies.Attach(new Company {Id = company.Id});
|
||||
entity.Content = "after";
|
||||
context.Entry(entity).Property(_ => _.Content).IsModified = true;
|
||||
context.Configuration.ValidateOnSaveEnabled = false;
|
||||
await context.SaveChangesAsync();
|
||||
await Verify(context);
|
||||
data.Entry(entity).Property(_ => _.Content).IsModified = true;
|
||||
data.Configuration.ValidateOnSaveEnabled = false;
|
||||
await data.SaveChangesAsync();
|
||||
await Verify(data);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateEntity()
|
||||
{
|
||||
using var database = await sqlInstance.Build();
|
||||
var context = database.Context;
|
||||
var data = database.Context;
|
||||
|
||||
context.Companies.Add(new Company
|
||||
data.Companies.Add(new Company
|
||||
{
|
||||
Content = "before",
|
||||
});
|
||||
await context.SaveChangesAsync();
|
||||
await data.SaveChangesAsync();
|
||||
|
||||
var company = context.Companies.Single();
|
||||
var company = data.Companies.Single();
|
||||
company.Content = "after";
|
||||
await Verify(context);
|
||||
await Verify(data);
|
||||
}
|
||||
|
||||
#region QueryableClassic
|
||||
|
@ -115,8 +115,8 @@ public class Tests :
|
|||
public async Task Queryable()
|
||||
{
|
||||
var database = await DbContextBuilder.GetDatabase("Queryable");
|
||||
var dbContext = database.Context;
|
||||
var queryable = dbContext.Companies.Where(x => x.Content == "value");
|
||||
var data = database.Context;
|
||||
var queryable = data.Companies.Where(x => x.Content == "value");
|
||||
await Verify(queryable);
|
||||
}
|
||||
#endregion
|
||||
|
|
|
@ -8,23 +8,23 @@ using Verify;
|
|||
class DbContextConverter :
|
||||
WriteOnlyJsonConverter<DbContext>
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, DbContext? context, JsonSerializer serializer)
|
||||
public override void WriteJson(JsonWriter writer, DbContext? data, JsonSerializer serializer)
|
||||
{
|
||||
if (context == null)
|
||||
if (data == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
writer.WriteStartObject();
|
||||
var entries = context.ChangeTracker.Entries().ToList();
|
||||
var entries = data.ChangeTracker.Entries().ToList();
|
||||
HandleAdded(entries, writer, serializer);
|
||||
HandleModified(entries, writer, serializer, context);
|
||||
HandleDeleted(entries, writer, serializer, context);
|
||||
HandleModified(entries, writer, serializer, data);
|
||||
HandleDeleted(entries, writer, serializer, data);
|
||||
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
static void HandleDeleted(List<DbEntityEntry> entries, JsonWriter writer, JsonSerializer serializer, DbContext context)
|
||||
static void HandleDeleted(List<DbEntityEntry> entries, JsonWriter writer, JsonSerializer serializer, DbContext data)
|
||||
{
|
||||
var deleted = entries
|
||||
.Where(x => x.State == EntityState.Deleted)
|
||||
|
@ -40,7 +40,7 @@ class DbContextConverter :
|
|||
{
|
||||
writer.WritePropertyName(entry.Entity.GetType().Name);
|
||||
writer.WriteStartObject();
|
||||
WriteId(writer, serializer, entry, context);
|
||||
WriteId(writer, serializer, entry, data);
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
|
|
|
@ -7,27 +7,27 @@ using Verify;
|
|||
class QueryableConverter :
|
||||
WriteOnlyJsonConverter
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, object? context, JsonSerializer serializer)
|
||||
public override void WriteJson(JsonWriter writer, object? data, JsonSerializer serializer)
|
||||
{
|
||||
if (context == null)
|
||||
if (data == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var sql = QueryToSql(context);
|
||||
var sql = QueryToSql(data);
|
||||
serializer.Serialize(writer, sql);
|
||||
}
|
||||
|
||||
public static string QueryToSql(object context)
|
||||
public static string QueryToSql(object data)
|
||||
{
|
||||
var entityType = context.GetType().GetGenericArguments().Single();
|
||||
var entityType = data.GetType().GetGenericArguments().Single();
|
||||
var queryableSerializer = typeof(QueryableSerializer<>).MakeGenericType(entityType);
|
||||
return (string) queryableSerializer.InvokeMember(
|
||||
name: "ToSql",
|
||||
invokeAttr: BindingFlags.InvokeMethod,
|
||||
binder: null,
|
||||
target: null,
|
||||
args: new[] {context});
|
||||
args: new[] {data});
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type type)
|
||||
|
|
Загрузка…
Ссылка в новой задаче