From 296f1b04746d48e6b10fc1eae4021c981877cd61 Mon Sep 17 00:00:00 2001 From: SimonCropp Date: Fri, 3 Apr 2020 11:32:46 +1100 Subject: [PATCH] cleanup --- readme.md | 32 ++++----- .../Snippets/DbContextBuilder.cs | 8 +-- src/Verify.EntityFramework.Tests/Tests.cs | 68 +++++++++--------- .../Converters/DbContextConverter.cs | 6 +- .../Converters/QueryableConverter.cs | 12 ++-- .../Snippets/DbContextBuilder.cs | 22 +++--- .../Tests.cs | 72 +++++++++---------- .../Converters/DbContextConverter.cs | 14 ++-- .../Converters/QueryableConverter.cs | 12 ++-- 9 files changed, 123 insertions(+), 123 deletions(-) diff --git a/readme.md b/readme.md index a257ee3..0949964 100644 --- a/readme.md +++ b/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); } ``` snippet source | anchor @@ -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); } ``` snippet source | anchor @@ -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); } ``` snippet source | anchor @@ -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); } ``` diff --git a/src/Verify.EntityFramework.Tests/Snippets/DbContextBuilder.cs b/src/Verify.EntityFramework.Tests/Snippets/DbContextBuilder.cs index e1c877e..abcc2f0 100644 --- a/src/Verify.EntityFramework.Tests/Snippets/DbContextBuilder.cs +++ b/src/Verify.EntityFramework.Tests/Snippets/DbContextBuilder.cs @@ -14,9 +14,9 @@ public static class DbContextBuilder static SqlInstance 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> GetDatabase(string suffix) diff --git a/src/Verify.EntityFramework.Tests/Tests.cs b/src/Verify.EntityFramework.Tests/Tests.cs index 3a35c6b..e114756 100644 --- a/src/Verify.EntityFramework.Tests/Tests.cs +++ b/src/Verify.EntityFramework.Tests/Tests.cs @@ -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 diff --git a/src/Verify.EntityFramework/Converters/DbContextConverter.cs b/src/Verify.EntityFramework/Converters/DbContextConverter.cs index e92e443..650a23d 100644 --- a/src/Verify.EntityFramework/Converters/DbContextConverter.cs +++ b/src/Verify.EntityFramework/Converters/DbContextConverter.cs @@ -8,16 +8,16 @@ using Verify; class DbContextConverter : WriteOnlyJsonConverter { - 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); diff --git a/src/Verify.EntityFramework/Converters/QueryableConverter.cs b/src/Verify.EntityFramework/Converters/QueryableConverter.cs index f8673cd..b848a63 100644 --- a/src/Verify.EntityFramework/Converters/QueryableConverter.cs +++ b/src/Verify.EntityFramework/Converters/QueryableConverter.cs @@ -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) diff --git a/src/Verify.EntityFrameworkClassic.Tests/Snippets/DbContextBuilder.cs b/src/Verify.EntityFrameworkClassic.Tests/Snippets/DbContextBuilder.cs index 62e88ff..9a5f8f8 100644 --- a/src/Verify.EntityFrameworkClassic.Tests/Snippets/DbContextBuilder.cs +++ b/src/Verify.EntityFrameworkClassic.Tests/Snippets/DbContextBuilder.cs @@ -10,14 +10,14 @@ public static class DbContextBuilder sqlInstance = new SqlInstance( buildTemplate: CreateDb, constructInstance: connection => new SampleDbContext(connection), - instanceSuffix:"Classic"); + instanceSuffix: "Classic"); } static SqlInstance 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> GetDatabase(string suffix) diff --git a/src/Verify.EntityFrameworkClassic.Tests/Tests.cs b/src/Verify.EntityFrameworkClassic.Tests/Tests.cs index 4e06f2c..cca4b60 100644 --- a/src/Verify.EntityFrameworkClassic.Tests/Tests.cs +++ b/src/Verify.EntityFrameworkClassic.Tests/Tests.cs @@ -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 diff --git a/src/Verify.EntityFrameworkClassic/Converters/DbContextConverter.cs b/src/Verify.EntityFrameworkClassic/Converters/DbContextConverter.cs index af4d949..a900a92 100644 --- a/src/Verify.EntityFrameworkClassic/Converters/DbContextConverter.cs +++ b/src/Verify.EntityFrameworkClassic/Converters/DbContextConverter.cs @@ -8,23 +8,23 @@ using Verify; class DbContextConverter : WriteOnlyJsonConverter { - 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 entries, JsonWriter writer, JsonSerializer serializer, DbContext context) + static void HandleDeleted(List 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(); } diff --git a/src/Verify.EntityFrameworkClassic/Converters/QueryableConverter.cs b/src/Verify.EntityFrameworkClassic/Converters/QueryableConverter.cs index 2404fcd..66dc20d 100644 --- a/src/Verify.EntityFrameworkClassic/Converters/QueryableConverter.cs +++ b/src/Verify.EntityFrameworkClassic/Converters/QueryableConverter.cs @@ -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)