PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/CTraderEmployeesTests/EmployeeTests.cs

https://github.com/techlunacy/Employees
C# | 328 lines | 276 code | 32 blank | 20 comment | 2 complexity | 0c45153ecd0d60ea2dbdc976ed8a598c MD5 | raw file
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Web.Mvc;
  7. using CTraderEmployees.Controllers;
  8. using CTraderEmployees.Models;
  9. using Microsoft.VisualStudio.TestTools.UnitTesting;
  10. namespace CTraderEmployeesTests
  11. {
  12. /// <summary>
  13. /// Summary description for UnitTest1
  14. /// </summary>
  15. [TestClass]
  16. public class EmployeeTests
  17. {
  18. public EmployeeTests()
  19. {
  20. //
  21. // TODO: Add constructor logic here
  22. //
  23. }
  24. private TestContext testContextInstance;
  25. /// <summary>
  26. ///Gets or sets the test context which provides
  27. ///information about and functionality for the current test run.
  28. ///</summary>
  29. public TestContext TestContext
  30. {
  31. get
  32. {
  33. return testContextInstance;
  34. }
  35. set
  36. {
  37. testContextInstance = value;
  38. }
  39. }
  40. #region Additional test attributes
  41. //
  42. // You can use the following additional attributes as you write your tests:
  43. //
  44. // Use ClassInitialize to run code before running the first test in the class
  45. // [ClassInitialize()]
  46. // public static void MyClassInitialize(TestContext testContext) { }
  47. //
  48. // Use ClassCleanup to run code after all tests in a class have run
  49. // [ClassCleanup()]
  50. // public static void MyClassCleanup() { }
  51. #endregion
  52. private EmployeeModel _currentEmployeeModel;
  53. private EmployeeModel _secondEmployeeModel;
  54. private Guid _identifier;
  55. private DataStore _dataStore;
  56. private const string InvalidRecord = "21d9fb81-bdf8-4c4e-b397-01018f30e90b|john|smith|Male|True";
  57. protected const string ExpectedFormattedRecordSetFirstRow = "21d9fb81-bdf8-4c4e-b397-01018f30e90b|john|smith|Male|5|True";
  58. protected const string ExpectedFormattedRecordSetSecondRow = "31d9fb81-bdf8-4c4e-b397-01018f30e90b|pete|John|Male|1|False";
  59. private const string Delimiter = "|";
  60. private string _path = "test.data";
  61. EmployeeController _employeeController;
  62. [TestInitialize]
  63. public void CreateEmployee()
  64. {
  65. _identifier = Guid.Parse("21d9fb81-bdf8-4c4e-b397-01018f30e90b");
  66. _currentEmployeeModel = new EmployeeModel { Id = _identifier, Age = 5, FirstName = "john", LastName = "smith", IsCurrentEmployee = true };
  67. var secondEmployeeId = Guid.Parse("31d9fb81-bdf8-4c4e-b397-01018f30e90b");
  68. _secondEmployeeModel = new EmployeeModel { Id = secondEmployeeId, Age = 1, FirstName = "pete", LastName = "John", Gender = EmployeeGender.Male, IsCurrentEmployee = false };
  69. _dataStore = new DataStore();
  70. _dataStore.CreateDataStore("test.data");
  71. _employeeController = new EmployeeController(_path);
  72. }
  73. [TestMethod]
  74. public void EmployeeExists()
  75. {
  76. Assert.IsNotNull(_currentEmployeeModel);
  77. }
  78. [TestMethod]
  79. public void EmployeeHasFields()
  80. {
  81. Assert.AreEqual(_identifier, _currentEmployeeModel.Id);
  82. Assert.AreEqual("john", _currentEmployeeModel.FirstName);
  83. Assert.AreEqual("smith", _currentEmployeeModel.LastName);
  84. Assert.AreEqual(5, _currentEmployeeModel.Age);
  85. Assert.IsTrue(_currentEmployeeModel.IsCurrentEmployee);
  86. Assert.AreEqual(EmployeeGender.Male, _currentEmployeeModel.Gender);
  87. }
  88. [TestMethod]
  89. public void SaveEmployee()
  90. {
  91. _dataStore.SaveRecord(_currentEmployeeModel);
  92. var records = File.ReadLines(_dataStore.Path);
  93. Assert.AreEqual(ExpectedFormattedRecordSetFirstRow, records.First());
  94. }
  95. [TestMethod]
  96. public void RecordExists()
  97. {
  98. _dataStore.SaveRecord(_currentEmployeeModel);
  99. Assert.IsTrue(_dataStore.IdExists(_currentEmployeeModel.Id));
  100. }
  101. [TestMethod]
  102. public void RemoveRecord()
  103. {
  104. _dataStore.SaveRecord(_currentEmployeeModel);
  105. Assert.IsTrue(_dataStore.IdExists(_currentEmployeeModel.Id));
  106. _dataStore.RemoveRecordById(_currentEmployeeModel.Id);
  107. Assert.IsFalse(_dataStore.IdExists(_currentEmployeeModel.Id));
  108. }
  109. [TestMethod]
  110. public void UpdateEmployee()
  111. {
  112. _dataStore.SaveRecord(_currentEmployeeModel);
  113. _currentEmployeeModel.FirstName = "James";
  114. _dataStore.SaveRecord(_currentEmployeeModel);
  115. var records = _dataStore.ReadRecords();
  116. Assert.AreNotEqual(ExpectedFormattedRecordSetFirstRow, records.ElementAt(0));
  117. }
  118. [TestMethod]
  119. public void HaveMultipleRecords()
  120. {
  121. _dataStore.SaveRecord(_currentEmployeeModel);
  122. _dataStore.SaveRecord(_secondEmployeeModel);
  123. Assert.IsTrue(_dataStore.IdExists(_currentEmployeeModel.Id));
  124. Assert.IsTrue(_dataStore.IdExists(_secondEmployeeModel.Id));
  125. }
  126. [TestMethod]
  127. public void ReturnEmployee()
  128. {
  129. _dataStore.SaveRecord(_currentEmployeeModel);
  130. var employeeModel = _dataStore.GetRecordById(_currentEmployeeModel.Id);
  131. Assert.AreEqual(employeeModel.Id, _currentEmployeeModel.Id);
  132. Assert.AreEqual(employeeModel.FirstName, _currentEmployeeModel.FirstName);
  133. Assert.AreEqual(employeeModel.LastName, _currentEmployeeModel.LastName);
  134. Assert.AreEqual(employeeModel.Age, _currentEmployeeModel.Age);
  135. Assert.AreEqual(employeeModel.IsCurrentEmployee, _currentEmployeeModel.IsCurrentEmployee);
  136. Assert.AreEqual(employeeModel.Gender, _currentEmployeeModel.Gender);
  137. }
  138. [TestMethod]
  139. public void ParseEmployee()
  140. {
  141. var employeeModel = EmployeeModel.Parse(Delimiter, _currentEmployeeModel.FormatRecord(Delimiter));
  142. Assert.AreEqual(employeeModel.Id, _currentEmployeeModel.Id);
  143. Assert.AreEqual(employeeModel.FirstName, _currentEmployeeModel.FirstName);
  144. Assert.AreEqual(employeeModel.LastName, _currentEmployeeModel.LastName);
  145. Assert.AreEqual(employeeModel.Age, _currentEmployeeModel.Age);
  146. Assert.AreEqual(employeeModel.IsCurrentEmployee, _currentEmployeeModel.IsCurrentEmployee);
  147. Assert.AreEqual(employeeModel.Gender, _currentEmployeeModel.Gender);
  148. }
  149. [TestMethod]
  150. public void GetAllEmployees()
  151. {
  152. _dataStore.SaveRecord(_currentEmployeeModel);
  153. _dataStore.SaveRecord(_secondEmployeeModel);
  154. var employeeModels = _dataStore.GetAllRecords();
  155. Assert.AreEqual(2, employeeModels.Count);
  156. }
  157. [TestMethod]
  158. public void TerminateEmployee()
  159. {
  160. _dataStore.SaveRecord(_currentEmployeeModel);
  161. var action = _employeeController.Terminate(_currentEmployeeModel.Id);
  162. Assert.IsNotNull(action);
  163. var localModel = _dataStore.GetRecordById(_currentEmployeeModel.Id);
  164. Assert.IsFalse(localModel.IsCurrentEmployee);
  165. }
  166. [TestMethod]
  167. public void EmployeeCreate()
  168. {
  169. var action = _employeeController.Create(_currentEmployeeModel);
  170. Assert.IsNotNull(action);
  171. var route = action as RedirectToRouteResult;
  172. var localModel = new EmployeeModel();
  173. if (route != null)
  174. {
  175. localModel = _dataStore.GetRecordById(Guid.Parse(route.RouteValues["id"].ToString()));
  176. }
  177. Assert.AreEqual(localModel.Id, _currentEmployeeModel.Id);
  178. Assert.AreEqual(localModel.FirstName, _currentEmployeeModel.FirstName);
  179. Assert.AreEqual(localModel.LastName, _currentEmployeeModel.LastName);
  180. Assert.AreEqual(localModel.Age, _currentEmployeeModel.Age);
  181. Assert.AreEqual(localModel.IsCurrentEmployee, _currentEmployeeModel.IsCurrentEmployee);
  182. Assert.AreEqual(localModel.Gender, _currentEmployeeModel.Gender);
  183. }
  184. [TestMethod]
  185. public void EmployeeCreateWithInvalidAge()
  186. {
  187. _currentEmployeeModel.Age = -1;
  188. var action = _employeeController.Create(_currentEmployeeModel);
  189. Assert.IsNotNull(action);
  190. var actionResult = _employeeController.CallWithModelValidation(c => c.Create(_currentEmployeeModel), _currentEmployeeModel);
  191. Assert.IsFalse(_employeeController.ModelState.IsValid);
  192. Assert.IsFalse(_employeeController.ModelState.IsValidField("Age"));
  193. Assert.IsTrue(_employeeController.ModelState.IsValidField("FirstName"));
  194. Assert.IsTrue(_employeeController.ModelState.IsValidField("LastName"));
  195. }
  196. [TestMethod]
  197. public void EmployeeEditWithInvalidAge()
  198. {
  199. _dataStore.SaveRecord(_currentEmployeeModel);
  200. _currentEmployeeModel.Age = -1;
  201. var actionResult = _employeeController.CallWithModelValidation(c => _employeeController.Edit(_currentEmployeeModel.Id, _currentEmployeeModel), _currentEmployeeModel);
  202. Assert.IsFalse(_employeeController.ModelState.IsValid);
  203. Assert.IsFalse(_employeeController.ModelState.IsValidField("Age"));
  204. Assert.IsTrue(_employeeController.ModelState.IsValidField("FirstName"));
  205. Assert.IsTrue(_employeeController.ModelState.IsValidField("LastName"));
  206. var localModel = _dataStore.GetRecordById(_currentEmployeeModel.Id);
  207. Assert.AreEqual(5, localModel.Age);
  208. }
  209. [TestMethod]
  210. public void EmployeeEditWithNoFirstName()
  211. {
  212. _dataStore.SaveRecord(_currentEmployeeModel);
  213. _currentEmployeeModel.FirstName = string.Empty;
  214. var actionResult = _employeeController.CallWithModelValidation(c => _employeeController.Edit(_currentEmployeeModel.Id, _currentEmployeeModel), _currentEmployeeModel);
  215. Assert.IsFalse(_employeeController.ModelState.IsValid);
  216. Assert.IsTrue(_employeeController.ModelState.IsValidField("Age"));
  217. Assert.IsFalse(_employeeController.ModelState.IsValidField("FirstName"));
  218. Assert.IsTrue(_employeeController.ModelState.IsValidField("LastName"));
  219. var localModel = _dataStore.GetRecordById(_currentEmployeeModel.Id);
  220. Assert.AreEqual("john", localModel.FirstName);
  221. }
  222. [TestMethod]
  223. public void EmployeeCreateWithNoFirstName()
  224. {
  225. _currentEmployeeModel.FirstName = string.Empty;
  226. var actionResult = _employeeController.CallWithModelValidation(c => _employeeController.Create(_currentEmployeeModel), _currentEmployeeModel);
  227. Assert.IsFalse(_employeeController.ModelState.IsValid);
  228. Assert.IsTrue(_employeeController.ModelState.IsValidField("Age"));
  229. Assert.IsFalse(_employeeController.ModelState.IsValidField("FirstName"));
  230. Assert.IsTrue(_employeeController.ModelState.IsValidField("LastName"));
  231. var idExists = _dataStore.IdExists(_currentEmployeeModel.Id);
  232. Assert.IsFalse(idExists);
  233. }
  234. [TestMethod]
  235. public void EmployeeDetails()
  236. {
  237. _dataStore.SaveRecord(_currentEmployeeModel);
  238. var action = _employeeController.Details(_currentEmployeeModel.Id) as ViewResult;
  239. Assert.IsNotNull(action);
  240. var localModel = (EmployeeModel)action.ViewData.Model;
  241. Assert.AreEqual(localModel.Id, _currentEmployeeModel.Id);
  242. Assert.AreEqual(localModel.FirstName, _currentEmployeeModel.FirstName);
  243. Assert.AreEqual(localModel.LastName, _currentEmployeeModel.LastName);
  244. Assert.AreEqual(localModel.Age, _currentEmployeeModel.Age);
  245. Assert.AreEqual(localModel.IsCurrentEmployee, _currentEmployeeModel.IsCurrentEmployee);
  246. Assert.AreEqual(localModel.Gender, _currentEmployeeModel.Gender);
  247. }
  248. [TestMethod]
  249. public void AllEmployeeList()
  250. {
  251. _dataStore.SaveRecord(_currentEmployeeModel);
  252. _dataStore.SaveRecord(_secondEmployeeModel);
  253. var action = _employeeController.Index(EmploymentStatusFilter.All) as ViewResult;
  254. Assert.IsNotNull(action);
  255. var localModel = (List<EmployeeModel>)action.ViewData.Model;
  256. Assert.AreEqual(2, localModel.Count);
  257. }
  258. [TestMethod]
  259. public void NonEmployeeList()
  260. {
  261. _dataStore.SaveRecord(_currentEmployeeModel);
  262. _dataStore.SaveRecord(_secondEmployeeModel);
  263. var action = _employeeController.Index(EmploymentStatusFilter.No) as ViewResult;
  264. Assert.IsNotNull(action);
  265. var localModel = (List<EmployeeModel>)action.ViewData.Model;
  266. Assert.AreEqual(1, localModel.Count);
  267. }
  268. [TestMethod]
  269. public void CurrentEmployeeList()
  270. {
  271. _dataStore.SaveRecord(_currentEmployeeModel);
  272. _dataStore.SaveRecord(_secondEmployeeModel);
  273. var action = _employeeController.Index(EmploymentStatusFilter.No) as ViewResult;
  274. Assert.IsNotNull(action);
  275. var localModel = (List<EmployeeModel>)action.ViewData.Model;
  276. Assert.AreEqual(1, localModel.Count);
  277. }
  278. [TestMethod]
  279. public void EmployeeHasId()
  280. {
  281. var employee = new EmployeeModel();
  282. Assert.IsNotNull(employee.Id);
  283. }
  284. [TestMethod]
  285. [ExpectedException(typeof(InvalidDataException))]
  286. public void ErrorParsing()
  287. {
  288. EmployeeModel.Parse(Delimiter, InvalidRecord);
  289. }
  290. [TestCleanup]
  291. public void RemoveDatastore()
  292. {
  293. _dataStore.RemoveDataStore();
  294. }
  295. }
  296. }