Exemplo n.º 1
0
        public void TestWholeReport()
        {
            CallbackCalled = 0;

            // CSV file to write the report to
            string ReportPath = AppDomain.CurrentDomain.BaseDirectory + "\\TestReport.csv";

            // Remove the CSV file if it exists
            if (File.Exists(ReportPath))
            {
                File.Delete(ReportPath);
            }

            // Set up an anonymous callback function to receive status
            Report.Status StatusCallback = delegate(int Number, int Total, string Setnb, string Name)
            {
                Assert.AreEqual(Number, 1);
                Assert.AreEqual(Total, 1);
                Assert.AreEqual(Setnb, "Alice");
                Assert.AreEqual(Name, "Alice Anderson");
                CallbackCalled++;
            };


            // Open the file to write the report to
            using (StreamWriter Writer = File.CreateText(ReportPath))
            {
                // Set up an anonymous callback function to write the rows to the database
                bool             HeaderWritten     = false;
                Report.WriteRows WriteRowsCallback = delegate(DataTable RowsToWrite)
                {
                    if (!HeaderWritten)
                    {
                        Report.WriteHeader(RowsToWrite, Writer);
                        HeaderWritten = true;
                    }
                    Report.WriteCSV(RowsToWrite, Writer);
                };



                // Write the CSV file
                Report report = new Report(DB, "social_unit_test_firstdegree", "social_unit_test_seconddegree", null, null);
                report.Generate(StatusCallback, WriteRowsCallback, null);
                Assert.AreEqual(CallbackCalled, 1);
            }

            // Compare it with the test report file
            string TestDataPath = AppDomain.CurrentDomain.BaseDirectory + "\\Test Data\\Full report.csv";

            CompareReport(ReportPath, TestDataPath);


            // Remove the CSV file
            if (File.Exists(ReportPath))
            {
                File.Delete(ReportPath);
            }
        }
Exemplo n.º 2
0
        public void TestReportWithFrankAndBob()
        {
            CallbackCalled = 0;

            // CSV file to write the report to
            string ReportPath = AppDomain.CurrentDomain.BaseDirectory + "\\FrankAndBobReport.csv";

            // Remove the CSV file if it exists
            if (File.Exists(ReportPath))
            {
                File.Delete(ReportPath);
            }

            // Set up an anonymous callback function to receive status
            Report.Status StatusCallback = delegate(int Number, int Total, string Setnb, string Name)
            {
                if (CallbackCalled == 0)
                {
                    Assert.AreEqual(Setnb, "Bob", "Setnb");
                    Assert.AreEqual(Name, "Bob Bingo", "Name");
                }
                else
                {
                    Assert.AreEqual(Setnb, "Frank", "Setnb");
                    Assert.AreEqual(Name, "Frank Flat", "Name");
                }
                CallbackCalled++;
            };

            // Open the file to write the report to
            using (StreamWriter Writer = File.CreateText(ReportPath))
            {
                // Set up an anonymous callback function to write the rows to the database
                bool             HeaderWritten     = false;
                Report.WriteRows WriteRowsCallback = delegate(DataTable RowsToWrite)
                {
                    if (!HeaderWritten)
                    {
                        Report.WriteHeader(RowsToWrite, Writer);
                        HeaderWritten = true;
                    }
                    Report.WriteCSV(RowsToWrite, Writer);
                };

                // Write the CSV file -- use incorrect case to make sure the match is case-insensitive
                Report report = new Report(DB, "social_unit_test_firstdegree", "social_unit_test_seconddegree", new List <string> {
                    "frank", "BOB"
                }, null);
                report.Generate(StatusCallback, WriteRowsCallback, null);
                Assert.AreEqual(CallbackCalled, 2, "CallbackCalled");
            }

            // Compare it with the test report file
            string TestDataPath = AppDomain.CurrentDomain.BaseDirectory + "\\Test Data\\Report for Frank and Bob.csv";

            TestReport.CompareReport(ReportPath, TestDataPath);

            // Remove the CSV file
            if (File.Exists(ReportPath))
            {
                File.Delete(ReportPath);
            }
        }
Exemplo n.º 3
0
        public void TestReportWithAliceJimmyFrankAndBob()
        {
            for (int iteration = 0; iteration < 2; iteration++)
            {
                CallbackCalled = 0;

                // CSV file to write the report to
                string ReportPath = AppDomain.CurrentDomain.BaseDirectory + "\\EveryoneReport.csv";

                // Remove the CSV file if it exists
                if (File.Exists(ReportPath))
                {
                    File.Delete(ReportPath);
                }

                // Set up an anonymous callback function to receive status
                Report.Status StatusCallback = delegate(int Number, int Total, string Setnb, string Name)
                {
                    switch (CallbackCalled)
                    {
                    case 0:
                        Assert.AreEqual(Setnb, "Alice", "Setnb");
                        Assert.AreEqual(Name, "Alice Anderson", "Name");
                        break;

                    case 1:
                        if (iteration == 0)
                        {
                            Assert.AreEqual(Setnb, "Bob", "Setnb");
                            Assert.AreEqual(Name, "Bob Bingo", "Name");
                        }
                        else
                        {
                            Assert.AreEqual(Setnb, "Jimmy", "Setnb");
                            Assert.AreEqual(Name, "Jimmy Johnson", "Name");
                        }
                        break;

                    case 2:
                        Assert.AreEqual(iteration, 0, "Callback should not be called twice for second iteration");
                        Assert.AreEqual(Setnb, "Frank", "Setnb");
                        Assert.AreEqual(Name, "Frank Flat", "Name");
                        break;

                    case 3:
                        Assert.AreEqual(iteration, 0, "Callback should not be called three times for second iteration");
                        Assert.AreEqual(Setnb, "Jimmy", "Setnb");
                        Assert.AreEqual(Name, "Jimmy Johnson", "Name");
                        break;

                    default:
                        Assert.Fail("CallbackCalled should only be 0, 1, 2 or 3");
                        break;
                    }
                    CallbackCalled++;
                };

                // Open the file to write the report to
                using (StreamWriter Writer = File.CreateText(ReportPath))
                {
                    // Set up an anonymous callback function to write the rows to the database
                    bool             HeaderWritten     = false;
                    Report.WriteRows WriteRowsCallback = delegate(DataTable RowsToWrite)
                    {
                        if (!HeaderWritten)
                        {
                            Report.WriteHeader(RowsToWrite, Writer);
                            HeaderWritten = true;
                        }
                        Report.WriteCSV(RowsToWrite, Writer);
                    };


                    // This will be one twice. The first time we'll do it for everyone, the second time we'll exclude
                    // Bob and Frank to test the ColleaguesToExclude parameter.
                    List <string> ColleaguesToExclude;
                    if (iteration == 0)
                    {
                        ColleaguesToExclude = new List <string>();
                    }
                    else
                    {
                        ColleaguesToExclude = new List <string>()
                        {
                            "FRANK", "Bob"
                        }
                    };

                    // Write the CSV file -- including extra items in the list of colleages to include
                    Report report = new Report(DB, "social_unit_test_firstdegree", "social_unit_test_seconddegree", new List <string> {
                        "Blah blah blah", "alice", "frank", "BOB", "jimmy", "bozo", "boffo"
                    }, ColleaguesToExclude);
                    report.Generate(StatusCallback, WriteRowsCallback, null);
                    if (iteration == 0)
                    {
                        Assert.AreEqual(CallbackCalled, 4, "CallbackCalled");
                    }
                    else
                    {
                        Assert.AreEqual(CallbackCalled, 2, "CallbackCalled");
                    }
                }

                // Compare it with the test report file -- use the full report for the first iteration (where
                // ColleaguesToExclude is null) and a report without Bob and Frank for the second one (where
                // ColleaguesToExclude contains Bob and Frank)
                string TestDataPath = AppDomain.CurrentDomain.BaseDirectory;
                if (iteration == 0)
                {
                    TestDataPath += "\\Test Data\\Report for Jimmy, Bob, Frank and Alice.csv";
                }
                else
                {
                    TestDataPath += "\\Test Data\\Report for Jimmy and Alice.csv";
                }

                TestReport.CompareReport(ReportPath, TestDataPath);


                // Remove the CSV file
                if (File.Exists(ReportPath))
                {
                    File.Delete(ReportPath);
                }
            }
        }
    }
Exemplo n.º 4
0
        public void TestGenerate()
        {
            CallbackCalled = 0;
            // Set up an anonymous callback function to receive status
            Report.Status StatusCallback = delegate(int Number, int Total, string Setnb, string Name)
            {
                Assert.AreEqual(Number, 1);
                Assert.AreEqual(Total, 1);
                Assert.AreEqual(Setnb, "Alice");
                Assert.AreEqual(Name, "Alice Anderson");
                CallbackCalled++;
            };


            // Create the report
            Report report = new Report(DB, "social_unit_test_firstdegree", "social_unit_test_seconddegree", null, null);

            DataTable Results = null;

            Report.WriteRows WriteRowsCallback = delegate(DataTable RowsToWrite)
            {
                Results = RowsToWrite;
            };

            report.Generate(StatusCallback, WriteRowsCallback, null);
            Assert.AreEqual(CallbackCalled, 1);
            Assert.IsNotNull(Results);

            // Expected rows for Alice/Carol/Lisa
            int[][] ExpectedAliceCarolLisa =
            {
                new int[] { 1991, 1, 1, 0, 0, 0, 0 },
                new int[] { 1992, 0, 1, 0, 0, 0, 0 },
                new int[] { 1993, 1, 2, 0, 0, 0, 0 },
                new int[] { 1994, 5, 7, 0, 0, 0, 0 },
                new int[] { 1995, 1, 8, 0, 0, 0, 0 },
                new int[] { 1996, 0, 8, 1, 1, 0, 0 },
                new int[] { 1997, 0, 8, 1, 2, 0, 0 },
                new int[] { 1998, 0, 8, 0, 2, 0, 0 },
                new int[] { 1999, 0, 8, 3, 5, 0, 0 },
            };

            VerifyColumnNames(Results);

            // Expected rows for Alice/Justin/John
            int[][] ExpectedAliceJustinJohn =
            {
                new int[] { 1994, 2, 2, 0, 0, 0, 0 },
                new int[] { 1995, 1, 3, 0, 0, 0, 0 },
                new int[] { 1996, 0, 3, 1, 1, 0, 0 },
                new int[] { 1997, 0, 3, 0, 1, 0, 0 },
                new int[] { 1998, 0, 3, 0, 1, 0, 0 },
                new int[] { 1999, 0, 3, 1, 2, 0, 0 },
                new int[] { 2000, 0, 3, 0, 2, 0, 0 },
                new int[] { 2001, 0, 3, 0, 2, 0, 0 },
                new int[] { 2002, 0, 3, 0, 2, 1, 1 },
                new int[] { 2003, 1, 4, 1, 3, 1, 2 },
                new int[] { 2004, 0, 4, 0, 3, 0, 2 },
                new int[] { 2005, 0, 4, 2, 5, 0, 2 },
            };

            // Extract the rows from the results for Alice/Carol/Lisa
            DataTable AliceCarolLisa = null;

            for (int i = 0; i < Results.Rows.Count; i++)
            {
                if (Results.Rows[i][0].ToString() == "Alice" &&
                    Results.Rows[i][1].ToString() == "Carol" &&
                    Results.Rows[i][2].ToString() == "Lisa")
                {
                    // Pull out the results
                    AliceCarolLisa = Results.Clone();
                    for (int j = 0; j < ExpectedAliceCarolLisa.Length; j++)
                    {
                        AliceCarolLisa.Rows.Add(Results.Rows[i + j].ItemArray);
                    }
                    i += ExpectedAliceCarolLisa.Length;
                }
            }
            CheckResultRows(AliceCarolLisa, ExpectedAliceCarolLisa, "Alice", "Carol", "Lisa", 1, ExpectedAliceCarolLisa.Length);



            // Extract the rows from the results for Alice/Justin/John
            DataTable AliceJustinJohn = null;

            for (int i = 0; i < Results.Rows.Count; i++)
            {
                if (Results.Rows[i][0].ToString() == "Alice" &&
                    Results.Rows[i][1].ToString() == "Justin" &&
                    Results.Rows[i][2].ToString() == "John")
                {
                    // Pull out the results
                    AliceJustinJohn = Results.Clone();
                    for (int j = 0; j < ExpectedAliceJustinJohn.Length; j++)
                    {
                        AliceJustinJohn.Rows.Add(Results.Rows[i + j].ItemArray);
                    }
                    i += ExpectedAliceJustinJohn.Length;
                }
            }
            CheckResultRows(AliceJustinJohn, ExpectedAliceJustinJohn, "Alice", "Justin", "John", 1, ExpectedAliceJustinJohn.Length);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Write the report to the specified file
        /// </summary>
        private void WriteReport_Click(object sender, EventArgs e)
        {
            this.Cursor  = Cursors.WaitCursor;
            this.Enabled = false;

            // Read the second-degree stars to exclude
            List <string> ColleaguesToInclude = null;

            if (IncludeFilePath.Text != "")
            {
                if (!File.Exists(IncludeFilePath.Text))
                {
                    MessageBox.Show("The list of second-degree stars to include does not exist.", "File not found");
                }
                else
                {
                    ColleaguesToInclude = new List <string>(File.ReadAllLines(IncludeFilePath.Text));
                }
            }

            try
            {
                // Set up an anonymous callback function to receive status
                Report.Status StatusCallback = delegate(int Number, int Total, string Setnb, string Name)
                {
                    statusStrip1.Items[1].Text = Name + " (" + Setnb + ")";
                    statusStrip1.Items[2].Text = Number.ToString() + " of " + Total.ToString();
                    Application.DoEvents();
                };

                // Set up an anonymous callback to receive detailed progress
                Report.DetailedProgress DetailedProgressCallback = delegate(int FirstDegree, int TotalFirstDegree,
                                                                            int SecondDegree, int TotalSecondDegree) {
                    ToolStripProgressBar progress = statusStrip2.Items[0] as ToolStripProgressBar;
                    progress.Minimum = 1;
                    progress.Maximum = TotalFirstDegree * 10;
                    // Use the second-degree stars to give smooth progress bar increments
                    progress.Value = Math.Min(TotalFirstDegree * 10,
                                              FirstDegree * 10 + (int)(10 * ((float)SecondDegree / (float)TotalSecondDegree)));
                    statusStrip2.Items[1].Text = FirstDegree + " of " + TotalFirstDegree + " 1st degree colleagues";
                    statusStrip2.Items[2].Text = SecondDegree + " of " + TotalSecondDegree + " 2nd degree colleagues";
                    Application.DoEvents();
                };


                // Check if the selected file exists. If it does, the user can either choose to continue processing the
                // report or overwrite it and restart processing.
                bool ContinueReport = false;
                if (File.Exists(ReportPath.Text))
                {
                    DialogResult Result = MessageBox.Show("The report file exists. Continue from where the previous report left off?\n(Click 'No' to overwrite the report and restart it from the beginning.)",
                                                          "Continue existing report file?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                    switch (Result)
                    {
                    case DialogResult.Cancel:
                        this.Enabled = true;
                        this.Cursor  = Cursors.Default;
                        return;

                    case DialogResult.No:
                        File.Delete(ReportPath.Text);
                        ContinueReport = false;
                        break;

                    case DialogResult.Yes:
                        File.Delete(ReportPath.Text + ".bak");
                        File.Move(ReportPath.Text, ReportPath.Text + ".bak");
                        ContinueReport = true;
                        break;
                    }
                }

                // Open the file to write the report to
                using (StreamWriter Writer = File.CreateText(ReportPath.Text))
                {
                    bool HeaderWritten = false;

                    // If continuing the previous report, copy the rows and create the list of stars to exclude
                    List <string> ColleaguesToExclude = new List <string>();
                    if (ContinueReport)
                    {
                        string[] OldReport = File.ReadAllLines(ReportPath.Text + ".bak");

                        // Copy the header
                        Writer.WriteLine(OldReport[0]);
                        HeaderWritten = true;

                        // Find the next block of rows. Write them to the file -- as long as they're not the
                        // last block.
                        int    CopiedLines = 1;
                        int    CurrentLine = 1;
                        string Setnb       = OldReport[CopiedLines].Substring(0, OldReport[CopiedLines].IndexOf(','));

                        // Loop through each block of setnbs until we run out of lines
                        while (CurrentLine < OldReport.Length - 1)
                        {
                            while (CurrentLine < OldReport.Length - 1 && OldReport[CurrentLine].StartsWith(Setnb + ","))
                            {
                                CurrentLine++;
                            }

                            if (CurrentLine < OldReport.Length - 1)
                            {
                                Setnb = OldReport[CopiedLines].Substring(0, OldReport[CopiedLines].IndexOf(','));
                                ColleaguesToExclude.Add(Setnb);
                                while (CopiedLines < CurrentLine)
                                {
                                    Writer.WriteLine(OldReport[CopiedLines]);
                                    CopiedLines++;
                                }
                            }
                        }
                    }

                    // Set up an anonymous callback function to write the rows to the database
                    Report.WriteRows WriteRowsCallback = delegate(DataTable RowsToWrite)
                    {
                        if (!HeaderWritten)
                        {
                            Report.WriteHeader(RowsToWrite, Writer);
                            HeaderWritten = true;
                        }
                        Report.WriteCSV(RowsToWrite, Writer);
                    };

                    Report report = new Report(new Database(DSN.Text), FirstDegreeDB.Text, SecondDegreeDB.Text, ColleaguesToInclude, null);
                    report.Generate(StatusCallback, WriteRowsCallback, DetailedProgressCallback);
                    MessageBox.Show("Wrote the report file '" + ReportPath.Text + "'", "Social networking report written");
                    statusStrip1.Items[1].Text = "Finished";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "An error occurred while writing the report");
            }
            this.Enabled = true;
            this.Cursor  = Cursors.Default;
        }