Geen omschrijving

DbPacketRepositoryTests.cs 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 NHibernate;
  30. using NUnit.Framework;
  31. using WhiteNoise.Domain.Abstract;
  32. using WhiteNoise.Domain.Concrete;
  33. using WhiteNoise.Domain.Entities;
  34. namespace WhiteNoise.Test.Domain.Concrete
  35. {
  36. /// <summary>
  37. /// Database packet repository tests.
  38. /// </summary>
  39. /// <exception cref='NotImplementedException'>
  40. /// Is thrown when a requested operation is not implemented for a given type.
  41. /// </exception>
  42. [TestFixture]
  43. public class DbPacketRepositoryTests : DbRepositoryTests<Packet>
  44. {
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="WhiteNoise.Test.Domain.Concrete.DbPacketRepositoryTests"/> class.
  47. /// </summary>
  48. public DbPacketRepositoryTests()
  49. {
  50. this.Items = new List<Packet>()
  51. {
  52. new Packet() { Type = "Type1", Data = new byte[] { } },
  53. new Packet() { Type = "Type1", Data = new byte[] { } },
  54. new Packet() { Type = "Type2", Data = new byte[] { } },
  55. new Packet() { Type = "Type2", Data = new byte[] { } },
  56. new Packet() { Type = "Type3", Data = new byte[] { } },
  57. new Packet() { Type = "Type3", Data = new byte[] { } },
  58. };
  59. }
  60. [Test]
  61. public void CanAddPacket()
  62. {
  63. var item = new Packet() { Type = "Type4", Data = new byte[] { } };
  64. using (ISession session = this.SessionFactory.OpenSession())
  65. {
  66. IPacketRepository repository = new DbPacketRepository(session);
  67. repository.Add(item);
  68. }
  69. // Use a different session to properly test the transaction.
  70. using (ISession session = this.SessionFactory.OpenSession())
  71. {
  72. var fromDatabase = session.Get<Packet>(item.Id);
  73. Assert.That(fromDatabase, Is.Not.Null);
  74. Assert.That(fromDatabase, Is.Not.SameAs(item));
  75. Assert.That(fromDatabase, Is.EqualTo(item.Type));
  76. }
  77. }
  78. [Test]
  79. public void CanDetermineEqualityOfPackets()
  80. {
  81. throw new NotImplementedException();
  82. }
  83. [Test]
  84. public void CanFetchRepository()
  85. {
  86. using (ISession session = this.SessionFactory.OpenSession())
  87. {
  88. IPacketRepository repository = new DbPacketRepository(session);
  89. var fromDatabase = repository.Collection;
  90. Assert.That(fromDatabase.Count, Is.EqualTo(this.Items.Count));
  91. foreach (var item in this.Items)
  92. {
  93. Assert.That(fromDatabase.Any(x => x.Type == item.Type));
  94. }
  95. }
  96. }
  97. [Test]
  98. public void CanFindById()
  99. {
  100. var item = this.Items.First();
  101. using (ISession session = this.SessionFactory.OpenSession())
  102. {
  103. IPacketRepository repository = new DbPacketRepository(session);
  104. var fromDatabase = repository.Find(item.Id);
  105. Assert.That(fromDatabase, Is.Not.Null);
  106. Assert.That(fromDatabase, Is.Not.SameAs(item));
  107. Assert.That(fromDatabase.Id, Is.EqualTo(item.Id));
  108. }
  109. }
  110. [Test]
  111. public void CanRemovePacket()
  112. {
  113. var item = this.Items.First();
  114. using (ISession session = this.SessionFactory.OpenSession())
  115. {
  116. IPacketRepository repository = new DbPacketRepository(session);
  117. repository.Remove(item);
  118. }
  119. // Use a different session to properly test the transaction.
  120. using (ISession session = this.SessionFactory.OpenSession())
  121. {
  122. var fromDatabase = session.Get<Packet>(item.Id);
  123. Assert.That(fromDatabase, Is.Null);
  124. }
  125. }
  126. [Test]
  127. public void CanUpdatePacket()
  128. {
  129. var item = this.Items.First();
  130. item.Type = @"Type6";
  131. using (ISession session = this.SessionFactory.OpenSession())
  132. {
  133. IPacketRepository repository = new DbPacketRepository(session);
  134. repository.Update(item);
  135. }
  136. // Use a different session to properly test the transaction.
  137. using (ISession session = this.SessionFactory.OpenSession())
  138. {
  139. var fromDatabase = session.Get<Packet>(item.Id);
  140. Assert.That(fromDatabase, Is.Not.Null);
  141. Assert.That(fromDatabase.Type, Is.EqualTo(item.Type));
  142. }
  143. }
  144. }
  145. }