// // DbPacketRepositoryTests.cs // // Author: // Bryan Allred // // Copyright (c) 2011 Bryan Allred // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; using System.Collections.Generic; using System.Linq; using FluentNHibernate.Testing; using NHibernate; using NUnit.Framework; using WhiteNoise.Domain.Abstract; using WhiteNoise.Domain.Concrete; using WhiteNoise.Domain.Entities; namespace WhiteNoise.Test.Domain.Concrete { /// /// Database packet repository tests. /// /// /// Is thrown when a requested operation is not implemented for a given type. /// [TestFixture] public class DbPacketRepositoryTests : DbRepositoryTests { /// /// Initializes a new instance of the class. /// public DbPacketRepositoryTests() { this.Items = new List() { new Packet() { Type = "Type1", Data = new byte[] { } }, new Packet() { Type = "Type1", Data = new byte[] { } }, new Packet() { Type = "Type2", Data = new byte[] { } }, new Packet() { Type = "Type2", Data = new byte[] { } }, new Packet() { Type = "Type3", Data = new byte[] { } }, new Packet() { Type = "Type3", Data = new byte[] { } }, }; } /// /// Determines whether this instance can add packet. /// /// /// true if this instance can add packet; otherwise, false. /// [Test] public void CanAddPacket() { using (ISession session = this.SessionFactory.OpenSession()) { new PersistenceSpecification(session) .CheckProperty(c => c.Type, "Type4") .CheckProperty(c => c.Data, new byte[] { }) .VerifyTheMappings(); } } /// /// Determines whether this instance can fetch repository. /// /// /// true if this instance can fetch repository; otherwise, false. /// [Test] public void CanFetchRepository() { using (ISession session = this.SessionFactory.OpenSession()) { IPacketRepository repository = new DbPacketRepository(session); var fromDatabase = repository.Collection; Assert.That(fromDatabase.Count, Is.EqualTo(this.Items.Count)); foreach (var item in this.Items) { Assert.That(fromDatabase.Any(x => x.Type == item.Type)); } } } /// /// Determines whether this instance can find by identifier. /// /// /// true if this instance can find by identifier; otherwise, false. /// [Test] public void CanFindById() { var item = this.Items.First(); using (ISession session = this.SessionFactory.OpenSession()) { IPacketRepository repository = new DbPacketRepository(session); var fromDatabase = repository.Find(item.Id); Assert.That(fromDatabase, Is.Not.Null); Assert.That(fromDatabase, Is.Not.SameAs(item)); Assert.That(fromDatabase.Id, Is.EqualTo(item.Id)); } } /// /// Determines whether this instance can remove packet. /// /// /// true if this instance can remove packet; otherwise, false. /// [Test] public void CanRemovePacket() { var item = this.Items.First(); using (ISession session = this.SessionFactory.OpenSession()) { IPacketRepository repository = new DbPacketRepository(session); repository.Remove(item); } // Use a different session to properly test the transaction. using (ISession session = this.SessionFactory.OpenSession()) { var fromDatabase = session.Get(item.Id); Assert.That(fromDatabase, Is.Null); } } /// /// Determines whether this instance can update packet. /// /// /// true if this instance can update packet; otherwise, false. /// [Test] public void CanUpdatePacket() { var item = this.Items.First(); item.Type = @"Type6"; using (ISession session = this.SessionFactory.OpenSession()) { IPacketRepository repository = new DbPacketRepository(session); repository.Update(item); } // Use a different session to properly test the transaction. using (ISession session = this.SessionFactory.OpenSession()) { var fromDatabase = session.Get(item.Id); Assert.That(fromDatabase, Is.Not.Null); Assert.That(fromDatabase.Type, Is.EqualTo(item.Type)); } } } }