public void File_Engine_Server_Project_Test()//Original function return lists tuple. { string path = @"c:\calisma\sievo\sievo.txt"; int projectID = 2; Sievo sievo = new Sievo() { ProjectID = 2, Description = "Harmonize Lactobacillus acidophilus sourcing", StartDate = DateTime.Parse("2014-01-01 00:00:00.005", CultureInfo.CreateSpecificCulture("en-US")), Category = "Dairy", Responsible = "Daisy Milks", SavingsAmount = null, Currency = null, Complexity = DataTypes.Concrete.Enums.Complexity.Simple }; List <Sievo> sievoList = new List <Sievo>(); sievoList.Add(sievo); List <string> columnOrder = new List <string>() { "Project", "Description", "Start date", "Category", "Responsible", "Savings amount", "Currency", "Complexity" }; Tuple <List <Sievo>, List <string> > expectedTuple = Tuple.Create(sievoList, columnOrder); Server.FileEngine fileEngine = new Server.FileEngine(); fileEngine.SetPath(path); Tuple <List <Sievo>, List <string> > actualTuple = fileEngine.Project(projectID); // Tests for the List item1 that holds the data row by row Assert.AreEqual(expectedTuple.Item1[0].Description, actualTuple.Item1[0].Description); Assert.AreEqual(expectedTuple.Item1[0].Category, actualTuple.Item1[0].Category); Assert.AreEqual(expectedTuple.Item1[0].Responsible, actualTuple.Item1[0].Responsible); Assert.AreEqual(expectedTuple.Item1[0].Currency, actualTuple.Item1[0].Currency); Assert.AreEqual(expectedTuple.Item1[0].ProjectID, actualTuple.Item1[0].ProjectID); Assert.AreEqual(expectedTuple.Item1[0].Complexity, actualTuple.Item1[0].Complexity); Assert.AreEqual(expectedTuple.Item1[0].SavingsAmount, actualTuple.Item1[0].SavingsAmount); Assert.AreEqual(expectedTuple.Item1[0].StartDate, actualTuple.Item1[0].StartDate); // Tests for the list item 2 that holds the order of the colums Assert.AreEqual(expectedTuple.Item2[0].ToString(), actualTuple.Item2[0].ToString()); Assert.AreEqual(expectedTuple.Item2[1].ToString(), actualTuple.Item2[1].ToString()); Assert.AreEqual(expectedTuple.Item2[2].ToString(), actualTuple.Item2[2].ToString()); Assert.AreEqual(expectedTuple.Item2[3].ToString(), actualTuple.Item2[3].ToString()); Assert.AreEqual(expectedTuple.Item2[4].ToString(), actualTuple.Item2[4].ToString()); Assert.AreEqual(expectedTuple.Item2[5].ToString(), actualTuple.Item2[5].ToString()); Assert.AreEqual(expectedTuple.Item2[6].ToString(), actualTuple.Item2[6].ToString()); Assert.AreEqual(expectedTuple.Item2[7].ToString(), actualTuple.Item2[7].ToString()); }
// This function is used for reading the file by type // which means the file is treated as a database entity public static Tuple <List <T>, List <string> > ReadFromFileByType(Type entity, string path) { IFormatProvider provider = CultureInfo.CreateSpecificCulture("en-US"); if (entity.Name == "Sievo") { List <Sievo> listSievo = new List <Sievo>(); int row = 0; try { foreach (string line in File.ReadLines(path)) { if (!line.StartsWith("/*") && !line.StartsWith("#")) { row++; string[] splitContent = line.Split("\t".ToCharArray()); if (row == 1) { columnOrder = splitContent; } else if (row > 1) { Sievo s = new Sievo(); for (int i = 0; i < splitContent.Length; i++) { if (columnOrder[i].ToString() == "Project") { s.ProjectID = Convert.ToInt16(splitContent[i]); } else if (columnOrder[i].ToString() == "Description") { s.Description = splitContent[i]; } else if (columnOrder[i].ToString() == "Start date") { DateTime value; if (DateTime.TryParse(splitContent[i], out value)) { s.StartDate = value; } else { throw new Exception("Start Date Format is invalid."); } } else if (columnOrder[i].ToString() == "Category") { s.Category = splitContent[i]; } else if (columnOrder[i].ToString() == "Responsible") { s.Responsible = splitContent[i]; } else if (columnOrder[i].ToString() == "Savings amount") { decimal result; if (splitContent[i] == "NULL") { s.SavingsAmount = null; } else { if (decimal.TryParse(splitContent[i], NumberStyles.AllowDecimalPoint, provider, out result)) { s.SavingsAmount = result; } else { throw new Exception("Savings Amount Format is invalid."); } } } else if (columnOrder[i].ToString() == "Currency") { s.Currency = splitContent[i] == "NULL" ? null : splitContent[i]; } else if (columnOrder[i].ToString() == "Complexity") { if (splitContent[i] == "Hazardous") { s.Complexity = Complexity.Hazardous; } else if (splitContent[i] == "Moderate") { s.Complexity = Complexity.Moderate; } else if (splitContent[i] == "Simple") { s.Complexity = Complexity.Simple; } else { throw new Exception("Given complexity value is not in the list"); } } } listSievo.Add(s); } } } return(Tuple.Create(listSievo.Cast <T>().ToList(), columnOrder.ToList())); } catch (Exception) { throw; } } else { return(null); } }