public void When_a_query_expression_with_topcount_is_executed_only_a_number_of_records_equals_to_the_page_size_is_returned() { var context = new XrmFakedContext(); //Create 20 contacts var contactList = new List <Entity>(); for (var i = 0; i < 20; i++) { contactList.Add(new Entity("contact") { Id = Guid.NewGuid() }); } context.Initialize(contactList); var qe = new QueryExpression() { EntityName = "contact" }; qe.ColumnSet = new ColumnSet(true); qe.TopCount = 10; var result = XrmFakedContext.TranslateQueryExpressionToLinq(context, qe).ToList(); Assert.True(result.Count == 10); }
public void When_translating_a_query_from_a_non_existing_entity_an_exception_is_thrown() { var context = new XrmFakedContext(); var guid1 = Guid.NewGuid(); var guid2 = Guid.NewGuid(); var guid3 = Guid.NewGuid(); IQueryable <Entity> data = new List <Entity>() { new Entity("account") { Id = guid1 }, new Entity("account") { Id = guid2 }, new Entity("contact") { Id = guid3 } }.AsQueryable(); context.Initialize(data); var qe = new QueryExpression() { EntityName = "nonexistingentityname" }; Assert.Throws <Exception>(() => XrmFakedContext.TranslateQueryExpressionToLinq(context, qe)); }
public void When_executing_a_query_expression_only_the_selected_columns_in_the_columnset_are_returned() { var context = new XrmFakedContext(); var contact1 = new Entity("contact") { Id = Guid.NewGuid() }; contact1["fullname"] = "Contact 1"; contact1["firstname"] = "First 1"; var contact2 = new Entity("contact") { Id = Guid.NewGuid() }; contact2["fullname"] = "Contact 2"; contact2["firstname"] = "First 2"; var contact3 = new Entity("contact") { Id = Guid.NewGuid() }; contact3["fullname"] = "Contact 3"; contact3["firstname"] = "First 3"; var account = new Entity("account") { Id = Guid.NewGuid() }; account["name"] = "Account 1"; contact1["parentcustomerid"] = account.ToEntityReference(); //Both contacts are related to the same account contact2["parentcustomerid"] = account.ToEntityReference(); context.Initialize(new List <Entity>() { account, contact1, contact2, contact3 }); var qe = new QueryExpression() { EntityName = "contact" }; qe.LinkEntities.Add( new LinkEntity() { LinkFromEntityName = "contact", LinkToEntityName = "account", LinkFromAttributeName = "parentcustomerid", LinkToAttributeName = "accountid", JoinOperator = JoinOperator.Inner, Columns = new ColumnSet(new string[] { "name" }) } ); //We only select fullname and parentcustomerid, firstname should not be included qe.ColumnSet = new ColumnSet(new string[] { "fullname", "parentcustomerid" }); var result = XrmFakedContext.TranslateQueryExpressionToLinq(context, qe); Assert.True(result.Count() == 2); var firstContact = result.FirstOrDefault(); var lastContact = result.LastOrDefault(); Assert.False(firstContact.Attributes.ContainsKey("firstname")); Assert.False(lastContact.Attributes.ContainsKey("firstname")); }
public void When_translating_a_query_from_a_non_existing_entity_an_empty_list_is_returned_when_using_dynamic_entities() { var context = new XrmFakedContext(); var guid1 = Guid.NewGuid(); var guid2 = Guid.NewGuid(); var guid3 = Guid.NewGuid(); IQueryable <Entity> data = new List <Entity>() { new Entity("account") { Id = guid1 }, new Entity("account") { Id = guid2 }, new Entity("contact") { Id = guid3 } }.AsQueryable(); context.Initialize(data); var qe = new QueryExpression() { EntityName = "nonexistingentityname" }; var result = XrmFakedContext.TranslateQueryExpressionToLinq(context, qe); Assert.Equal(0, result.Count()); }
public void When_executing_a_query_expression_with_tomorrow_operator_time_part_is_ignored() { var context = new XrmFakedContext(); var contact1 = new Entity("contact") { Id = Guid.NewGuid(), ["anniversary"] = DateTime.Today.AddSeconds(3) }; var contact2 = new Entity("contact") { Id = Guid.NewGuid(), ["anniversary"] = DateTime.Today.AddDays(1).AddSeconds(3) }; context.Initialize(new List <Entity>() { contact1, contact2 }); var qe = new QueryExpression() { EntityName = "contact" }; qe.ColumnSet = new ColumnSet(true); qe.Criteria = new FilterExpression(LogicalOperator.And); var condition = new ConditionExpression("anniversary", ConditionOperator.Tomorrow, new DateTime(2017, 07, 28, 0, 0, 0)); qe.Criteria.AddCondition(condition); var result = XrmFakedContext.TranslateQueryExpressionToLinq(context, qe).ToList(); Assert.True(result.Count() == 1); Assert.True(result[0].Id == contact2.Id); }
public void When_translating_a_null_query_expression_the_linq_query_is_also_null() { var context = new XrmFakedContext(); var result = XrmFakedContext.TranslateQueryExpressionToLinq(context, null); Assert.True(result == null); }
public void When_executing_a_query_expression_with_equals_operator_right_result_is_returned() { var context = new XrmFakedContext(); var contact1 = new Entity("contact") { Id = Guid.NewGuid() }; contact1["fullname"] = "Contact 1"; contact1["firstname"] = "First 1"; var contact2 = new Entity("contact") { Id = Guid.NewGuid() }; contact2["fullname"] = "Contact 2"; contact2["firstname"] = "First 2"; context.Initialize(new List <Entity>() { contact1, contact2 }); var qe = new QueryExpression() { EntityName = "contact" }; qe.ColumnSet = new ColumnSet(true); qe.Criteria = new FilterExpression(LogicalOperator.And); var condition = new ConditionExpression("fullname", ConditionOperator.Equal, "Contact 1"); qe.Criteria.AddCondition(condition); var result = XrmFakedContext.TranslateQueryExpressionToLinq(context, qe).ToList(); Assert.True(result.Count() == 1); }
public void When_executing_a_query_expression_with_greaterthanorequal_operator_right_result_is_returned() { var ctx = new XrmFakedContext(); var ct1 = new Contact() { Id = Guid.NewGuid(), NickName = "Al" }; var ct2 = new Contact() { Id = Guid.NewGuid(), NickName = "Bob" }; var ct3 = new Contact() { Id = Guid.NewGuid(), NickName = "Charlie" }; ctx.Initialize(new[] { ct2, ct3, ct1 }); var qe = new QueryExpression() { EntityName = "contact" }; qe.ColumnSet = new ColumnSet(true); qe.Criteria = new FilterExpression(LogicalOperator.And); var condition = new ConditionExpression("nickname", ConditionOperator.GreaterEqual, "Bob"); qe.Criteria.AddCondition(condition); var result = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, qe).ToList(); Assert.Equal(2, result.Count); Assert.Equal("Bob", result[0]["nickname"]); Assert.Equal("Charlie", result[1]["nickname"]); }
public void When_executing_a_query_expression_with_a_simple_join_right_result_is_returned() { var context = new XrmFakedContext(); var contact1 = new Entity("contact") { Id = Guid.NewGuid() }; contact1["fullname"] = "Contact 1"; var contact2 = new Entity("contact") { Id = Guid.NewGuid() }; contact2["fullname"] = "Contact 2"; var account = new Entity("account") { Id = Guid.NewGuid() }; account["name"] = "Account 1"; contact1["parentcustomerid"] = account.ToEntityReference(); //Both contacts are related to the same account contact2["parentcustomerid"] = account.ToEntityReference(); context.Initialize(new List <Entity>() { account, contact1, contact2 }); var qe = new QueryExpression() { EntityName = "contact" }; qe.LinkEntities.Add( new LinkEntity() { LinkFromEntityName = "contact", LinkToEntityName = "account", LinkFromAttributeName = "parentcustomerid", LinkToAttributeName = "accountid", JoinOperator = JoinOperator.Inner, Columns = new ColumnSet(new string[] { "name" }) } ); qe.ColumnSet = new ColumnSet(new string[] { "fullname", "parentcustomerid" }); var result = XrmFakedContext.TranslateQueryExpressionToLinq(context, qe); Assert.True(result.Count() == 2); //2 Contacts related to the same account var firstContact = result.FirstOrDefault(); var secondContact = result.LastOrDefault(); Assert.True(firstContact.Attributes.Count == 3); Assert.True(secondContact.Attributes.Count == 3); Assert.True(firstContact["fullname"].Equals(contact1["fullname"])); Assert.True((firstContact["account.name"] as AliasedValue).Value.Equals(account["name"])); Assert.True(secondContact["fullname"].Equals(contact2["fullname"])); Assert.True((secondContact["account.name"] as AliasedValue).Value.Equals(account["name"])); }
public void When_executing_a_query_expression_with_a_not_implemented_operator_pull_request_exception_is_thrown() { var context = new XrmFakedContext(); var contact1 = new Entity("contact") { Id = Guid.NewGuid() }; contact1["fullname"] = "Contact 1"; contact1["firstname"] = "First 1"; var contact2 = new Entity("contact") { Id = Guid.NewGuid() }; contact2["fullname"] = "Contact 2"; contact2["firstname"] = "First 2"; context.Initialize(new List <Entity>() { contact1, contact2 }); var qe = new QueryExpression() { EntityName = "contact" }; qe.ColumnSet = new ColumnSet(true); qe.Criteria = new FilterExpression(LogicalOperator.And); var condition = new ConditionExpression("fullname", ConditionOperator.LastXFiscalPeriods, "Contact 1"); qe.Criteria.AddCondition(condition); Assert.Throws <PullRequestException>(() => XrmFakedContext.TranslateQueryExpressionToLinq(context, qe).ToList()); }
public void When_a_query_expression_without_pagination_is_executed_a_maximum_of_maxretrievecount_records_are_returned() { var context = new XrmFakedContext(); //Create 20 contacts var contactList = new List <Entity>(); for (var i = 0; i < context.MaxRetrieveCount + 1; i++) { contactList.Add(new Entity("contact") { Id = Guid.NewGuid() }); } context.Initialize(contactList); var qe = new QueryExpression() { EntityName = "contact" }; qe.ColumnSet = new ColumnSet(true); var result = XrmFakedContext.TranslateQueryExpressionToLinq(context, qe).ToList(); Assert.True(result.Count == context.MaxRetrieveCount); }
public void When_a_query_expression_with_pagination_is_executed_with_the_requested_page_size() { var context = new XrmFakedContext(); //Create 20 contacts var contactList = new List <Entity>(); for (var i = 0; i < context.MaxRetrieveCount; i++) { contactList.Add(new Entity("contact") { Id = Guid.NewGuid() }); } context.Initialize(contactList); var qe = new QueryExpression() { EntityName = "contact" }; qe.ColumnSet = new ColumnSet(true); qe.TopCount = 100; var result = XrmFakedContext.TranslateQueryExpressionToLinq(context, qe).ToList(); Assert.True(result.Count == 100); }
public void When_executing_a_query_with_aboveorequals_oprator_right_result_is_returned() { var context = new XrmFakedContext(); var account1 = new Entity("account") { Id = Guid.NewGuid() }; var account2 = new Entity("account") { Id = Guid.NewGuid() }; account2["parentaccountid"] = account1.ToEntityReference(); var account3 = new Entity("account") { Id = Guid.NewGuid() }; context.Initialize(new List <Entity>() { account1, account2, account3 }); var qe = new QueryExpression("account"); qe.ColumnSet.AllColumns = true; #if !FAKE_XRM_EASY && !FAKE_XRM_EASY_2013 qe.Criteria.AddCondition("accountid", ConditionOperator.AboveOrEqual, account2.Id); #endif //TranslateConditionExpressionAboveOrEqual var result = XrmFakedContext.TranslateQueryExpressionToLinq(context, qe).ToList(); Assert.True(result.Count() == 2); }
public void When_executing_a_query_expression_join_with_orphans_these_are_not_returned() { var context = new XrmFakedContext(); var contact1 = new Entity("contact") { Id = Guid.NewGuid() }; contact1["fullname"] = "Contact 1"; var contact2 = new Entity("contact") { Id = Guid.NewGuid() }; contact2["fullname"] = "Contact 2"; var contact3 = new Entity("contact") { Id = Guid.NewGuid() }; contact3["fullname"] = "Contact 3"; var account = new Entity("account") { Id = Guid.NewGuid() }; account["name"] = "Account 1"; contact1["parentcustomerid"] = account.ToEntityReference(); //Both contacts are related to the same account contact2["parentcustomerid"] = account.ToEntityReference(); //Contact3 doesnt have a parent customer, but must be returned anyway (left outer) context.Initialize(new List <Entity>() { account, contact1, contact2, contact3 }); var qe = new QueryExpression() { EntityName = "contact" }; qe.LinkEntities.Add( new LinkEntity() { LinkFromEntityName = "contact", LinkToEntityName = "account", LinkFromAttributeName = "parentcustomerid", LinkToAttributeName = "accountid", JoinOperator = JoinOperator.Inner, Columns = new ColumnSet(new string[] { "name" }) } ); qe.ColumnSet = new ColumnSet(new string[] { "fullname", "parentcustomerid" }); var result = XrmFakedContext.TranslateQueryExpressionToLinq(context, qe); Assert.True(result.Count() == 2); var firstContact = result.FirstOrDefault(); var lastContact = result.LastOrDefault(); Assert.True(firstContact["fullname"].Equals(contact1["fullname"])); Assert.True(lastContact["fullname"].Equals(contact2["fullname"])); }
public void When_executing_a_query_expression_without_columnset_no_attributes_are_returned() { var context = new XrmFakedContext(); var contact1 = new Entity("contact") { Id = Guid.NewGuid() }; contact1["fullname"] = "Contact 1"; contact1["firstname"] = "First 1"; var contact2 = new Entity("contact") { Id = Guid.NewGuid() }; contact2["fullname"] = "Contact 2"; contact2["firstname"] = "First 2"; var contact3 = new Entity("contact") { Id = Guid.NewGuid() }; contact3["fullname"] = "Contact 3"; contact3["firstname"] = "First 3"; var account = new Entity("account") { Id = Guid.NewGuid() }; account["name"] = "Account 1"; contact1["parentcustomerid"] = account.ToEntityReference(); //Both contacts are related to the same account contact2["parentcustomerid"] = account.ToEntityReference(); context.Initialize(new List <Entity>() { account, contact1, contact2, contact3 }); var qe = new QueryExpression() { EntityName = "contact" }; qe.LinkEntities.Add( new LinkEntity() { LinkFromEntityName = "contact", LinkToEntityName = "account", LinkFromAttributeName = "parentcustomerid", LinkToAttributeName = "accountid", JoinOperator = JoinOperator.Inner, Columns = new ColumnSet(false) } ); qe.ColumnSet = new ColumnSet(false); var result = XrmFakedContext.TranslateQueryExpressionToLinq(context, qe); Assert.True(result.Count() == 2); var firstContact = result.FirstOrDefault(); var lastContact = result.LastOrDefault(); Assert.True(firstContact.Attributes.Count == 0); //Contact 1 Assert.True(lastContact.Attributes.Count == 0); //Contact 2 }
public void When_executing_a_query_expression_with_on_or_after_operator_time_part_is_ignored() { var context = new XrmFakedContext(); var contact1 = new Entity("contact") { Id = Guid.NewGuid(), ["anniversary"] = new DateTime(2017, 07, 28, 10, 10, 10) }; var contact2 = new Entity("contact") { Id = Guid.NewGuid(), ["anniversary"] = new DateTime(2017, 07, 28, 0, 0, 0) }; var contact3 = new Entity("contact") { Id = Guid.NewGuid(), ["anniversary"] = new DateTime(2017, 07, 29, 0, 0, 0) }; var contact4 = new Entity("contact") { Id = Guid.NewGuid(), ["anniversary"] = new DateTime(2017, 07, 27, 0, 0, 0) //Not included }; context.Initialize(new List <Entity>() { contact1, contact2, contact3, contact4 }); var qe = new QueryExpression() { EntityName = "contact" }; qe.ColumnSet = new ColumnSet(true); qe.Criteria = new FilterExpression(LogicalOperator.And); var condition = new ConditionExpression("anniversary", ConditionOperator.OnOrAfter, new DateTime(2017, 07, 28, 0, 0, 0)); qe.Criteria.AddCondition(condition); var result = XrmFakedContext.TranslateQueryExpressionToLinq(context, qe).ToList(); Assert.True(result.Count() == 3); Assert.True(result[0].Id == contact1.Id); Assert.True(result[1].Id == contact2.Id); Assert.True(result[2].Id == contact3.Id); }
public void When_executing_a_query_expression_with_an_attribute_in_columnset_that_doesnt_exists_null_value_is_returned_with_dynamic_entities() { var context = new XrmFakedContext(); var contact1 = new Entity("contact") { Id = Guid.NewGuid() }; contact1["fullname"] = "Contact 1"; contact1["firstname"] = "First 1"; var account = new Entity("account") { Id = Guid.NewGuid() }; account["name"] = "Account 1"; contact1["parentcustomerid"] = account.ToEntityReference(); //Both contacts are related to the same account context.Initialize(new List <Entity>() { account, contact1 }); var qe = new QueryExpression() { EntityName = "contact" }; //We only select fullname and parentcustomerid, firstname should not be included qe.ColumnSet = new ColumnSet(new string[] { "this attribute doesnt exists!" }); Assert.DoesNotThrow(() => XrmFakedContext.TranslateQueryExpressionToLinq(context, qe).ToList()); var list = XrmFakedContext.TranslateQueryExpressionToLinq(context, qe).ToList(); Assert.True(list[0].Attributes.ContainsKey("this attribute doesnt exists!")); Assert.Equal(null, list[0].Attributes["this attribute doesnt exists!"]); }
public void When_querying_early_bound_entities_unexisting_attribute_raises_exception_when_linked_to() { var context = new XrmFakedContext(); var service = context.GetFakedOrganizationService(); var role = new Role() { Id = Guid.NewGuid() }; var parentRole = new Role() { Id = Guid.NewGuid() }; context.Initialize(new[] { role, parentRole }); using (var ctx = new XrmServiceContext(service)) { var qe = new QueryExpression() { EntityName = "role" }; qe.LinkEntities.Add( new LinkEntity() { LinkFromEntityName = "role", LinkToEntityName = "role", LinkFromAttributeName = "parentroleid", LinkToAttributeName = "thisAttributeDoesntExists", JoinOperator = JoinOperator.Inner, Columns = new ColumnSet(new string[] { "roleid" }) } ); Assert.Throws <FaultException <OrganizationServiceFault> >(() => XrmFakedContext.TranslateQueryExpressionToLinq(context, qe).ToList()); } }
public void When_executing_a_query_with_aboveorequals_oprator_in_LinkedEntity_right_result_is_returned() { var context = new XrmFakedContext(); var account1 = new Entity("account") { Id = Guid.NewGuid() }; var account2 = new Entity("account") { Id = Guid.NewGuid() }; account2["parentaccountid"] = account1.ToEntityReference(); var account3 = new Entity("account") { Id = Guid.NewGuid() }; var contact1 = new Entity("contact") { Id = Guid.NewGuid() }; contact1["parentcustomerid"] = account1.ToEntityReference(); var contact2 = new Entity("contact") { Id = Guid.NewGuid() }; contact2["parentcustomerid"] = account1.ToEntityReference(); var contact3 = new Entity("contact") { Id = Guid.NewGuid() }; contact3["parentcustomerid"] = account2.ToEntityReference(); var contact4 = new Entity("contact") { Id = Guid.NewGuid() }; contact4["parentcustomerid"] = account3.ToEntityReference(); context.Initialize(new List <Entity>() { account1, account2, account3, contact1, contact2, contact3, contact4 }); // Instantiate QueryExpression query var query = new QueryExpression("contact"); query.ColumnSet.AllColumns = true; var query_account = query.AddLink("account", "parentcustomerid", "accountid"); #if !FAKE_XRM_EASY && !FAKE_XRM_EASY_2013 query_account.LinkCriteria.AddCondition("accountid", ConditionOperator.AboveOrEqual, account2.Id); #endif //TranslateConditionExpressionAboveOrEqual var result = XrmFakedContext.TranslateQueryExpressionToLinq(context, query).ToList(); Assert.True(result.Count() == 3); }
public OrganizationResponse Execute(OrganizationRequest req, XrmFakedContext ctx) { var request = req as RetrieveMultipleRequest; if (request.Query is QueryExpression) { var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, request.Query as QueryExpression); var list = linqQuery.ToList(); list.ForEach(e => PopulateFormattedValues(e)); var response = new RetrieveMultipleResponse { Results = new ParameterCollection { { "EntityCollection", new EntityCollection(list) } } }; return(response); } else if (request.Query is FetchExpression) { var fetchXml = (request.Query as FetchExpression).Query; var xmlDoc = XrmFakedContext.ParseFetchXml(fetchXml); var queryExpression = XrmFakedContext.TranslateFetchXmlDocumentToQueryExpression(ctx, xmlDoc); var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, queryExpression); var list = linqQuery.ToList(); if (xmlDoc.IsAggregateFetchXml()) { list = XrmFakedContext.ProcessAggregateFetchXml(ctx, xmlDoc, list); } list.ForEach(e => PopulateFormattedValues(e)); var response = new RetrieveMultipleResponse { Results = new ParameterCollection { { "EntityCollection", new EntityCollection(list) } } }; return(response); } else if (request.Query is QueryByAttribute) { //We instantiate a QueryExpression to be executed as we have the implementation done already var query = request.Query as QueryByAttribute; var qe = new QueryExpression(query.EntityName); qe.ColumnSet = query.ColumnSet; qe.Criteria = new FilterExpression(); for (var i = 0; i < query.Attributes.Count; i++) { qe.Criteria.AddCondition(new ConditionExpression(query.Attributes[i], ConditionOperator.Equal, query.Values[i])); } //QueryExpression now done... execute it! var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, qe as QueryExpression); var list = linqQuery.ToList(); list.ForEach(e => PopulateFormattedValues(e)); var response = new RetrieveMultipleResponse { Results = new ParameterCollection { { "EntityCollection", new EntityCollection(list) } } }; return(response); } else { throw PullRequestException.NotImplementedOrganizationRequest(request.Query.GetType()); } }
public OrganizationResponse Execute(OrganizationRequest req, XrmFakedContext ctx) { var request = req as RetrieveMultipleRequest; List <Entity> list = null; PagingInfo pageInfo = null; QueryExpression qe; string entityName = null; if (request.Query is QueryExpression) { qe = (request.Query as QueryExpression).Clone(); entityName = qe.EntityName; var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, qe); list = linqQuery.ToList(); } else if (request.Query is FetchExpression) { var fetchXml = (request.Query as FetchExpression).Query; var xmlDoc = XrmFakedContext.ParseFetchXml(fetchXml); qe = XrmFakedContext.TranslateFetchXmlDocumentToQueryExpression(ctx, xmlDoc); entityName = qe.EntityName; var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, qe); list = linqQuery.ToList(); if (xmlDoc.IsAggregateFetchXml()) { list = XrmFakedContext.ProcessAggregateFetchXml(ctx, xmlDoc, list); } } else if (request.Query is QueryByAttribute) { // We instantiate a QueryExpression to be executed as we have the implementation done already var query = request.Query as QueryByAttribute; qe = new QueryExpression(query.EntityName); entityName = qe.EntityName; qe.ColumnSet = query.ColumnSet; qe.Criteria = new FilterExpression(); for (var i = 0; i < query.Attributes.Count; i++) { qe.Criteria.AddCondition(new ConditionExpression(query.Attributes[i], ConditionOperator.Equal, query.Values[i])); } qe.PageInfo = query.PageInfo; // QueryExpression now done... execute it! var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, qe); list = linqQuery.ToList(); } else { throw PullRequestException.NotImplementedOrganizationRequest(request.Query.GetType()); } if (qe.Distinct) { list = GetDistinctEntities(list); } // Handle the top count before taking paging into account if (qe.TopCount != null && qe.TopCount.Value < list.Count) { list = list.Take(qe.TopCount.Value).ToList(); } // Handle TotalRecordCount here? int totalRecordCount = -1; if (qe?.PageInfo?.ReturnTotalRecordCount == true) { totalRecordCount = list.Count; } // Handle paging var pageSize = ctx.MaxRetrieveCount; pageInfo = qe.PageInfo; int pageNumber = 1; if (pageInfo != null && pageInfo.PageNumber > 0) { pageNumber = pageInfo.PageNumber; pageSize = pageInfo.Count == 0 ? ctx.MaxRetrieveCount : pageInfo.Count; } // Figure out where in the list we need to start and how many items we need to grab int numberToGet = pageSize; int startPosition = 0; if (pageNumber != 1) { startPosition = (pageNumber - 1) * pageSize; } if (list.Count < pageSize) { numberToGet = list.Count; } else if (list.Count - pageSize * (pageNumber - 1) < pageSize) { numberToGet = list.Count - (pageSize * (pageNumber - 1)); } var recordsToReturn = startPosition + numberToGet > list.Count ? new List <Entity>() : list.GetRange(startPosition, numberToGet); recordsToReturn.ForEach(e => e.ApplyDateBehaviour(ctx)); recordsToReturn.ForEach(e => PopulateFormattedValues(e)); var response = new RetrieveMultipleResponse { Results = new ParameterCollection { { "EntityCollection", new EntityCollection(recordsToReturn) } } }; response.EntityCollection.EntityName = entityName; response.EntityCollection.MoreRecords = (list.Count - pageSize * pageNumber) > 0; response.EntityCollection.TotalRecordCount = totalRecordCount; if (response.EntityCollection.MoreRecords) { var first = response.EntityCollection.Entities.First(); var last = response.EntityCollection.Entities.Last(); response.EntityCollection.PagingCookie = $"<cookie page=\"{pageNumber}\"><{first.LogicalName}id last=\"{last.Id.ToString("B").ToUpper()}\" first=\"{first.Id.ToString("B").ToUpper()}\" /></cookie>"; } return(response); }
public OrganizationResponse Execute(OrganizationRequest req, XrmFakedContext ctx) { var request = req as RetrieveMultipleRequest; List <Entity> list = null; PagingInfo pageInfo = null; int? topCount = null; if (request.Query is QueryExpression) { var qe = request.Query as QueryExpression; pageInfo = qe.PageInfo; topCount = qe.TopCount; //need to request 1 extra to get a fill if there are more records if (topCount != null) { qe.TopCount = topCount + 1; } if (qe.PageInfo.Count > 0) { qe.TopCount = qe.PageInfo.Count + 1; } var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, request.Query as QueryExpression); list = linqQuery.ToList(); } else if (request.Query is FetchExpression) { var fetchXml = (request.Query as FetchExpression).Query; var xmlDoc = XrmFakedContext.ParseFetchXml(fetchXml); var queryExpression = XrmFakedContext.TranslateFetchXmlDocumentToQueryExpression(ctx, xmlDoc); pageInfo = queryExpression.PageInfo; topCount = queryExpression.TopCount; //need to request 1 extra to get a fill if there are more records if (topCount != null) { queryExpression.TopCount = topCount + 1; } if (queryExpression.PageInfo.Count > 0) { queryExpression.TopCount = queryExpression.PageInfo.Count + 1; } var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, queryExpression); list = linqQuery.ToList(); if (xmlDoc.IsAggregateFetchXml()) { list = XrmFakedContext.ProcessAggregateFetchXml(ctx, xmlDoc, list); } } else if (request.Query is QueryByAttribute) { //We instantiate a QueryExpression to be executed as we have the implementation done already var query = request.Query as QueryByAttribute; var qe = new QueryExpression(query.EntityName); qe.ColumnSet = query.ColumnSet; qe.Criteria = new FilterExpression(); for (var i = 0; i < query.Attributes.Count; i++) { qe.Criteria.AddCondition(new ConditionExpression(query.Attributes[i], ConditionOperator.Equal, query.Values[i])); } pageInfo = qe.PageInfo; topCount = qe.TopCount; //need to request 1 extra to be able check if there are more records if (topCount != null) { qe.TopCount = topCount + 1; } if (qe.PageInfo.Count > 0) { qe.TopCount = qe.PageInfo.Count + 1; } //QueryExpression now done... execute it! var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, qe as QueryExpression); list = linqQuery.ToList(); } else { throw PullRequestException.NotImplementedOrganizationRequest(request.Query.GetType()); } list.ForEach(e => e.ApplyDateBehaviour(ctx)); list.ForEach(e => PopulateFormattedValues(e)); var recordCount = list.Count(); var pageSize = recordCount; int pageNumber = 1; if (pageInfo != null && pageInfo.PageNumber > 0 && pageInfo.Count > 0) { pageSize = pageInfo.Count; pageNumber = pageInfo.PageNumber; } else if (topCount != null) { pageSize = topCount.Value; } var response = new RetrieveMultipleResponse { Results = new ParameterCollection { { "EntityCollection", new EntityCollection(list.Take(pageSize).ToList()) } } }; response.EntityCollection.MoreRecords = recordCount > pageSize; if (response.EntityCollection.MoreRecords) { var first = response.EntityCollection.Entities.First(); var last = response.EntityCollection.Entities.Last(); response.EntityCollection.PagingCookie = String.Format( "<cookie page=\"{0}\"><{1}id last=\"{2}\" first=\"{3}\" /></cookie>", pageNumber, first.LogicalName, last.Id.ToString("B").ToUpper(), first.Id.ToString("B").ToUpper()); } return(response); }
public void When_executing_a_query_expression_with_an_attribute_in_columnset_that_doesnt_exists_exception_is_raised_with_early_bound_entities() { var context = new XrmFakedContext(); var contact1 = new Contact() { Id = Guid.NewGuid() }; contact1["fullname"] = "Contact 1"; contact1["firstname"] = "First 1"; var account = new Account() { Id = Guid.NewGuid() }; account["name"] = "Account 1"; contact1["parentcustomerid"] = account.ToEntityReference(); //Both contacts are related to the same account context.Initialize(new List <Entity>() { account, contact1 }); var qe = new QueryExpression() { EntityName = "contact" }; //We only select fullname and parentcustomerid, firstname should not be included qe.ColumnSet = new ColumnSet(new string[] { "this attribute doesnt exists!" }); var exception = Assert.Throws <FaultException <OrganizationServiceFault> >(() => XrmFakedContext.TranslateQueryExpressionToLinq(context, qe).ToList()); Assert.Equal(exception.Detail.ErrorCode, OrganizationServiceFaultQueryBuilderNoAttributeException.ErrorCode); }
public void When_executing_a_query_expression_with_all_attributes_all_of_them_are_returned() { var context = new XrmFakedContext(); var contact1 = new Entity("contact") { Id = Guid.NewGuid() }; contact1["fullname"] = "Contact 1"; contact1["firstname"] = "First 1"; var contact2 = new Entity("contact") { Id = Guid.NewGuid() }; contact2["fullname"] = "Contact 2"; contact2["firstname"] = "First 2"; var contact3 = new Entity("contact") { Id = Guid.NewGuid() }; contact3["fullname"] = "Contact 3"; contact3["firstname"] = "First 3"; var account = new Entity("account") { Id = Guid.NewGuid() }; account["name"] = "Account 1"; contact1["parentcustomerid"] = account.ToEntityReference(); //Both contacts are related to the same account contact2["parentcustomerid"] = account.ToEntityReference(); context.Initialize(new List <Entity>() { account, contact1, contact2, contact3 }); var qe = new QueryExpression() { EntityName = "contact" }; qe.LinkEntities.Add( new LinkEntity() { LinkFromEntityName = "contact", LinkToEntityName = "account", LinkFromAttributeName = "parentcustomerid", LinkToAttributeName = "accountid", JoinOperator = JoinOperator.Inner } ); //We only select fullname and parentcustomerid, firstname should not be included qe.ColumnSet = new ColumnSet(true); var result = XrmFakedContext.TranslateQueryExpressionToLinq(context, qe); Assert.True(result.Count() == 2); var firstContact = result.FirstOrDefault(); var lastContact = result.LastOrDefault(); //Contact 1 attributes = 4 + 6 (the extra six are the CreatedOn, ModifiedOn, CreatedBy, ModifiedBy, OwnerId + StateCode attributes generated automatically //+ Attributes from the join(account) = 2 + 6 Assert.True(firstContact.Attributes.Count == 18); Assert.True(lastContact.Attributes.Count == 18); }
public void When_executing_a_query_expression_with_an_attribute_in_columnset_in_a_linked_entity_that_doesnt_exists_descriptive_exception_is_thrown() { var context = new XrmFakedContext(); var contact1 = new Contact() { Id = Guid.NewGuid() }; contact1["fullname"] = "Contact 1"; contact1["firstname"] = "First 1"; var contact2 = new Contact() { Id = Guid.NewGuid() }; contact2["fullname"] = "Contact 2"; contact2["firstname"] = "First 2"; var contact3 = new Contact() { Id = Guid.NewGuid() }; contact3["fullname"] = "Contact 3"; contact3["firstname"] = "First 3"; var account = new Entity("account") { Id = Guid.NewGuid() }; account["name"] = "Account 1"; contact1["parentcustomerid"] = account.ToEntityReference(); //Both contacts are related to the same account contact2["parentcustomerid"] = account.ToEntityReference(); context.Initialize(new List <Entity>() { account, contact1, contact2, contact3 }); var qe = new QueryExpression() { EntityName = "contact" }; qe.LinkEntities.Add( new LinkEntity() { LinkFromEntityName = "contact", LinkToEntityName = "account", LinkFromAttributeName = "parentcustomerid", LinkToAttributeName = "accountid", JoinOperator = JoinOperator.Inner, Columns = new ColumnSet(new string[] { "this attribute does not exists" }) } ); //We only select fullname and parentcustomerid, firstname should not be included qe.ColumnSet = new ColumnSet(new string[] { "this attribute doesnt exists!" }); var exception = Assert.Throws <FaultException <OrganizationServiceFault> >(() => XrmFakedContext.TranslateQueryExpressionToLinq(context, qe).ToList()); Assert.Equal(exception.Detail.ErrorCode, OrganizationServiceFaultQueryBuilderNoAttributeException.ErrorCode); }