PageRenderTime 37ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/TestSpss/SpssVariableTest.cs

#
C# | 217 lines | 203 code | 10 blank | 4 comment | 1 complexity | e033ee909b4d07aa37441a991c273732 MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.Linq;
  3. using System.Diagnostics;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. namespace Spss.Testing
  6. {
  7. /// <summary>
  8. /// Summary description for SpssVariableTest
  9. /// </summary>
  10. [TestClass]
  11. [DeploymentItem("x86", "x86")]
  12. [DeploymentItem("x64", "x64")]
  13. public class SpssVariableTest
  14. {
  15. public SpssVariableTest()
  16. {
  17. }
  18. [TestMethod]
  19. public void NewStringVariable()
  20. {
  21. SpssStringVariable var = new SpssStringVariable();
  22. var.Name = "new string";
  23. }
  24. [TestMethod]
  25. public void NewNumericVariable()
  26. {
  27. SpssNumericVariable var = new SpssNumericVariable();
  28. var.Name = "new numeric";
  29. }
  30. [TestMethod]
  31. public void NewDateVariable()
  32. {
  33. SpssDateVariable var = new SpssDateVariable();
  34. var.Name = "new date";
  35. }
  36. [TestMethod]
  37. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  38. public void VarNameTooLong()
  39. {
  40. SpssVariable var = new SpssStringVariable();
  41. var.Name = new string('a', SpssSafeWrapper.SPSS_MAX_VARNAME + 1);
  42. }
  43. [TestMethod]
  44. [ExpectedException(typeof(ArgumentNullException))]
  45. public void VarNameToNull()
  46. {
  47. SpssVariable var = new SpssStringVariable();
  48. var.Name = null;
  49. }
  50. [TestMethod]
  51. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  52. public void VarLabelTooLong()
  53. {
  54. SpssVariable var = new SpssStringVariable();
  55. var.Label = new string('a', SpssSafeWrapper.SPSS_MAX_VARLABEL + 1);
  56. }
  57. [TestMethod]
  58. public void VarLabelNeverNull()
  59. {
  60. SpssVariable var = new SpssStringVariable();
  61. Assert.IsNotNull(var.Label);
  62. var.Label = null;
  63. Assert.IsNotNull(var.Label);
  64. Assert.AreEqual(string.Empty, var.Label);
  65. }
  66. [TestMethod]
  67. public void SetStringValueLabels()
  68. {
  69. SpssStringVariable var = new SpssStringVariable();
  70. var.ValueLabels.Add("h", "hello");
  71. var.ValueLabels.Add("H", "hi");
  72. var.ValueLabels.Add("b", "bye");
  73. Assert.AreEqual("hello", var.ValueLabels["h"]);
  74. Assert.AreEqual("hi", var.ValueLabels["H"]);
  75. Assert.AreEqual(3, var.ValueLabels.Count);
  76. }
  77. [TestMethod]
  78. public void GetLongStringValueLabels()
  79. {
  80. using (SpssDataDocument docRead = SpssDataDocument.Open(TestBase.GoodFilename, SpssFileAccess.Read))
  81. {
  82. SpssStringVariable var = (SpssStringVariable)docRead.Variables["longStr"];
  83. // long strings can never have value labels
  84. Assert.AreEqual(0, var.ValueLabels.Count);
  85. }
  86. }
  87. [TestMethod]
  88. public void SetNumericValueLabels()
  89. {
  90. SpssNumericVariable var = new SpssNumericVariable();
  91. var.ValueLabels.Add(1, "Never");
  92. var.ValueLabels[2] = "Rarely";
  93. var.ValueLabels.Add(3, "Sometimes");
  94. var.ValueLabels.Add(4, "Often");
  95. var.ValueLabels.Add(5, "Very Often");
  96. Assert.AreEqual(5, var.ValueLabels.Count);
  97. Assert.AreEqual("Sometimes", var.ValueLabels[3]);
  98. Assert.AreEqual("Rarely", var.ValueLabels[2]);
  99. var.ValueLabels.Remove(4);
  100. Assert.IsFalse(var.ValueLabels.ContainsKey(4));
  101. Assert.AreEqual(4, var.ValueLabels.Count);
  102. }
  103. [TestMethod]
  104. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  105. public void SetStringTooLong()
  106. {
  107. using (SpssDataDocument docAppend = SpssDataDocument.Open(TestBase.AppendFilename, SpssFileAccess.Append))
  108. {
  109. SpssStringVariable var = (SpssStringVariable)docAppend.Variables["charLabels"];
  110. Debug.Assert(var.Length == 8);
  111. SpssCase row = docAppend.Cases.New();
  112. row["charLabels"] = new string('a', var.Length + 1);
  113. }
  114. }
  115. [TestMethod]
  116. public void SetMissingValueNumeric()
  117. {
  118. int rowIndex;
  119. using (SpssDataDocument docAppend = SpssDataDocument.Open(TestBase.AppendFilename, SpssFileAccess.Append))
  120. {
  121. SpssCase row = docAppend.Cases.New();
  122. rowIndex = row.Position;
  123. row["num"] = double.NaN;
  124. row.Commit();
  125. }
  126. using (SpssDataDocument docAppend = SpssDataDocument.Open(TestBase.AppendFilename, SpssFileAccess.Read))
  127. {
  128. SpssCase row = docAppend.Cases[rowIndex];
  129. double val = (double)row["num"];
  130. Assert.AreEqual(double.NaN, val);
  131. }
  132. }
  133. [TestMethod]
  134. public void SetMissingValueNumericByNull()
  135. {
  136. int rowIndex;
  137. using (SpssDataDocument docAppend = SpssDataDocument.Open(TestBase.AppendFilename, SpssFileAccess.Append))
  138. {
  139. SpssCase row = docAppend.Cases.New();
  140. rowIndex = row.Position;
  141. row["num"] = null;
  142. row.Commit();
  143. }
  144. using (SpssDataDocument docAppend = SpssDataDocument.Open(TestBase.AppendFilename, SpssFileAccess.Read))
  145. {
  146. SpssCase row = docAppend.Cases[rowIndex];
  147. double? val = (double?)row["num"];
  148. Assert.IsFalse(val.HasValue);
  149. }
  150. }
  151. [TestMethod]
  152. public void SetMissingValueDateByNull()
  153. {
  154. int rowIndex;
  155. using (SpssDataDocument docAppend = SpssDataDocument.Open(TestBase.AppendFilename, SpssFileAccess.Append))
  156. {
  157. SpssCase row = docAppend.Cases.New();
  158. rowIndex = row.Position;
  159. row["dateVar"] = null;
  160. row.Commit();
  161. }
  162. using (SpssDataDocument docAppend = SpssDataDocument.Open(TestBase.AppendFilename, SpssFileAccess.Read))
  163. {
  164. SpssCase row = docAppend.Cases[rowIndex];
  165. DateTime? val = (DateTime?)row["dateVar"];
  166. Assert.IsFalse(val.HasValue);
  167. }
  168. }
  169. [TestMethod]
  170. public void ReadDecimalPlaces()
  171. {
  172. using (SpssDataDocument docRead = SpssDataDocument.Open(TestBase.GoodFilename, SpssFileAccess.Read))
  173. {
  174. SpssNumericVariable var = docRead.Variables[0] as SpssNumericVariable;
  175. Assert.IsNotNull(var, "First variable expected to be numeric.");
  176. Assert.AreEqual(2, var.WriteDecimal);
  177. }
  178. }
  179. [TestMethod]
  180. public void ReadLabel()
  181. {
  182. using (SpssDataDocument docRead = SpssDataDocument.Open(TestBase.GoodFilename, SpssFileAccess.Read))
  183. {
  184. SpssVariable var = docRead.Variables[0];
  185. Assert.AreEqual("on butter", var.Label);
  186. }
  187. }
  188. [TestMethod]
  189. public void ReadNullLabel()
  190. {
  191. using (SpssDataDocument docRead = SpssDataDocument.Open(TestBase.GoodFilename, SpssFileAccess.Read))
  192. {
  193. SpssVariable var = docRead.Variables[3];
  194. Assert.AreEqual(string.Empty, var.Label);
  195. }
  196. }
  197. [TestMethod]
  198. public void ReadNullValueLabels()
  199. {
  200. using (SpssDataDocument docRead = SpssDataDocument.Open(TestBase.GoodFilename, SpssFileAccess.Read))
  201. {
  202. SpssVariable var = docRead.Variables[3];
  203. Assert.IsFalse(var.GetValueLabels().Any());
  204. }
  205. }
  206. }
  207. }