Skip to content

Commit

Permalink
+ Adding extra tests for string and guid key tables
Browse files Browse the repository at this point in the history
  • Loading branch information
artiomchi committed Dec 10, 2018
1 parent c218138 commit c066812
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/FlexLabs.EntityFrameworkCore.Upsert/HelpLinks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ namespace FlexLabs.EntityFrameworkCore.Upsert
{
/// <summary>
/// Thrown when using unsupported columns as the upsert match expression.
/// See: https://flexlabs.org/plink/upsert.sqlite3
/// See: https://go.flexlabs.org/upsert.identitykeymatch
/// </summary>
/// <seealso cref=""/>
public sealed class InvalidMatchColumnsException : Exception
{
internal InvalidMatchColumnsException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace FlexLabs.EntityFrameworkCore.Upsert
{
/// <summary>
/// Thrown when using unsupported expressions in the update clause
/// See: https://go.flexlabs.org/upsert.expressions
/// </summary>
public class UnsupportedExpressionException : Exception
{
Expand Down
26 changes: 26 additions & 0 deletions test/FlexLabs.EntityFrameworkCore.Upsert.Tests/EF/Base/Tables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
public DbSet<PageVisit> PageVisits { get; set; }
public DbSet<SchemaTable> SchemaTable { get; set; }
public DbSet<Status> Statuses { get; set; }
public DbSet<GuidKeyAutoGen> GuidKeysAutoGen { get; set; }
public DbSet<GuidKey> GuidKeys { get; set; }
public DbSet<StringKeyAutoGen> StringKeysAutoGen { get; set; }
public DbSet<StringKey> StringKeys { get; set; }

public enum DbDriver
{
Expand Down
107 changes: 107 additions & 0 deletions test/FlexLabs.EntityFrameworkCore.Upsert.Tests/EF/BasicTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<InvalidMatchColumnsException>(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<InvalidMatchColumnsException>(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));
}
}
}
}

0 comments on commit c066812

Please sign in to comment.