Created
May 20, 2012 21:31
-
-
Save tzarger/2759623 to your computer and use it in GitHub Desktop.
UniqueConstraints Throws Veto on SaveChanges
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel.DataAnnotations; | |
using Raven.Client.UniqueConstraints; | |
namespace UnitTests.Tests.Areas.Administration.Controllers { | |
public class Account { | |
public Guid Id { get; set; } | |
[Required] | |
[UniqueConstraint] | |
public string Username { get; set; } | |
[Required] | |
[UniqueConstraint] | |
public string Email { get; set; } | |
[Required] | |
public string Password { get; set; } | |
[Required] | |
public string FirstName { get; set; } | |
[Required] | |
public string LastName { get; set; } | |
public DateTimeOffset Created { get; set; } | |
public DateTimeOffset LastModified { get; set; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel.DataAnnotations; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using Raven.Client; | |
using Raven.Client.Document; | |
using Raven.Client.UniqueConstraints; | |
namespace UnitTests.Tests.Areas.Administration.Controllers { | |
[TestClass] | |
public class AccountsControllerTest { | |
[TestMethod] | |
public void Editing_Document_With_Unique_Constraints_SaveChanges_Vetos_Put() { | |
IDocumentStore store = Initialize( "RavenDb" ); | |
var accountId = Guid.Empty; | |
using( var session = store.OpenSession() ) { | |
var account = SeedAccount(); | |
var check = session.CheckForUniqueConstraints( account ); | |
if( check.ConstraintsAreFree() ) { | |
session.Store( account ); | |
session.SaveChanges(); | |
accountId = account.Id; | |
} | |
Assert.AreNotEqual( Guid.Empty, account.Id ); | |
} | |
// I would think on an edit, you would not need to CheckForUniqueConstraints | |
using( var session = store.OpenSession() ) { | |
var account = session.Load<Account>( accountId ); | |
account.FirstName = "ChangedName"; | |
try { | |
session.SaveChanges(); | |
Assert.Equals( "ChangedName", account.FirstName ); | |
} catch( Exception ex ) { | |
// We enter here due to constraint issue... | |
//Assert.Fail(); | |
//Let's move on to test with checking for constraints first... | |
} | |
} | |
// Just for giggles, performing a CheckForUniqueConstraints which fails obviously | |
using( var session = store.OpenSession() ) { | |
var account = session.Load<Account>( accountId ); | |
account.FirstName = "ChangedName"; | |
var check = session.CheckForUniqueConstraints( account ); | |
if( check.ConstraintsAreFree() ) { | |
session.SaveChanges(); | |
Assert.Equals( "ChangedName", account.FirstName ); | |
} else { | |
Assert.Inconclusive(); | |
} | |
} | |
} | |
private static IDocumentStore Initialize( string connectionStringName ) { | |
var instance = new DocumentStore { | |
ConnectionStringName = connectionStringName | |
} | |
.RegisterListener( new UniqueConstraintsStoreListener() ); | |
instance.Conventions.TransformTypeTagNameToDocumentKeyPrefix = PreserveTypeTagNameToDocumentKeyPrefix; | |
instance.Initialize(); | |
return instance; | |
} | |
private static string PreserveTypeTagNameToDocumentKeyPrefix( string typeTagName ) { | |
return typeTagName; | |
} | |
private static Account SeedAccount() { | |
var uniquePostFix = Guid.NewGuid().ToString().Replace( "-", "" ); | |
return new Account { Username = "User" + uniquePostFix, Email = "test" + uniquePostFix + "@test.com", FirstName = "User", LastName = "Testing", Password = "testing", Created = DateTimeOffset.Now, LastModified = DateTimeOffset.Now }; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment