예제 #1
0
        //public static void CompareQuestionsFiles(string directory)
        public static void CompareJsonFiles(string directory, string jsonFile, IEnumerable <string> elementsToCompare = null, IEnumerable <string> documentReference = null)
        {
            string baseDirectory = XmlConfig.CurrentConfig.GetExplicit("CardDirectory");
            int    okCount       = 0;
            int    notOkCount    = 0;
            int    notFoundCount = 0;

            foreach (string questionDirectory in GetQuestionDirectories())
            {
                if (!questionDirectory.StartsWith(baseDirectory))
                {
                    throw new PBException($"wrong directory \"{directory}\"");
                }
                string questionDirectory2 = questionDirectory.Substring(baseDirectory.Length + 1);
                // questions_01.json questions.json
                string file1     = zPath.Combine(questionDirectory, @"data\" + jsonFile);
                string lastFile1 = QuestionsManager.GetLastFileNumber(file1, startIndex: 1);
                if (lastFile1 == file1)
                {
                    Trace.WriteLine($"question file not found \"{file1}\"");
                    notFoundCount++;
                }
                else
                {
                    // ".questions.json"
                    string file2 = zPath.Combine(directory, questionDirectory2 + "." + jsonFile);
                    Trace.WriteLine($"compare question files \"{lastFile1}\" \"{file2}\"");
                    CompareDocumentsResult result = DocumentComparator.CompareBsonDocumentFiles(lastFile1, file2,
                                                                                                comparatorOptions: DocumentComparatorOptions.ReturnNotEqualDocuments | DocumentComparatorOptions.ResultNotEqualElements,
                                                                                                //elementsToCompare: new string[] { "Year", "Type", "Number", "QuestionText", "Choices", "SourceLine" },
                                                                                                elementsToCompare: elementsToCompare,
                                                                                                //documentReference: new string[] { "document1.Year", "document1.Number", "document1.SourceFile", "document1.SourceLine", "document2.SourceFile", "document2.SourceLine" });
                                                                                                documentReference: documentReference);
                    //.zGetResults().zSave(@"c:\pib\dev_data\exe\runsource\valentin\test\S2\compare.json", jsonIndent: true);
                    if (result.Equal)
                    {
                        Trace.WriteLine("  ok");
                        okCount++;
                    }
                    else
                    {
                        Trace.WriteLine($"  not ok");
                        string resultFile = zPath.Combine(directory, zPath.GetFileNameWithoutExtension(file2) + ".comp.json");
                        //Trace.WriteLine($"  not ok, (\"{resultFile}\")");
                        result.GetResultDocuments().zSave(resultFile, jsonIndent: true);
                        notOkCount++;
                    }
                }
            }
            Trace.WriteLine($"{okCount} ok {notOkCount} not ok {notFoundCount} not found");
        }