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

/TestSpss/SpssConvertTest.cs

#
C# | 155 lines | 121 code | 17 blank | 17 comment | 1 complexity | 0bb46e5da18deb763fa7381b98bb0e66 MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.Data;
  3. using System.CodeDom.Compiler;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. namespace Spss.Testing
  6. {
  7. /// <summary>
  8. ///This is a test class for Spss.SpssConvert and is intended
  9. ///to contain all Spss.SpssConvert Unit Tests
  10. ///</summary>
  11. [TestClass()]
  12. [DeploymentItem("x86", "x86")]
  13. [DeploymentItem("x64", "x64")]
  14. public class SpssConvertTest
  15. {
  16. private TestContext testContextInstance;
  17. /// <summary>
  18. ///Gets or sets the test context which provides
  19. ///information about and functionality for the current test run.
  20. ///</summary>
  21. public TestContext TestContext
  22. {
  23. get
  24. {
  25. return testContextInstance;
  26. }
  27. set
  28. {
  29. testContextInstance = value;
  30. }
  31. }
  32. /// <summary>
  33. ///Initialize() is called once during test execution before
  34. ///test methods in this test class are executed.
  35. ///</summary>
  36. [TestInitialize()]
  37. public void Initialize()
  38. {
  39. tblTest = new DataTable();
  40. DataColumn cID = tblTest.Columns.Add("ID", typeof(int));
  41. DataColumn cStr = tblTest.Columns.Add("str", typeof(string));
  42. DataColumn cDate = tblTest.Columns.Add("date", typeof(DateTime));
  43. DataColumn cFloat = tblTest.Columns.Add("float", typeof(float));
  44. const int cIDv = 3;
  45. const string cStrv = "hello";
  46. DateTime cDatev = DateTime.Now;
  47. const float cFloatv = 3.553F;
  48. object[] values = { cIDv, cStrv, cDatev, cFloatv };
  49. tblTest.Rows.Add(values);
  50. }
  51. /// <summary>
  52. ///Cleanup() is called once during test execution after
  53. ///test methods in this class have executed unless
  54. ///this test class' Initialize() method throws an exception.
  55. ///</summary>
  56. [TestCleanup()]
  57. public void Cleanup()
  58. {
  59. }
  60. DataTable tblTest;
  61. [TestMethod]
  62. [ExpectedException(typeof(ArgumentNullException))]
  63. public void toDdiNull()
  64. {
  65. SpssConvert.ToDdi((string)null);
  66. }
  67. [Ignore]
  68. [TestMethod]
  69. public void testT1()
  70. {
  71. SpssConvert.ToDdi(@"C:\Program Files\SPSS\T1.sav");
  72. }
  73. [Ignore]
  74. [TestMethod]
  75. public void testlistSurveys()
  76. {
  77. SpssConvert.ToDdi(@"C:\Program Files\SPSS\listsurveys.sav");
  78. }
  79. [TestMethod]
  80. public void smoking()
  81. {
  82. SpssConvert.ToDdi(@"C:\Program Files\SPSS\smoking.sav");
  83. }
  84. [TestMethod]
  85. public void anorectic()
  86. {
  87. SpssConvert.ToDdi(@"C:\Program Files\SPSS\anorectic.sav");
  88. }
  89. [TestMethod]
  90. public void MetaData()
  91. {
  92. string SAVfilename;
  93. using (System.CodeDom.Compiler.TempFileCollection tfc = new System.CodeDom.Compiler.TempFileCollection())
  94. {
  95. SAVfilename = tfc.AddExtension("sav", true);
  96. SpssConvert.ToFile(tblTest, tblTest.Select(), SAVfilename, FillInMetaData);
  97. Console.WriteLine("The file with metadata is stored at: " + SAVfilename);
  98. }
  99. }
  100. protected void FillInMetaData(SpssVariable var)
  101. {
  102. switch (var.Name)
  103. {
  104. case "ID":
  105. var numericVar = (SpssNumericVariable)var;
  106. var.Label = "Some ID label";
  107. numericVar.ValueLabels[1] = "one";
  108. numericVar.ValueLabels[2] = "two";
  109. numericVar.ValueLabels[3] = "three";
  110. numericVar.ValueLabels[4] = "four";
  111. break;
  112. case "str":
  113. var.Label = "some str label";
  114. break;
  115. case "date":
  116. var.Label = "some date label";
  117. break;
  118. case "float":
  119. var.Label = "some float label";
  120. break;
  121. }
  122. }
  123. [TestMethod]
  124. public void ToFile()
  125. {
  126. DataTable dt = new DataTable();
  127. dt.Columns.Add("var1", typeof(int));
  128. dt.Columns.Add("var2", typeof(string));
  129. dt.Rows.Add(new object[] { 1, "hi" });
  130. using (TempFileCollection tfc = new TempFileCollection())
  131. {
  132. tfc.KeepFiles = false;
  133. string filename = tfc.AddExtension("sav");
  134. SpssConvert.ToFile(dt, dt.Select(), filename, null);
  135. }
  136. }
  137. }
  138. }