PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Tags/0.11Beta/Formsy.Test.Nunit/FormsyServiceTest.cs

#
C# | 378 lines | 221 code | 109 blank | 48 comment | 8 complexity | bd8a658d2677ba6e16fe49c2f4a833ef MD5 | raw file
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. //using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Formsy.Data.Services;
  7. using Formsy.Data.Linq;
  8. using NUnit.Framework;
  9. using Formsy.Data.Interfaces;
  10. using Anthony.Library;
  11. using System.Data.SqlTypes;
  12. namespace Formsy.Test
  13. {
  14. [TestFixture]
  15. public class FormsyServiceTest
  16. {
  17. string m_FormName = "UnitTest";
  18. Dictionary<string, string> m_Dictionary;
  19. Dictionary<string, string> m_DictionaryUpdate;
  20. int m_EntryId = 27;
  21. DateTime m_MinDate;
  22. DateTime m_MaxDate;
  23. public FormsyServiceTest()
  24. {
  25. }
  26. // TESTS
  27. // csv methods
  28. //DataTable GetCSV(string competitionName, DateTime start, DateTime end, string path, List<string> ignoreList);
  29. //DataTable GetHeadings(List<IFormsyEntry> entries, List<string> nonDataHeadings, DataTable table, List<string> ignoreList);
  30. #region Setup and TearDown
  31. [TestFixtureSetUp]
  32. public void Setup()
  33. {
  34. m_Dictionary = new Dictionary<string, string>();
  35. m_Dictionary.Add("First Name", "Anthony");
  36. m_Dictionary.Add("Last Name", "Dang");
  37. m_Dictionary.Add("Email", "formsy_recipient@yourdomain.com");
  38. m_DictionaryUpdate = new Dictionary<string, string>();
  39. m_DictionaryUpdate.Add("First Name", "Avril");
  40. m_DictionaryUpdate.Add("Last Name", "Lavigne");
  41. m_DictionaryUpdate.Add("Email", "noemail@noemail.com");
  42. m_MinDate = DateTime.Parse(SqlDateTime.MinValue.ToString());
  43. m_MaxDate = DateTime.Parse(SqlDateTime.MaxValue.ToString());
  44. }
  45. [TestFixtureTearDown]
  46. public void TearDown()
  47. {
  48. }
  49. #endregion
  50. #region InsertEntry
  51. // IFormsyEntry InsertEntry(string formName);
  52. private void InsertEntry()
  53. {
  54. IFormsyEntry entry = HttpDataService.Instance.FormsyService.InsertEntry(m_FormName);
  55. Assert.IsNotNull(entry);
  56. IFormsyForm form = HttpDataService.Instance.FormsyService.GetForm(m_FormName);
  57. Assert.IsNotNull(form);
  58. m_EntryId = entry.Id;
  59. }
  60. #endregion
  61. #region InsertEntryData
  62. // List<IFormsyEntryData> InsertEntryData(int entryId, Dictionary<string, string> dictionary);
  63. // protected void AddXmlToEntry(int entryId, List<FormsyEntryData> dataItems)
  64. // IFormsyEntry GetEntry(int entryId);
  65. // public List<IFormsyEntryData> GetEntryDatas(int entryId)
  66. [Test]
  67. public void InsertEntryData()
  68. {
  69. // create an entry first!
  70. InsertEntry();
  71. // get formId
  72. int formId = GetFormId();
  73. // insert
  74. var datasInserted = HttpDataService.Instance.FormsyService.InsertEntryData(formId, m_EntryId, m_Dictionary);
  75. Assert.IsNotEmpty(datasInserted);
  76. // get data out
  77. // tests GetEntryDatas(int entryId)
  78. var datasRetrieved = HttpDataService.Instance.FormsyService.GetEntryDatas(m_EntryId);
  79. Assert.IsNotEmpty(datasRetrieved);
  80. // compare
  81. Assert.AreEqual(datasInserted.Count, datasRetrieved.Count);
  82. for (int i = 0; i < datasInserted.Count; i++)
  83. {
  84. Assert.AreEqual(datasInserted[i].Id, datasRetrieved[i].Id);
  85. Assert.AreEqual(datasInserted[i].EntryId, datasRetrieved[i].EntryId);
  86. Assert.AreEqual(datasInserted[i].Name, datasRetrieved[i].Name);
  87. Assert.AreEqual(datasInserted[i].Value, datasRetrieved[i].Value);
  88. }
  89. // get xml from entry, ensure that it was serialized and saved.
  90. // get xml from entry - tests GetEntry()
  91. IFormsyEntry entry = HttpDataService.Instance.FormsyService.GetEntry(m_EntryId);
  92. Assert.IsNotNull(entry);
  93. List<FormsyEntryData> list = datasInserted.Cast<FormsyEntryData>().ToList();
  94. string xml = XmlSerializing.ToXml(list, typeof(List<FormsyEntryData>));
  95. // tests saved xml
  96. Assert.AreEqual(entry.Xml, xml);
  97. // test deserialization.
  98. List<FormsyEntryData> deserializedDatas = (List<FormsyEntryData>)XmlSerializing.FromXml(entry.Xml, typeof(List<FormsyEntryData>));
  99. for (int i = 0; i < deserializedDatas.Count; i++)
  100. {
  101. Assert.AreEqual(datasInserted[i].Id, deserializedDatas[i].Id);
  102. Assert.AreEqual(datasInserted[i].EntryId, deserializedDatas[i].EntryId);
  103. Assert.AreEqual(datasInserted[i].Name, deserializedDatas[i].Name);
  104. Assert.AreEqual(datasInserted[i].Value, deserializedDatas[i].Value);
  105. }
  106. }
  107. #endregion
  108. #region UpdateEntryData
  109. // List<IFormsyEntryData> UpdateEntryData(int entryId, Dictionary<string, string> dictionary);
  110. [Test]
  111. public void UpdateEntryData()
  112. {
  113. InsertEntryData();
  114. // get data out
  115. var datasRetrieved = HttpDataService.Instance.FormsyService.GetEntryDatas(m_EntryId);
  116. Assert.IsNotEmpty(datasRetrieved);
  117. List<IFormsyEntryData> cloned = new List<IFormsyEntryData>();
  118. // make a copy of the items because the items in datasRetrieved are pointing to the data context.
  119. foreach (var v in datasRetrieved)
  120. {
  121. FormsyEntryData d = new FormsyEntryData();
  122. d.EntryId = v.EntryId;
  123. d.Id = v.Id;
  124. d.Name = v.Name;
  125. d.Value = v.Value;
  126. cloned.Add(d);
  127. }
  128. // get formId
  129. int formId = GetFormId();
  130. // update
  131. var datasUpdated = HttpDataService.Instance.FormsyService.UpdateEntryData(formId, m_EntryId, m_DictionaryUpdate);
  132. Assert.IsNotEmpty(datasUpdated);
  133. // compare sizes
  134. Assert.AreEqual(datasRetrieved.Count, datasUpdated.Count);
  135. // compare values
  136. for (int i = 0; i < datasUpdated.Count; i++)
  137. {
  138. Assert.AreEqual(datasUpdated[i].Id, cloned[i].Id);
  139. Assert.AreEqual(datasUpdated[i].EntryId, cloned[i].EntryId);
  140. Assert.AreEqual(datasUpdated[i].Name, cloned[i].Name);
  141. Assert.AreNotEqual(datasUpdated[i].Value, cloned[i].Value); // the values were changed
  142. }
  143. // test that the xml was serialized and saved in the entry
  144. // get xml from entry - tests GetEntry()
  145. IFormsyEntry entry = HttpDataService.Instance.FormsyService.GetEntry(m_EntryId);
  146. Assert.IsNotNull(entry);
  147. // test saved xml
  148. string xml = XmlSerializing.ToXml(datasUpdated.Cast<FormsyEntryData>().ToList(), typeof(List<FormsyEntryData>));
  149. Assert.AreEqual(entry.Xml, xml);
  150. // test deserialization.
  151. List<FormsyEntryData> deserializedDatas = (List<FormsyEntryData>)XmlSerializing.FromXml(entry.Xml, typeof(List<FormsyEntryData>));
  152. CompareLists(datasUpdated.Cast<FormsyEntryData>().ToList(), deserializedDatas);
  153. }
  154. #endregion
  155. #region GetForm
  156. // IFormsyForm GetForm(string formName);
  157. [Test]
  158. public void GetForm()
  159. {
  160. var form = HttpDataService.Instance.FormsyService.GetForm(m_FormName);
  161. Assert.IsNotNull(form);
  162. Assert.AreEqual(form.Name, m_FormName);
  163. }
  164. #endregion
  165. #region EnsureFormExists
  166. // void EnsureFormExists(string formName);
  167. // IFormsyForm CreateForm(string formName);
  168. [Test]
  169. public void EnsureFormExists()
  170. {
  171. // make a form name with suffix
  172. int i = 0;
  173. string formName = m_FormName + i;
  174. while (true)
  175. {
  176. IFormsyForm test = HttpDataService.Instance.FormsyService.GetForm(formName);
  177. if (test == null)
  178. break;
  179. formName = m_FormName + i++;
  180. }
  181. // create a form
  182. IFormsyForm createdForm = HttpDataService.Instance.FormsyService.CreateForm(formName);
  183. Assert.IsNotNull(createdForm);
  184. // get the form (ensure it exists)
  185. IFormsyForm form = HttpDataService.Instance.FormsyService.EnsureFormExists(formName);
  186. Assert.IsNotNull(form);
  187. // ensure it's the same form
  188. Assert.AreEqual(createdForm.Id, form.Id);
  189. Assert.AreEqual(createdForm.Name, form.Name);
  190. }
  191. #endregion
  192. #region GetEntries
  193. //List<IFormsyEntry> GetEntries(string formName, DateTime start, DateTime end);
  194. [Test]
  195. public void GetEntries()
  196. {
  197. List<IFormsyEntry> entries = HttpDataService.Instance.FormsyService.GetEntries(m_FormName, m_MinDate, m_MaxDate);
  198. Assert.IsNotNull(entries);
  199. Assert.IsNotEmpty(entries);
  200. }
  201. #endregion
  202. #region GetEntryDatas
  203. //public List<IFormsyEntryData> GetEntryDatas(int entryId)
  204. [Test]
  205. public void GetEntryDatas()
  206. {
  207. List<IFormsyEntryData> datas = HttpDataService.Instance.FormsyService.GetEntryDatas(m_EntryId);
  208. Assert.IsNotNull(datas);
  209. Assert.IsNotEmpty(datas);
  210. }
  211. #endregion
  212. #region TestEntryToDataXmlConsistency
  213. [Test]
  214. public void TestEntryToDataXmlConsistency()
  215. {
  216. // get entries
  217. List<IFormsyEntry> entries = HttpDataService.Instance.FormsyService.GetEntries(m_FormName, m_MinDate, m_MaxDate);
  218. Assert.IsNotNull(entries);
  219. Assert.IsNotEmpty(entries);
  220. foreach (var entry in entries)
  221. {
  222. // get datas for this entry
  223. List<IFormsyEntryData> datas = HttpDataService.Instance.FormsyService.GetEntryDatas(entry.Id);
  224. Assert.IsNotNull(datas);
  225. Assert.IsNotEmpty(datas);
  226. // serialize the datas to xml
  227. string xml = XmlSerializing.ToXml(datas.Cast<FormsyEntryData>().ToList(), typeof(List<FormsyEntryData>));
  228. // check xml against entry.Xml
  229. Assert.AreEqual(xml, entry.Xml);
  230. // double-check by deserializing entry.Xml, and checking against datas list
  231. List<FormsyEntryData> datasFromEntry = (List<FormsyEntryData>)XmlSerializing.FromXml(entry.Xml, typeof(List<FormsyEntryData>));
  232. CompareLists(datas.Cast<FormsyEntryData>().ToList(), datasFromEntry);
  233. }
  234. }
  235. #endregion
  236. #region GetEntryDatas
  237. //public List<IFormsyEntryData> GetEntryDatas(int entryId)
  238. [Test]
  239. public void XmlTest()
  240. {
  241. var data = HttpDataService.Instance.FormsyService.GetEntryDatas(m_EntryId);
  242. List<FormsyEntryData> cased = data.Cast<FormsyEntryData>().ToList();
  243. string xml = XmlSerializing.ToXml(cased, cased.GetType());
  244. var v = XmlSerializing.FromXml(xml, cased.GetType());
  245. }
  246. #endregion
  247. protected void CompareLists(List<FormsyEntryData> list1, List<FormsyEntryData> list2)
  248. {
  249. // compare sizes
  250. Assert.AreEqual(list1.Count, list2.Count);
  251. // compare values
  252. for (int i = 0; i < list1.Count; i++)
  253. {
  254. Assert.AreEqual(list1[i].Id, list2[i].Id);
  255. Assert.AreEqual(list1[i].EntryId, list2[i].EntryId);
  256. Assert.AreEqual(list1[i].Name, list2[i].Name);
  257. Assert.AreEqual(list1[i].Value, list2[i].Value); // the values were changed
  258. }
  259. }
  260. private int GetFormId()
  261. {
  262. var db = new FormsyDataContext();
  263. int formId = (from entry in db.FormsyEntries
  264. where entry.Id == m_EntryId
  265. select entry.FormId).Single();
  266. return formId;
  267. }
  268. }
  269. }