Skip to content
This repository has been archived by the owner on Jun 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #421 from npavlov/master
Browse files Browse the repository at this point in the history
Add in fiscal year fetch handler
  • Loading branch information
jordimontana82 authored Jan 28, 2020
2 parents 756a775 + 01fb6ac commit 7a04fb5
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 11 deletions.
6 changes: 5 additions & 1 deletion FakeXrmEasy.Shared/Extensions/XmlExtensionsForFetchXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public static class XmlExtensionsForFetchXml
ConditionOperator.LastXMonths,
ConditionOperator.LastXWeeks,
ConditionOperator.LastXYears,
ConditionOperator.NextXWeeks
ConditionOperator.NextXWeeks,
ConditionOperator.InFiscalYear
};

public static bool IsAttributeTrue(this XElement elem, string attributeName)
Expand Down Expand Up @@ -520,6 +521,9 @@ public static ConditionExpression ToConditionExpression(this XElement elem, XrmF
case "next-week":
op = ConditionOperator.NextWeek;
break;
case "in-fiscal-year":
op = ConditionOperator.InFiscalYear;
break;
#if FAKE_XRM_EASY_9
case "contain-values":
op = ConditionOperator.ContainValues;
Expand Down
1 change: 1 addition & 0 deletions FakeXrmEasy.Shared/FakeXrmEasy.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<Compile Include="$(MSBuildThisFileDirectory)FakeMessageExecutors\UpsertRequestExecutor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FakeMessageExecutors\WhoAmIRequestExecutor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FakeMessageExecutors\WinOpportunityRequestExecutor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FiscalYearSettings.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IXrmFakedContext.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Metadata\DateTimeAttributeBehavior.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Metadata\MetadataGenerator.cs" />
Expand Down
25 changes: 25 additions & 0 deletions FakeXrmEasy.Shared/FiscalYearSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;

namespace FakeXrmEasy
{
[EntityLogicalName("organization")]
public class FiscalYearSettings
{
[AttributeLogicalName("fiscalcalendarstart")]
public DateTime StartDate { get; set; }

[AttributeLogicalName("fiscalperiodtype")]
public Template FiscalPeriodTemplate { get; set; }

public enum Template
{
Annually = 2000,
SemiAnnually = 2001,
Quarterly = 2002,
Monthly = 2003,
FourWeek = 2004
}
}
}
2 changes: 2 additions & 0 deletions FakeXrmEasy.Shared/XrmFakedContext.DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public partial class XrmFakedContext : IXrmContext
{
public TimeZoneInfo SystemTimeZone { get; set; }

public FiscalYearSettings FiscalYearSettings { get; set; }

public Dictionary<string, Dictionary<string, DateTimeAttributeBehavior>> DateBehaviour { get; set; }

private static Dictionary<string, Dictionary<string, DateTimeAttributeBehavior>> DefaultDateBehaviour()
Expand Down
13 changes: 11 additions & 2 deletions FakeXrmEasy.Shared/XrmFakedContext.Queries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,8 @@ protected static Expression TranslateConditionExpression(QueryExpression qe, Xrm
case ConditionOperator.LastWeek:
case ConditionOperator.ThisWeek:
case ConditionOperator.NextWeek:
operatorExpression = TranslateConditionExpressionBetweenDates(c, getNonBasicValueExpr, containsAttributeExpression);
case ConditionOperator.InFiscalYear:
operatorExpression = TranslateConditionExpressionBetweenDates(c, getNonBasicValueExpr, containsAttributeExpression, context);
break;

case ConditionOperator.Next7Days:
Expand Down Expand Up @@ -1679,7 +1680,7 @@ protected static Expression TranslateConditionExpressionLast(TypedConditionExpre
/// <summary>
/// Takes a condition expression which needs translating into a 'between two dates' expression and works out the relevant dates
/// </summary>
protected static Expression TranslateConditionExpressionBetweenDates(TypedConditionExpression tc, Expression getAttributeValueExpr, Expression containsAttributeExpr)
protected static Expression TranslateConditionExpressionBetweenDates(TypedConditionExpression tc, Expression getAttributeValueExpr, Expression containsAttributeExpr, XrmFakedContext context)
{
var c = tc.CondExpression;

Expand All @@ -1690,6 +1691,7 @@ protected static Expression TranslateConditionExpressionBetweenDates(TypedCondit
var thisYear = today.Year;
var thisMonth = today.Month;


switch (c.Operator)
{
case ConditionOperator.ThisYear: // From first day of this year to last day of this year
Expand Down Expand Up @@ -1731,6 +1733,13 @@ protected static Expression TranslateConditionExpressionBetweenDates(TypedCondit
fromDate = today.ToFirstDayOfDeltaWeek(1);
toDate = today.ToLastDayOfDeltaWeek(1).AddDays(1);
break;
case ConditionOperator.InFiscalYear:
var fiscalYear = (int)c.Values[0];
c.Values.Clear();
var fiscalYearDate = context.FiscalYearSettings?.StartDate ?? new DateTime(fiscalYear, 4, 1);
fromDate = fiscalYearDate;
toDate = fiscalYearDate.AddYears(1).AddDays(-1);
break;
}

c.Values.Add(fromDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ public void FetchXml_Operator_Last_Seven_Days_Translation()
</filter>
</entity>
</fetch>";

var query = XrmFakedContext.TranslateFetchXmlToQueryExpression(ctx, fetchXml);

Assert.True(query.Criteria != null);
Expand Down Expand Up @@ -1300,6 +1300,37 @@ public void FetchXml_Operator_ThisYear_Execution()
Assert.Equal(((DateTime)collection.Entities[2]["anniversary"]).Year, thisYear);
}

[Fact]
public void FetchXml_Operator_InFiscalYear_Execution()
{
var today = DateTime.Today;
var thisYear = today.Year;

var ctx = new XrmFakedContext();
ctx.FiscalYearSettings = new FiscalYearSettings() { StartDate = new DateTime(thisYear, 1, 2), FiscalPeriodTemplate = FiscalYearSettings.Template.Annually };
var fetchXml = $@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='contact'>
<attribute name='anniversary' />
<filter type='and'>
<condition attribute='anniversary' operator='in-fiscal-year' value='{thisYear}' />
</filter>
</entity>
</fetch>";

var ct1 = new Contact() { Id = Guid.NewGuid(), Anniversary = new DateTime(thisYear, 1, 2) }; // Second day of this year - should be returned
var ct2 = new Contact() { Id = Guid.NewGuid(), Anniversary = new DateTime(thisYear, 12, 31) }; // Last day of this year - should be returned
var ct3 = new Contact() { Id = Guid.NewGuid(), Anniversary = new DateTime(thisYear + 1, 1, 2) }; // Second day of next year - should not be returned
ctx.Initialize(new[] { ct1, ct2, ct3 });
var service = ctx.GetOrganizationService();

var collection = service.RetrieveMultiple(new FetchExpression(fetchXml));

Assert.Equal(2, collection.Entities.Count);

Assert.Equal(((DateTime)collection.Entities[0]["anniversary"]).Year, thisYear);
Assert.Equal(((DateTime)collection.Entities[1]["anniversary"]).Year, thisYear);
}

[Fact]
public void FetchXml_Operator_ThisMonth_Execution()
{
Expand Down Expand Up @@ -1366,7 +1397,7 @@ public void FetchXml_Operator_LastMonth_Execution()
Assert.Equal(2, collection.Entities.Count);

Assert.Equal(((DateTime)collection.Entities[0]["anniversary"]).Month, lastMonth);
Assert.Equal(((DateTime)collection.Entities[1]["anniversary"]).Month, lastMonth);
Assert.Equal(((DateTime)collection.Entities[1]["anniversary"]).Month, lastMonth);
}

[Fact]
Expand All @@ -1384,7 +1415,7 @@ public void FetchXml_Operator_NextMonth_Execution()

var today = DateTime.Today;
var thisYear = today.Year;
var thisMonth = today.Month;
var thisMonth = today.Month;
var nextMonth = new DateTime(thisYear, thisMonth, 1).AddMonths(1).Month;
var ct1 = new Contact() { Id = Guid.NewGuid(), Anniversary = today }; // Today - Should not be returned
var ct2 = new Contact() { Id = Guid.NewGuid(), Anniversary = new DateTime(thisYear, thisMonth, 1) }; // First day of this month - should not be returned
Expand All @@ -1393,7 +1424,7 @@ public void FetchXml_Operator_NextMonth_Execution()
var ct5 = new Contact() { Id = Guid.NewGuid(), Anniversary = new DateTime(thisYear, thisMonth, 1).AddMonths(2).AddDays(-1) }; // Last day of next month - should be returned
var ct6 = new Contact() { Id = Guid.NewGuid(), Anniversary = new DateTime(thisYear, thisMonth, 1).AddDays(-1) }; // Last day of last month - should not be returned
var ct7 = new Contact() { Id = Guid.NewGuid(), Anniversary = new DateTime(thisYear, thisMonth, 1).AddMonths(-1) }; // First day of last month - should not be returned

ctx.Initialize(new[] { ct1, ct2, ct3, ct4, ct5, ct6, ct7 });
var service = ctx.GetFakedOrganizationService();

Expand Down Expand Up @@ -1580,9 +1611,9 @@ public void FetchXml_Operator_Next_X_Weeks_Execution()
</fetch>";

var date = DateTime.Now;
var ct1 = new Contact() { Id = Guid.NewGuid(), Anniversary = date.AddDays(7*2) }; //Should be returned
var ct2 = new Contact() { Id = Guid.NewGuid(), Anniversary = date.AddDays(7*4) }; //Shouldnt
ctx.Initialize(new[] { ct1, ct2});
var ct1 = new Contact() { Id = Guid.NewGuid(), Anniversary = date.AddDays(7 * 2) }; //Should be returned
var ct2 = new Contact() { Id = Guid.NewGuid(), Anniversary = date.AddDays(7 * 4) }; //Shouldnt
ctx.Initialize(new[] { ct1, ct2 });
var service = ctx.GetOrganizationService();

var collection = service.RetrieveMultiple(new FetchExpression(fetchXml));
Expand Down Expand Up @@ -1789,7 +1820,7 @@ public void FetchXml_Operator_Next_Week_Execution()
Assert.Equal(retrievedUser, ct1.Id);
}


#if FAKE_XRM_EASY_9
[Fact]
public void FetchXml_Operator_ContainValues_Translation()
Expand Down

0 comments on commit 7a04fb5

Please sign in to comment.