PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/TestSpss/SpssVariableTest.cs

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