private void LoadData(string filePath) { lblDataCount.Text = "Count: "; lblErrorCount.Text = "Count: "; listBox1.Items.Clear(); if (System.IO.File.Exists(filePath)) { DbCsvPlusError oError = new DbCsvPlusError(); DbCsvPlusRules oRules = null; string sampleRoot = Application.StartupPath + @"\UnitTests\Files\"; if (!tbFileName.Text.StartsWith(sampleRoot)) { oRules = new DbCsvPlusRules(true, true, false, "", ""); tbDataMsg.Text = "New file data, no rules loaded -see code."; } else { // This is from our sample set, so lets load some special rules oRules = new DbCsvPlusRules(true, true, false, "Date,Description,Amount", "Account Number,Currency"); oRules.ForceDataTypes = "System.DateTime, System.String, System.Double"; oRules.DataTypeFormats = "yyyy/MM/dd" + "," + "^[C][H][Q][#](?<P1>[0-9]{5})[-](?<P2>[0-9]{10})$" + "," + // this will create 3 capture groups (all-index0, P1-index1, P2-index2) it also forces a specific number length ""; tbDataMsg.Text = "Rules loaded for default test cases."; } DataTable oDt = DbCsvPlus.LoadDataTable(tbFileName.Text, null, true, false, false, ',', oRules, oError); gridData.DataSource = oDt; gridError.DataSource = oError.ErrorDataTable; if (oDt != null) { lblDataCount.Text = "Count: " + oDt.Rows.Count; } if (oError.ErrorDataTable != null) { lblErrorCount.Text = "Count: " + oError.ErrorDataTable.Rows.Count; } if (oError.ErrorFlagSet != null) { foreach (string key in oError.ErrorFlagSet.Keys) { listBox1.Items.Add(key); } } } }
public void CsvSaveTest(string testName, string inputSubPath, string outputTableName, DbCsvPlusRules oRules, DbCsvPlusError oError) { //inputSubPath = inputSubPath.Replace("/", @"\"); string binFolderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string fullPath = binFolderPath + @"/" + inputSubPath; // No Rules or Error --- loading data without any rule or error objects .... // we will assume clean data for the sake of testing the save DataTable oDt = DbCsvPlus.LoadDataTable(fullPath, null, true, false, false, ',', oRules, oError); if (oDt == null) { Assert.Fail("Data file failed to load."); } else { string saveRoot = Path.GetDirectoryName(fullPath); bool isOK = DbCsvPlus.SaveDataTable(oDt, saveRoot, outputTableName, true, false, false, false, null, "csv"); Assert.IsTrue(isOK, "Save operation threw an error."); // Load saved data string saveFullPath = saveRoot + "\\" + outputTableName + ".csv"; DataTable oDtSave = DbCsvPlus.LoadDataTable(saveFullPath, null, true, false, false, ',', null, null); // compare row and column counts Assert.AreEqual(oDt.Rows.Count, oDtSave.Rows.Count, "Row counts do not match."); Assert.AreEqual(oDt.Columns.Count, oDtSave.Columns.Count, "Column counts do not match."); // compare column details for (int ix = 0; ix < oDt.Columns.Count; ix++) { Assert.AreEqual(oDt.Columns[ix].ColumnName, oDtSave.Columns[ix].ColumnName, "Column name mis-match."); Assert.AreEqual(oDt.Columns[ix].DataType, oDtSave.Columns[ix].DataType, "Column type mis-match."); } // compare row data for (int ix = 0; ix < oDt.Rows.Count; ix++) { DataRow oRowOriginal = oDt.Rows[ix]; DataRow oRowSave = oDt.Rows[ix]; foreach (DataColumn oCol in oDt.Columns) { Assert.AreEqual(oRowOriginal[oCol.ColumnName], oRowSave[oCol.ColumnName], "Data value mis-match."); } } } }
public void CsvLoadTest(string testName, DbCsvPlusRules oRules, DbCsvPlusError oError, string fileSubPath, int rowCount, int discardCount, string flagSet) { //fileSubPath = fileSubPath.Replace("/", @"\"); string binFolderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string fullPath = binFolderPath + @"/" + fileSubPath; //DbCsvPlusError oError = new DbCsvPlusError(System.IO.Path.GetFileName(fullPath)); DataTable oDt = DbCsvPlus.LoadDataTable(fullPath, null, true, false, false, ',', oRules, oError); if (oDt == null) { Assert.Fail("Data file failed to load."); } else { if (oError == null) { Assert.AreEqual(rowCount, oDt.Rows.Count, "Unexpected row count."); } else { // Make sure we match on ALL the flags if (oError.ErrorFlagSet.Count == 0 && flagSet == "") { Assert.AreEqual(rowCount, oDt.Rows.Count, "Unexpected row count."); if (oError.ErrorDataTable != null) { Assert.AreEqual(discardCount, oError.ErrorDataTable.Rows.Count, "Unexpected discard count."); } } else { bool isOK = true; string[] aFlagSet = flagSet.Split(','); foreach (string key in oError.ErrorFlagSet.Keys) { if (!aFlagSet.Contains(key)) { isOK = false; break; } } if (isOK) { foreach (string key in aFlagSet) { if (!oError.ErrorFlagSet.ContainsKey(key)) { isOK = false; break; } } } string errorsActual = string.Join(",", oError.ErrorFlagSet.Keys); Assert.IsTrue(isOK, "Unexpected error flag output. Expected:" + flagSet + " Actual:" + errorsActual); Assert.AreEqual(rowCount, oDt.Rows.Count, "Unexpected row count."); if (oError.ErrorDataTable != null) { Assert.AreEqual(discardCount, oError.ErrorDataTable.Rows.Count, "Unexpected discard count."); } } } } }