PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/TestSpss/SpssConvertTest.cs

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