Exemplo n.º 1
0
        public bool Compare(out string error, out string warning)
        {
            bool noIssues = true;

            error   = string.Empty;
            warning = string.Empty;
            int numErrors   = 0;
            int numWarnings = 0;

            foreach (var refChapter in referenceChapters)
            {
                string refName = refChapter.chapterName;

                CsvChapter testChapter = this.csvToTestChapters.Find(c => c.chapterName == refName);
                // check if new file has the same chapters
                if (testChapter != null)
                {
                    bool hasEqualNumberOfRows = testChapter.dbTable.Rows.Count == refChapter.dbTable.Rows.Count;
                    if (!hasEqualNumberOfRows)
                    {
                        error += string.Format("Number of rows not matching for table {0}: is {1} should be {2}\n",
                                               refName,
                                               testChapter.dbTable.Rows.Count,
                                               refChapter.dbTable.Rows.Count);
                        numErrors++;
                    }
                    foreach (DataColumn column in refChapter.dbTable.Columns)
                    {
                        // check for the same column name
                        if (-1 == testChapter.dbTable.Columns.IndexOf(column.ColumnName))
                        {
                            error += string.Format("Missing Column: {0} \n", column.ColumnName);
                            numErrors++;
                        }
                        else
                        {
                            // check for the same column datatypes
                            Type oldType = column.DataType;
                            Type newType = testChapter.dbTable.Columns[column.ColumnName].DataType;
                            if (oldType != newType)
                            {
                                error += string.Format("DataType of Column changed in table {0} from {1} to {2}\n", refName, oldType.ToString(), newType.ToString());
                                numErrors++;
                            }
                            else if (hasEqualNumberOfRows)
                            {
                                for (int idxRow = 0; idxRow < refChapter.dbTable.Rows.Count; idxRow++)
                                {
                                    var rowRef  = refChapter.dbTable.Rows[idxRow];
                                    var rowTest = testChapter.dbTable.Rows[idxRow];

                                    if (column.DataType == typeof(Double))
                                    {
                                        double valueOld = (double)rowRef[column.ColumnName];
                                        double valueNew = (double)rowTest[column.ColumnName];
                                        if (Math.Abs(valueOld - valueNew) > Epsilon)
                                        {
                                            error += string.Format("DataSet difference at (Capt:{0}|Col:{1}|line:{2}) from {3} to {4}\n",
                                                                   refName,
                                                                   column.ColumnName,
                                                                   idxRow,
                                                                   rowRef[column.ColumnName],
                                                                   rowTest[column.ColumnName]);
                                            numErrors++;
                                        }
                                    }
                                    else
                                    {
                                        if (!rowRef[column.ColumnName].Equals(rowTest[column.ColumnName]))
                                        {
                                            error += string.Format("DataSet difference at (Capt:{0}|Col:{1}|line:{2}) from {3} to {4}\n",
                                                                   refName,
                                                                   column.ColumnName,
                                                                   idxRow,
                                                                   rowRef[column.ColumnName],
                                                                   rowTest[column.ColumnName]);
                                            numErrors++;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (refChapter.columnNames.Count < testChapter.columnNames.Count)
                    {
                        warning += string.Format("Added column(s) to {0}\n", refName);
                        numWarnings++;
                    }
                }
                else
                {
                    error += string.Format("Missing Chapter: {0}\n", refName);
                    numErrors++;
                }
            }

            // check if new file has more chapters
            if (csvToTestChapters.Count > referenceChapters.Count)
            {
                warning += string.Format("Added Chapter(s)\n");
                numWarnings++;
            }
            if (numWarnings > 0)
            {
                warning += string.Format("WARNINGS:{0}", numWarnings);
            }
            if (numErrors > 0)
            {
                error += string.Format("ERRORS:{0}", numErrors);
            }
            noIssues = numErrors == 0;
            return(noIssues);
        }
Exemplo n.º 2
0
        public static List <CsvChapter> ReadCsvFile(string filename, out string error, bool debugOut = false, char seperatorCharacter = ',', char quotationCharacter = '\"')
        {
            error = string.Empty;
            List <CsvChapter> chapters = null;

            if (!File.Exists(filename))
            {
                error += string.Format("Error File not found: '{0}'", filename);
                return(null);
            }

            using (StreamReader reader = new StreamReader(filename))
            {
                chapters = new List <CsvChapter>();
                CsvChapter currentChapter       = null;
                bool       readColumnHeaderLine = false;
                bool       readColumnInfoLine   = false;
                bool       startNewChapter      = true;
                string     line;
                while ((line = reader.ReadLine()) != null)
                {
                    if (string.IsNullOrWhiteSpace(line))
                    {
                        startNewChapter = true;
                        continue;
                    }
                    string[] tokens = CsvChapter.Split(line, seperatorCharacter, quotationCharacter);
                    if (tokens.Length == 1 && startNewChapter)
                    {
                        currentChapter = new CsvChapter
                        {
                            chapterName = tokens[0],
                            filePath    = filename
                        };
                        chapters.Add(currentChapter);
                        readColumnHeaderLine = true;
                        startNewChapter      = false;
                    }
                    else if (readColumnHeaderLine)
                    {
                        currentChapter.columnNames.AddRange(tokens);
                        readColumnHeaderLine = false;
                        readColumnInfoLine   = true;
                    }
                    else if (readColumnInfoLine)
                    {
                        currentChapter.columnInfos.AddRange(tokens);
                        readColumnInfoLine = false;
                    }
                    else
                    {
                        List <string> row = new List <string>();
                        row.AddRange(tokens);
                        currentChapter.content.Add(row);
                    }
                }

                bool success = true;
                foreach (var chapter in chapters)
                {
                    if (debugOut)
                    {
                        Console.WriteLine("Processed chapter {0}", chapter.chapterName);
                    }
                    success &= chapter.InitChapter(out string initErrorMsg);
                    if (!success)
                    {
                        error += initErrorMsg;
                        if (debugOut)
                        {
                            Console.WriteLine("Error:");
                        }
                        if (debugOut)
                        {
                            Console.WriteLine(initErrorMsg);
                        }
                        if (debugOut)
                        {
                            Console.WriteLine("... failed");
                        }
                        continue;
                    }
                    success &= chapter.ProcessData(out string errorMsg);
                    if (!success)
                    {
                        error += errorMsg;
                        if (debugOut)
                        {
                            Console.WriteLine("Error:");
                        }
                        if (debugOut)
                        {
                            Console.WriteLine(errorMsg);
                        }
                        if (debugOut)
                        {
                            Console.WriteLine("... failed");
                        }
                    }
                    else
                    {
                        if (debugOut)
                        {
                            Console.WriteLine("... success", chapter.chapterName);
                        }
                    }
                }
            }
            return(chapters);
        }