public void GetFirstDayOfWeekReturnsSameDayForFirstDayOfWeek() { //arrange var monday = DateTime.Parse("6 Apr 2015"); //act var actual = PeriodConditionHelper.GetFirstDayOfWeek(monday); //assert Assert.That(actual, Is.EqualTo(monday)); }
public void GetFirstDayOfWeekReturnsMondayDateForDayInMiddleOfWeek() { //arrange var wednesday = DateTime.Parse("8 Apr 2015"); //act var actual = PeriodConditionHelper.GetFirstDayOfWeek(wednesday); //assert var monday = DateTime.Parse("6 Apr 2015"); Assert.That(actual, Is.EqualTo(monday)); }
internal static IEnumerable <long> InsertDateFieldDummyData(EntityType definition, string newRecordPrefix, string analyserFieldColumnName, string oper, string value, bool isDateOnlyField, int financialYearStartMonth) { // generate 3 random dateTime values DateTime minDate, maxDate; var conditionOper = (ConditionType)Enum.Parse(typeof(ConditionType), oper); int? argument = null; if (!string.IsNullOrEmpty(value)) { argument = int.Parse(value); } // workout minDate and maxDate PeriodConditionHelper.GetPeriodFromConditionType(conditionOper, DateTime.Today, argument, financialYearStartMonth, isDateOnlyField, out minDate, out maxDate); // get a random value between min and max date values var rand = new Random(); var dateDiffTicks = maxDate.Ticks - minDate.Ticks; var newEntities = new long[3]; var textFieldColumnId = definition.Fields.FirstOrDefault(f => f.Name == "TextField").Id; var analyserFieldColumnId = definition.Fields.FirstOrDefault(f => f.Name == analyserFieldColumnName).Id; // get using (DatabaseContext.GetContext(false)) { for (int ctr = 0; ctr < 3; ctr++) { string randomTextFieldColValue = string.Format("{0}-{1}", newRecordPrefix, DateTime.Now.ToUniversalTime().ToLongTimeString()); var randPercentNum = rand.Next(1, 100); var randomTicks = dateDiffTicks * randPercentNum / 100; var dt = minDate.AddTicks(randomTicks); // add a random percentage of difference to the min date value // server stores value in utc. 'dateToSave' is saved as it is. // in case of dateOnly field, time portion is ignored and only date value is used. thats why we save 'dt' as it is. // in case of dateAndTime field, the whole vale is adjusted to current timezone. Thats why we save the utc vale of 'dt'. var dateToSave = isDateOnlyField ? dt : dt.ToUniversalTime(); var entity = Entity.Create(definition.Id); entity.SetField(textFieldColumnId, randomTextFieldColValue); entity.SetField(analyserFieldColumnId, dateToSave); entity.Save(); newEntities[ctr] = entity.Id; } } return(newEntities); }
public void GetStartAndEndDatesOfQuarterRange() { //arrange var date = DateTime.Parse("25 Feb 2014"); var startQuarterIndex = PeriodConditionHelper.GetQuarterIndexSinceBc(date); var endQuarterIndex = startQuarterIndex + 1; //act DateTime startDate, startDateOfNextQuarter; PeriodConditionHelper.GetStartAndEndDateOfQuarterRange(startQuarterIndex, endQuarterIndex, out startDate, out startDateOfNextQuarter); //assert Assert.That(startDate, Is.EqualTo(DateTime.Parse("01 Jan 2014"))); Assert.That(startDateOfNextQuarter, Is.EqualTo(DateTime.Parse("01 Jul 2014"))); }
/// <summary> /// Builds a predicate that examines the raw data (as its native .Net type) to determine if /// it passes the specified filter. /// </summary> /// <remarks> /// This overload assumes that the values will not be null. /// </remarks> /// <param name="condition">The condition to be applied.</param> /// <param name="gridResultColumn">The grid result column.</param> /// <param name="conditionalFormatter">The conditional formatter.</param> /// <returns>A predicate that accepts a data value and returns true if the value is acceptable on the basis of the filter.</returns> private static Predicate <object> BuildValueFilterPredicateNoNulls(Condition condition, ResultColumn gridResultColumn, ConditionalFormatter conditionalFormatter) { // Get and check argument TypedValue argument = condition.Argument; string sArgument = null; IComparable cArgument = null; // Handle null strings. TODO: Improve this hack if (argument != null && argument.Value == null && argument.Type is StringType) { argument.Value = ""; } if (argument == null || argument.Value == null) { if (ConditionTypeHelper.GetArgumentCount(condition.Operator) != 0) { throw new ArgumentException(@"Condition has no (or null) argument.", "condition"); } } else { sArgument = argument.Value as string; cArgument = argument.Value as IComparable; } bool isDate = condition.ColumnType is DateType; bool isCurrency = condition.ColumnType is CurrencyType; // Return lambda to evaluate test // 'cell' is statically type object switch (condition.Operator) { case ConditionType.Unspecified: return(cell => true); // assert false - predicates should not be built from filters with unspecified operators. Silently ignore. case ConditionType.Equal: if (argument.Type is StringType) { return(cell => ((string)cell).Equals(argument.ValueString, StringComparison.CurrentCultureIgnoreCase)); } return(cell => argument != null && argument.Value.Equals(isCurrency ? ConvertCurrencyCell(cell, condition.ColumnType) : cell)); case ConditionType.NotEqual: if (argument.Type is StringType) { return(cell => !((string)cell).Equals(argument.ValueString, StringComparison.CurrentCultureIgnoreCase)); } return(cell => argument != null && !argument.Value.Equals(isCurrency ? ConvertCurrencyCell(cell, condition.ColumnType) : cell)); case ConditionType.GreaterThan: return(cell => cArgument != null && cArgument.CompareTo(isCurrency ? ConvertCurrencyCell(cell, condition.ColumnType) : cell) < 0); case ConditionType.GreaterThanOrEqual: return(cell => cArgument != null && cArgument.CompareTo(isCurrency ? ConvertCurrencyCell(cell, condition.ColumnType) : cell) <= 0); case ConditionType.LessThan: return(cell => cArgument != null && cArgument.CompareTo(isCurrency ? ConvertCurrencyCell(cell, condition.ColumnType) : cell) > 0); case ConditionType.LessThanOrEqual: return(cell => cArgument != null && cArgument.CompareTo(isCurrency ? ConvertCurrencyCell(cell, condition.ColumnType) : cell) >= 0); case ConditionType.Contains: return(cell => ((string)cell).IndexOf(sArgument, StringComparison.CurrentCultureIgnoreCase) >= 0); case ConditionType.StartsWith: return(cell => ((string)cell).StartsWith(sArgument, StringComparison.CurrentCultureIgnoreCase)); case ConditionType.EndsWith: return(cell => ((string)cell).EndsWith(sArgument, StringComparison.CurrentCultureIgnoreCase)); case ConditionType.AnyOf: return(cell => AnyOf((long)cell, condition.Arguments)); case ConditionType.AnyExcept: return(cell => AnyExcept((long)cell, condition.Arguments)); case ConditionType.Today: case ConditionType.ThisMonth: case ConditionType.ThisQuarter: case ConditionType.ThisYear: case ConditionType.CurrentFinancialYear: case ConditionType.LastNDays: case ConditionType.NextNDays: case ConditionType.LastNMonths: case ConditionType.NextNMonths: case ConditionType.LastNQuarters: case ConditionType.NextNQuarters: case ConditionType.LastNYears: case ConditionType.NextNYears: case ConditionType.LastNFinancialYears: case ConditionType.NextNFinancialYears: case ConditionType.LastNDaysTillNow: case ConditionType.NextNDaysFromNow: case ConditionType.ThisWeek: case ConditionType.LastNWeeks: case ConditionType.NextNWeeks: return(cell => { DateTime minDate, maxDate; var cellAsDateTime = isDate ? (DateTime)cell : UtcToLocal((DateTime)cell); const int startMonthOfFinancialYear = 7; //TODO: this comes from old code. Reported as a bug to Diana to improve to respect tenant FY configuration. PeriodConditionHelper.GetPeriodFromConditionType(condition.Operator, DateTime.Today, cArgument != null ? cArgument as int?: null, startMonthOfFinancialYear, isDate, out minDate, out maxDate); return cellAsDateTime >= minDate && cellAsDateTime < maxDate; }); case ConditionType.DateEquals: { if (argument != null && argument.Value != null) { DateTime today = ((DateTime)argument.Value).Date; DateTime threshold = today.AddDays(1); return(cell => { DateTime dCell = isDate ? (DateTime)cell : UtcToLocal((DateTime)cell); return threshold.CompareTo(dCell) > 0 && today.CompareTo(dCell) <= 0; }); } } break; case ConditionType.IsTrue: return(cell => ((bool)cell)); case ConditionType.IsFalse: return(cell => !((bool)cell)); case ConditionType.IsNull: return(cell => string.Empty.Equals(cell)); // this method does not get called if it is truly null case ConditionType.IsNotNull: return(cell => !string.Empty.Equals(cell)); // this method does not get called if it is truly null case ConditionType.CurrentUser: return(IsCurrentUser); case ConditionType.AnyBelowStructureLevel: case ConditionType.AnyAboveStructureLevel: case ConditionType.AnyAtOrAboveStructureLevel: case ConditionType.AnyAtOrBelowStructureLevel: return(cell => ApplyStructureViewCondition((string)cell, condition, gridResultColumn, conditionalFormatter)); default: throw new Exception("Unknown filter operator."); } return(null); }
[TestCase(8055, Result = 2014)] //Q4 of 2014 public int GetYearFromQuarter(int quarter) { return(PeriodConditionHelper.GetYearFromQuarter(quarter)); }
[TestCase(8055, Result = 10)] //Q4 of 2014 public int GetFirstMonthOfQuarter(int quarter) { return(PeriodConditionHelper.GetFirstMonthOfQuarter(quarter)); }
[TestCase("2014 Dec 31", Result = 8055 /*2013x4 => 2013 years since 0001*/)] //last day-of-year public int GetQuarterIndexSinceBc(string dateAsString) { var date = DateTime.Parse(dateAsString); return(PeriodConditionHelper.GetQuarterIndexSinceBc(date)); }