Guest User

Untitled

a guest
Oct 11th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Raven.Client;
  5. using Raven.Client.Document;
  6. using Raven.Client.UniqueConstraints;
  7.  
  8. namespace UnitTests.Tests.Areas.Administration.Controllers {
  9. [TestClass]
  10. public class AccountsControllerTest {
  11.  
  12.  
  13. [TestMethod]
  14. public void Editing_Document_With_Unique_Constraints_SaveChanges_Vetos_Put() {
  15. IDocumentStore store = Initialize( "RavenDb" );
  16. var accountId = Guid.Empty;
  17.  
  18. using( var session = store.OpenSession() ) {
  19. var account = SeedAccount();
  20. var check = session.CheckForUniqueConstraints( account );
  21. if( check.ConstraintsAreFree() ) {
  22. session.Store( account );
  23. session.SaveChanges();
  24. accountId = account.Id;
  25. }
  26. Assert.AreNotEqual( Guid.Empty, account.Id );
  27. }
  28.  
  29. // I would think on an edit, you would not need to CheckForUniqueConstraints
  30. using( var session = store.OpenSession() ) {
  31. var account = session.Load<Account>( accountId );
  32. account.FirstName = "ChangedName";
  33. try {
  34. session.SaveChanges();
  35. Assert.Equals( "ChangedName", account.FirstName );
  36. } catch( Exception ex ) {
  37. // We enter here due to constraint issue...
  38. //Assert.Fail();
  39. //Let's move on to test with checking for constraints first...
  40. }
  41. }
  42.  
  43. // Just for giggles, performing a CheckForUniqueConstraints which fails obviously
  44. using( var session = store.OpenSession() ) {
  45. var account = session.Load<Account>( accountId );
  46. account.FirstName = "ChangedName";
  47. var check = session.CheckForUniqueConstraints( account );
  48. if( check.ConstraintsAreFree() ) {
  49. session.SaveChanges();
  50. Assert.Equals( "ChangedName", account.FirstName );
  51. } else {
  52. Assert.Inconclusive();
  53. }
  54. }
  55. }
  56.  
  57. private static IDocumentStore Initialize( string connectionStringName ) {
  58. var instance = new DocumentStore {
  59. ConnectionStringName = connectionStringName
  60. }
  61. .RegisterListener( new UniqueConstraintsStoreListener() );
  62.  
  63. instance.Conventions.TransformTypeTagNameToDocumentKeyPrefix = PreserveTypeTagNameToDocumentKeyPrefix;
  64. instance.Initialize();
  65. return instance;
  66. }
  67.  
  68. private static string PreserveTypeTagNameToDocumentKeyPrefix( string typeTagName ) {
  69. return typeTagName;
  70. }
  71.  
  72. private static Account SeedAccount() {
  73. var uniquePostFix = Guid.NewGuid().ToString().Replace( "-", "" );
  74. return new Account { Username = "User" + uniquePostFix, Email = "test" + uniquePostFix + "@test.com", FirstName = "User", LastName = "Testing", Password = "testing", Created = DateTimeOffset.Now, LastModified = DateTimeOffset.Now };
  75. }
  76. }
  77. }
Add Comment
Please, Sign In to add comment