Keine Beschreibung

DbPacketRepositoryTests.cs 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // DbPacketRepositoryTests.cs
  3. //
  4. // Author:
  5. // Bryan Allred <bryan.allred@gmail.com>
  6. //
  7. // Copyright (c) 2011 Bryan Allred
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using System.Collections.Generic;
  28. using System.Linq;
  29. using FluentNHibernate.Testing;
  30. using NHibernate;
  31. using NUnit.Framework;
  32. using WhiteNoise.Domain.Abstract;
  33. using WhiteNoise.Domain.Concrete;
  34. using WhiteNoise.Domain.Entities;
  35. namespace WhiteNoise.Test.Domain.Concrete
  36. {
  37. /// <summary>
  38. /// Database packet repository tests.
  39. /// </summary>
  40. /// <exception cref='NotImplementedException'>
  41. /// Is thrown when a requested operation is not implemented for a given type.
  42. /// </exception>
  43. [TestFixture]
  44. public class DbPacketRepositoryTests : DbRepositoryTests<Packet>
  45. {
  46. /// <summary>
  47. /// Initializes a new instance of the <see cref="WhiteNoise.Test.Domain.Concrete.DbPacketRepositoryTests"/> class.
  48. /// </summary>
  49. public DbPacketRepositoryTests()
  50. {
  51. this.Items = new List<Packet>()
  52. {
  53. new Packet() { Type = "Type1", Data = new byte[] { } },
  54. new Packet() { Type = "Type1", Data = new byte[] { } },
  55. new Packet() { Type = "Type2", Data = new byte[] { } },
  56. new Packet() { Type = "Type2", Data = new byte[] { } },
  57. new Packet() { Type = "Type3", Data = new byte[] { } },
  58. new Packet() { Type = "Type3", Data = new byte[] { } },
  59. };
  60. }
  61. /// <summary>
  62. /// Determines whether this instance can add packet.
  63. /// </summary>
  64. /// <returns>
  65. /// <c>true</c> if this instance can add packet; otherwise, <c>false</c>.
  66. /// </returns>
  67. [Test]
  68. public void CanAddPacket()
  69. {
  70. using (ISession session = this.SessionFactory.OpenSession())
  71. {
  72. new PersistenceSpecification<Packet>(session)
  73. .CheckProperty(c => c.Type, "Type4")
  74. .CheckProperty(c => c.Data, new byte[] { })
  75. .VerifyTheMappings();
  76. }
  77. }
  78. /// <summary>
  79. /// Determines whether this instance can fetch repository.
  80. /// </summary>
  81. /// <returns>
  82. /// <c>true</c> if this instance can fetch repository; otherwise, <c>false</c>.
  83. /// </returns>
  84. [Test]
  85. public void CanFetchRepository()
  86. {
  87. using (ISession session = this.SessionFactory.OpenSession())
  88. {
  89. IPacketRepository repository = new DbPacketRepository(session);
  90. var fromDatabase = repository.Collection;
  91. Assert.That(fromDatabase.Count, Is.EqualTo(this.Items.Count));
  92. foreach (var item in this.Items)
  93. {
  94. Assert.That(fromDatabase.Any(x => x.Type == item.Type));
  95. }
  96. }
  97. }
  98. /// <summary>
  99. /// Determines whether this instance can find by identifier.
  100. /// </summary>
  101. /// <returns>
  102. /// <c>true</c> if this instance can find by identifier; otherwise, <c>false</c>.
  103. /// </returns>
  104. [Test]
  105. public void CanFindById()
  106. {
  107. var item = this.Items.First();
  108. using (ISession session = this.SessionFactory.OpenSession())
  109. {
  110. IPacketRepository repository = new DbPacketRepository(session);
  111. var fromDatabase = repository.Find(item.Id);
  112. Assert.That(fromDatabase, Is.Not.Null);
  113. Assert.That(fromDatabase, Is.Not.SameAs(item));
  114. Assert.That(fromDatabase.Id, Is.EqualTo(item.Id));
  115. }
  116. }
  117. /// <summary>
  118. /// Determines whether this instance can remove packet.
  119. /// </summary>
  120. /// <returns>
  121. /// <c>true</c> if this instance can remove packet; otherwise, <c>false</c>.
  122. /// </returns>
  123. [Test]
  124. public void CanRemovePacket()
  125. {
  126. var item = this.Items.First();
  127. using (ISession session = this.SessionFactory.OpenSession())
  128. {
  129. IPacketRepository repository = new DbPacketRepository(session);
  130. repository.Remove(item);
  131. }
  132. // Use a different session to properly test the transaction.
  133. using (ISession session = this.SessionFactory.OpenSession())
  134. {
  135. var fromDatabase = session.Get<Packet>(item.Id);
  136. Assert.That(fromDatabase, Is.Null);
  137. }
  138. }
  139. /// <summary>
  140. /// Determines whether this instance can update packet.
  141. /// </summary>
  142. /// <returns>
  143. /// <c>true</c> if this instance can update packet; otherwise, <c>false</c>.
  144. /// </returns>
  145. [Test]
  146. public void CanUpdatePacket()
  147. {
  148. var item = this.Items.First();
  149. item.Type = @"Type6";
  150. using (ISession session = this.SessionFactory.OpenSession())
  151. {
  152. IPacketRepository repository = new DbPacketRepository(session);
  153. repository.Update(item);
  154. }
  155. // Use a different session to properly test the transaction.
  156. using (ISession session = this.SessionFactory.OpenSession())
  157. {
  158. var fromDatabase = session.Get<Packet>(item.Id);
  159. Assert.That(fromDatabase, Is.Not.Null);
  160. Assert.That(fromDatabase.Type, Is.EqualTo(item.Type));
  161. }
  162. }
  163. }
  164. }