Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7082517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:03:50+00:00 2026-05-28T07:03:50+00:00

I’m novice in unit testing field, so I have some questions: Is it a

  • 0

I’m novice in unit testing field, so I have some questions:

Is it a good practice to make method that returns a DataTable? If yes, how could I test it then?

This is a function that they ask me to test:

public static DataTable GeneratePeriods(DateTime startDate, DateTime endDate, string periodicity)
{
    try
    {
        using (DataTable periods = new DataTable())
        {
            periods.Columns.Add(new DataColumn("Dates", typeof(DateTime)));
            int startYear = startDate.Year;
            int endYear = endDate.Year;
            int Diff = endYear - startYear + 1;
            int LoopYear;
            /* This generates all regular periods from the begin date to the end date */
            switch (periodicity)
            {
                //monthly
                case "Mensuelle":
                    for (int j = 0; j < Diff; j++)
                    {
                        LoopYear = startYear + j;
                        for (int i = 1; i <= 12; i++)
                            if (i != 12)
                                periods.Rows.Add(new DateTime(LoopYear, i + 1, 1).AddDays(-1));
                            else
                                periods.Rows.Add(new DateTime(LoopYear, 12, 31));
                    }
                    break;
                //quarterly
                case "Trimestrielle":
                    for (int j = 0; j < Diff; j++)
                    {
                        LoopYear = startYear + j;
                        for (int i = 1; i <= 4; i++)
                            if (i != 4)
                                periods.Rows.Add(new DateTime(LoopYear, (i * 3) + 1, 1).AddDays(-1));
                            else
                                periods.Rows.Add(new DateTime(LoopYear, 12, 31));
                    }
                    break;
                //biannual
                case "Semestrielle":
                    for (int j = 0; j < Diff; j++)
                    {
                        LoopYear = startYear + j;
                        for (int i = 1; i <= 2; i++)
                            if (i != 2)
                                periods.Rows.Add(new DateTime(LoopYear, (i * 6) + 1, 1).AddDays(-1));
                            else
                                periods.Rows.Add(new DateTime(LoopYear, 12, 31));
                    }
                    break;
                //annual
                case "Annuelle":
                    for (int j = 0; j < Diff; j++)
                    {
                        LoopYear = startYear + j;
                        for (int i = 1; i <= 1; i++)
                            periods.Rows.Add(new DateTime(LoopYear, 12, 31));
                    }
                    break;
            }
            //this adds startDate in periods datatable if it doesn't exist
            if (periods.Select(String.Format("Dates = '{0}'", startDate)).Length == 0)
                periods.Rows.Add(startDate);
            //this adds endDate date in periods datatable if it doesn't exist
            if (periods.Select(String.Format("Dates = '{0}'", endDate.AddDays(-1))).Length == 0)
                periods.Rows.Add(endDate);
            //this removes all date ranges below the startDate
            DataRow[] dr = periods.Select(String.Format("Dates < '{0}'", startDate));
            foreach (DataRow row in dr)
                periods.Rows.Remove(row);
            //this removes all date ranges above the endDate
            DataRow[] dr1 = periods.Select(String.Format("Dates >'{0}'", endDate.AddDays(-1)));
            foreach (DataRow row in dr1)
                periods.Rows.Remove(row);
            //this adds endDate date in periods datatable if it doesn't exist for the second time ! (I personnaly don't know why it's duplicated but it dosen't work without this =) )
            if (periods.Select(String.Format("Dates = '{0}'", endDate.AddDays(-1))).Length == 0)
                periods.Rows.Add(endDate);
            DataView dv = new DataView(periods) { Sort = "Dates ASC" };
            // this initialize a new datatable with sorted dates
            DataTable dt_dates = dv.ToTable();
            // this initialize a new datatable 
            DataTable dt_periods = new DataTable();
            dt_periods.Columns.Add("Periods", typeof(string));
            dt_periods.Columns.Add("NombreJours", typeof(int));
            // this loop creates period ranges shown to the user (Du startDate au endDate)
            DateTime dateDebutPeriode;
            DateTime dateFinPeriode;
            int NombreJours;
            for (int i = 0; i < dv.Table.Rows.Count - 1; i++)
                if (i == 0)
                {
                    dateDebutPeriode = DateTime.Parse(dt_dates.Rows[i]["Dates"].ToString());
                    dateFinPeriode = DateTime.Parse(dt_dates.Rows[i + 1]["Dates"].ToString());
                    NombreJours = dateFinPeriode.Subtract(dateDebutPeriode).Days + 1;
                    dt_periods.Rows.Add(String.Format("Du {0} au {1}", dateDebutPeriode.ToShortDateString(), dateFinPeriode.ToShortDateString()), NombreJours);
                }
                else
                    if (i == dv.Table.Rows.Count - 2)
                    {
                        dateDebutPeriode = DateTime.Parse(dt_dates.Rows[i]["Dates"].ToString()).AddDays(1);
                        dateFinPeriode = DateTime.Parse(dt_dates.Rows[i + 1]["Dates"].ToString()).AddDays(-1);
                        NombreJours = dateFinPeriode.Subtract(dateDebutPeriode).Days + 1;
                        dt_periods.Rows.Add(String.Format("Du {0} au {1}", dateDebutPeriode.ToShortDateString(), dateFinPeriode.ToShortDateString()), NombreJours);
                    }
                    else
                    {
                        dateDebutPeriode = DateTime.Parse(dt_dates.Rows[i]["Dates"].ToString()).AddDays(1);
                        dateFinPeriode = DateTime.Parse(dt_dates.Rows[i + 1]["Dates"].ToString());
                        NombreJours = dateFinPeriode.Subtract(dateDebutPeriode).Days + 1;
                        dt_periods.Rows.Add(String.Format("Du {0} au {1}", dateDebutPeriode.ToShortDateString(), dateFinPeriode.ToShortDateString()), NombreJours);
                    }
            return dt_periods;
        }
    }
    catch (InvalidOperationException)
    {
        throw;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:03:51+00:00Added an answer on May 28, 2026 at 7:03 am

    What you can test here, is whether returned DataTable structure is indeed what you expect. Those are plain and simple unit tests. Determine what is expected from GeneratePeriods method (as in, what do you want it to do?), and write tests checking that it is in fact what method does. For example:

    • test whether correct number of periods was generated
    • test whether given period has correctly set rows/columns
    • test whether row data is in correct format
    • …and probably lot more

    On a side note, there’s nothing wrong with returning DataTable from function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.