public void TestMeSHHeadingReport() { // Set up the database with test publications (and don't forget to add the // publication types!) DB = new Database("Publication Harvester Unit Test"); Harvester harvester = new Harvester(DB); harvester.CreateTables(); PublicationTypes PubTypes = new PublicationTypes( AppDomain.CurrentDomain.BaseDirectory + "\\Unit Tests\\TestPublicationTypes", "PublicationTypes.csv" ); PubTypes.WriteToDB(DB); reports = new Reports(DB, AppDomain.CurrentDomain.BaseDirectory + "\\Unit Tests\\TestReports\\pubmed_jifs.xls"); Assert.IsTrue(reports.Weights.Count == 10); TestHarvester.GetPublicationsFromInput1XLS_Using_MockNCBI(false, new string[] { "eng" }, 22); // Write the MeSH Heading report StreamWriter writer = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\MeSHHeadingReport.csv"); Reports.ReportStatus StatusCallback = delegate(int number, int total, Person person, bool ProgressBarOnly) { // }; Reports.ReportMessage MessageCallback = delegate(string Message) { // }; reports.MeSHHeadingReport(writer, StatusCallback, MessageCallback); writer.Close(); // Verify that the MeSH headings were written properly // Read the rows back from the file string ConnectionString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + AppDomain.CurrentDomain.BaseDirectory + ";"; OdbcConnection Connection = new OdbcConnection(ConnectionString); OdbcDataAdapter DataAdapter = new OdbcDataAdapter ("SELECT * FROM [MeSHHeadingReport.csv]", Connection); DataTable Results = new DataTable(); int Rows = DataAdapter.Fill(Results); Connection.Close(); int numChecked = 0; // Check a few selected results foreach (DataRow Row in Results.Rows) { string Setnb = Row[0].ToString(); int Year = Convert.ToInt32(Row[1]); string Heading = Row[2].ToString(); int Count = Convert.ToInt32(Row[3]); switch (Setnb) { case "A6009400": // Van Eys if ((Year == 1998) && (Heading == "Humans")) { Assert.IsTrue(Count == 2); numChecked++; } if ((Year == 1998) && (Heading == "Child")) { Assert.IsTrue(Count == 1); numChecked++; } if ((Year == 2001) && (Heading == "Humans")) { Assert.IsTrue(Count == 1); numChecked++; } break; case "A5702471": // Guillemin if ((Year == 2005) && (Heading == "Hypothalamic Hormones/*physiology")) { Assert.IsTrue(Count == 1); numChecked++; } break; } } Assert.IsTrue(numChecked == 4); File.Delete(AppDomain.CurrentDomain.BaseDirectory + "\\MeSHHeadingReport.csv"); }
public void TestSkipSetnbs() { // Set up the database with test publications (and don't forget to add the // publication types!) DB = new Database("Publication Harvester Unit Test"); Harvester harvester = new Harvester(DB); harvester.CreateTables(); PublicationTypes PubTypes = new PublicationTypes( AppDomain.CurrentDomain.BaseDirectory + "\\Unit Tests\\TestPublicationTypes", "PublicationTypes.csv" ); PubTypes.WriteToDB(DB); reports = new Reports(DB, AppDomain.CurrentDomain.BaseDirectory + "\\Unit Tests\\TestReports\\pubmed_jifs.xls"); Assert.IsTrue(reports.Weights.Count == 10); TestHarvester.GetPublicationsFromInput1XLS_Using_MockNCBI(false, new string[] { "eng" }, 22); people = new People(DB); Reports.ReportStatus StatusCallback = delegate(int number, int total, Person person, bool ProgressBarOnly) { // }; Reports.ReportMessage MessageCallback = delegate(string Message) { // }; // Set up the Setnbs to skip ArrayList SetnbsToSkip = new ArrayList(); SetnbsToSkip.Add("A5401532"); SetnbsToSkip.Add("A6009400"); // Verify that the people report skips the setnbs StreamWriter writer = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\TestSkipSetnbs.csv"); reports.PeopleReport(SetnbsToSkip, writer, StatusCallback, MessageCallback); writer.Close(); StreamReader reader = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "\\TestSkipSetnbs.csv"); string Line = reader.ReadLine(); // skip the header row while ((Line = reader.ReadLine()) != null) { Assert.IsFalse(Line.StartsWith("A5401532")); Assert.IsFalse(Line.StartsWith("A6009400")); } reader.Close(); // Verify that the people report skips the setnbs writer = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\TestSkipSetnbs.csv", false); reports.PubsReport(SetnbsToSkip, writer, StatusCallback, MessageCallback); writer.Close(); reader = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "\\TestSkipSetnbs.csv"); Line = reader.ReadLine(); // skip the header row while ((Line = reader.ReadLine()) != null) { Assert.IsFalse(Line.StartsWith("A5401532")); Assert.IsFalse(Line.StartsWith("A6009400")); } reader.Close(); File.Delete(AppDomain.CurrentDomain.BaseDirectory + "\\TestSkipSetnbs.csv"); }
/// <summary> /// Write the People or Publications report /// </summary> private void PeopleOrPublicationsReport(WhichReport whichReport, bool ContinuePreviousReport, Reports reports) { ArrayList SetnbsToSkip = null; string Filename = Folder.Text + "\\"; if (whichReport == WhichReport.People) { Filename += People.Text; } else { Filename += Publications.Text; } // If we're continuing the previous report, rename it to a backup filename, // then copy every person in it to the file to append -- except for the // last person, which we'll restart. if (ContinuePreviousReport && File.Exists(Filename)) { try { SetnbsToSkip = Reports.BackupReportAndGetSetnbs(Filename); } catch (Exception ex) { AddLogEntry("An error occurred while creating the report '" + Filename + "': " + ex.Message); return; } } // Make an anonymous callback function that keeps track of the callback data Reports.ReportStatus StatusCallback = delegate(int number, int total, Person person, bool StatusBarOnly) { toolStripProgressBar1.Minimum = 0; toolStripProgressBar1.Maximum = total; toolStripProgressBar1.Value = number; if (!StatusBarOnly) { AddLogEntry("Writing " + person.Last + " " + person.Setnb + " (" + number.ToString() + " of " + total.ToString() + ")"); } Application.DoEvents(); }; // Make an anonymous callback function to receive message Reports.ReportMessage MessageCallback = delegate(string Message) { AddLogEntry(Message); Application.DoEvents(); }; // Write the Stars report StreamWriter writer; try { // Open the writer to append the file only if continuing from a previous report writer = new StreamWriter(Filename, ContinuePreviousReport); } catch (Exception ex) { AddLogEntry("An error occurred while creating the report '" + Filename + "': " + ex.Message); return; } try { if (whichReport == WhichReport.People) { reports.PeopleReport(SetnbsToSkip, writer, StatusCallback, MessageCallback); } else { reports.PubsReport(SetnbsToSkip, writer, StatusCallback, MessageCallback); } } catch (Exception ex) { AddLogEntry("An error occurred while creating the report '" + Filename + "': " + ex.Message); } finally { writer.Close(); } }
public void TestEntireReport() { string Setnb = ""; // Set up the database with test publications (and don't forget to add the // publication types!) DB = new Database("Publication Harvester Unit Test"); Harvester harvester = new Harvester(DB); harvester.CreateTables(); PublicationTypes PubTypes = new PublicationTypes( AppDomain.CurrentDomain.BaseDirectory + "\\Unit Tests\\TestPublicationTypes", "PublicationTypes.csv" ); PubTypes.WriteToDB(DB); reports = new Reports(DB, AppDomain.CurrentDomain.BaseDirectory + "\\Unit Tests\\TestReports\\pubmed_jifs.xls"); Assert.IsTrue(reports.Weights.Count == 10); TestHarvester.GetPublicationsFromInput1XLS_Using_MockNCBI(false, new string[] { "eng" }, 22); people = new People(DB); // Write the report StreamWriter writer = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\TestEntireReport.csv"); Reports.ReportStatus StatusCallback = delegate(int number, int total, Person person, bool ProgressBarOnly) { // }; Reports.ReportMessage MessageCallback = delegate(string Message) { // }; reports.PeopleReport(null, writer, StatusCallback, MessageCallback); writer.Close(); // Read the report into an array var lines = File.ReadAllLines($"{AppDomain.CurrentDomain.BaseDirectory}\\TestEntireReport.csv") .Select(line => line.Split(new char[] { ',' })); var header = lines.First(); var data = lines.Skip(1).ToList(); Assert.AreEqual(85, data.Count); string ReportData(string setnb, string year, string column) { var index = Array.IndexOf(header, column); var row = data.Where(line => line[0] == setnb && line[1] == year); Assert.AreEqual(1, row.Count(), $"Unable to find setnb={setnb} year={year} in TestEntireReport.csv"); return(row.First()[index]); } var q = ReportData("A5401532", "1997", "wghtd_pubcount_pos1"); // Read the report file that was generated by hand (TestEntireReport_Data.xls) // and check each value against the report that was generated by Reports() string[] Columns = { "setnb", "year", "pubcount", "wghtd_pubcount", "pubcount_pos1", "wghtd_pubcount_pos1", "pubcount_posN", "wghtd_pubcount_posN", "pubcount_posM", "wghtd_pubcount_posM", "pubcount_posNTL", "wghtd_pubcount_posNTL", "pubcount_pos2", "wghtd_pubcount_pos2", "123pubcount", "wghtd_123pubcount", "123pubcount_pos1", "wghtd_123pubcount_pos1", "123pubcount_posN", "wghtd_123pubcount_posN", "123pubcount_posM", "wghtd_123pubcount_posM", "123pubcount_posNTL", "wghtd_123pubcount_posNTL", "123pubcount_pos2", "wghtd_123pubcount_pos2", "1pubcount", "wghtd_1pubcount", "1pubcount_pos1", "wghtd_1pubcount_pos1", "1pubcount_posN", "wghtd_1pubcount_posN", "1pubcount_posM", "wghtd_1pubcount_posM", "1pubcount_posNTL", "wghtd_1pubcount_posNTL", "1pubcount_pos2", "wghtd_1pubcount_pos2", "2pubcount", "wghtd_2pubcount", "2pubcount_pos1", "wghtd_2pubcount_pos1", "2pubcount_posN", "wghtd_2pubcount_posN", "2pubcount_posM", "wghtd_2pubcount_posM", "2pubcount_posNTL", "wghtd_2pubcount_posNTL", "2pubcount_pos2", "wghtd_2pubcount_pos2", "3pubcount", "wghtd_3pubcount", "3pubcount_pos1", "wghtd_3pubcount_pos1", "3pubcount_posN", //"wghtd_3pubcount_posN", "3pubcount_posM", "wghtd_3pubcount_posM", "3pubcount_posNTL", "wghtd_3pubcount_posNTL", "3pubcount_pos2", "wghtd_3pubcount_pos2", "4pubcount", "wghtd_4pubcount", "4pubcount_pos1", "wghtd_4pubcount_pos1", "4pubcount_posN", "wghtd_4pubcount_posN", "4pubcount_posM", "wghtd_4pubcount_posM", "4pubcount_posNTL", "wghtd_4pubcount_posNTL", "4pubcount_pos2", "wghtd_4pubcount_pos2" }; DataTable HandGeneratedData = NpoiHelper.ReadExcelFileToDataTable(AppDomain.CurrentDomain.BaseDirectory + "\\Unit Tests\\TestReports", "TestEntireReport_Data.xls"); Assert.AreEqual(HandGeneratedData.Rows.Count, 85); var valuesChecked = 0; for (int RowNum = 0; RowNum < HandGeneratedData.Rows.Count; RowNum++) { // Find the rows in the hand-generated data and the report DataRow HandGeneratedRow = HandGeneratedData.Rows[RowNum]; Setnb = HandGeneratedRow[0].ToString(); int Year = Convert.ToInt32(HandGeneratedRow[1]); for (int i = 2; i < Columns.Length; i++) { valuesChecked++; String columnName = Columns[i]; var actualValue = ReportData(Setnb, Year.ToString(), columnName); string expectedValue = HandGeneratedRow[columnName].ToString(); Assert.AreEqual(expectedValue, actualValue, Setnb + "/" + Year.ToString() + "/" + columnName + " -- hand generated has " + expectedValue + ", report has" + actualValue); } } Assert.AreEqual((HandGeneratedData.Rows.Count) * (Columns.Length - 2), valuesChecked); // Use BackupReportAndGetSetnbs to back up the report -- check that it // returns the correct list of Setnbs and removes the last one from // the file. The last Setnb should still be in Setnb. ArrayList Setnbs = Reports.BackupReportAndGetSetnbs(AppDomain.CurrentDomain.BaseDirectory + "\\TestEntireReport.csv"); // Read the backup file that was created, make sure that the last setnb in the // file isn't contained and the others are StreamReader reader = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "\\TestEntireReport.csv.bak"); string Line = reader.ReadLine(); //skip the header row while ((Line = reader.ReadLine()) != null) // Find the last setnb in the original file { Setnb = Line.Substring(0, 8); } string RemovedSetnb = Setnb; Assert.IsFalse(Setnbs.Contains(RemovedSetnb)); reader.Close(); // Verify that the new file contains only the other setnbs Assert.IsTrue(Setnbs.Count == 3); Assert.IsFalse(Setnbs.Contains(RemovedSetnb)); reader = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "\\TestEntireReport.csv"); Line = reader.ReadLine(); //skip the header row while ((Line = reader.ReadLine()) != null) { Setnb = Line.Substring(0, 8); Assert.IsTrue(Setnbs.Contains(Setnb)); Assert.IsFalse(Setnb == RemovedSetnb); } reader.Close(); // Delete the temporary files File.Delete(AppDomain.CurrentDomain.BaseDirectory + "\\TestEntireReport.csv"); File.Delete(AppDomain.CurrentDomain.BaseDirectory + "\\TestEntireReport.csv.bak"); }
/// <summary> /// Generate the reports /// </summary> private void GenerateReports_Click(object sender, EventArgs e) { // Make an anonymous callback function that keeps track of the callback data // and one that reports messages Reports.ReportStatus StatusCallback = delegate(int number, int total, Person person, bool StatusBarOnly) { toolStripProgressBar1.Minimum = 0; toolStripProgressBar1.Maximum = total; toolStripProgressBar1.Value = number; if (!StatusBarOnly) { AddLogEntry("Writing " + person.Last + " " + person.Setnb + " (" + number.ToString() + " of " + total.ToString() + ")"); } Application.DoEvents(); }; Reports.ReportMessage MessageCallback = delegate(string Message) { AddLogEntry(Message); Application.DoEvents(); }; if ((JournalWeights.Text == "") || (!File.Exists(JournalWeights.Text))) { MessageBox.Show("Please specify a valid Journal Weights file.", "Unable to Generate Reports", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } AddLogEntry("Generating reports"); this.Enabled = false; Reports reports = new Reports(DB, JournalWeights.Text); StreamWriter writer = null; int NumReports = 0; if (DoPeople.Checked) { NumReports++; toolStripStatusLabel1.Text = "People"; // Set the people report sections based on the listbox string[] ReportSections = new string[peopleReportSections.Items.Count]; for (int i = 0; i < peopleReportSections.Items.Count; i++) { ReportSections[i] = peopleReportSections.Items[i].ToString(); } reports.PeopleReportSections = ReportSections; PeopleOrPublicationsReport(WhichReport.People, ContinuePeople.Checked, reports); } if (DoPublications.Checked) { NumReports++; toolStripStatusLabel1.Text = "Publications"; PeopleOrPublicationsReport(WhichReport.Publications, ContinuePeople.Checked, reports); } if (DoMeSHHeadings.Checked) { writer = null; try { NumReports++; toolStripStatusLabel1.Text = "MeSHHeadings"; AddLogEntry("Writing MeSH Headings report"); toolStripProgressBar1.Value = 0; writer = new StreamWriter(Folder.Text + "\\" + MeSHHeadings.Text, false); reports.MeSHHeadingReport(writer, StatusCallback, MessageCallback); } catch (Exception ex) { AddLogEntry("An error occurred while writing the MeSH Headings report: " + ex.Message); } finally { if (writer != null) { writer.Close(); } } } if (DoGrantIDs.Checked) { writer = null; try { NumReports++; toolStripStatusLabel1.Text = "Grants"; AddLogEntry("Writing Grants report"); toolStripProgressBar1.Value = 0; writer = new StreamWriter(Folder.Text + "\\" + GrantIDs.Text, false); reports.GrantsReport(writer); } catch (Exception ex) { AddLogEntry("An error occurred while writing the Grants report: " + ex.Message); } finally { if (writer != null) { writer.Close(); } } } toolStripStatusLabel1.Text = "Done"; AddLogEntry(NumReports.ToString() + " reports written"); // Refresh the radio buttons this.Enabled = true; CheckForExistingFiles(); }