// =====================================================================================
// Copyright © 2005 by . All rights are reserved.
//
// If you like this code then feel free to go ahead and use it.
// The only thing I ask is that you don't remove or alter my copyright notice.
//
// Your use of this software is entirely at your own risk. I make no claims or
// warrantees about the reliability or fitness of this code for any particular purpose.
// If you make changes or additions to this code please mark your code as being yours.
//
// website , email OmarALZabir@gmail.com, msn oazabir@hotmail.com
// =====================================================================================
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using NUnit.Framework;
using SmartInstitute.DataAccessLayer.Factories;
namespace SmartInstitute.DataAccessLayer.UnitTests
{
/// <summary>
/// Provides tests for the and User objects (entity, collection and repository).
/// </summary>
[TestFixture]
public class UserRepositoryTest
{
// the User instance used to test the repository.
private User mock;
// the UserCollection instance used to test the repository.
UserCollection mockCollection;
/// <summary>
/// Creates a new <see cref="UserRepositoryTest"/> instance.
/// </summary>
public UserRepositoryTest()
{
this.mock = (User)CreateMockInstance();
}
/// <summary>
/// This method is used to construct the test environment prior to running the tests.
/// </summary>
[TestFixtureSetUp]
public void Init()
{
System.Console.WriteLine(new String('-', 75));
//System.Console.WriteLine("-- Testing the User Entity with the " + Configuration.Settings.ClientType + " --");
System.Console.WriteLine(new String('-', 75));
}
/// <summary>
/// This method is used to restore the environment after the tests are completed.
/// </summary>
[TestFixtureTearDown]
public void Dispose()
{
System.Console.WriteLine();
System.Console.WriteLine();
}
/// <summary>
/// Inserts a mock User entity into the database.
/// </summary>
[Test]
public void Step_1_Insert()
{
Assert.IsTrue(UserRepository.Current.Insert(this.mock), "Insert failed");
System.Console.WriteLine("UserRepository.Current.Insert(this.mock):");
System.Console.WriteLine(this.mock);
}
/// <summary>
/// Selects all User objects of the database.
/// </summary>
[Test]
public void Step_2_SelectAll()
{
mockCollection = UserRepository.Current.GetAll();
Assert.IsTrue(mockCollection.Count > 0, "No records returned.");
System.Console.WriteLine("UserRepository.Current.GetAll():");
System.Console.WriteLine(mockCollection);
if (mockCollection.Count > 0)
{
UserRepository.Current.DeepLoad(mockCollection[0]);
}
}
/// <summary>
/// Deep load all User children.
/// </summary>
[Test]
public void Step_3_DeepLoad()
{
if (mockCollection.Count > 0)
{
mockCollection.Shuffle();
UserRepository.Current.DeepLoad(mockCollection[0]);
System.Console.WriteLine("User instance correctly deep loaded at 1 level.");
mockCollection.Shuffle();
UserRepository.Current.DeepLoad(mockCollection[0], true);
System.Console.WriteLine("User instance correctly deep loaded at N level.");
}
}
/// <summary>
/// Updates a mock User entity into the database.
/// </summary>
[Test]
public void Step_4_Update()
{
this.mock = UpdateMockInstance(this.mock);
Assert.IsTrue(UserRepository.Current.Update(this.mock), "Update failed.");
// TODO : select sur l'id
// TODO : verif si l'object recup? est egal
System.Console.WriteLine("UserRepository.Current.Update(this.mock):");
System.Console.WriteLine(this.mock);
}
/// <summary>
/// Delete the mock User entity into the database.
/// </summary>
[Test]
public void Step_5_Delete()
{
Assert.IsTrue(UserRepository.Current.Delete(this.mock), "Delete failed.");
System.Console.WriteLine("UserRepository.Current.Delete(this.mock):");
System.Console.WriteLine(this.mock);
}
#region Serialization tests
/// <summary>
/// Serialize the mock User entity into a temporary file.
/// </summary>
[Test]
public void Step_6_SerializeEntity()
{
string fileName = "temp_User.xml";
XmlSerializer mySerializer = new XmlSerializer(typeof(User));
StreamWriter myWriter = new StreamWriter(fileName);
mySerializer.Serialize(myWriter, this.mock);
myWriter.Close();
System.Console.WriteLine("this.mock correctly serialized to a temporary file.");
}
/// <summary>
/// Deserialize the mock User entity from a temporary file.
/// </summary>
[Test]
public void Step_7_DeserializeEntity()
{
string fileName = "temp_User.xml";
XmlSerializer mySerializer = new XmlSerializer(typeof(User));
FileStream myFileStream = new FileStream(fileName, FileMode.Open);
this.mock = (User) mySerializer.Deserialize(myFileStream);
myFileStream.Close();
File.Delete(fileName);
System.Console.WriteLine("this.mock correctly deserialized from a temporary file.");
}
/// <summary>
/// Serialize a User collection into a temporary file.
/// </summary>
[Test]
public void Step_8_SerializeCollection()
{
string fileName = "temp_UserCollection.xml";
UserCollection mockCollection = new UserCollection();
mockCollection.Add(this.mock);
XmlSerializer mySerializer = new XmlSerializer(typeof(UserCollection));
StreamWriter myWriter = new StreamWriter(fileName);
mySerializer.Serialize(myWriter, mockCollection);
myWriter.Close();
System.Console.WriteLine("UserCollection correctly serialized to a temporary file.");
}
/// <summary>
/// Deserialize a User collection from a temporary file.
/// </summary>
[Test]
public void Step_9_DeserializeCollection()
{
string fileName = "temp_UserCollection.xml";
XmlSerializer mySerializer = new XmlSerializer(typeof(UserCollection));
FileStream myFileStream = new FileStream(fileName, FileMode.Open);
UserCollection mockCollection = (UserCollection) mySerializer.Deserialize(myFileStream);
myFileStream.Close();
File.Delete(fileName);
System.Console.WriteLine("UserCollection correctly deserialized from a temporary file.");
}
#endregion
#region Mock Instance
///<summary>
/// Returns a Typed User Entity with mock values.
///</summary>
public User CreateMockInstance()
{
User mock = new User();
mock.FirstName = "test"; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.LastName = "test"; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.MiddleName = "test"; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.NickName = "test"; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.DateOfBirth = new DateTime(2005, 7, 24, 23, 7, 30, 656); /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.Sex = (int)12; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.Nationality = "test"; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.Phone = "test"; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.PermanentAddress1 = "test"; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.PermanentAddress2 = "test"; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.TemporaryAddress1 = "test"; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.TemporaryAddress2 = "test"; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.Email = "test"; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.UserName = "test"; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.UserPassword = "test"; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.UserType = (int)12; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.UserStatus = (int)12; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.Religion = (int)12; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.MaritalStatus = (int)12; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.BloodGroup = "test"; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.PictureLocation = "test"; /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
mock.ChangeStamp = new DateTime(2005, 7, 24, 23, 7, 30, 656); /*new Guid("20E43088-4618-4F4A-B8AD-FC31B50D94CD")*/
UserGroupCollection _collection0 = UserGroupRepository.Current.GetAll();
_collection0.Shuffle();
if (_collection0.Count > 0)
{
mock.UserGroupID = _collection0[0].ID;
}
return (User)mock;
}
///<summary>
/// Update the Typed User Entity with modified mock values.
///</summary>
public User UpdateMockInstance(User mock)
{
mock.FirstName = "test2";
mock.LastName = "test2";
mock.MiddleName = "test2";
mock.NickName = "test2";
mock.DateOfBirth = new DateTime(2005, 7, 24, 23, 7, 30, 656);
mock.Sex = (int)13;
mock.Nationality = "test2";
mock.Phone = "test2";
mock.PermanentAddress1 = "test2";
mock.PermanentAddress2 = "test2";
mock.TemporaryAddress1 = "test2";
mock.TemporaryAddress2 = "test2";
mock.Email = "test2";
mock.UserName = "test2";
mock.UserPassword = "test2";
mock.UserType = (int)13;
mock.UserStatus = (int)13;
mock.Religion = (int)13;
mock.MaritalStatus = (int)13;
mock.BloodGroup = "test2";
mock.PictureLocation = "test2";
mock.ChangeStamp = new DateTime(2005, 7, 24, 23, 7, 30, 656);
UserGroupCollection _collection0 = UserGroupRepository.Current.GetAll();
_collection0.Shuffle();
if (_collection0.Count > 0)
{
mock.UserGroupID = _collection0[0].ID;
}
return (User)mock;
}
#endregion
}
}