Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 };
- }
- }
- }
Add Comment
Please, Sign In to add comment