如何测试返回DataTable的方法?

时间:2012-01-14 17:38:42

标签: c# .net unit-testing

我是单元测试领域的新手,所以我有一些问题:

制作返回DataTable的方法是一个好习惯吗?如果是,那我怎么测试呢?

这是他们要求我测试的功能:

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 个答案:

答案 0 :(得分:0)

您可以在此测试的是,返回的DataTable结构是否确实符合您的预期。这些是简单而简单的单元测试。从GeneratePeriods方法确定的预期(例如,您希望它做什么?),并编写测试,检查它实际上是什么方法。例如:

  • 测试是否生成了正确的句点数
  • 测试给定期间是否正确设置了行/列
  • 测试行数据是否格式正确
  • ......可能还有更多

另一方面,从函数返回DataTable没有错。