diff --git a/src/FlexLabs.EntityFrameworkCore.Upsert/HelpLinks.cs b/src/FlexLabs.EntityFrameworkCore.Upsert/HelpLinks.cs index f469337..daf6a29 100644 --- a/src/FlexLabs.EntityFrameworkCore.Upsert/HelpLinks.cs +++ b/src/FlexLabs.EntityFrameworkCore.Upsert/HelpLinks.cs @@ -3,7 +3,9 @@ internal class HelpLinks { internal const string Sqlite3Instructions = "https://go.flexlabs.org/upsert.sqlite3"; + // Also referenced in the xml comment for InvalidMatchColumnsException internal const string IdentityKeyMatchError = "https://go.flexlabs.org/upsert.identitykeymatch"; + // Also referenced in the xml comment for UnsupportedExpressionException // Also referenced in the xml comment for UpsertCommandBuilder.WithFallbackExpressionCompiler internal const string SupportedExpressions = "https://go.flexlabs.org/upsert.expressions"; } diff --git a/src/FlexLabs.EntityFrameworkCore.Upsert/InvalidMatchColumnsException.cs b/src/FlexLabs.EntityFrameworkCore.Upsert/InvalidMatchColumnsException.cs index 7da4c38..b5342bc 100644 --- a/src/FlexLabs.EntityFrameworkCore.Upsert/InvalidMatchColumnsException.cs +++ b/src/FlexLabs.EntityFrameworkCore.Upsert/InvalidMatchColumnsException.cs @@ -4,9 +4,8 @@ namespace FlexLabs.EntityFrameworkCore.Upsert { /// /// Thrown when using unsupported columns as the upsert match expression. - /// See: https://flexlabs.org/plink/upsert.sqlite3 + /// See: https://go.flexlabs.org/upsert.identitykeymatch /// - /// public sealed class InvalidMatchColumnsException : Exception { internal InvalidMatchColumnsException() diff --git a/src/FlexLabs.EntityFrameworkCore.Upsert/UnsupportedExpressionException.cs b/src/FlexLabs.EntityFrameworkCore.Upsert/UnsupportedExpressionException.cs index fca8703..65d62cc 100644 --- a/src/FlexLabs.EntityFrameworkCore.Upsert/UnsupportedExpressionException.cs +++ b/src/FlexLabs.EntityFrameworkCore.Upsert/UnsupportedExpressionException.cs @@ -4,6 +4,7 @@ namespace FlexLabs.EntityFrameworkCore.Upsert { /// /// Thrown when using unsupported expressions in the update clause + /// See: https://go.flexlabs.org/upsert.expressions /// public class UnsupportedExpressionException : Exception { diff --git a/test/FlexLabs.EntityFrameworkCore.Upsert.Tests/EF/Base/Tables.cs b/test/FlexLabs.EntityFrameworkCore.Upsert.Tests/EF/Base/Tables.cs index 0fa65fa..7998088 100644 --- a/test/FlexLabs.EntityFrameworkCore.Upsert.Tests/EF/Base/Tables.cs +++ b/test/FlexLabs.EntityFrameworkCore.Upsert.Tests/EF/Base/Tables.cs @@ -63,4 +63,30 @@ public class Status public string Name { get; set; } public DateTime LastChecked { get; set; } } + + public class GuidKeyAutoGen + { + public Guid ID { get; set; } + public string Name { get; set; } + } + + public class GuidKey + { + [Key, DatabaseGenerated(DatabaseGeneratedOption.None)] + public Guid ID { get; set; } + public string Name { get; set; } + } + + public class StringKeyAutoGen + { + public string ID { get; set; } + public string Name { get; set; } + } + + public class StringKey + { + [Key, DatabaseGenerated(DatabaseGeneratedOption.None)] + public string ID { get; set; } + public string Name { get; set; } + } } diff --git a/test/FlexLabs.EntityFrameworkCore.Upsert.Tests/EF/Base/TestDbContext.cs b/test/FlexLabs.EntityFrameworkCore.Upsert.Tests/EF/Base/TestDbContext.cs index 73510ca..1c1338c 100644 --- a/test/FlexLabs.EntityFrameworkCore.Upsert.Tests/EF/Base/TestDbContext.cs +++ b/test/FlexLabs.EntityFrameworkCore.Upsert.Tests/EF/Base/TestDbContext.cs @@ -34,6 +34,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) public DbSet PageVisits { get; set; } public DbSet SchemaTable { get; set; } public DbSet Statuses { get; set; } + public DbSet GuidKeysAutoGen { get; set; } + public DbSet GuidKeys { get; set; } + public DbSet StringKeysAutoGen { get; set; } + public DbSet StringKeys { get; set; } public enum DbDriver { diff --git a/test/FlexLabs.EntityFrameworkCore.Upsert.Tests/EF/BasicTest.cs b/test/FlexLabs.EntityFrameworkCore.Upsert.Tests/EF/BasicTest.cs index c2d1d84..383bb8e 100644 --- a/test/FlexLabs.EntityFrameworkCore.Upsert.Tests/EF/BasicTest.cs +++ b/test/FlexLabs.EntityFrameworkCore.Upsert.Tests/EF/BasicTest.cs @@ -206,6 +206,10 @@ private void ResetDb(TestDbContext.DbDriver driver) dbContext.SchemaTable.RemoveRange(dbContext.SchemaTable); dbContext.PageVisits.RemoveRange(dbContext.PageVisits); dbContext.Books.RemoveRange(dbContext.Books); + dbContext.GuidKeysAutoGen.RemoveRange(dbContext.GuidKeysAutoGen); + dbContext.GuidKeys.RemoveRange(dbContext.GuidKeys); + dbContext.StringKeysAutoGen.RemoveRange(dbContext.StringKeysAutoGen); + dbContext.StringKeys.RemoveRange(dbContext.StringKeys); dbContext.Countries.Add(_dbCountry); dbContext.PageVisits.Add(_dbVisitOld); @@ -259,6 +263,25 @@ public void Upsert_InitialDbState(TestDbContext.DbDriver driver) } } + [Theory] + [MemberData(nameof(GetDatabaseEngines))] + public void Upsert_EFCore_KeyAutoGen(TestDbContext.DbDriver driver) + { + ResetDb(driver); + using (var dbContext = new TestDbContext(_dataContexts[driver])) + { + dbContext.GuidKeysAutoGen.Add(new GuidKeyAutoGen { Name = "test" }); + dbContext.StringKeysAutoGen.Add(new StringKeyAutoGen { Name = "test" }); + dbContext.SaveChanges(); + + // Ensuring EFCore generates empty values for Guid and string keys + Assert.Collection(dbContext.GuidKeysAutoGen, + e => Assert.NotEqual(Guid.Empty, e.ID)); + Assert.Collection(dbContext.StringKeysAutoGen, + e => Assert.NotEmpty(e.ID)); + } + } + [Theory] [MemberData(nameof(GetDatabaseEngines))] public void Upsert_InvalidMatchColumn_NoOn(TestDbContext.DbDriver driver) @@ -1251,5 +1274,89 @@ public void Upsert_JsonData(TestDbContext.DbDriver driver) j => Assert.True(JToken.DeepEquals(JObject.Parse(newJson.Data), JObject.Parse(j.Data)))); } } + + [Theory] + [MemberData(nameof(GetDatabaseEngines))] + public void Upsert_GuidKey_AutoGenThrows(TestDbContext.DbDriver driver) + { + ResetDb(driver); + using (var dbContext = new TestDbContext(_dataContexts[driver])) + { + Assert.Throws(delegate + { + var newItem = new GuidKeyAutoGen + { + ID = Guid.NewGuid(), + Name = "test", + }; + + dbContext.GuidKeysAutoGen.Upsert(newItem) + .Run(); + }); + } + } + + [Theory] + [MemberData(nameof(GetDatabaseEngines))] + public void Upsert_StringKey_AutoGenThrows(TestDbContext.DbDriver driver) + { + ResetDb(driver); + using (var dbContext = new TestDbContext(_dataContexts[driver])) + { + Assert.Throws(delegate + { + var newItem = new StringKeyAutoGen + { + ID = Guid.NewGuid().ToString(), + Name = "test", + }; + + dbContext.StringKeysAutoGen.Upsert(newItem) + .Run(); + }); + } + } + + [Theory] + [MemberData(nameof(GetDatabaseEngines))] + public void Upsert_GuidKey(TestDbContext.DbDriver driver) + { + ResetDb(driver); + using (var dbContext = new TestDbContext(_dataContexts[driver])) + { + var newItem = new GuidKey + { + ID = Guid.NewGuid(), + Name = "test", + }; + + dbContext.GuidKeys.Upsert(newItem) + .Run(); + + Assert.Collection(dbContext.GuidKeys.OrderBy(j => j.ID), + j => Assert.Equal(newItem.ID, j.ID)); + } + } + + [Theory] + [MemberData(nameof(GetDatabaseEngines))] + public void Upsert_StringKey(TestDbContext.DbDriver driver) + { + ResetDb(driver); + using (var dbContext = new TestDbContext(_dataContexts[driver])) + { + var newItem = new StringKey + { + ID = Guid.NewGuid().ToString(), + Name = "test", + }; + + dbContext.StringKeys.Upsert(newItem) + .Run(); + + Assert.Collection(dbContext.StringKeys.OrderBy(j => j.ID), + j => Assert.Equal(newItem.ID, j.ID)); + } + } } }